refs #1497 add ogGetDiskType

pull/1/head
Natalia Serrano 2025-02-13 11:21:53 +01:00
parent b4fe9232ba
commit 4142459c88
2 changed files with 69 additions and 29 deletions

View File

@ -626,39 +626,57 @@ def ogGetDiskSize (disk):
#@exception OG_ERR_NOTFOUND disco no detectado o no es un dispositivo de bloques.
#@note Requisitos: udevadm
#*/ ##
def ogGetDiskType(*args):
# Variables locales
DEV = MAJOR = TYPE = None
def ogGetDiskType (dev):
DEV = os.path.basename (dev)
# Si se solicita, mostrar ayuda.
if len(args) == 1 and args[0] == "help":
SystemLib.ogHelp('ogGetDiskType', 'ogGetDiskType path_device', 'ogGetDiskType /dev/sdb => USB')
return
bn = os.path.basename (DEV)
MAJOR = None
with open ('/proc/partitions', 'r') as fd:
while True:
l = fd.readline()
if not l: break
items = l.split()
if len(items) < 4: continue
if items[3] == bn:
MAJOR = items[0]
break
# Error si no se recibe 1 parámetro.
if len(args) != 1:
SystemLib.ogRaiseError(OG_ERR_FORMAT)
return
TYPE = None
with open ('/proc/devices', 'r') as fd:
within_block_section = False
while True:
l = fd.readline()
if not l: break
if 'Block' in l:
within_block_section = True
continue
if not within_block_section:
continue
items = l.split()
if len(items) < 2: continue
if items[0] == MAJOR:
TYPE = items[1].upper()
break
# Obtener el driver del dispositivo de bloques.
DEV = args[0].split("/dev/")[1]
MAJOR = subprocess.getoutput(f"awk -v D='{DEV}' '{{if ($4==D) print $1;}}' /proc/partitions")
TYPE = subprocess.getoutput(f"awk -v D={MAJOR} '/Block/ {{bl=1}} {{if ($1==D&&bl) print toupper($2)}}' /proc/devices")
# Devolver mnemónico del driver de dispositivo.
if TYPE == "SD":
TYPE = "DISK"
if subprocess.getoutput(f"udevadm info -q property {args[0]} 2>/dev/null | grep -q '^ID_BUS=usb'"):
TYPE = "USB"
elif TYPE == "BLKEXT":
TYPE = "NVM"
elif TYPE == "SR" or TYPE.startswith("IDE"):
TYPE = "CDROM" # FIXME Comprobar discos IDE.
elif TYPE == "MD" or TYPE.startswith("CCISS"):
TYPE = "RAID"
elif TYPE == "DEVICE-MAPPER":
TYPE = "MAPPER" # FIXME Comprobar LVM y RAID.
print(TYPE)
return
if 'SD' == TYPE:
TYPE = 'DISK'
udevadm_out = subprocess.run (['udevadm', 'info', '-q', 'property', dev], capture_output=True, text=True).stdout
for l in udevadm_out.splitlines():
if 'ID_BUS=usb' == l[0:10]:
TYPE = 'USB'
elif 'BLKEXT' == TYPE:
TYPE = 'NVM'
elif 'SR' == TYPE or TYPE.startswith ('IDE'):
TYPE = 'CDROM' # FIXME Comprobar discos IDE.
elif 'MD' == TYPE or TYPE.startswith ('CCISS'):
TYPE = 'RAID'
elif 'DEVICE-MAPPER' == TYPE:
TYPE = 'MAPPER' # FIXME Comprobar LVM y RAID.
return TYPE
#/**

View File

@ -0,0 +1,22 @@
#!/usr/bin/python3
import sys
import argparse
from SystemLib import ogHelp
from DiskLib import ogGetDiskType
parser = argparse.ArgumentParser (add_help=False)
parser.add_argument ('dev')
if 2 == len (sys.argv) and 'help' == sys.argv[1]:
#parser.print_help() sale en inglés aunque la locale indique otra cosa
ogHelp ('ogGetDiskType', 'ogGetDiskType path_device', ['ogGetDiskType /dev/sdb'])
sys.exit (0)
args = parser.parse_args()
ret = ogGetDiskType (args.dev)
if ret is not None:
if ret == True: sys.exit (0)
elif ret == False: sys.exit (1)
else: print (ret)