storage: Pass optional mount point to partition expansion

- Helps test cases.

- In future, we can resize non-root partitions.

Tests:

- On an amd64 disk image, apply this patch. Increase the image size. Boot the
image. During first setup. The root partition should get expanded successfully
and show full disk size.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-10-06 09:37:04 -07:00 committed by James Valleroy
parent 244d0ba50c
commit 5ff172aedd
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -33,6 +33,10 @@ def parse_arguments():
help='Expand a partition to take adjacent free space')
subparser.add_argument('device',
help='Partition which needs to be resized')
subparser.add_argument(
'--mount-point', default='/',
help=('Mount point which the device is mounted. '
'Needed for btrfs filesystems'))
subparser = subparsers.add_parser('mount', help='Mount a filesystem')
subparser.add_argument('--block-device',
@ -68,6 +72,7 @@ def subcommand_is_partition_expandable(arguments):
def subcommand_expand_partition(arguments):
"""Expand a partition to take adjacent free space."""
device = arguments.device
mount_point = arguments.mount_point
device, requested_partition, free_space = _get_free_space(device)
if requested_partition['table_type'] == 'msdos' and \
@ -80,7 +85,7 @@ def subcommand_expand_partition(arguments):
_move_gpt_second_header(device)
_resize_partition(device, requested_partition, free_space)
_resize_file_system(device, requested_partition, free_space)
_resize_file_system(device, requested_partition, free_space, mount_point)
def _move_gpt_second_header(device):
@ -126,15 +131,16 @@ def _resize_partition(device, requested_partition, free_space):
sys.exit(5)
def _resize_file_system(device, requested_partition, free_space):
def _resize_file_system(device, requested_partition, free_space,
mount_point='/'):
"""Resize a file system inside a partition."""
if requested_partition['type'] == 'btrfs':
_resize_btrfs(device, requested_partition, free_space)
_resize_btrfs(device, requested_partition, free_space, mount_point)
elif requested_partition['type'] == 'ext4':
_resize_ext4(device, requested_partition, free_space)
_resize_ext4(device, requested_partition, free_space, mount_point)
def _resize_ext4(device, requested_partition, free_space):
def _resize_ext4(device, requested_partition, _free_space, _mount_point):
"""Resize an ext4 file system inside a partition."""
partition_device = _get_partition_device(device,
requested_partition['number'])
@ -147,10 +153,10 @@ def _resize_ext4(device, requested_partition, free_space):
sys.exit(6)
def _resize_btrfs(device, requested_partition, free_space):
def _resize_btrfs(_device, _requested_partition, _free_space, mount_point='/'):
"""Resize a btrfs file system inside a partition."""
try:
command = ['btrfs', 'filesystem', 'resize', 'max', '/']
command = ['btrfs', 'filesystem', 'resize', 'max', mount_point]
subprocess.run(command, stdout=subprocess.DEVNULL, check=True)
except subprocess.CalledProcessError as exception:
print('Error expanding filesystem:', exception, file=sys.stderr)