diff --git a/ogclient/lib/python3/DiskLib.py b/ogclient/lib/python3/DiskLib.py index ff2367f..0510f29 100644 --- a/ogclient/lib/python3/DiskLib.py +++ b/ogclient/lib/python3/DiskLib.py @@ -61,12 +61,11 @@ def ogCreatePartitions (disk, parts): PTTYPE = ogGetPartitionTableType (disk) if not PTTYPE: PTTYPE = 'MSDOS' # Por defecto para discos vacíos. - - if 'GPT' == PTTYPE: - return ogCreateGptPartitions (disk, parts) - elif 'MSDOS' != PTTYPE: + if PTTYPE not in ['MSDOS', 'GPT']: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, PTTYPE) return None + if 'MSDOS' == PTTYPE: CACHE_ID = ogTypeToId ('CACHE', 'MSDOS') + else: CACHE_ID = ogTypeToId ('CACHE', 'GPT_GUID') # Se calcula el ultimo sector del disco (total de sectores usables) SECTORS = ogGetLastSector (disk) @@ -95,7 +94,8 @@ def ogCreatePartitions (disk, parts): if CACHESIZE: SECTORS = SECTORS - CACHESIZE + 2048 - (SECTORS - CACHESIZE) % 2048 - 1 else: - START = 63 + if 'MSDOS' == PTTYPE: START = 63 + else: START = 2048 if CACHESIZE: SECTORS -= CACHESIZE @@ -105,23 +105,29 @@ def ogCreatePartitions (disk, parts): NVME_PREFIX = 'p' if 'nvme' in DISK else '' EXTSTART = EXTSIZE = 0 for p in parts: - # Conservar los datos de la partición de caché. - if f'{ND} {PART}' == CACHEPART and CACHESIZE: - sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={SECTORS+1}, size={CACHESIZE}, Id=ca\n' - PART += 1 - #continue # Leer formato de cada parámetro - Tipo:Tamaño TYPE, SIZE = p.split (':') if TYPE is None or 'None' == TYPE: TYPE='EMPTY' + + # Obtener identificador de tipo de partición válido. + if 'GPT' == PTTYPE: + ID = ogTypeToId (TYPE, 'GPT_GUID') + else: + ID = ogTypeToId (TYPE, PTTYPE) + + # Conservar los datos de la partición de caché. + if f'{ND} {PART}' == CACHEPART and CACHESIZE: + sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={SECTORS+1}, size={CACHESIZE}, type={CACHE_ID}\n' + PART += 1 + #continue + try: SIZE = int (SIZE) except ValueError: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, SIZE) return None - # Obtener identificador de tipo de partición válido. - ID = ogTypeToId (TYPE, 'MSDOS') if 'CACHE' == TYPE or not ID: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, TYPE) return None @@ -133,8 +139,8 @@ def ogCreatePartitions (disk, parts): # Comprobar si la partición es extendida. if '5' == ID: - if PART > 4: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, '') + if 'GPT' == PTTYPE: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, 'EXTENDED') return None ## la extendida debe ser: @@ -163,22 +169,26 @@ def ogCreatePartitions (disk, parts): EXTSIZE = SIZE-2048 # Incluir particiones lógicas dentro de la partición extendida. if 5 == PART: - if not EXTSTART: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, '') - return None - START = EXTSTART - SECTORS = EXTSTART+EXTSIZE + if 'GPT' == PTTYPE: + pass ## sin problema, la partición 5 es simplemente una más + else: + if not EXTSTART: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, 'PART==5 and not EXTSTART') + return None + START = EXTSTART + SECTORS = EXTSTART+EXTSIZE # Generar datos para la partición. # En el caso de que la partición sea EMPTY no se crea nada if 'EMPTY' != TYPE: - sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={START}, size={SIZE}, Id={ID}\n' + sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={START}, size={SIZE}, type={ID}\n' START += SIZE - # Error si se supera el nº total de sectores. - if '4096' == IOSIZE and PART > 4: + if 'MSDOS' == PTTYPE and '4096' == IOSIZE and PART > 4: START += 2048 + + # Error si se supera el nº total de sectores. if START > SECTORS: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'{START//2} > {SECTORS//2}') return None @@ -187,13 +197,14 @@ def ogCreatePartitions (disk, parts): # Si no se indican las 4 particiones primarias, definirlas como vacías, conservando la partición de caché. while PART <= 4: if f'{ND} {PART}' == CACHEPART and CACHESIZE: - sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={SECTORS+1}, size={CACHESIZE}, Id=ca\n' + sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start={SECTORS+1}, size={CACHESIZE}, type={CACHE_ID}\n' else: - sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start=0, size=0, Id=0\n' + sfdisk_input += f'{DISK}{NVME_PREFIX}{PART} : start=0, size=0, type=0\n' PART += 1 + # Si se define partición extendida sin lógicas, crear particion 5 vacía. if 5 == PART and EXTSTART: - sfdisk_input += f'{DISK}5 : start={EXTSTART}, SIZE={EXTSIZE}, Id=0\n' + sfdisk_input += f'{DISK}5 : start={EXTSTART}, SIZE={EXTSIZE}, type=0\n' # Desmontar los sistemas de archivos del disco antes de realizar las operaciones. FileSystemLib.ogUnmountAll (ND) @@ -226,94 +237,8 @@ def ogCreatePartitions (disk, parts): #@todo Definir atributos (arranque, oculta) y tamaños en MB, GB, etc. #*/ ## def ogCreateGptPartitions (disk, parts): - ND = disk - DISK = ogDiskToDev (ND) - if not DISK: return None - - # Se calcula el ultimo sector del disco (total de sectores usables) - SECTORS = ogGetLastSector (disk) - - # Se recalcula el nº de sectores del disco si existe partición de caché. - CACHESIZE = 0 - CACHEPART = CacheLib.ogFindCache() - if CACHEPART: - cache_disk, cache_part = CACHEPART.split() - if ND == cache_disk: - CACHESIZE = int (CacheLib.ogGetCacheSize()) * 2 - if CACHESIZE: - SECTORS -= CACHESIZE - - # Si el disco es GPT empieza en el sector 2048 por defecto, pero podria cambiarse - ALIGN = int (subprocess.run (['sgdisk', '-D', DISK], capture_output=True, text=True).stdout) - START = ALIGN - PART = 1 - print (f'START ({START}) SECTORS ({SECTORS}) PART ({PART})') - - # Leer parámetros con definición de particionado. - DELOPTIONS = [] - OPTIONS = [] - for p in parts: - # Conservar los datos de la partición de caché. - if f'{ND} {PART}' == CACHEPART and CACHESIZE: - PART += 1 - - # Leer formato de cada parámetro - Tipo:Tamaño - TYPE, SIZE = p.split (':') - try: - SIZE = int (SIZE) - except ValueError: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, SIZE) - return None - - # Error si la partición es extendida (no válida en discos GPT). - if 'EXTENDED' == TYPE: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, 'EXTENDED') - return None - - # Comprobar si existe la particion actual, capturamos su tamaño para ver si cambio o no - PARTSIZE = ogGetPartitionSize (ND, PART) - # En sgdisk no se pueden redimensionar las particiones, es necesario borrarlas y volver a crealas - if PARTSIZE: - DELOPTIONS.append (f'-d{PART}') - - # Creamos la particion - # Obtener identificador de tipo de partición válido. - ID = ogTypeToId (TYPE, 'GPT') - if 'CACHE' == TYPE or not ID: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, TYPE) - return None - # Comprobar tamaño numérico y convertir en sectores de 512 B. - SIZE *= 2 - # SIZE debe ser múltiplo de ALIGN, si no gdisk lo mueve automáticamente. - SIZE = (SIZE // ALIGN) * ALIGN - - # Generar datos para la partición. - # En el caso de que la partición sea EMPTY no se crea nada - if 'EMPTY' != TYPE: - OPTIONS += [f'-n{PART}:{START}:+{SIZE}', f'-t{PART}:{ID}'] - - START += SIZE - - # Error si se supera el nº total de sectores. - if START > SECTORS: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'{START//2} > {SECTORS//2}') - return None - - PART += 1 - - # Desmontar los sistemas de archivos del disco antes de realizar las operaciones. - FileSystemLib.ogUnmountAll (ND) - if CACHESIZE: CacheLib.ogUnmountCache() - - # Si la tabla de particiones no es valida, volver a generarla. - ogCreatePartitionTable (ND, 'GPT') - - # Definir particiones y notificar al kernel. - p = subprocess.run (['sgdisk'] + DELOPTIONS + OPTIONS + [DISK], capture_output=True, text=True) - print (f'sgdisk rc ({p.returncode}) stdout ({p.stdout}) stderr ({p.stderr})') - subprocess.run (['partprobe', DISK]) - if CACHESIZE: CacheLib.ogMountCache() - return not p.returncode + print ('nati in ogCreateGptPartitions(), return False') + return False #/** @@ -1303,6 +1228,22 @@ def ogSetPartitionType (disk, par, t): #ogTypeToId ('LINUX', 'MSDOS') => "83" def ogTypeToId (type, pttype='MSDOS'): data = { + 'GPT_GUID': { + ## https://en.wikipedia.org/wiki/GUID_Partition_Table?useskin=vector + 'WINDOWS': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7', + 'NTFS': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7', + 'FAT32': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7', + 'FAT16': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7', + 'FAT12': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7', + 'WIN-RESERV': 'E3C9E316-0B5C-4DB8-817D-F92DF00215AE', + 'LINUX-SWAP': '0657FD6D-A4AB-43C4-84E5-0933C84B4F4F', + 'LINUX': '0FC63DAF-8483-4772-8E79-3D69D8477DE4', + 'EXT2': '0FC63DAF-8483-4772-8E79-3D69D8477DE4', + 'EXT3': '0FC63DAF-8483-4772-8E79-3D69D8477DE4', + 'EXT4': '0FC63DAF-8483-4772-8E79-3D69D8477DE4', + 'CACHE': '0FC63DAF-8483-4772-8E79-3D69D8477DE4', + 'EFI': 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B', + }, 'GPT': { 'EMPTY': '0', 'WINDOWS': '0700',