From 9f171511042d29542d8578957c6692c2c9457583 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 16 Feb 2012 16:52:58 -0500 Subject: DataSourceNoCloud: allow reading user-data and meta-data from simple files This allows you to attach a disk in ISO9660 or vfat filesystem format labeled 'cidata' with 'user-data' and 'meta-data' on it. It provides a much easier way to interact with cloud-init in nocloud than mounting the image or the OVF method. --- cloudinit/DataSourceNoCloud.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'cloudinit/DataSourceNoCloud.py') diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index fa64f2e5..6c744b82 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -23,6 +23,7 @@ import cloudinit.DataSource as DataSource from cloudinit import seeddir as base_seeddir from cloudinit import log import cloudinit.util as util +import errno class DataSourceNoCloud(DataSource.DataSource): @@ -64,6 +65,26 @@ class DataSourceNoCloud(DataSource.DataSource): found.append(self.seeddir) log.debug("using seeded cache data in %s" % self.seeddir) + fslist = util.find_devs_with("TYPE=vfat") + fslist.extend(util.find_devs_with("TYPE=iso9660")) + + label_list = util.find_devs_with("LABEL=cidata") + devlist = list(set(fslist) & set(label_list)) + devlist.sort(reverse=True) + + for dev in devlist: + try: + (newmd, newud) = util.mount_callback_umount(dev, + util.read_seeded) + md = util.mergedict(newmd, md) + ud = newud + log.debug("using data from %s" % dev) + found.append(dev) + break + except OSError, e: + if e.errno != errno.ENOENT: + raise + # there was no indication on kernel cmdline or data # in the seeddir suggesting this handler should be used. if len(found) == 0: -- cgit v1.2.3 From 02aea2383e0ea020fd6c2e74ffcaad8983820a9d Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 17 Feb 2012 12:14:48 -0500 Subject: support reading network interface config from DataSourceNoCloud document usage of DataSourceNoCloud from vfat or iso disk. --- ChangeLog | 1 + cloudinit/DataSourceNoCloud.py | 41 +++++++++++++++++++++++++++++-- doc/nocloud/README | 55 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 doc/nocloud/README (limited to 'cloudinit/DataSourceNoCloud.py') diff --git a/ChangeLog b/ChangeLog index 52d170c8..cd92618d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -26,6 +26,7 @@ - DataSourceOVF: only search for OVF data on ISO9660 filesystems (LP: #898373) - DataSourceConfigDrive: support getting data from openstack config drive (LP: #857378) - DataSourceNoCloud: support seed from external disk of ISO or vfat (LP: #857378) + - DataSourceNoCloud: support inserting /etc/network/interfaces 0.6.2: - fix bug where update was not done unless update was explicitly set. It would not be run if 'upgrade' or packages were set to be installed diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index 6c744b82..1e28edbd 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -24,6 +24,7 @@ from cloudinit import seeddir as base_seeddir from cloudinit import log import cloudinit.util as util import errno +import subprocess class DataSourceNoCloud(DataSource.DataSource): @@ -31,6 +32,7 @@ class DataSourceNoCloud(DataSource.DataSource): userdata = None userdata_raw = None supported_seed_starts = ("/", "file://") + dsmode = "local" seed = None cmdline_id = "ds=nocloud" seeddir = base_seeddir + '/nocloud' @@ -42,7 +44,7 @@ class DataSourceNoCloud(DataSource.DataSource): def get_data(self): defaults = { - "instance-id": "nocloud" + "instance-id": "nocloud", "dsmode": "net" } found = [] @@ -84,14 +86,20 @@ class DataSourceNoCloud(DataSource.DataSource): except OSError, e: if e.errno != errno.ENOENT: raise + except util.mountFailedError: + log.warn("Failed to mount %s when looking for seed" % dev) # there was no indication on kernel cmdline or data # in the seeddir suggesting this handler should be used. if len(found) == 0: return False + seeded_interfaces = None + # the special argument "seedfrom" indicates we should # attempt to seed the userdata / metadata from its value + # its primarily value is in allowing the user to type less + # on the command line, ie: ds=nocloud;s=http://bit.ly/abcdefg if "seedfrom" in md: seedfrom = md["seedfrom"] seedfound = False @@ -104,6 +112,9 @@ class DataSourceNoCloud(DataSource.DataSource): (seedfrom, self.__class__)) return False + if 'network-interfaces' in md: + seeded_interfaces = self.dsmode + # this could throw errors, but the user told us to do it # so if errors are raised, let them raise (md_seed, ud) = util.read_seeded(seedfrom, timeout=None) @@ -114,10 +125,35 @@ class DataSourceNoCloud(DataSource.DataSource): found.append(seedfrom) md = util.mergedict(md, defaults) + + # update the network-interfaces if metadata had 'network-interfaces' + # entry and this is the local datasource, or 'seedfrom' was used + # and the source of the seed was self.dsmode + # ('local' for NoCloud, 'net' for NoCloudNet') + if ('network-interfaces' in md and + (self.dsmode in ("local", seeded_interfaces))): + log.info("updating network interfaces from nocloud") + + util.write_file("/etc/network/interfaces", + md['network-interfaces']) + try: + (out, err) = util.subp(['ifup', '--all']) + if len(out) or len(err): + log.warn("ifup --all had stderr: %s" % err) + + except subprocess.CalledProcessError as exc: + log.warn("ifup --all failed: %s" % (exc.output[1])) + self.seed = ",".join(found) self.metadata = md self.userdata_raw = ud - return True + + if md['dsmode'] == self.dsmode: + return True + + log.debug("%s: not claiming datasource, dsmode=%s" % + (self, md['dsmode'])) + return False # returns true or false indicating if cmdline indicated @@ -166,6 +202,7 @@ class DataSourceNoCloudNet(DataSourceNoCloud): cmdline_id = "ds=nocloud-net" supported_seed_starts = ("http://", "https://", "ftp://") seeddir = base_seeddir + '/nocloud-net' + dsmode = "net" datasources = ( diff --git a/doc/nocloud/README b/doc/nocloud/README new file mode 100644 index 00000000..c94b206a --- /dev/null +++ b/doc/nocloud/README @@ -0,0 +1,55 @@ +The data source 'NoCloud' and 'NoCloudNet' allow the user to provide user-data +and meta-data to the instance without running a network service (or even without +having a network at all) + +You can provide meta-data and user-data to a local vm boot via files on a vfat +or iso9660 filesystem. These user-data and meta-data files are expected to be +in the format described in doc/example/seed/README . Basically, user-data is +simply user-data and meta-data is a yaml formated file representing what you'd +find in the EC2 metadata service. + +Given a disk 12.04 cloud image in 'disk.img', you can create a sufficient disk +by following the example below. + +## create user-data and meta-data files that will be used +## to modify image on first boot +$ { echo instance-id: iid-local01; echo local-hostname: cloudimg; } > meta-data + +$ printf "#cloud-config\npassword: passw0rd\nchpasswd: { expire: False }\nssh_pwauth: True\n" > user-data + +## create a disk to attach with some user-data and meta-data +$ genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data + +## alternatively, create a vfat filesystem with same files +## $ truncate --size 2M seed.img +## $ mkfs.vfat -n cidata seed.img +## $ mcopy -oi seed.img user-data meta-data :: + +## create a new qcow image to boot, backed by your original image +$ qemu-img create -f qcow2 -b disk.img boot-disk.img + +## boot the image and login as 'ubuntu' with password 'passw0rd' +## note, passw0rd was set as password through the user-data above, +## there is no password set on these images. +$ kvm -m 256 \ + -net nic -net user,hostfwd=tcp::2222-:22 \ + -drive file=boot-disk.img,if=virtio \ + -drive file=seed.iso,if=virtio + +Note, that the instance-id provided ('iid-local01' above) is what is used to +determine if this is "first boot". So if you are making updates to user-data +you will also have to change that, or start the disk fresh. + + +Also, you can inject an /etc/network/interfaces file by providing the content +for that file in the 'network-interfaces' field of metadata. Example metadata: + instance-id: iid-abcdefg + network-interfaces: | + iface eth0 inet static + address 192.168.1.10 + network 192.168.1.0 + netmask 255.255.255.0 + broadcast 192.168.1.255 + gateway 192.168.1.254 + hostname: myhost + -- cgit v1.2.3 From f7eba5ca8aaaccd6b88f40c5c6f30715bd185069 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 28 Feb 2012 18:48:58 -0500 Subject: DataSourceNoCloud: fix local cloud sources other than from devices The purely local non-device (vfat/iso9660) sources were broken by the last set of changes here. This restores them to functional. If the seed is from a device, then the default behavior is to be 'net' mode. For seed via cmdline, the user can specify 'ds=nocloud-net' and for seed via filesystem seed dir, they can just populate the other directory. To make it easier, when attaching a seed device, the user does not need to specify 'dsmode' of 'net' in the metadata file. They still can, but that is the default. It seems that that is more likely to be what is desired. LP: #942695 --- cloudinit/DataSourceNoCloud.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'cloudinit/DataSourceNoCloud.py') diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index 1e28edbd..62ecc088 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -44,7 +44,7 @@ class DataSourceNoCloud(DataSource.DataSource): def get_data(self): defaults = { - "instance-id": "nocloud", "dsmode": "net" + "instance-id": "nocloud", "dsmode": self.dsmode } found = [] @@ -80,6 +80,14 @@ class DataSourceNoCloud(DataSource.DataSource): util.read_seeded) md = util.mergedict(newmd, md) ud = newud + + # for seed from a device, the default mode is 'net'. + # that is more likely to be what is desired. + # If they want dsmode of local, then they must + # specify that. + if 'dsmode' not in md: + md['dsmode'] = "net" + log.debug("using data from %s" % dev) found.append(dev) break -- cgit v1.2.3