From 59697ffc622596c1c25a225da54533cf1dfbf8ed Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Mon, 20 Apr 2020 10:03:17 +0300 Subject: [PATCH 1/2] read cdrom userdata from spec location Signed-off-by: Avi Deitcher --- pkg/metadata/provider_cdrom.go | 46 ++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/metadata/provider_cdrom.go b/pkg/metadata/provider_cdrom.go index b181b3dcf..34b9608fe 100644 --- a/pkg/metadata/provider_cdrom.go +++ b/pkg/metadata/provider_cdrom.go @@ -9,17 +9,23 @@ import ( ) const ( - configFile = "config" - cdromDevs = "/dev/sr[0-9]*" + metadataFile = "meta-data" + userdataFile = "user-data" + userdataFallback = "config" + cdromDevs = "/dev/sr[0-9]*" +) + +var ( + userdataFiles = []string{userdataFile, userdataFallback} ) // ProviderCDROM is the type implementing the Provider interface for CDROMs -// It looks for a file called 'configFile' in the root +// It looks for file called 'meta-data', 'user-data' or 'config' in the root type ProviderCDROM struct { - device string - mountPoint string - err error - data []byte + device string + mountPoint string + err error + userdata, metadata []byte } // ListCDROMs lists all the cdroms in the system @@ -39,10 +45,28 @@ func ListCDROMs() []Provider { // NewCDROM returns a new ProviderCDROM func NewCDROM(device string) *ProviderCDROM { mountPoint, err := ioutil.TempDir("", "cd") - p := ProviderCDROM{device, mountPoint, err, []byte{}} + p := ProviderCDROM{device, mountPoint, err, []byte{}, []byte{}} if err == nil { if p.err = p.mount(); p.err == nil { - p.data, p.err = ioutil.ReadFile(path.Join(p.mountPoint, configFile)) + // read the userdata - we read the spec file and the fallback, but eventually + // will remove the fallback + for _, f := range userdataFiles { + userdata, err := ioutil.ReadFile(path.Join(p.mountPoint, f)) + // did we find a file? + if err == nil && userdata != nil { + p.userdata = userdata + break + } + } + if p.userdata == nil { + p.err = fmt.Errorf("no userdata file found at any of %v", userdataFiles) + } + // read the metadata + metadata, err := ioutil.ReadFile(path.Join(p.mountPoint, metadataFile)) + // did we find a file? + if err == nil && metadata != nil { + p.metadata = metadata + } p.unmount() } } @@ -55,12 +79,12 @@ func (p *ProviderCDROM) String() string { // Probe checks if the CD has the right file func (p *ProviderCDROM) Probe() bool { - return len(p.data) != 0 + return len(p.userdata) != 0 } // Extract gets both the CDROM specific and generic userdata func (p *ProviderCDROM) Extract() ([]byte, error) { - return p.data, p.err + return p.userdata, p.err } // mount mounts a CDROM/DVD device under mountPoint From 84bfea53b0149722fe9c9a6dc85de33d91cbe02e Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Mon, 20 Apr 2020 11:51:31 +0300 Subject: [PATCH 2/2] Add examples Signed-off-by: Avi Deitcher --- examples/aws.yml | 2 +- examples/docker-for-mac.yml | 2 +- examples/gcp.yml | 2 +- examples/hetzner.yml | 2 +- examples/openstack.yml | 2 +- examples/packet.yml | 2 +- examples/scaleway.yml | 2 +- examples/vultr.yml | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/aws.yml b/examples/aws.yml index 4c5bc9413..32b6ccbf8 100644 --- a/examples/aws.yml +++ b/examples/aws.yml @@ -13,7 +13,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 services: - name: rngd image: linuxkit/rngd:7fab61cca793113280397dcee8159f35dc37adcb diff --git a/examples/docker-for-mac.yml b/examples/docker-for-mac.yml index 0c3b6cc7b..1fcd9ed32 100644 --- a/examples/docker-for-mac.yml +++ b/examples/docker-for-mac.yml @@ -11,7 +11,7 @@ init: onboot: # support metadata for optional config in /run/config - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 - name: sysctl image: linuxkit/sysctl:541f60fe3676611328e89e8bac251fc636b1a6aa - name: sysfs diff --git a/examples/gcp.yml b/examples/gcp.yml index 8235604bf..8eec59d38 100644 --- a/examples/gcp.yml +++ b/examples/gcp.yml @@ -13,7 +13,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 services: - name: getty image: linuxkit/getty:48f66df198981e692084bf70ab72b9fe2be0f880 diff --git a/examples/hetzner.yml b/examples/hetzner.yml index 114e0836c..4a5d7edc9 100644 --- a/examples/hetzner.yml +++ b/examples/hetzner.yml @@ -18,7 +18,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 command: ["/usr/bin/metadata", "hetzner"] services: - name: rngd diff --git a/examples/openstack.yml b/examples/openstack.yml index a8a1c0938..d85480077 100644 --- a/examples/openstack.yml +++ b/examples/openstack.yml @@ -13,7 +13,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 command: ["/usr/bin/metadata", "openstack"] services: - name: rngd diff --git a/examples/packet.yml b/examples/packet.yml index 56dc7f667..9055158f0 100644 --- a/examples/packet.yml +++ b/examples/packet.yml @@ -18,7 +18,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 command: ["/usr/bin/metadata", "packet"] services: - name: rngd diff --git a/examples/scaleway.yml b/examples/scaleway.yml index e937c1cca..865db0e2e 100644 --- a/examples/scaleway.yml +++ b/examples/scaleway.yml @@ -16,7 +16,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 services: - name: getty image: linuxkit/getty:48f66df198981e692084bf70ab72b9fe2be0f880 diff --git a/examples/vultr.yml b/examples/vultr.yml index 076b77ded..547adf96c 100644 --- a/examples/vultr.yml +++ b/examples/vultr.yml @@ -13,7 +13,7 @@ onboot: image: linuxkit/dhcpcd:2f8a9b670aa6e96a09db56ec45c9f07ef2a811ee command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"] - name: metadata - image: linuxkit/metadata:12794dd50fd1563dee6c960710fd8b923dba6231 + image: linuxkit/metadata:04ce7519c2ea2eaf99bbdc76bb01fc036eed7ab0 command: ["/usr/bin/metadata", "vultr"] services: - name: getty