2018-06-14 23:43:06 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
class StorageChecker:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.actions = {}
|
|
|
|
self.unmounted_swap_ids = set()
|
2018-07-07 09:41:30 +00:00
|
|
|
self.path_to_mount = {}
|
2018-06-14 23:43:06 +00:00
|
|
|
|
|
|
|
def _check_partition(self, action):
|
|
|
|
assert 'device' in action
|
2018-07-07 08:59:47 +00:00
|
|
|
assert 'size' in action
|
2018-06-14 23:43:06 +00:00
|
|
|
assert action['device'] in self.actions
|
|
|
|
assert 'ptable' in self.actions[action['device']]
|
2019-05-01 22:45:57 +00:00
|
|
|
if action['flag'] in ('boot', 'bios_grub', 'prep'):
|
2018-07-07 09:41:30 +00:00
|
|
|
assert self.actions[action['device']]['type'] == 'disk'
|
2020-04-13 21:40:21 +00:00
|
|
|
if action.get('preserve', True):
|
|
|
|
assert 'number' in action
|
2018-06-14 23:43:06 +00:00
|
|
|
|
|
|
|
def _check_format(self, action):
|
|
|
|
assert 'volume' in action
|
|
|
|
assert action['volume'] in self.actions
|
|
|
|
if action['fstype'] == 'swap':
|
|
|
|
self.unmounted_swap_ids.add(action['id'])
|
|
|
|
|
|
|
|
def _check_mount(self, action):
|
|
|
|
assert 'device' in action
|
|
|
|
assert action['device'] in self.actions
|
|
|
|
if not action.get('path'):
|
|
|
|
assert self.actions[action['device']]['fstype'] == "swap"
|
|
|
|
self.unmounted_swap_ids.remove(action['device'])
|
2018-07-07 09:41:30 +00:00
|
|
|
else:
|
|
|
|
self.path_to_mount[action['path']] = action
|
2018-06-14 23:43:06 +00:00
|
|
|
|
2018-07-07 08:59:47 +00:00
|
|
|
def _check_raid(self, action):
|
|
|
|
assert 'raidlevel' in action
|
|
|
|
for d in action['devices']:
|
|
|
|
assert d in self.actions
|
|
|
|
|
|
|
|
def _check_lvm_volgroup(self, action):
|
|
|
|
assert 'name' in action
|
|
|
|
for d in action['devices']:
|
|
|
|
assert d in self.actions
|
|
|
|
|
|
|
|
def _check_lvm_partition(self, action):
|
|
|
|
assert 'name' in action
|
|
|
|
assert 'size' in action
|
|
|
|
assert isinstance(action['size'], str)
|
|
|
|
assert action['volgroup'] in self.actions
|
|
|
|
|
2018-06-14 23:43:06 +00:00
|
|
|
def check(self, action):
|
|
|
|
assert 'type' in action
|
|
|
|
assert 'id' in action
|
|
|
|
m = getattr(self, '_check_{type}'.format(**action), None)
|
|
|
|
if m:
|
|
|
|
m(action)
|
|
|
|
self.actions[action['id']] = action
|
|
|
|
|
|
|
|
def final_checks(self):
|
2018-07-07 09:41:30 +00:00
|
|
|
# Check we mounted all the swap devices
|
2018-06-14 23:43:06 +00:00
|
|
|
if len(self.unmounted_swap_ids) > 0:
|
|
|
|
raise Exception(
|
|
|
|
"some swap formats had no mounts: {}".format(
|
|
|
|
self.unmounted_swap_ids))
|
|
|
|
|
2018-07-07 09:41:30 +00:00
|
|
|
# Check we mounted /
|
|
|
|
assert '/' in self.path_to_mount
|
|
|
|
|
|
|
|
# Check /boot is on a physical disk
|
|
|
|
if '/boot' in self.path_to_mount:
|
|
|
|
boot_mount = self.path_to_mount['/boot']
|
|
|
|
else:
|
|
|
|
boot_mount = self.path_to_mount['/']
|
|
|
|
boot_fs = self.actions[boot_mount['device']]
|
|
|
|
boot_part = self.actions[boot_fs['volume']]
|
|
|
|
assert boot_part['type'] == 'partition', boot_part['type']
|
|
|
|
boot_device = self.actions[boot_part['device']]
|
|
|
|
assert boot_device['type'] == 'disk'
|
|
|
|
|
2018-06-14 23:43:06 +00:00
|
|
|
|
|
|
|
config = yaml.safe_load(open(sys.argv[1]))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
storage_checker = StorageChecker()
|
|
|
|
|
|
|
|
for action in config['storage']['config']:
|
|
|
|
try:
|
|
|
|
storage_checker.check(action)
|
|
|
|
except Exception:
|
|
|
|
print('checking {} failed'.format(action))
|
|
|
|
raise
|
|
|
|
|
|
|
|
storage_checker.final_checks()
|
|
|
|
|
2020-04-01 03:54:46 +00:00
|
|
|
for arg in sys.argv[2:]:
|
|
|
|
k, expected = arg.split('=', 1)
|
|
|
|
expected = yaml.safe_load(expected)
|
|
|
|
v = config
|
|
|
|
for part in k.split('.'):
|
|
|
|
v = v[part]
|
|
|
|
assert v == expected, "{!r} != {!r}".format(v, expected)
|
2018-06-14 23:43:06 +00:00
|
|
|
|
|
|
|
main()
|