refs #1513 add ogSetPartitionId

pull/1/head
Natalia Serrano 2025-02-14 13:54:20 +01:00
parent 788ae31b0f
commit 0d8d543526
1 changed files with 24 additions and 40 deletions

View File

@ -10,6 +10,7 @@ import filecmp
import subprocess
import shutil
import os
import re
import stat
from pathlib import Path
@ -1167,52 +1168,35 @@ def ogSetPartitionActive (disk, par):
#@exception OG_ERR_PARTITION Error al cambiar el id. de partición.
#@attention Requisitos: fdisk, sgdisk
#*/ ##
def ogSetPartitionId(*args):
# Variables locales
DISK = None
PART = None
PTTYPE = None
ID = None
def ogSetPartitionId (disk, par, id):
DISK = ogDiskToDev (disk)
if not DISK: return None
# Si se solicita, mostrar ayuda.
if len(args) == 1 and args[0] == "help":
SystemLib.ogHelp('ogSetPartitionId', 'ogSetPartitionId int_ndisk int_npartition hex_partid', 'ogSetPartitionId 1 1 7')
return
PART = ogDiskToDev (disk, par)
if not PART: return None
# Error si no se reciben 3 parámetros.
if len(args) != 3:
SystemLib.ogRaiseError(OG_ERR_FORMAT)
return
# Error si el id. de partición no es hexadecimal.
if not re.match ('^[0-9A-Fa-f]+$', id):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_OUTOFLIMIT, id)
return None
# Sustituye nº de disco y nº partición por su dispositivo.
DISK = ogDiskToDev(args[0])
if DISK is None:
return
PART = ogDiskToDev(args[0], args[1])
if PART is None:
return
# Error si el id. de partición no es hexadecimal.
ID = args[2].upper()
if not re.match("^[0-9A-F]+$", ID):
SystemLib.ogRaiseError(OG_ERR_OUTOFLIMIT, args[2])
return
# Elección del tipo de partición.
PTTYPE = ogGetPartitionTableType(args[0])
if PTTYPE == "GPT":
subprocess.run(["sgdisk", f"-t{args[1]}:{ID}", DISK], stderr=subprocess.DEVNULL)
elif PTTYPE == "MSDOS":
subprocess.run(["sfdisk", f"--id", DISK, args[1], ID], stderr=subprocess.DEVNULL)
# Elección del tipo de partición.
PTTYPE = ogGetPartitionTableType (disk)
if 'GPT' == PTTYPE:
p = subprocess.run (['sgdisk', f'-t{par}:{id.upper()}', DISK])
elif 'MSDOS' == PTTYPE:
p = subprocess.run (['sfdisk', '--id', DISK, par, id.upper()])
else:
SystemLib.ogRaiseError(OG_ERR_OUTOFLIMIT, f"{args[0]},{PTTYPE}")
return
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_OUTOFLIMIT, f'{disk},{PTTYPE}')
return None
# MSDOS) Correcto si fdisk sin error o con error pero realiza Syncing
if subprocess.run(["partprobe", DISK], stderr=subprocess.DEVNULL).returncode == 0:
return
# MSDOS) Correcto si fdisk sin error o con error pero realiza Syncing
if 0 == p.returncode:
subprocess.run (['partprobe', DISK])
return True
else:
SystemLib.ogRaiseError(OG_ERR_PARTITION, f"{args[0]},{args[1]},{args[2]}")
return
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk},{part},{id}')
return None
#/**