From 970525f07e1f5def72a5ac442554511a0c7ac528 Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Mon, 9 Jan 2023 12:08:56 -0700 Subject: [PATCH] util: move device filtering With multiple functions filtering out MAJORs of 1 or 7, move this function to utils and use it. --- probert/filesystem.py | 31 ++++++++++++++----------------- probert/storage.py | 18 +----------------- probert/utils.py | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/probert/filesystem.py b/probert/filesystem.py index 38f9b55..fa6c82e 100644 --- a/probert/filesystem.py +++ b/probert/filesystem.py @@ -21,7 +21,7 @@ import pyudev -from probert.utils import sane_block_devices +from probert.utils import interesting_storage_devs log = logging.getLogger('probert.filesystems') @@ -194,21 +194,18 @@ def probe(context=None, enabled_probes=None, **kw): need_fs_sizing = 'filesystem_sizing' in enabled_probes - for device in sane_block_devices(context): - # Ignore block major=1 (ramdisk) and major=7 (loopback) - # these won't ever be used in recreating storage on target systems. - if device['MAJOR'] not in ["1", "7"]: - fs_info = get_device_filesystem(device, need_fs_sizing) - # The ID_FS_ udev values come from libblkid, which contains code to - # recognize lots of different things that block devices or their - # partitions can contain (filesystems, lvm PVs, bcache, ...). We - # only want to report things that are mountable filesystems here, - # which libblkid conveniently tags with ID_FS_USAGE=filesystem. - # Swap is a bit of a special case because it is not a mountable - # filesystem in the usual sense, but subiquity still needs to - # generate mount actions for it. Crypto is a disguised filesystem. - if fs_info.get("USAGE") in ("filesystem", "crypto") or \ - fs_info.get("TYPE") == "swap": - filesystems[device['DEVNAME']] = fs_info + for device in interesting_storage_devs(context): + fs_info = get_device_filesystem(device, need_fs_sizing) + # The ID_FS_ udev values come from libblkid, which contains code to + # recognize lots of different things that block devices or their + # partitions can contain (filesystems, lvm PVs, bcache, ...). We + # only want to report things that are mountable filesystems here, + # which libblkid conveniently tags with ID_FS_USAGE=filesystem. + # Swap is a bit of a special case because it is not a mountable + # filesystem in the usual sense, but subiquity still needs to + # generate mount actions for it. Crypto is a disguised filesystem. + if fs_info.get("USAGE") in ("filesystem", "crypto") or \ + fs_info.get("TYPE") == "swap": + filesystems[device['DEVNAME']] = fs_info return filesystems diff --git a/probert/storage.py b/probert/storage.py index be1ceac..c234046 100644 --- a/probert/storage.py +++ b/probert/storage.py @@ -21,7 +21,7 @@ from probert.utils import ( read_sys_block_size_bytes, - sane_block_devices, + interesting_storage_devs, udev_get_attributes, ) from probert import (bcache, dasd, dmcrypt, filesystem, lvm, mount, multipath, @@ -90,22 +90,6 @@ def is_virtual(self): return self.devpath.startswith('/devices/virtual/') -def interesting_storage_devs(context): - skip_majors = ( - '1', # ignore ram disks - '7', # ignore loopback devices - ) - - for device in sane_block_devices(context): - if device['MAJOR'] in skip_majors: - continue - major, minor = device.get('ID_PART_ENTRY_DISK', '0:0').split(':') - if major in skip_majors: - # also skip partitions that are on a device we don't want - continue - yield device - - def blockdev_probe(context=None, **kw): """ Non-class method for extracting relevant block devices from pyudev.Context(). diff --git a/probert/utils.py b/probert/utils.py index 64aabcf..d20f1ef 100644 --- a/probert/utils.py +++ b/probert/utils.py @@ -250,3 +250,19 @@ def sane_block_devices(context): # Shouldn't happen but apparently does! (LP: #1868109) continue yield device + + +def interesting_storage_devs(context): + skip_majors = ( + '1', # ignore ram disks + '7', # ignore loopback devices + ) + + for device in sane_block_devices(context): + if device['MAJOR'] in skip_majors: + continue + major, minor = device.get('ID_PART_ENTRY_DISK', '0:0').split(':') + if major in skip_majors: + # also skip partitions that are on a device we don't want + continue + yield device