From e3e62e8bdf7e2bf02fdeacfc7625a08a67a9db21 Mon Sep 17 00:00:00 2001 From: Joseph Bajin Date: Mon, 18 Aug 2014 10:32:54 -0400 Subject: new: Added FreeBSD support to ConfigDrive --- cloudinit/util.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/util.py b/cloudinit/util.py index bc681f4a..a8a26325 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1294,12 +1294,16 @@ def ensure_dir(path, mode=None): @contextlib.contextmanager -def unmounter(umount): +def unmounter(umount, lazy_support=True): try: yield umount finally: if umount: - umount_cmd = ["umount", '-l', umount] + # Do not use Lazy Mode on some systems (freebsd) + if lazy_support: + umount_cmd = ["umount", '-l', umount] + else: + umount_cmd = ["umount", umount] subp(umount_cmd) @@ -1382,7 +1386,12 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): # Be nice and ensure it ends with a slash if not mountpoint.endswith("/"): mountpoint += "/" - with unmounter(umount): + # Set lazy_support to false if FreeBSD + if device.startswith("/dev/cd"): + lazy_support = False + else: + lazy_support = True + with unmounter(umount,lazy_support): if data is None: ret = callback(mountpoint) else: -- cgit v1.2.3 From 7dbebd3fe9384ba90fce88cb715472d92e00e987 Mon Sep 17 00:00:00 2001 From: Joseph Bajin Date: Mon, 18 Aug 2014 11:16:45 -0400 Subject: fix: Updated some syntax to be pep8 compliant --- cloudinit/sources/DataSourceConfigDrive.py | 6 +++--- cloudinit/util.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/sources/DataSourceConfigDrive.py b/cloudinit/sources/DataSourceConfigDrive.py index 7416ceec..82caf6eb 100644 --- a/cloudinit/sources/DataSourceConfigDrive.py +++ b/cloudinit/sources/DataSourceConfigDrive.py @@ -38,7 +38,7 @@ VALID_DSMODES = ("local", "net", "pass", "disabled") FS_TYPES = ('vfat', 'iso9660') LABEL_TYPES = ('config-2',) POSSIBLE_MOUNTS = ('sr', 'cd') -OPTICAL_DEVICES = tuple(('/dev/%s%s' % (z,i) for z in POSSIBLE_MOUNTS +OPTICAL_DEVICES = tuple(('/dev/%s%s' % (z, i) for z in POSSIBLE_MOUNTS for i in range(0, 2))) @@ -75,10 +75,10 @@ class DataSourceConfigDrive(openstack.SourceMixin, sources.DataSource): # Set mtype if freebsd and turn off sync if dev.startswith("/dev/cd"): mtype = "cd9660" - sync = False + sync = False else: mtype = None - sync = True + sync = True results = util.mount_cb(dev, read_config_drive, mtype=mtype, sync=sync) found = dev diff --git a/cloudinit/util.py b/cloudinit/util.py index a8a26325..82d75843 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1391,7 +1391,7 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): lazy_support = False else: lazy_support = True - with unmounter(umount,lazy_support): + with unmounter(umount, lazy_support): if data is None: ret = callback(mountpoint) else: -- cgit v1.2.3 From 26e6c265277cf5e29b8af311f2bb8759b0e811cd Mon Sep 17 00:00:00 2001 From: Joseph Bajin Date: Sat, 23 Aug 2014 14:04:00 -0400 Subject: Removed using lazy mode for umount Safer for cloud-init to not use lazy mode for unmount --- cloudinit/util.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/util.py b/cloudinit/util.py index 82d75843..6405db23 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1294,16 +1294,12 @@ def ensure_dir(path, mode=None): @contextlib.contextmanager -def unmounter(umount, lazy_support=True): +def unmounter(umount): try: yield umount finally: if umount: - # Do not use Lazy Mode on some systems (freebsd) - if lazy_support: - umount_cmd = ["umount", '-l', umount] - else: - umount_cmd = ["umount", umount] + umount_cmd = ["umount", umount] subp(umount_cmd) @@ -1386,12 +1382,7 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): # Be nice and ensure it ends with a slash if not mountpoint.endswith("/"): mountpoint += "/" - # Set lazy_support to false if FreeBSD - if device.startswith("/dev/cd"): - lazy_support = False - else: - lazy_support = True - with unmounter(umount, lazy_support): + with unmounter(umount): if data is None: ret = callback(mountpoint) else: -- cgit v1.2.3 From f4d3f05827b3302960eedb6278500a3b007eb807 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 22 Sep 2014 14:35:03 -0400 Subject: support 'mtype' as a list, and fix up freebsd mount types this supports a list of input, and cleans up that list for the platform specific mount types. Basically, mtype = None means 'mount -t auto' or the equivalent for the platform. and 'iso9660' means "iso type". --- cloudinit/sources/DataSourceOVF.py | 3 +- cloudinit/util.py | 78 +++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 25 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/sources/DataSourceOVF.py b/cloudinit/sources/DataSourceOVF.py index 2f53c1ba..7ba60735 100644 --- a/cloudinit/sources/DataSourceOVF.py +++ b/cloudinit/sources/DataSourceOVF.py @@ -215,8 +215,7 @@ def transport_iso9660(require_iso=True): continue try: - (fname, contents) = util.mount_cb(fullp, - get_ovf_env, mtype=mtype) + (fname, contents) = util.mount_cb(fullp, get_ovf_env, mtype=mtype) except util.MountFailedError: LOG.debug("%s not mountable as iso9660" % fullp) continue diff --git a/cloudinit/util.py b/cloudinit/util.py index 8558eb87..d233e6e1 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1346,37 +1346,69 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): Mount the device, call method 'callback' passing the directory in which it was mounted, then unmount. Return whatever 'callback' returned. If data != None, also pass data to callback. + + mtype is a filesystem type. it may be a list, string (a single fsname) + or a list of fsnames. """ + + if isinstance(mtype, str): + mtypes = [mtype] + elif isinstance(mtype, (list, tuple)): + mtypes = list(mtype) + elif mtype is None: + mtypes = None + + # clean up 'mtype' input a bit based on platform. + platform = platform.system.lower() + if platform == "linux": + if mtypes is None: + mtypes = ["auto"] + elif platform.endswith("bsd"): + if mtypes is None: + mtypes = ['ufs', 'cd9660', 'vfat'] + for index, mtype in enumerate(mtypes): + if mtype == "iso9660": + mtypes[index] = "cd9660" + else: + mtypes = [] + mounted = mounts() with tempdir() as tmpd: umount = False if device in mounted: mountpoint = mounted[device]['mountpoint'] else: - try: - mountcmd = ['mount'] - mountopts = [] - if rw: - mountopts.append('rw') - else: - mountopts.append('ro') - if sync: - # This seems like the safe approach to do - # (ie where this is on by default) - mountopts.append("sync") - if mountopts: - mountcmd.extend(["-o", ",".join(mountopts)]) - if mtype: - mountcmd.extend(['-t', mtype]) - mountcmd.append(device) - mountcmd.append(tmpd) - subp(mountcmd) - umount = tmpd # This forces it to be unmounted (when set) - mountpoint = tmpd - except (IOError, OSError) as exc: - raise MountFailedError(("Failed mounting %s " - "to %s due to: %s") % + for mtype in mtypes: + mountpoint = None + try: + mountcmd = ['mount'] + mountopts = [] + if rw: + mountopts.append('rw') + else: + mountopts.append('ro') + if sync: + # This seems like the safe approach to do + # (ie where this is on by default) + mountopts.append("sync") + if mountopts: + mountcmd.extend(["-o", ",".join(mountopts)]) + if mtype: + mountcmd.extend(['-t', mtype]) + mountcmd.append(device) + mountcmd.append(tmpd) + subp(mountcmd) + umount = tmpd # This forces it to be unmounted (when set) + mountpoint = tmpd + break + except (IOError, OSError) as exc: + LOG.debug("Failed mount of '%s' as '%s': %s", + device, mtype, exc) + pass + if not mountpoint: + raise MountFailedError("Failed mounting %s to %s due to: %s" % (device, tmpd, exc)) + # Be nice and ensure it ends with a slash if not mountpoint.endswith("/"): mountpoint += "/" -- cgit v1.2.3 From cdcf11eeae36e5da5d7ea8a3dca1611987a52089 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 29 Sep 2014 08:42:20 -0400 Subject: fix re-use of 'platform' in local scope --- cloudinit/util.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/util.py b/cloudinit/util.py index d233e6e1..63c34b32 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1359,11 +1359,11 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): mtypes = None # clean up 'mtype' input a bit based on platform. - platform = platform.system.lower() - if platform == "linux": + platform_lower = platform.system.lower() + if platform_lower == "linux": if mtypes is None: mtypes = ["auto"] - elif platform.endswith("bsd"): + elif platform_lower.endswith("bsd"): if mtypes is None: mtypes = ['ufs', 'cd9660', 'vfat'] for index, mtype in enumerate(mtypes): -- cgit v1.2.3 From 143424fcd8666432bb943640b9231c1045e9c5e7 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 29 Sep 2014 08:55:08 -0400 Subject: further platform cleanup --- cloudinit/util.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'cloudinit/util.py') diff --git a/cloudinit/util.py b/cloudinit/util.py index 63c34b32..76e91951 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1359,18 +1359,19 @@ def mount_cb(device, callback, data=None, rw=False, mtype=None, sync=True): mtypes = None # clean up 'mtype' input a bit based on platform. - platform_lower = platform.system.lower() - if platform_lower == "linux": + platsys = platform.system().lower() + if platsys == "linux": if mtypes is None: mtypes = ["auto"] - elif platform_lower.endswith("bsd"): + elif platsys.endswith("bsd"): if mtypes is None: mtypes = ['ufs', 'cd9660', 'vfat'] for index, mtype in enumerate(mtypes): if mtype == "iso9660": mtypes[index] = "cd9660" else: - mtypes = [] + # we cannot do a smart "auto", so just call 'mount' once with no -t + mtypes = [''] mounted = mounts() with tempdir() as tmpd: -- cgit v1.2.3