From 34bc9c3c89e90d7bc2dcd52135aa8792e07504c0 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Thu, 30 Jan 2025 11:48:44 +0100 Subject: [PATCH] refs #1411 add ogUnmountAll --- client/lib/engine/bin/DiskLib.py | 56 +++++++++---------- client/lib/engine/bin/FileSystemLib.py | 20 ++----- client/shared/functions/ogGetPartitionsNumber | 22 ++++++++ client/shared/functions/ogUnmountAll | 22 ++++++++ 4 files changed, 74 insertions(+), 46 deletions(-) create mode 100755 client/shared/functions/ogGetPartitionsNumber create mode 100755 client/shared/functions/ogUnmountAll diff --git a/client/lib/engine/bin/DiskLib.py b/client/lib/engine/bin/DiskLib.py index e700cc4..2332052 100755 --- a/client/lib/engine/bin/DiskLib.py +++ b/client/lib/engine/bin/DiskLib.py @@ -851,39 +851,33 @@ def ogGetPartitionSize (disk, par): #@attention Requisitos: parted #@note Notas sin especificar #*/ ## -def ogGetPartitionsNumber(*args): - # Variables locales - DISK = None +def ogGetPartitionsNumber (disk): + DISK = ogDiskToDev (disk) + if not DISK: return None + out = 0 - # Si se solicita, mostrar ayuda. - if len(args) == 1 and args[0] == "help": - SystemLib.ogHelp('ogGetPartitionsNumber', 'ogGetPartitionsNumber int_ndisk', 'ogGetPartitionsNumber 1 => 3') - return + pttype = ogGetPartitionTableType (disk) + if pttype in ['GPT', 'MSDOS']: + partx_out = subprocess.run (['partx', '-gso', 'NR', DISK], capture_output=True, text=True).stdout + lines = partx_out.splitlines() + out = lines[-1].strip() + elif 'LVM' == pttype: + lvs_out = subprocess.run (['lvs', '--noheadings', DISK], capture_output=True, text=True).stdout + lines = lvs_out.splitlines() + out = len (lines) + elif 'ZPOOL' == pttype: + if subprocess.run (['zpool', 'list']).returncode: + subprocess.run (['modprobe', 'zfs']) + subprocess.run (['zpool', 'import', '-f', '-R', '/mnt', '-N', '-a']) + blkid = subprocess.run (['blkid', '-s', 'LABEL', '-o', 'value', DISK], capture_output=True, text=True).stdout + zfs_out = subprocess.run (['zfs', 'list', '-Hp', '-o', 'name,canmount,mountpoint', '-r', blkid], capture_output=True, text=True).stdout + out = 0 + for l in zfs_out.splitlines(): + items = l.split() + if len(items) < 3: continue + if 'on' == items[1] and 'none' != items[2]: out += 1 - # Error si no se recibe 1 parámetro. - if len(args) != 1: - SystemLib.ogRaiseError(OG_ERR_FORMAT) - return - - # Contar el número de veces que aparece el disco en su lista de particiones. - DISK = ogDiskToDev(args[0]) - if DISK is None: - return - PTTYPE = ogGetPartitionTableType(args[0]) - if PTTYPE in ["GPT", "MSDOS"]: - output = subprocess.getoutput(f"partx -gso NR {DISK} 2>/dev/null | awk -v p=0 '{{p=$1}} END {{print p}}'") - elif PTTYPE == "LVM": - output = subprocess.getoutput(f"lvs --noheadings {DISK} 2>/dev/null | wc -l") - elif PTTYPE == "ZPOOL": - subprocess.run(["zpool", "list"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # Check if zpool command exists - subprocess.run(["modprobe", "zfs"]) # Load zfs module if not already loaded - subprocess.run(["zpool", "import", "-f", "-R", "/mnt", "-N", "-a"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # Import all zpools - output = subprocess.getoutput(f"zfs list -Hp -o name,canmount,mountpoint -r $(blkid -s LABEL -o value {DISK}) | awk '$2==\"on\" && $3!=\"none\" {{c++}} END {{print c}}'") - else: - output = None - - print(output) - return + return int (out) #/** diff --git a/client/lib/engine/bin/FileSystemLib.py b/client/lib/engine/bin/FileSystemLib.py index 83e5c26..9b01d35 100755 --- a/client/lib/engine/bin/FileSystemLib.py +++ b/client/lib/engine/bin/FileSystemLib.py @@ -849,21 +849,11 @@ def ogUnmountFs(disk, par): #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@warning No se desmonta la partición marcada como caché local. #*/ ## -def ogUnmountAll(int_ndisk): - - if len(sys.argv) != 3: - SystemLib.ogRaiseError ( - [], - ogGlobals.OG_ERR_FORMAT, - "Not enough arguments" - ) - return - - # Obtener partición y punto de montaje. - DISK = DiskLib.ogDiskToDev(int_ndisk) - for PART in range(1, DiskLib.ogGetPartitionsNumber(int_ndisk) + 1): - if ogGetFsType(int_ndisk, PART) != "CACHE": - ogUnmount(int_ndisk, PART) +def ogUnmountAll (disk): + DISK = DiskLib.ogDiskToDev (disk) + for PART in range (1, DiskLib.ogGetPartitionsNumber (disk) + 1): + if 'CACHE' != ogGetFsType (disk, PART): + ogUnmount (disk, PART) #/** diff --git a/client/shared/functions/ogGetPartitionsNumber b/client/shared/functions/ogGetPartitionsNumber new file mode 100755 index 0000000..de6bbe7 --- /dev/null +++ b/client/shared/functions/ogGetPartitionsNumber @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from DiskLib import ogGetPartitionsNumber + +parser = argparse.ArgumentParser (add_help=False) +parser.add_argument ('disk') + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogGetPartitionsNumber', 'ogGetPartitionsNumber int_ndisk', ['ogGetPartitionsNumber 1']) + sys.exit (0) + +args = parser.parse_args() +ret = ogGetPartitionsNumber (args.disk) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret) diff --git a/client/shared/functions/ogUnmountAll b/client/shared/functions/ogUnmountAll new file mode 100755 index 0000000..5b69eb7 --- /dev/null +++ b/client/shared/functions/ogUnmountAll @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from FileSystemLib import ogUnmountAll + +parser = argparse.ArgumentParser (add_help=False) +parser.add_argument ('disk') + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogUnmountAll', 'ogUnmountAll int_ndisk', ['ogUnmountAll 1']) + sys.exit (0) + +args = parser.parse_args() +ret = ogUnmountAll (args.disk) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret)