def test_commit(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, { 'bootable': False, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 }), (2, { 'bootable': True, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 })] with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = (None, None) dp.commit() mock_disk_partitioner_exec.assert_called_once_with( mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') mock_utils_exc.assert_called_once_with('fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1])
def make_partitions(dev, root_mb, swap_mb, ephemeral_mb, commit=True): """Create partitions for root, swap and ephemeral on a disk device. :param root_mb: Size of the root partition in mebibytes (MiB). :param swap_mb: Size of the swap partition in mebibytes (MiB). If 0, no swap partition will be created. :param ephemeral_mb: Size of the ephemeral partition in mebibytes (MiB). If 0, no ephemeral partition will be created. :param commit: True/False. Default for this setting is True. If False partitions will not be written to disk. :returns: A dictionary containing the partition type as Key and partition path as Value for the partitions created by this method. """ part_template = dev + '-part%d' part_dict = {} dp = disk_partitioner.DiskPartitioner(dev) if ephemeral_mb: part_num = dp.add_partition(ephemeral_mb) part_dict['ephemeral'] = part_template % part_num if swap_mb: part_num = dp.add_partition(swap_mb, fs_type='linux-swap') part_dict['swap'] = part_template % part_num # NOTE(lucasagomes): Make the root partition the last partition. This # enables tools like cloud-init's growroot utility to expand the root # partition until the end of the disk. part_num = dp.add_partition(root_mb) part_dict['root'] = part_template % part_num if commit: # write to the disk dp.commit() return part_dict
def test_commit_with_device_is_busy_once(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, { 'bootable': False, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 }), (2, { 'bootable': True, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 })] fuser_outputs = iter([("/dev/fake: 10000 10001", None), (None, None)]) with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.side_effect = fuser_outputs dp.commit() mock_disk_partitioner_exec.assert_called_once_with( mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') mock_utils_exc.assert_called_with('fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1]) self.assertEqual(2, mock_utils_exc.call_count)
def test_add_partition(self): dp = disk_partitioner.DiskPartitioner('/dev/fake') dp.add_partition(1024) dp.add_partition(512, fs_type='linux-swap') dp.add_partition(2048, bootable=True) expected = [(1, { 'bootable': False, 'fs_type': '', 'type': 'primary', 'size': 1024 }), (2, { 'bootable': False, 'fs_type': 'linux-swap', 'type': 'primary', 'size': 512 }), (3, { 'bootable': True, 'fs_type': '', 'type': 'primary', 'size': 2048 })] partitions = [(n, p) for n, p in dp.get_partitions()] self.assertThat(partitions, HasLength(3)) self.assertEqual(expected, partitions)
def test_commit_with_device_disconnected(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, { 'bootable': False, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 }), (2, { 'bootable': True, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 })] with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = (None, "Specified filename /dev/fake" " does not exist.") self.assertRaises(exception.InstanceDeployFailure, dp.commit) mock_disk_partitioner_exec.assert_called_once_with( mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') mock_utils_exc.assert_called_with('fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1]) self.assertEqual(20, mock_utils_exc.call_count)
def test_commit_with_device_is_always_busy(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, { 'bootable': False, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 }), (2, { 'bootable': True, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 })] with mock.patch.object(dp, 'get_partitions') as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = ("/dev/fake: 10000 10001", None) self.assertRaises(exception.InstanceDeployFailure, dp.commit) mock_disk_partitioner_exec.assert_called_once_with( 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') mock_utils_exc.assert_called_with('fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1]) self.assertEqual(20, mock_utils_exc.call_count)
def make_partitions(dev, root_mb, swap_mb, ephemeral_mb): """Create partitions for root and swap on a disk device.""" dp = disk_partitioner.DiskPartitioner(dev) if ephemeral_mb: dp.add_partition(ephemeral_mb) dp.add_partition(swap_mb, fs_type='linux-swap') dp.add_partition(root_mb) else: dp.add_partition(root_mb) dp.add_partition(swap_mb, fs_type='linux-swap') dp.commit()
def test_commit(self, mock_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, { 'bootable': False, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 }), (2, { 'bootable': True, 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1 })] with mock.patch.object(dp, 'get_partitions') as mock_gp: mock_gp.return_value = fake_parts dp.commit() mock_exec.assert_called_once_with('mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on')
def make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios"): """Partition the disk device. Create partitions for root, swap, ephemeral and configdrive on a disk device. :param root_mb: Size of the root partition in mebibytes (MiB). :param swap_mb: Size of the swap partition in mebibytes (MiB). If 0, no partition will be created. :param ephemeral_mb: Size of the ephemeral partition in mebibytes (MiB). If 0, no partition will be created. :param configdrive_mb: Size of the configdrive partition in mebibytes (MiB). If 0, no partition will be created. :param commit: True/False. Default for this setting is True. If False partitions will not be written to disk. :param boot_option: Can be "local" or "netboot". "netboot" by default. :param boot_mode: Can be "bios" or "uefi". "bios" by default. :returns: A dictionary containing the partition type as Key and partition path as Value for the partitions created by this method. """ LOG.debug("Starting to partition the disk device: %(dev)s", {'dev': dev}) part_template = dev + '-part%d' part_dict = {} # For uefi localboot, switch partition table to gpt and create the efi # system partition as the first partition. if boot_mode == "uefi" and boot_option == "local": dp = disk_partitioner.DiskPartitioner(dev, disk_label="gpt") part_num = dp.add_partition(CONF.deploy.efi_system_partition_size, fs_type='fat32', bootable=True) part_dict['efi system partition'] = part_template % part_num else: dp = disk_partitioner.DiskPartitioner(dev) if ephemeral_mb: LOG.debug("Add ephemeral partition (%(size)d MB) to device: %(dev)s", { 'dev': dev, 'size': ephemeral_mb }) part_num = dp.add_partition(ephemeral_mb) part_dict['ephemeral'] = part_template % part_num if swap_mb: LOG.debug("Add Swap partition (%(size)d MB) to device: %(dev)s", { 'dev': dev, 'size': swap_mb }) part_num = dp.add_partition(swap_mb, fs_type='linux-swap') part_dict['swap'] = part_template % part_num if configdrive_mb: LOG.debug( "Add config drive partition (%(size)d MB) to device: " "%(dev)s", { 'dev': dev, 'size': configdrive_mb }) part_num = dp.add_partition(configdrive_mb) part_dict['configdrive'] = part_template % part_num # NOTE(lucasagomes): Make the root partition the last partition. This # enables tools like cloud-init's growroot utility to expand the root # partition until the end of the disk. LOG.debug("Add root partition (%(size)d MB) to device: %(dev)s", { 'dev': dev, 'size': root_mb }) part_num = dp.add_partition(root_mb, bootable=(boot_option == "local" and boot_mode == "bios")) part_dict['root'] = part_template % part_num if commit: # write to the disk dp.commit() return part_dict