refs #1166 add ogExtendFs()

pull/1/head
Natalia Serrano 2024-11-21 17:49:21 +01:00
parent 410f47fba2
commit 9a78f9f2c7
2 changed files with 116 additions and 76 deletions

View File

@ -1,3 +1,12 @@
#/**
#@file FileSystem.lib
#@brief Librería o clase FileSystem
#@class FileSystem
#@brief Funciones para gestión de sistemas de archivos.
#@version 1.1.0
#@warning License: GNU GPLv3+
#*/
import subprocess import subprocess
import sys import sys
import os.path import os.path
@ -8,6 +17,20 @@ import DiskLib
import CacheLib import CacheLib
import FileSystemLib import FileSystemLib
#/**
# ogCheckFs int_ndisk int_nfilesys
#@brief Comprueba el estado de un sistema de archivos.
#@param int_ndisk nº de orden del disco
#@param int_nfilesys nº de orden del sistema de archivos
#@return (nada)
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
#@note Requisitos: *fsck*
#@warning No se comprueban sistemas de archivos montados o bloqueados.
#@todo Definir salidas.
#*/ ##
def ogCheckFs(int_ndisk, int_nfilesys): def ogCheckFs(int_ndisk, int_nfilesys):
if len(sys.argv) != 3: if len(sys.argv) != 3:
@ -119,97 +142,84 @@ def ogCheckFs(int_ndisk, int_nfilesys):
ogUnlock(int_ndisk, int_nfilesys) ogUnlock(int_ndisk, int_nfilesys)
return ERRCODE return ERRCODE
def ogExtendFs():
# Error si no se reciben 2 parámetros. #/**
if len(sys.argv) != 3: # ogExtendFs int_ndisk int_nfilesys
SystemLib.ogRaiseError( #@brief Extiende un sistema de archivos al tamaño de su partición.
"session", #@param int_ndisk nº de orden del disco
ogGlobals.OG_ERR_FORMAT, #@param int_nfilesys nº de orden del sistema de archivos
f"Not enough arguments" #@return (nada)
) #@exception OG_ERR_FORMAT Formato incorrecto.
return #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
#@note Requisitos: *resize*
#*/ ##
def ogExtendFs (disk, par):
PART = DiskLib.ogDiskToDev (disk, par)
if not PART: return
# Obtener partición. data = {
PART = DiskLib.ogDiskToDev(int(sys.argv[1]), int(sys.argv[2])) 'EXT2': { 'prog': 'resize2fs', 'params': '-f', },
if not PART: 'EXT3': { 'prog': 'resize2fs', 'params': '-f', },
return 'EXT4': { 'prog': 'resize2fs', 'params': '-f', },
'BTRFS': { 'prog': 'btrfs', 'params': 'filesystem resize max', 'domount': True },
'REISERFS': { 'prog': 'resize_reiserfs', 'params': '-f', },
'REISER4': { 'prog': 'resize_reiserfs', 'params': '-f', },
'NTFS': { 'prog': 'ntfsresize', 'params': '-f', 'input': 'y' },
'F2FS': { 'unsupported': True },
'JFS': { 'unsupported': True },
'NILFS2': { 'unsupported': True }, # try "nilfs-resize"
'XFS': { 'unsupported': True },
'EXFAT': { 'unsupported': True },
'FAT32': { 'unsupported': True }, # try "fatresize"
'FAT16': { 'unsupported': True }, # try "fatresize"
'HFS': { 'unsupported': True },
'HFSPLUS': { 'unsupported': True },
'UFS': { 'unsupported': True },
}
# Redimensionar al tamaño máximo según el tipo de partición. type = ogGetFsType (disk, par)
TYPE = ogGetFsType(int(sys.argv[1]), int(sys.argv[2])) if type in data:
if TYPE == "EXT[234]": prog = data[type]['prog'] if 'prog' in data[type] else None
PROG = "resize2fs" params = data[type]['params'] if 'params' in data[type] else None
PARAMS = "-f" domount = data[type]['domount'] if 'domount' in data[type] else False
elif TYPE == "BTRFS": input = data[type]['input'] if 'input' in data[type] else None
PROG = "btrfs"
PARAMS = "filesystem resize max"
DOMOUNT = True # Debe estar montado.
elif TYPE == "REISERFS" or TYPE == "REISER4":
PROG = "resize_reiserfs"
PARAMS = "-f"
elif TYPE == "F2FS" or TYPE == "JFS" or TYPE == "NILFS2" or TYPE == "XFS" or TYPE == "EXFAT" or TYPE == "FAT32" or TYPE == "FAT16" or TYPE == "HFS" or TYPE == "HFSPLUS" or TYPE == "UFS":
return # No se reduce (por el momento).
elif TYPE == "NTFS":
PROG = "ntfsresize"
PARAMS = "<<<\"y\" -f"
else: else:
SystemLib.ogRaiseError( SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk} {par} {type}')
"session",
ogGlobals.OG_ERR_PARTITION,
f"{int(sys.argv[1])} {int(sys.argv[2])} {TYPE}"
)
return return
# Salida normal si no se va a aplicar la operación. if not prog: return
if not PROG:
return
# Error si el sistema de archivos no se queda en el estado de montaje adecuado. if domount:
if DOMOUNT: PART = ogMount (disk, par)
PART = ogMount(int(sys.argv[1]), int(sys.argv[2])) if not PART: return
if not PART:
return
else: else:
ogUnmount(int(sys.argv[1]), int(sys.argv[2])) ogUnmount (disk, par)
if ogIsMounted(int(sys.argv[1]), int(sys.argv[2])): if ogIsMounted (disk, par):
SystemLib.ogRaiseError( SystemLib.ogRaiseError([], ogGlobals.OG_ERR_PARTITION, f'{disk} {par}')
"session",
ogGlobals.OG_ERR_PARTITION,
f"{int(sys.argv[1])} {int(sys.argv[2])}"
)
return return
# Error si el sistema de archivos está bloqueado. # Error si el sistema de archivos está bloqueado.
if ogIsLocked(int(sys.argv[1]), int(sys.argv[2])): if ogIsLocked (disk, par):
SystemLib.ogRaiseError( SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_LOCKED, f'{disk} {par}')
"session",
ogGlobals.OG_ERR_LOCKED,
f"{int(sys.argv[1])} {int(sys.argv[2])}"
)
return return
# Redimensionar en modo uso exclusivo. # Redimensionar en modo uso exclusivo.
ogLock(int(sys.argv[1]), int(sys.argv[2])) ogLock (disk, par)
try: try:
subprocess.run([PROG, PARAMS, PART], capture_output=True) if input:
ERRCODE = 0 rc = subprocess.run ([prog] + params.split() + [PART], input=input, text=True).returncode
else:
rc = subprocess.run ([prog] + params.split() + [PART]).returncode
except FileNotFoundError: except FileNotFoundError:
SystemLib.ogRaiseError( SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTEXEC, prog)
"session", rc = ogGlobals.OG_ERR_NOTEXEC
ogGlobals.OG_ERR_NOTEXEC,
PROG
)
ERRCODE = ogGlobals.OG_ERR_NOTEXEC
except: except:
SystemLib.ogRaiseError( SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk} {par}')
"session", rc = ogGlobals.OG_ERR_PARTITION
ogGlobals.OG_ERR_PARTITION,
f"{int(sys.argv[1])} {int(sys.argv[2])}"
)
ERRCODE = ogGlobals.OG_ERR_PARTITION
ogUnlock(int(sys.argv[1]), int(sys.argv[2])) ogUnlock (disk, par)
return ERRCODE return not rc ## reverse to indicate success
#/** #/**
@ -608,6 +618,13 @@ def ogMount(*args):
elif 2 == len (args): elif 2 == len (args):
return ogMountFs(args[0], args[1]) return ogMountFs(args[0], args[1])
#/**
# ogMountFirstFs int_ndisk
#@brief Monta el primer sistema de archivos disponible en el disco.
#@param int_ndisk nº de orden del disco
#@return Punto de montaje del primer sistema de archivos detectado
#*/ ##
def ogMountFirstFs(int_ndisk): def ogMountFirstFs(int_ndisk):
# Obtener número de particiones del disco. # Obtener número de particiones del disco.
NPARTS = DiskLib.ogGetPartitionsNumber(int_ndisk) NPARTS = DiskLib.ogGetPartitionsNumber(int_ndisk)
@ -737,6 +754,20 @@ def ogMountCdrom():
return return
return MNTDIR return MNTDIR
#/**
# ogReduceFs int_ndisk int_nfilesys
#@brief Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
#@param int_ndisk nº de orden del disco
#@param int_nfilesys nº de orden del sistema de archivos
#@return int_tamañoKB - tamaño en KB
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
#@warning En Windows, se borran los ficheros de hiberanción y de paginación.
#@warning El sistema de archivos se amplía al mínimo + 10%.
#@note Requisitos: *resize*
#*/ ##
def ogReduceFs(int_ndisk, int_nfilesys): def ogReduceFs(int_ndisk, int_nfilesys):
# Error si no se reciben 2 parámetros. # Error si no se reciben 2 parámetros.
@ -906,6 +937,15 @@ def ogUnmountAll(int_ndisk):
if ogGetFsType(int_ndisk, PART) != "CACHE": if ogGetFsType(int_ndisk, PART) != "CACHE":
ogUnmount(int_ndisk, PART) ogUnmount(int_ndisk, PART)
#/**
# ogUnsetDirtyBit int_ndisk int_npart
#@brief Inhabilita el Dirty Bit del sistema de ficheros NTFS para evitar un CHKDSK en el primer arranque
#@param int_ndisk nº de orden del disco
#@param int_npart nº de orden de partición
#@return Nada
#@exception OG_ERR_FORMAT Formato incorrecto.
#*/ ##
def ogUnsetDirtyBit(int_ndisk, int_nfilesys): def ogUnsetDirtyBit(int_ndisk, int_nfilesys):
# Error si no se reciben 2 parámetros. # Error si no se reciben 2 parámetros.

View File

@ -55,7 +55,7 @@ def ogGetOsType(disk, partition):
if os_version: if os_version:
return os_version.split(":", 1)[0] return os_version.split(":", 1)[0]
else: else:
return "Unknown" return None
except Exception as e: except Exception as e:
print(f"Error en ogGetOsType: {e}") print(f"Error en ogGetOsType: {e}")
return "Unknown" return "Unknown"