3 from ceph_volume import process
6 def get_partuuid(device):
8 If a device is a partition, it will probably have a PARTUUID on it that
9 will persist and can be queried against `blkid` later to detect the actual
12 out, err, rc = process.call(
13 ['blkid', '-s', 'PARTUUID', '-o', 'value', device]
15 return ' '.join(out).strip()
18 def get_device_from_partuuid(partuuid):
20 If a device has a partuuid, query blkid so that it can tell us what that
23 out, err, rc = process.call(
24 ['blkid', '-t', 'PARTUUID="%s"' % partuuid, '-o', 'device']
26 return ' '.join(out).strip()
29 def _stat_is_device(stat_obj):
31 Helper function that will interpret ``os.stat`` output directly, so that other
32 functions can call ``os.stat`` once and interpret that result several times
34 return stat.S_ISBLK(stat_obj)
37 def lsblk(device, columns=None):
39 Create a dictionary of identifying values for a device using ``lsblk``.
40 Each supported column is a key, in its *raw* format (all uppercase
41 usually). ``lsblk`` has support for certain "columns" (in blkid these
42 would be labels), and these columns vary between distributions and
43 ``lsblk`` versions. The newer versions support a richer set of columns,
44 while older ones were a bit limited.
46 These are the default lsblk columns reported which are safe to use for
50 KNAME internal kernel device name
51 MAJ:MIN major:minor device number
52 FSTYPE filesystem type
53 MOUNTPOINT where the device is mounted
54 LABEL filesystem LABEL
58 MODEL device identifier
59 SIZE size of the device
60 STATE state of the device
63 MODE device node permissions
64 ALIGNMENT alignment offset
65 MIN-IO minimum I/O size
66 OPT-IO optimal I/O size
67 PHY-SEC physical sector size
68 LOG-SEC logical sector size
69 ROTA rotational device
70 SCHED I/O scheduler name
71 RQ-SIZE request queue size
73 DISC-ALN discard alignment offset
74 DISC-GRAN discard granularity
75 DISC-MAX discard max bytes
76 DISC-ZERO discard zeroes data
78 There is a bug in ``lsblk`` where using all the available (supported)
79 columns will result in no output (!), in order to workaround this the
80 following columns have been removed from the default reporting columns:
82 * RQ-SIZE (request queue size)
83 * MIN-IO minimum I/O size
84 * OPT-IO optimal I/O size
86 These should be available however when using `columns`. For example::
88 >>> lsblk('/dev/sda1', columns=['OPT-IO'])
91 Normal CLI output, as filtered by the flags in this function will look like ::
93 $ lsblk --nodeps -P -o NAME,KNAME,MAJ:MIN,FSTYPE,MOUNTPOINT
94 NAME="sda1" KNAME="sda1" MAJ:MIN="8:1" FSTYPE="ext4" MOUNTPOINT="/"
96 :param columns: A list of columns to report as keys in its original form.
99 'NAME', 'KNAME', 'MAJ:MIN', 'FSTYPE', 'MOUNTPOINT', 'LABEL', 'UUID',
100 'RO', 'RM', 'MODEL', 'SIZE', 'STATE', 'OWNER', 'GROUP', 'MODE',
101 'ALIGNMENT', 'PHY-SEC', 'LOG-SEC', 'ROTA', 'SCHED', 'TYPE', 'DISC-ALN',
102 'DISC-GRAN', 'DISC-MAX', 'DISC-ZERO'
104 device = device.rstrip('/')
105 columns = columns or default_columns
106 # --nodeps -> Avoid adding children/parents to the device, only give information
107 # on the actual device we are querying for
108 # -P -> Produce pairs of COLUMN="value"
109 # -o -> Use the columns specified or default ones provided by this function
110 command = ['lsblk', '--nodeps', '-P', '-o']
111 command.append(','.join(columns))
112 command.append(device)
113 out, err, rc = process.call(command)
118 # parse the COLUMN="value" output to construct the dictionary
119 pairs = ' '.join(out).split()
123 column, value = pair.split('=')
126 parsed[column] = value.strip().strip().strip('"')
130 def _lsblk_type(device):
132 Helper function that will use the ``TYPE`` label output of ``lsblk`` to determine
133 if a device is a partition or disk.
134 It does not process the output to return a boolean, but it does process it to return the
136 out, err, rc = process.call(
137 ['blkid', '-s', 'PARTUUID', '-o', 'value', device]
139 return ' '.join(out).strip()
144 Boolean to determine if a given device is a block device (**not**
147 For example: /dev/sda would return True, but not /dev/sdc1
149 if not os.path.exists(dev):
151 # use lsblk first, fall back to using stat
152 TYPE = lsblk(dev).get('TYPE')
154 return TYPE == 'disk'
157 return _stat_is_device(os.lstat(dev).st_mode)
158 if stat.S_ISBLK(os.lstat(dev)):
163 def is_partition(dev):
165 Boolean to determine if a given device is a partition, like /dev/sda1
167 if not os.path.exists(dev):
169 # use lsblk first, fall back to using stat
170 TYPE = lsblk(dev).get('TYPE')
172 return TYPE == 'part'
175 stat_obj = os.stat(dev)
176 if _stat_is_device(stat_obj.st_mode):
179 major = os.major(stat_obj.st_rdev)
180 minor = os.minor(stat_obj.st_rdev)
181 if os.path.exists('/sys/dev/block/%d:%d/partition' % (major, minor)):