summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorGonéri Le Bouder <goneri@lebouder.net>2020-04-24 15:42:36 -0400
committerGitHub <noreply@github.com>2020-04-24 13:42:36 -0600
commit72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c (patch)
tree753d4a0791b0f044f789c57f025dc5d6e2558030 /cloudinit/util.py
parente4499963d663b52ca4b66423c0445909d710cfbd (diff)
downloadvyos-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/util.py')
-rw-r--r--cloudinit/util.py52
1 files changed, 40 insertions, 12 deletions
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():