diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-05-02 17:16:52 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-05-09 13:09:02 +0200 |
commit | 803ba74510df3400da4d5d02351f3858460cffda (patch) | |
tree | ed28d5ca7811b1e7abbc5b6e8b89307a1e8f58e9 /src/utils/fs.py | |
parent | dd999bfe34e7f812d4ce61828f262ee06b5d4fa4 (diff) |
utils: rewrite ogExtendFs
Drop subprocess call to bash function ogExtendFs. Use a native python
solution with subprocess calls to the required underlying tools.
Use get_filesystem_type to get the present filesystem from a partition
and call the corresponding filesystem grow function.
Filesystem specific functions are declared "_extend_{filesystem}" and
should not be imported elsewhere.
Each filesystem specific function wraps a subprocess call to the
required underlying program:
- NTFS filesystems: "ntfsresize -f [partition]"
- ext4 filesystems: "resize2fs -f [partition]"
Set NTFS related subprocess stdin to 'y' because human input cannot be
unset with other ntfsresize parameters.
Diffstat (limited to 'src/utils/fs.py')
-rw-r--r-- | src/utils/fs.py | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/utils/fs.py b/src/utils/fs.py index bffdb8c..a0c12d4 100644 --- a/src/utils/fs.py +++ b/src/utils/fs.py @@ -101,14 +101,21 @@ def ogReduceFs(disk, part): def ogExtendFs(disk, part): """ - Bash function 'ogExtendFs' wrapper + Grow filesystem of a partition. Supports ext4 and ntfs partitions. + Unsupported filesystem or invalid paths don't raise an exception, + instead this method logs a warning message and does nothing. """ - subprocess.run(f'ogMount {disk} {part}', - shell=True) - proc = subprocess.run(f'ogExtendFs {disk} {part}', - shell=True) - if proc.returncode != 0: - logging.warn(f'ogExtendFs exited with non zero code: {proc.returncode}') + partdev = get_partition_device(disk, part) + fstype = get_filesystem_type(partdev) + + umount(partdev) + if fstype == 'ext4': + _extend_resize2fs(partdev) + elif fstype == 'ntfs': + _extend_ntfsresize(partdev) + else: + logging.warn(f'Unable to grow filesystem at {partdev}. ' + f'Unsupported filesystem "{fstype}".') def mkfs(fs, disk, partition, label=None): @@ -220,3 +227,17 @@ def _reduce_ntfsresize(partdev): cmd_resize = shlex.split(f'ntfsresize -fs {new_size:.0f} {partdev}') with open('/tmp/command.log', 'ab', 0) as logfile: subprocess.run(cmd_resize, input='y', stderr=STDOUT, encoding='utf-8') + + +def _extend_resize2fs(partdev): + cmd_resize2fs = shlex.split(f'resize2fs -f {partdev}') + proc = subprocess.run(cmd_resize2fs) + if proc.returncode != 0: + raise RuntimeError(f'Error growing ext4 filesystem at {partdev}') + + +def _extend_ntfsresize(partdev): + cmd_ntfsresize = shlex.split(f'ntfsresize -f {partdev}') + proc = subprocess.run(cmd_resize2fs, input='y') + if proc.returncode != 0: + raise RuntimeError(f'Error growing ntfs filesystem at {partdev}') |