def create_new_root(storage, boot_drive): """Create a new root from the given devices. :param storage: an instance of Blivet :param boot_drive: a name of the bootloader drive :return: a new root """ devices = filter_unsupported_disklabel_devices( collect_new_devices(storage=storage, boot_drive=boot_drive)) bootloader_devices = filter_unsupported_disklabel_devices( collect_bootloader_devices(storage=storage, boot_drive=boot_drive)) swaps = [d for d in devices if d.format.type == "swap"] mounts = { d.format.mountpoint: d for d in devices if getattr(d.format, "mountpoint", None) } for device in devices: if device in bootloader_devices: mounts[device.format.name] = device return Root(name=get_new_root_name(), mounts=mounts, swaps=swaps)
def collect_unused_devices(storage): """Collect devices that are not used in existing or new installations. :param storage: an instance of Blivet :return: a list of devices """ used_devices = set(collect_used_devices(storage)) unused = [ d for d in storage.devices if d.disks and d.media_present and not d.partitioned and ( d.direct or d.isleaf) and d not in used_devices ] # Add incomplete VGs and MDs incomplete = [ d for d in storage.devicetree._devices if not getattr(d, "complete", True) ] # Add partitioned devices with unsupported format. unsupported = [d for d in storage.partitioned if not d.format.supported] return filter_unsupported_disklabel_devices(unused + incomplete + unsupported)
def collect_new_devices(storage, boot_drive): """Collect new devices. :param storage: an instance of Blivet :param boot_drive: a name of the bootloader drive :return: a list of devices """ # A device scheduled for formatting only belongs in the new root. new_devices = [ d for d in storage.devices if d.direct and not d.format.exists and not d.partitioned ] # If mount points have been assigned to any existing devices, go ahead # and pull those in along with any existing swap devices. It doesn't # matter if the formats being mounted exist or not. new_mounts = [d for d in storage.mountpoints.values() if d.exists] if new_mounts or new_devices: new_devices.extend(storage.mountpoints.values()) new_devices.extend(collect_bootloader_devices(storage, boot_drive)) # Remove duplicates, but keep the order. return filter_unsupported_disklabel_devices( list(dict.fromkeys(new_devices)))
def collect_bootloader_devices(storage, boot_drive): """Collect the bootloader devices. :param storage: an instance of Blivet :param boot_drive: a name of the bootloader drive :return: a list of devices """ devices = [] for device in storage.devices: if device.format.type not in ["biosboot", "prepboot"]: continue # Boot drive may not be setup because it IS one of these. if not boot_drive or boot_drive in (d.name for d in device.disks): devices.append(device) return filter_unsupported_disklabel_devices(devices)
def collect_roots(storage): """Collect roots of existing installations. :param storage: an instance of Blivet :return: a list of roots """ roots = [] supported_devices = set(filter_unsupported_disklabel_devices(storage.devices)) # Get the name of the new installation. new_root_name = get_new_root_name() for root in storage.roots: # Get the name. name = root.name # Get the supported swap devices. swaps = [ d for d in root.swaps if d in supported_devices and (d.format.exists or root.name == new_root_name) ] # Get the supported mount points. mounts = { m: d for m, d in root.mounts.items() if d in supported_devices and (d.format.exists or root.name == new_root_name) and d.disks } if not swaps and not mounts: continue # Add a root with supported devices. roots.append(Root( name=name, mounts=mounts, swaps=swaps )) return roots