diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-02-07 09:52:49 +0100 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-02-08 17:11:44 +0100 |
commit | eac942660fde877a8594828e5784ba532dfc814e (patch) | |
tree | b3f709728d8a7018714f1ac70401df5882bae5dd | |
parent | 29c53e54e9459cd652c908e2e13b616778e6e4f1 (diff) |
utils: add fs.py
Adds utility module which wraps several mkfs.* calls as a subprocess.
The main utility function is mkfs(fs, disk, partition, label), which
subsequently calls the corresponding mkfs_*(partition_device) function.
mkfs() supports specifying a drive label where supported.
Other modules using fs.py should call mkfs() only.
-rw-r--r-- | src/utils/fs.py | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/utils/fs.py b/src/utils/fs.py index bce6022..7b0e1c3 100644 --- a/src/utils/fs.py +++ b/src/utils/fs.py @@ -9,11 +9,14 @@ import logging import os import subprocess +import shlex -from subprocess import DEVNULL, PIPE +from subprocess import DEVNULL, PIPE, STDOUT import psutil +from src.utils.disk import get_partition_device + def find_mountpoint(path): """ @@ -100,3 +103,65 @@ def ogExtendFs(disk, part): shell=True) if proc.returncode != 0: logging.warn(f'ogExtendFs exited with non zero code: {proc.returncode}') + + +def mkfs(fs, disk, partition, label=None): + """ + Install any supported filesystem. Target partition is specified a disk + number and partition number. This function uses utility functions to + translate disk and partition number into a partition device path. + + If filesystem and partition are correct, calls the corresponding mkfs_* + function with the partition device path. If not, ValueError is raised. + """ + logging.debug(f'mkfs({fs}, {disk}, {partition}, {label})') + fsdict = { + 'ext4': mkfs_ext4, + 'ntfs': mkfs_ntfs, + 'fat32': mkfs_fat32, + } + + if fs not in fsdict: + logging.warn(f'mkfs aborted, invalid target filesystem.') + raise ValueError('Invalid target filesystem') + + try: + partdev = get_partition_device(disk, partition) + except ValueError as e: + logging.warn(f'mkfs aborted, invalid partition.') + raise e + + fsdict[fs](partdev, label) + + +def mkfs_ext4(partdev, label=None): + if label: + cmd = shlex.split(f'mkfs.ext4 -L {label} -F {partdev}') + else: + cmd = shlex.split(f'mkfs.ext4 -F {partdev}') + with open('/tmp/command.log', 'wb', 0) as logfile: + subprocess.run(cmd, + stdout=logfile, + stderr=STDOUT) + + +def mkfs_ntfs(partdev, label=None): + if label: + cmd = shlex.split(f'mkfs.ntfs -f -L {label} {partdev}') + else: + cmd = shlex.split(f'mkfs.ntfs -f {partdev}') + with open('/tmp/command.log', 'wb', 0) as logfile: + subprocess.run(cmd, + stdout=logfile, + stderr=STDOUT) + + +def mkfs_fat32(partdev, label=None): + if label: + cmd = shlex.split(f'mkfs.vfat -n {label} -F32 {partdev}') + else: + cmd = shlex.split(f'mkfs.vfat -F32 {partdev}') + with open('/tmp/command.log', 'wb', 0) as logfile: + subprocess.run(cmd, + stdout=logfile, + stderr=STDOUT) |