From eac942660fde877a8594828e5784ba532dfc814e Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Tue, 7 Feb 2023 09:52:49 +0100 Subject: 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. --- src/utils/fs.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'src/utils') 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) -- cgit v1.2.3-18-g5258