From 2744273b82f4389b84058ea52f4d974522079a06 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Mon, 3 Feb 2025 12:48:30 +0100 Subject: [PATCH] refs #1427 add ogGetOsUuid, fix bugs --- client/lib/engine/bin/DiskLib.py | 5 ++-- client/lib/engine/bin/InventoryLib.py | 40 +++++++++------------------ client/shared/functions/ogGetOsUuid | 23 +++++++++++++++ client/shared/scripts/configureOs.py | 4 +++ 4 files changed, 43 insertions(+), 29 deletions(-) create mode 100755 client/shared/functions/ogGetOsUuid diff --git a/client/lib/engine/bin/DiskLib.py b/client/lib/engine/bin/DiskLib.py index 51f54b9..114afd6 100755 --- a/client/lib/engine/bin/DiskLib.py +++ b/client/lib/engine/bin/DiskLib.py @@ -836,7 +836,8 @@ def ogGetPartitionsNumber (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() + if len(lines): + out = lines[-1].strip() elif 'LVM' == pttype: lvs_out = subprocess.run (['lvs', '--noheadings', DISK], capture_output=True, text=True).stdout lines = lvs_out.splitlines() @@ -1255,7 +1256,7 @@ def ogLockDisk(*args): #*/ ## def ogSetPartitionActive (disk, par): if InventoryLib.ogIsEfiActive(): - SystemLib.ogEcho (['session', 'log'], 'warning', f'EFI: {ogGlobals.lang.MSG_DONTUSE} {ogSetPartitionActive}') + SystemLib.ogEcho (['session', 'log'], 'warning', f'EFI: {ogGlobals.lang.MSG_DONTUSE} ogSetPartitionActive') return DISK = ogDiskToDev (disk) diff --git a/client/lib/engine/bin/InventoryLib.py b/client/lib/engine/bin/InventoryLib.py index a708b3d..7101b5f 100755 --- a/client/lib/engine/bin/InventoryLib.py +++ b/client/lib/engine/bin/InventoryLib.py @@ -65,36 +65,22 @@ def ogGetOsType(disk, partition): #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositiv #*/ ## -def ogGetOsUuid(): - # Si se solicita, mostrar ayuda. - if len(sys.argv) > 1 and sys.argv[1] == "help": - SystemLib.ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_nfilesys", sys.argv[0] + " 1 2 => 540e47c6-8e78-4178-aa46-042e4803fb16") - return +def ogGetOsUuid (disk, par): + mntdir = FileSystemLib.ogMount (disk, par) + if not mntdir: return None - # Error si no se reciben 2 parametros. - if len(sys.argv) != 3: - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_FORMAT, - f"Error: {sys.argv[0]} need 2 arguments.", - ) - return - - # Montar la particion, si no lo estaba previamente. - MNTDIR = FileSystemLib.ogMount(sys.argv[1], sys.argv[2]) - if not MNTDIR: - return - - # Obtener UUID según el tipo de sistema operativo. - os_type = ogGetOsType(sys.argv[1], sys.argv[2]) - if os_type == "Linux": +# Obtener UUID según el tipo de sistema operativo. + os_type = ogGetOsType (disk, par) + if 'Linux' == os_type: # Leer el UUID del sistema de ficheros raíz o el fichero de identificador. - uuid = subprocess.check_output(["findmnt", "-no", "UUID", MNTDIR], stderr=subprocess.DEVNULL).decode().strip() or open(os.path.join(MNTDIR, "etc", "machine-id")).read().strip() - print(uuid) - elif os_type == "Windows": + uuid = subprocess.run (['findmnt', '-no', 'UUID', mntdir], capture_output=True, text=True).stdout.strip() + if not uuid: + uuid = open (os.path.join (mntdir, 'etc', 'machine-id')).read().strip() + return uuid + elif 'Windows' == os_type: # Leer identificador en clave de registro. - uuid = RegistryLib.ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Cryptography\\MachineGuid") - print(uuid) + uuid = RegistryLib.ogGetRegistryValue (mntdir, 'SOFTWARE', r'\Microsoft\Cryptography\MachineGuid') + return uuid #/** diff --git a/client/shared/functions/ogGetOsUuid b/client/shared/functions/ogGetOsUuid new file mode 100755 index 0000000..306d96c --- /dev/null +++ b/client/shared/functions/ogGetOsUuid @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from InventoryLib import ogGetOsUuid + +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 ('ogGetOsUuid', 'ogGetOsUuid int_ndisk int_nfilesys', ['ogGetOsUuid 1 2']) + sys.exit (0) + +args = parser.parse_args() +ret = ogGetOsUuid (args.disk, args.par) + +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/scripts/configureOs.py b/client/shared/scripts/configureOs.py index 13248df..bd61865 100755 --- a/client/shared/scripts/configureOs.py +++ b/client/shared/scripts/configureOs.py @@ -16,6 +16,7 @@ import os.path import subprocess import ogGlobals +import SystemLib import DiskLib import FileSystemLib import NetLib @@ -61,6 +62,9 @@ if 'Windows' == ostype: if InventoryLib.ogIsEfiActive(): # Si es UEFI copio el cargador de arranque a la partición EFI e instalo Grub. UEFILib.ogRestoreEfiBootLoader (disk, par) esp = DiskLib.ogGetEsp() + if not esp: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, 'ESP') + sys.exit (1) efidisk, efipart = esp.split() BootLib.ogGrubInstallMbr (efidisk, efipart, 'TRUE') else: