refs #1098 add ogDevToDisk()
parent
269fcd288d
commit
c24b7ca337
|
@ -309,42 +309,48 @@ def ogDeletePartitionTable(*args):
|
||||||
subprocess.run(["fdisk", DISK], input="o\nw", text=True)
|
subprocess.run(["fdisk", DISK], input="o\nw", text=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
def ogDevToDisk(dev):
|
#/**
|
||||||
# Variables locales
|
# ogDevToDisk path_device | LABEL="str_label" | UUID="str_uuid"
|
||||||
|
#@brief Devuelve el nº de orden de dicso (y partición) correspondiente al nombre de fichero de dispositivo o a la etiqueta o UUID del sistema de archivos asociado.
|
||||||
|
#@param path_device Camino del fichero de dispositivo.
|
||||||
|
#@param str_label etiqueta de sistema de archivos.
|
||||||
|
#@param str_uuid UUID de sistema de archivos.
|
||||||
|
#@return int_ndisk (para dispositivo de disco)
|
||||||
|
#@return int_ndisk int_npartition (para dispositivo de partición).
|
||||||
|
#@exception OG_ERR_FORMAT Formato incorrecto.
|
||||||
|
#@exception OG_ERR_NOTFOUND Dispositivo no detectado.
|
||||||
|
#@note Solo se acepta en cada llamada 1 de los 3 tipos de parámetros.
|
||||||
|
#*/ ##
|
||||||
|
def ogDevToDisk(arg_dev):
|
||||||
CACHEFILE = "/var/cache/disks.cfg"
|
CACHEFILE = "/var/cache/disks.cfg"
|
||||||
PART = None
|
|
||||||
n = 1
|
|
||||||
|
|
||||||
# Si se solicita, mostrar ayuda.
|
if '=' in arg_dev:
|
||||||
if dev == "help":
|
# arg_dev is "FOO=bar"
|
||||||
ogHelp("ogDevToDisk", "ogDevToDisk path_device | LABEL=str_label | UUID=str_uuid",
|
cmd = None
|
||||||
"ogDevToDisk /dev/sda => 1",
|
if arg_dev.startswith("LABEL="): cmd = ['blkid', '-L', arg_dev[6:]]
|
||||||
"ogDevToDisk /dev/sda1 => 1 1",
|
elif arg_dev.startswith("PARTLABEL="): cmd = ['realpath', '/dev/disk/by-partlabel/'+arg_dev[11:]]
|
||||||
"ogDevToDisk LABEL=CACHE => 1 4")
|
elif arg_dev.startswith("PARTUUID="): cmd = ['realpath', '/dev/disk/by-partuuid/'+arg_dev[10:]]
|
||||||
|
elif arg_dev.startswith("UUID="): cmd = ['blkid', '-U', arg_dev[5:]]
|
||||||
|
if not cmd:
|
||||||
|
ogRaiseError([], ogGlobals.OG_ERR_FORMAT, arg_dev)
|
||||||
|
return
|
||||||
|
DEV = subprocess.run (cmd, capture_output=True, text=True).stdout.strip()
|
||||||
|
else:
|
||||||
|
# arg_dev is "/dev/something"
|
||||||
|
DEV = arg_dev
|
||||||
|
|
||||||
|
if not os.path.exists(DEV):
|
||||||
|
ogRaiseError([], ogGlobals.OG_ERR_NOTFOUND, arg_dev)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Error si no se recibe 1 parámetro.
|
|
||||||
if len(dev) != 1:
|
|
||||||
ogRaiseError(OG_ERR_FORMAT)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Obtener dispositivo a partir de camino, etiqueta o UUID.
|
|
||||||
DEV = dev[0]
|
|
||||||
if DEV.startswith("LABEL="):
|
|
||||||
DEV = subprocess.getoutput(f"blkid -L {DEV[6:]}")
|
|
||||||
elif DEV.startswith("PARTLABEL="):
|
|
||||||
DEV = subprocess.getoutput(f"realpath /dev/disk/by-partlabel/{DEV[11:]} 2>/dev/null")
|
|
||||||
elif DEV.startswith("PARTUUID="):
|
|
||||||
DEV = subprocess.getoutput(f"realpath /dev/disk/by-partuuid/{DEV[10:]} 2>/dev/null")
|
|
||||||
elif DEV.startswith("UUID="):
|
|
||||||
DEV = subprocess.getoutput(f"blkid -U {DEV[5:]}")
|
|
||||||
|
|
||||||
# Error si no es fichero de bloques o directorio (para LVM).
|
# Error si no es fichero de bloques o directorio (para LVM).
|
||||||
if not os.path.exists(DEV):
|
m = os.stat (DEV, follow_symlinks=True).st_mode
|
||||||
ogRaiseError(OG_ERR_NOTFOUND, dev)
|
if not stat.S_ISBLK (m) and not stat.S_ISDIR (m):
|
||||||
|
ogRaiseError([], ogGlobals.OG_ERR_NOTFOUND, arg_dev)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Buscar en fichero de caché de discos.
|
# Buscar en fichero de caché de discos.
|
||||||
|
PART = None
|
||||||
if os.path.exists(CACHEFILE):
|
if os.path.exists(CACHEFILE):
|
||||||
with open(CACHEFILE, 'r') as f:
|
with open(CACHEFILE, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
|
@ -353,19 +359,19 @@ def ogDevToDisk(dev):
|
||||||
PART = parts[0]
|
PART = parts[0]
|
||||||
break
|
break
|
||||||
|
|
||||||
if PART:
|
if PART: return PART
|
||||||
return PART
|
|
||||||
|
|
||||||
# Si no se encuentra, procesa todos los discos para devolver su nº de orden y de partición.
|
# Si no se encuentra, procesa todos los discos para devolver su nº de orden y de partición.
|
||||||
disks = ogDiskToDev()
|
disks = ogDiskToDev()
|
||||||
|
n = 1
|
||||||
for d in disks:
|
for d in disks:
|
||||||
NVME_PREFIX = "p" if "nvme" in d else ""
|
NVME_PREFIX = "p" if "nvme" in d else ""
|
||||||
if DEV.startswith(d):
|
if DEV.startswith(d):
|
||||||
return f"{n} {DEV[len(d) + len(NVME_PREFIX):]}"
|
return f"{n} {DEV[len(d) + len(NVME_PREFIX):]}"
|
||||||
n += 1
|
n += 1
|
||||||
|
|
||||||
ogRaiseError(OG_ERR_NOTFOUND, dev)
|
ogRaiseError([], ogGlobals.OG_ERR_NOTFOUND, arg_dev)
|
||||||
return OG_ERR_NOTFOUND
|
return
|
||||||
|
|
||||||
def _getAllDisks():
|
def _getAllDisks():
|
||||||
ret = []
|
ret = []
|
||||||
|
@ -464,7 +470,7 @@ def ogDiskToDev (arg_disk=None, arg_part=None):
|
||||||
|
|
||||||
# No params: return all disks
|
# No params: return all disks
|
||||||
if arg_disk is None:
|
if arg_disk is None:
|
||||||
return " ".join(ALLDISKS)
|
return ALLDISKS
|
||||||
|
|
||||||
# arg_disk is set: return it
|
# arg_disk is set: return it
|
||||||
if arg_part is None:
|
if arg_part is None:
|
||||||
|
|
Loading…
Reference in New Issue