refs #1332 add ogReduceFs, fix bugs

pull/1/head
Natalia Serrano 2025-01-28 15:42:45 +01:00
parent 6538fe90b3
commit ad9c498e38
2 changed files with 96 additions and 62 deletions

View File

@ -253,9 +253,9 @@ def ogFormatFs (disk, par, fs=None, label=None):
subprocess.run (['umount', PART]) subprocess.run (['umount', PART])
try: try:
if input: if input:
errcode = subprocess.run ([prog] + params.split (' ') + [PART]) errcode = subprocess.run ([prog] + params.split (' ') + [PART]).returncode
else: else:
errcode = subprocess.run ([prog] + params.split (' ') + [PART], input=input, text=True) errcode = subprocess.run ([prog] + params.split (' ') + [PART], input=input, text=True).returncode
except FileNotFoundError: except FileNotFoundError:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTEXEC, prog) SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTEXEC, prog)
errcode = ogGlobals.OG_ERR_NOTEXEC errcode = ogGlobals.OG_ERR_NOTEXEC
@ -681,71 +681,82 @@ def ogMountCdrom():
#@warning El sistema de archivos se amplía al mínimo + 10%. #@warning El sistema de archivos se amplía al mínimo + 10%.
#@note Requisitos: *resize* #@note Requisitos: *resize*
#*/ ## #*/ ##
def ogReduceFs(int_ndisk, int_nfilesys): def ogReduceFs (disk, par):
PART = DiskLib.ogDiskToDev (disk, par)
# Error si no se reciben 2 parámetros. if not PART: return
if len(sys.argv) != 3:
SystemLib.ogRaiseError (
[],
ogGlobals.OG_ERR_FORMAT,
"Not enough arguments"
)
return
# Obtener partición.
PART = DiskLib.ogDiskToDev(int_ndisk, int_nfilesys)
if not PART:
return
# Redimensionar según el tipo de partición. # Redimensionar según el tipo de partición.
TYPE = ogGetFsType(int_ndisk, int_nfilesys) type = ogGetFsType (disk, par)
if TYPE == "EXT4": if type in ['EXT2', 'EXT3', 'EXT4']:
ogUnmount(int_ndisk, int_nfilesys) ogUnmount (disk, par)
subprocess.run(["resize2fs", "-fpM", PART], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) rc = subprocess.run (['resize2fs', '-fpM', PART], capture_output=True, text=True).returncode
elif TYPE == "BTRFS": if rc:
MNTDIR = ogMount(int_ndisk, int_nfilesys) SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f"{disk},{par}")
SIZE = subprocess.run(["btrfs", "filesystem", "show", MNTDIR], capture_output=True, text=True) return None
SIZE = SIZE.stdout.strip().split(" ")[6] elif 'BTRFS' == type:
SIZE = int(float(SIZE) * 1.1 + 1) mntdir = ogMount (disk, par)
subprocess.run(["btrfs", "filesystem", "resize", str(SIZE), MNTDIR], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # Calcular tamaño ocupado + 10%, redondeado + 1 (incluyendo letra de unidad).
elif TYPE in ["REISERFS", "REISER4"]: btrfs_lines = subprocess.run (['btrfs', 'filesystem', 'show', mntdir], capture_output=True, text=True).stdout.splitlines()
MNTDIR = ogMount(int_ndisk, int_nfilesys) for l in btrfs_lines:
SIZE = int(subprocess.run(["df", "-k", MNTDIR], capture_output=True, text=True).stdout.strip().split("\n")[1].split()[2]) if 'devid' not in l: continue
SIZE = SIZE * 110 // 100 ## 'devid 2 size 8.89GiB used 1.00GiB path /dev/sda4'
ogUnmount(int_ndisk, int_nfilesys) devid_str, devid, size_str, size, used_str, used, path_str, path = l.split()
subprocess.run(["resize_reiserfs", "-s" + str(SIZE) + "K", PART], input="y\n", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) if PART != os.path.realpath (path): continue
elif TYPE == "NTFS": (sz, unit) = re.search ('^([^A-Z]+)([A-Z])', used).groups()
ogUnmount(int_ndisk, int_nfilesys) sz = float (sz) * 1.1 + 1
MAXSIZE, SIZE = subprocess.run(["ntfsresize", "-fi", PART], capture_output=True, text=True) size = f'{str(sz)}{unit}'
MAXSIZE = MAXSIZE.strip().split(" ")[3] subprocess.run (['btrfs', 'filesystem', 'resize', size, mntdir], capture_output=True, text=True)
SIZE = SIZE.strip().split(" ")[4] break
SIZE = int(float(SIZE) * 1.1 / 1024 + 1) * 1024 elif type in ['REISERFS', 'REISER4']:
RETVAL = 1 mntdir = ogMount (disk, par)
while RETVAL != 0 and SIZE + EXTRASIZE < MAXSIZE: df_lines = subprocess.run (['df', '-k', mntdir], capture_output=True, text=True).stdout.splitlines()
EXTRASIZE = subprocess.run(["ntfsresize", "-fns", str(SIZE), PART], capture_output=True, text=True) for l in df_lines:
EXTRASIZE = int(EXTRASIZE.stdout.strip()) if EXTRASIZE.stdout.strip() else 0 if 'Filesystem' in l: continue
RETVAL = EXTRASIZE != 0 fs, blocks, used, avail, use_pct, mntpt = l.split()
SIZE += EXTRASIZE size = str (int (used) * 1.1)
if SIZE < MAXSIZE: ogUnmount (disk, par)
subprocess.run(["ntfsresize", "-fs", str(SIZE), PART], input="y\n", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run (['resize_reiserfs', f'-s{size}K', PART], input='y\n', capture_output=True, text=True)
elif TYPE in ["FAT32", "FAT16"]: break
# No se reduce (por el momento). elif type == 'NTFS':
pass ogUnmount (disk, par)
elif TYPE == "HFS" or TYPE == "HFSPLUS": nr_lines = subprocess.run (['ntfsresize', '-fi', PART], capture_output=True, text=True).stdout.splitlines()
# No se reduce (por el momento). maxsize = None
pass size = None
elif TYPE == "UFS": for l in nr_lines:
# No se reduce (por el momento). if 'device size' in l:
maxsize = float (l.split()[3])
if 'resize at' in l:
size = l.split()[4]
size = int ((int (size) * 1.1 / 1024 + 1) * 1024)
if not maxsize and not size:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk},{par}')
return None
import time
extrasize = 0
retval = 1
while retval != 0 and size+extrasize < maxsize:
nr = subprocess.run (['ntfsresize', '-fns', str(size), PART], capture_output=True, text=True)
for l in nr.stdout.splitlines():
if 'Needed relocations' not in l: continue
extrasize = int ((int (l.split()[3])*1.1/1024+1)*1024)
break
retval = nr.returncode
size += extrasize
if size < maxsize:
rc = subprocess.run (['ntfsresize', '-fs', str(size), PART], input='y\n', capture_output=True, text=True).returncode
if rc:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f"{disk},{par}")
return None
elif type in ['FAT32', 'FAT16', 'F2FS', 'JFS', 'NILFS2', 'XFS', 'EXFAT', 'HFS', 'HFSPLUS', 'UFS']:
pass pass
else: else:
SystemLib.ogRaiseError ( SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f"{disk},{par}")
[], return None
ogGlobals.OG_ERR_PARTITION,
f"{int_ndisk}, {int_nfilesys}"
)
# Devuelve tamaño del sistema de ficheros. return ogGetFsSize (disk, par)
return ogGetFsSize(int_ndisk, int_nfilesys)
#/** #/**

View File

@ -0,0 +1,23 @@
#!/usr/bin/python3
import sys
import argparse
from SystemLib import ogHelp
from FileSystemLib import ogReduceFs
parser = argparse.ArgumentParser (add_help=False)
parser.add_argument ('disk')
parser.add_argument ('par')
if 2 == len (sys.argv) and 'help' == sys.argv[1]:
#parser.print_help() sale en inglés aunque la locale indique otra cosa
ogHelp ('ogReduceFs', 'ogReduceFs int_ndisk int_nfilesys', ['ogReduceFs 1 1'])
sys.exit (0)
args = parser.parse_args()
ret = ogReduceFs (args.disk, args.par)
if ret is not None:
if ret == True: sys.exit (0)
elif ret == False: sys.exit (1)
else: print (ret)