From 3a7c13de6222e9abaa890067a7703c5c05377d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yao=20Wei=20=28=E9=AD=8F=E9=8A=98=E5=BB=B7=29?= Date: Mon, 18 Dec 2023 11:17:23 +0100 Subject: [PATCH] reset_partition accepts fixed size --- subiquity/common/types.py | 2 +- subiquity/server/controllers/filesystem.py | 7 +++- .../controllers/tests/test_filesystem.py | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/subiquity/common/types.py b/subiquity/common/types.py index 712f2699c2..0f2d3cf483 100644 --- a/subiquity/common/types.py +++ b/subiquity/common/types.py @@ -530,7 +530,7 @@ class GuidedChoiceV2: recovery_key: Optional[RecoveryKey] = None sizing_policy: Optional[SizingPolicy] = SizingPolicy.SCALED - reset_partition: bool = False + reset_partition: bool | int | str = False @attr.s(auto_attribs=True) diff --git a/subiquity/server/controllers/filesystem.py b/subiquity/server/controllers/filesystem.py index 9995e4e990..803496028e 100644 --- a/subiquity/server/controllers/filesystem.py +++ b/subiquity/server/controllers/filesystem.py @@ -29,6 +29,7 @@ from curtin import swap from curtin.commands.extract import AbstractSourceHandler from curtin.storage_config import ptable_part_type_to_flag +from curtin.util import human2bytes from subiquity.common.apidef import API from subiquity.common.errorreport import ErrorReportKind @@ -707,7 +708,11 @@ async def guided( raise Exception("failed to locate gap after adding boot") if choice.reset_partition: - if self.app.opts.dry_run: + if type(choice.reset_partition) in (int, str): + reset_size = int(human2bytes(choice.reset_partition)) + part_align = disk.alignment_data().part_align + reset_size = align_up(reset_size, part_align) + elif self.app.opts.dry_run: reset_size = DRY_RUN_RESET_SIZE else: cp = await arun_command(["du", "-sb", "/cdrom"]) diff --git a/subiquity/server/controllers/tests/test_filesystem.py b/subiquity/server/controllers/tests/test_filesystem.py index 4321601df1..9145a786ff 100644 --- a/subiquity/server/controllers/tests/test_filesystem.py +++ b/subiquity/server/controllers/tests/test_filesystem.py @@ -459,6 +459,44 @@ async def test_guided_reset_partition(self): self.assertEqual(DRY_RUN_RESET_SIZE, d1p2.size) self.assertEqual("/", d1p3.mount) + async def test_fixed_reset_partition(self): + await self._guided_setup(Bootloader.UEFI, "gpt") + target = GuidedStorageTargetReformat( + disk_id=self.d1.id, allowed=default_capabilities + ) + fixed_reset_size = 12 << 30 + await self.controller.guided( + GuidedChoiceV2( + target=target, + capability=GuidedCapability.DIRECT, + reset_partition=fixed_reset_size, + ) + ) + [d1p1, d1p2, d1p3] = self.d1.partitions() + self.assertEqual("/boot/efi", d1p1.mount) + self.assertEqual(None, d1p2.mount) + self.assertEqual(fixed_reset_size, d1p2.size) + self.assertEqual("/", d1p3.mount) + + async def test_fixed_reset_partition_human(self): + await self._guided_setup(Bootloader.UEFI, "gpt") + target = GuidedStorageTargetReformat( + disk_id=self.d1.id, allowed=default_capabilities + ) + fixed_reset_size = "12G" + await self.controller.guided( + GuidedChoiceV2( + target=target, + capability=GuidedCapability.DIRECT, + reset_partition=fixed_reset_size, + ) + ) + [d1p1, d1p2, d1p3] = self.d1.partitions() + self.assertEqual("/boot/efi", d1p1.mount) + self.assertEqual(None, d1p2.mount) + self.assertEqual(12 << 30, d1p2.size) + self.assertEqual("/", d1p3.mount) + async def test_guided_reset_partition_only(self): await self._guided_setup(Bootloader.UEFI, "gpt") target = GuidedStorageTargetReformat(