diff options
author | Gonéri Le Bouder <goneri@lebouder.net> | 2020-04-24 15:42:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-24 13:42:36 -0600 |
commit | 72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c (patch) | |
tree | 753d4a0791b0f044f789c57f025dc5d6e2558030 /cloudinit | |
parent | e4499963d663b52ca4b66423c0445909d710cfbd (diff) | |
download | vyos-cloud-init-72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c.tar.gz vyos-cloud-init-72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c.zip |
BSD: find_devs_with_ refactoring (#298)
Refactoring of the `find_devs_with_*bsd()` methods:
- centralize everything in `util.py`
- add test coverage
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/sources/DataSourceNoCloud.py | 25 | ||||
-rw-r--r-- | cloudinit/util.py | 52 |
2 files changed, 46 insertions, 31 deletions
diff --git a/cloudinit/sources/DataSourceNoCloud.py b/cloudinit/sources/DataSourceNoCloud.py index 71d5fc2e..72d86c41 100644 --- a/cloudinit/sources/DataSourceNoCloud.py +++ b/cloudinit/sources/DataSourceNoCloud.py @@ -36,27 +36,14 @@ class DataSourceNoCloud(sources.DataSource): return "%s [seed=%s][dsmode=%s]" % (root, self.seed, self.dsmode) def _get_devices(self, label): - if util.is_FreeBSD(): - devlist = [ - p for p in ['/dev/msdosfs/' + label, '/dev/iso9660/' + label] - if os.path.exists(p)] - elif util.is_NetBSD(): - out, _err = util.subp(['sysctl', '-n', 'hw.disknames'], rcs=[0]) - devlist = [] - for dev in out.split(): - mscdlabel_out, _ = util.subp(['mscdlabel', dev], rcs=[0, 1]) - if ('label "%s"' % label) in mscdlabel_out: - devlist.append('/dev/' + dev) - devlist.append('/dev/' + dev + 'a') # NetBSD 7 - else: - fslist = util.find_devs_with("TYPE=vfat") - fslist.extend(util.find_devs_with("TYPE=iso9660")) + fslist = util.find_devs_with("TYPE=vfat") + fslist.extend(util.find_devs_with("TYPE=iso9660")) - label_list = util.find_devs_with("LABEL=%s" % label.upper()) - label_list.extend(util.find_devs_with("LABEL=%s" % label.lower())) + label_list = util.find_devs_with("LABEL=%s" % label.upper()) + label_list.extend(util.find_devs_with("LABEL=%s" % label.lower())) - devlist = list(set(fslist) & set(label_list)) - devlist.sort(reverse=True) + devlist = list(set(fslist) & set(label_list)) + devlist.sort(reverse=True) return devlist def _get_data(self): diff --git a/cloudinit/util.py b/cloudinit/util.py index 818904b1..4cae7ec8 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1245,19 +1245,44 @@ def close_stdin(): os.dup2(fp.fileno(), sys.stdin.fileno()) +def find_devs_with_freebsd(criteria=None, oformat='device', + tag=None, no_cache=False, path=None): + if not criteria: + return glob.glob("/dev/msdosfs/*") + glob.glob("/dev/iso9660/*") + if criteria.startswith("LABEL="): + label = criteria.lstrip("LABEL=") + devlist = [ + p for p in ['/dev/msdosfs/' + label, '/dev/iso9660/' + label] + if os.path.exists(p)] + elif criteria == "TYPE=vfat": + devlist = glob.glob("/dev/msdosfs/*") + elif criteria == "TYPE=iso9660": + devlist = glob.glob("/dev/iso9660/*") + return devlist + + def find_devs_with_netbsd(criteria=None, oformat='device', tag=None, no_cache=False, path=None): - if not path: - path = "/dev/cd0" - cmd = ["mscdlabel", path] - out, _ = subp(cmd, capture=True, decode="replace", rcs=[0, 1]) - result = out.split() - if result and len(result) > 2: - if criteria == "TYPE=iso9660" and "ISO" in result: - return [path] - if criteria == "LABEL=CONFIG-2" and '"config-2"' in result: - return [path] - return [] + devlist = [] + label = None + _type = None + if criteria: + if criteria.startswith("LABEL="): + label = criteria.lstrip("LABEL=") + if criteria.startswith("TYPE="): + _type = criteria.lstrip("TYPE=") + out, _err = subp(['sysctl', '-n', 'hw.disknames'], rcs=[0]) + for dev in out.split(): + if label or _type: + mscdlabel_out, _ = subp(['mscdlabel', dev], rcs=[0, 1]) + if label and not ('label "%s"' % label) in mscdlabel_out: + continue + if _type == "iso9660" and "ISO filesystem" not in mscdlabel_out: + continue + if _type == "vfat" and "ISO filesystem" in mscdlabel_out: + continue + devlist.append('/dev/' + dev) + return devlist def find_devs_with_openbsd(criteria=None, oformat='device', @@ -1290,7 +1315,10 @@ def find_devs_with(criteria=None, oformat='device', LABEL=<label> UUID=<uuid> """ - if is_NetBSD(): + if is_FreeBSD(): + return find_devs_with_freebsd(criteria, oformat, + tag, no_cache, path) + elif is_NetBSD(): return find_devs_with_netbsd(criteria, oformat, tag, no_cache, path) elif is_OpenBSD(): |