refs #1411 add ogUnmountAll

code-review
Natalia Serrano 2025-01-30 11:48:44 +01:00
parent e73e91dbc3
commit 34bc9c3c89
4 changed files with 74 additions and 46 deletions

View File

@ -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)
#/**

View File

@ -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)
#/**

View File

@ -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)

View File

@ -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)