def test_make_boot_disk_PREP(self): controller = make_controller(Bootloader.PREP) disk1 = make_disk(controller.model, preserve=False) disk2 = make_disk(controller.model, preserve=False) disk2p1 = controller.model.add_partition( disk2, size=disk2.free_for_partitions) controller.make_boot_disk(disk1) self.assertEqual(len(disk1.partitions()), 1) self.assertEqual(disk1.partitions()[0].flag, "prep") self.assertEqual(disk1.partitions()[0].wipe, "zero") self.assertEqual(controller.model.grub_install_device, disk1.partitions()[0]) size_before = disk2p1.size controller.make_boot_disk(disk2) self.assertEqual(len(disk1.partitions()), 0) self.assertEqual(len(disk2.partitions()), 2) self.assertEqual(disk2.partitions()[1], disk2p1) self.assertEqual(disk2.partitions()[0].size + disk2p1.size, size_before) self.assertEqual(disk2.partitions()[0].flag, "prep") self.assertEqual(disk2.partitions()[0].wipe, "zero") self.assertEqual(controller.model.grub_install_device, disk2.partitions()[0])
def test_boot_disk_no_resilient(self): for bl in Bootloader: if bl == Bootloader.NONE: continue manipulator = make_manipulator(bl) manipulator.supports_resilient_boot = False disk1 = make_disk(manipulator.model, preserve=False) disk2 = make_disk(manipulator.model, preserve=False) disk2p1 = manipulator.model.add_partition( disk2, size=disk2.free_for_partitions) manipulator.add_boot_disk(disk1) self.assertIsBootDisk(manipulator, disk1) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(disk1.partitions()[0]) size_before = disk2p1.size manipulator.add_boot_disk(disk2) self.assertIsNotBootDisk(manipulator, disk1) self.assertIsBootDisk(manipulator, disk2) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(disk2.partitions()[0]) self.assertEqual(len(disk2.partitions()), 2) self.assertEqual(disk2.partitions()[1], disk2p1) self.assertEqual(disk2.partitions()[0].size + disk2p1.size, size_before)
def test_make_boot_disk_UEFI_existing(self): controller = make_controller(Bootloader.UEFI) disk1 = make_disk(controller.model, preserve=True) disk1p1 = controller.model.add_partition(disk1, size=512 << 20, flag="boot") disk1p1.preserve = True disk2 = make_disk(controller.model, preserve=True) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, None) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt, None) controller.make_boot_disk(disk1) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, None) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt.device.volume, disk1p1) self.assertEqual(disk1p1.fs().fstype, "fat32") controller.make_boot_disk(disk2) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, None) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt.device.volume, disk2.partitions()[0])
def test_make_boot_disk_UEFI(self): controller = make_controller(Bootloader.UEFI) disk1 = make_disk(controller.model, preserve=False) disk2 = make_disk(controller.model, preserve=False) disk2p1 = controller.model.add_partition( disk2, size=disk2.free_for_partitions) controller.make_boot_disk(disk1) self.assertEqual(len(disk1.partitions()), 1) self.assertEqual(disk1.partitions()[0].flag, "boot") self.assertEqual(controller.model.grub_install_device, None) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt.device.volume, disk1.partitions()[0]) self.assertEqual(disk1.partitions()[0].fs().fstype, "fat32") size_before = disk2p1.size controller.make_boot_disk(disk2) self.assertEqual(len(disk1.partitions()), 0) self.assertEqual(len(disk2.partitions()), 2) self.assertEqual(disk2.partitions()[1], disk2p1) self.assertEqual(disk2.partitions()[0].size + disk2p1.size, size_before) self.assertEqual(disk2.partitions()[0].flag, "boot") self.assertEqual(controller.model.grub_install_device, None) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt.device.volume, disk2.partitions()[0])
def test_partition_action_DELETE(self): model = make_model() part1 = make_partition(model) self.assertActionPossible(part1, DeviceAction.DELETE) fs = model.add_filesystem(part1, 'ext4') self.assertActionPossible(part1, DeviceAction.DELETE) model.add_mount(fs, '/') self.assertActionPossible(part1, DeviceAction.DELETE) part2 = make_partition(model) model.add_volgroup('vg1', {part2}) self.assertActionNotPossible(part2, DeviceAction.DELETE) part = make_partition(make_model(Bootloader.BIOS), flag='bios_grub') self.assertActionNotPossible(part, DeviceAction.DELETE) part = make_partition(make_model(Bootloader.UEFI), flag='boot') self.assertActionNotPossible(part, DeviceAction.DELETE) part = make_partition(make_model(Bootloader.PREP), flag='prep') self.assertActionNotPossible(part, DeviceAction.DELETE) # You cannot delete a partition from a disk that has # pre-existing partitions (only reformat) disk2 = make_disk(model, preserve=True) disk2p1 = make_partition(model, disk2, preserve=True) self.assertActionNotPossible(disk2p1, DeviceAction.DELETE)
def test_disk_action_FORMAT(self): model, disk = make_model_and_disk() self.assertActionPossible(disk, DeviceAction.FORMAT) make_partition(model, disk) self.assertActionNotPossible(disk, DeviceAction.FORMAT) disk2 = make_disk(model) model.add_volgroup('vg1', {disk2}) self.assertActionNotPossible(disk2, DeviceAction.FORMAT)
def test_make_boot_disk_BIOS_existing(self): controller = make_controller(Bootloader.BIOS) disk1 = make_disk(controller.model, preserve=True) disk1p1 = controller.model.add_partition(disk1, size=1 << 20, flag="bios_grub") disk1p1.preserve = True disk2 = make_disk(controller.model, preserve=False) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, None) controller.make_boot_disk(disk1) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, disk1) controller.make_boot_disk(disk2) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, disk2)
def test_boot_disk_resilient(self): for bl in Bootloader: if bl == Bootloader.NONE: continue controller = make_controller(bl) controller.supports_resilient_boot = True disk1 = make_disk(controller.model, preserve=False) disk2 = make_disk(controller.model, preserve=False) disk2p1 = controller.model.add_partition( disk2, size=disk2.free_for_partitions) controller.add_boot_disk(disk1) self.assertIsBootDisk(controller, disk1) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(disk1.partitions()[0]) size_before = disk2p1.size controller.add_boot_disk(disk2) self.assertIsBootDisk(controller, disk1) self.assertIsBootDisk(controller, disk2) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(disk1.partitions()[0]) self.assertNotMounted(disk2.partitions()[0]) self.assertEqual(len(disk2.partitions()), 2) self.assertEqual(disk2.partitions()[1], disk2p1) self.assertEqual( disk2.partitions()[0].size + disk2p1.size, size_before) controller.remove_boot_disk(disk1) self.assertIsNotBootDisk(controller, disk1) self.assertIsBootDisk(controller, disk2) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(disk2.partitions()[0]) self.assertEqual(len(disk1.partitions()), 0) controller.remove_boot_disk(disk2) self.assertIsNotBootDisk(controller, disk2) self.assertEqual(len(disk2.partitions()), 1) self.assertEqual(disk2p1.size, size_before)
def test_disk_action_TOGGLE_BOOT_BIOS(self): model = make_model(Bootloader.BIOS) # Disks with msdos partition tables can always be the BIOS boot disk. dos_disk = make_disk(model, ptable='msdos', preserve=True) self.assertActionPossible(dos_disk, DeviceAction.TOGGLE_BOOT) # Even if they have existing partitions make_partition(model, dos_disk, size=dos_disk.free_for_partitions, preserve=True) self.assertActionPossible(dos_disk, DeviceAction.TOGGLE_BOOT) # (we never create dos partition tables so no need to test # preserve=False case). # GPT disks with new partition tables can always be the BIOS boot disk gpt_disk = make_disk(model, ptable='gpt', preserve=False) self.assertActionPossible(gpt_disk, DeviceAction.TOGGLE_BOOT) # Even if they are filled with partitions (we resize partitions to fit) make_partition(model, gpt_disk, size=dos_disk.free_for_partitions) self.assertActionPossible(gpt_disk, DeviceAction.TOGGLE_BOOT) # GPT disks with existing partition tables but no partitions can be the # BIOS boot disk (in general we ignore existing empty partition tables) gpt_disk2 = make_disk(model, ptable='gpt', preserve=True) self.assertActionPossible(gpt_disk2, DeviceAction.TOGGLE_BOOT) # If there is an existing *partition* though, it cannot be the boot # disk make_partition(model, gpt_disk2, preserve=True) self.assertActionNotPossible(gpt_disk2, DeviceAction.TOGGLE_BOOT) # Unless there is already a bios_grub partition we can reuse gpt_disk3 = make_disk(model, ptable='gpt', preserve=True) make_partition(model, gpt_disk3, flag="bios_grub", preserve=True) make_partition(model, gpt_disk3, preserve=True) self.assertActionPossible(gpt_disk3, DeviceAction.TOGGLE_BOOT) # Edge case city: the bios_grub partition has to be first gpt_disk4 = make_disk(model, ptable='gpt', preserve=True) make_partition(model, gpt_disk4, preserve=True) make_partition(model, gpt_disk4, flag="bios_grub", preserve=True) self.assertActionNotPossible(gpt_disk4, DeviceAction.TOGGLE_BOOT)
def test_mounting_partition_makes_boot_disk(self): controller = make_controller(Bootloader.UEFI) disk1 = make_disk(controller.model, preserve=True) disk1p1 = controller.model.add_partition( disk1, size=512 << 20, flag="boot") disk1p1.preserve = True disk1p2 = controller.model.add_partition( disk1, size=disk1.free_for_partitions) disk1p2.preserve = True controller.partition_disk_handler( disk1, disk1p2, {'fstype': 'ext4', 'mount': '/'}) efi_mnt = controller.model._mount_for_path("/boot/efi") self.assertEqual(efi_mnt.device.volume, disk1p1)
def _test_TOGGLE_BOOT_boot_partition(self, bl, flag): # The logic for when TOGGLE_BOOT is enabled for both UEFI and PREP # bootloaders turns out to be the same, modulo the special flag that # has to be present on a partition. model = make_model(bl) # A disk with a new partition table can always be the UEFI/PREP boot # disk. new_disk = make_disk(model, preserve=False) self.assertActionPossible(new_disk, DeviceAction.TOGGLE_BOOT) # Even if they are filled with partitions (we resize partitions to fit) make_partition(model, new_disk, size=new_disk.free_for_partitions) self.assertActionPossible(new_disk, DeviceAction.TOGGLE_BOOT) # A disk with an existing but empty partitions can also be the # UEFI/PREP boot disk. old_disk = make_disk(model, preserve=True, ptable='gpt') self.assertActionPossible(old_disk, DeviceAction.TOGGLE_BOOT) # If there is an existing partition though, it cannot. make_partition(model, old_disk, preserve=True) self.assertActionNotPossible(old_disk, DeviceAction.TOGGLE_BOOT) # If there is an existing ESP/PReP partition though, fine! make_partition(model, old_disk, flag=flag, preserve=True) self.assertActionPossible(old_disk, DeviceAction.TOGGLE_BOOT)
def test_make_boot_disk_PREP_existing(self): controller = make_controller(Bootloader.PREP) disk1 = make_disk(controller.model, preserve=True) disk1p1 = controller.model.add_partition(disk1, size=8 << 20, flag="prep") disk1p1.preserve = True disk2 = make_disk(controller.model, preserve=False) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, None) controller.make_boot_disk(disk1) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(controller.model.grub_install_device, disk1p1) self.assertEqual(disk1p1.wipe, 'zero') controller.make_boot_disk(disk2) self.assertEqual(disk1.partitions(), [disk1p1]) self.assertEqual(disk1p1.wipe, None) self.assertEqual(controller.model.grub_install_device, disk2.partitions()[0]) self.assertEqual(disk2.partitions()[0].flag, "prep") self.assertEqual(controller.model.grub_install_device, disk2.partitions()[0])
def test_disk_action_REFORMAT(self): model = make_model() disk1 = make_disk(model, preserve=False) self.assertActionNotPossible(disk1, DeviceAction.REFORMAT) disk1p1 = make_partition(model, disk1, preserve=False) self.assertActionPossible(disk1, DeviceAction.REFORMAT) model.add_volgroup('vg0', {disk1p1}) self.assertActionNotPossible(disk1, DeviceAction.REFORMAT) disk2 = make_disk(model, preserve=True) self.assertActionNotPossible(disk2, DeviceAction.REFORMAT) disk2p1 = make_partition(model, disk2, preserve=True) self.assertActionPossible(disk2, DeviceAction.REFORMAT) model.add_volgroup('vg1', {disk2p1}) self.assertActionNotPossible(disk2, DeviceAction.REFORMAT) disk3 = make_disk(model, preserve=False) model.add_volgroup('vg2', {disk3}) self.assertActionNotPossible(disk3, DeviceAction.REFORMAT) disk4 = make_disk(model, preserve=True) model.add_volgroup('vg2', {disk4}) self.assertActionNotPossible(disk4, DeviceAction.REFORMAT)
def test_disk_action_PARTITION(self): model, disk = make_model_and_disk() self.assertActionPossible(disk, DeviceAction.PARTITION) make_partition(model, disk, size=disk.free_for_partitions // 2) self.assertActionPossible(disk, DeviceAction.PARTITION) make_partition(model, disk, size=disk.free_for_partitions) self.assertActionNotPossible(disk, DeviceAction.PARTITION) # Can partition a disk with .preserve=True disk2 = make_disk(model) disk2.preserve = True self.assertActionPossible(disk2, DeviceAction.PARTITION) # But not if it has a partition. make_partition(model, disk2, preserve=True) self.assertActionNotPossible(disk2, DeviceAction.PARTITION)
def test_boot_disk_existing(self): for bl in Bootloader: if bl == Bootloader.NONE: continue manipulator = make_manipulator(bl) disk1 = make_disk(manipulator.model, preserve=True) part = self.add_existing_boot_partition(manipulator, disk1) wipe_before = part.wipe manipulator.add_boot_disk(disk1) self.assertIsBootDisk(manipulator, disk1) if bl == Bootloader.UEFI: self.assertIsMountedAtBootEFI(part) manipulator.remove_boot_disk(disk1) self.assertIsNotBootDisk(manipulator, disk1) self.assertEqual(len(disk1.partitions()), 1) self.assertEqual(part.wipe, wipe_before) if bl == Bootloader.UEFI: self.assertNotMounted(part)
def make_manipulator_and_disk(bootloader=None): manipulator = make_manipulator(bootloader) return manipulator, make_disk(manipulator.model)
def make_controller_and_disk(bootloader=None): controller = make_controller(bootloader) return controller, make_disk(controller.model)
def test_disk_action_REMOVE(self): model = make_model() disks = [make_disk(model) for i in range(5)] self._test_remove_action(model, disks)