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. #@exception OG_ERR_NOTFOUND disco no detectado o no es un dispositivo de bloques.
#@note Requisitos: udevadm #@note Requisitos: udevadm
#*/ ## #*/ ##
def ogGetDiskType(*args): def ogGetDiskType (dev):
# Variables locales DEV = os.path.basename (dev)
DEV = MAJOR = TYPE = None
# Si se solicita, mostrar ayuda. bn = os.path.basename (DEV)
if len(args) == 1 and args[0] == "help": MAJOR = None
SystemLib.ogHelp('ogGetDiskType', 'ogGetDiskType path_device', 'ogGetDiskType /dev/sdb => USB') with open ('/proc/partitions', 'r') as fd:
return 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. TYPE = None
if len(args) != 1: with open ('/proc/devices', 'r') as fd:
SystemLib.ogRaiseError(OG_ERR_FORMAT) within_block_section = False
return 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. # Devolver mnemónico del driver de dispositivo.
if TYPE == "SD": if 'SD' == TYPE:
TYPE = "DISK" TYPE = 'DISK'
if subprocess.getoutput(f"udevadm info -q property {args[0]} 2>/dev/null | grep -q '^ID_BUS=usb'"): udevadm_out = subprocess.run (['udevadm', 'info', '-q', 'property', dev], capture_output=True, text=True).stdout
TYPE = "USB" for l in udevadm_out.splitlines():
elif TYPE == "BLKEXT": if 'ID_BUS=usb' == l[0:10]:
TYPE = "NVM" TYPE = 'USB'
elif TYPE == "SR" or TYPE.startswith("IDE"): elif 'BLKEXT' == TYPE:
TYPE = "CDROM" # FIXME Comprobar discos IDE. TYPE = 'NVM'
elif TYPE == "MD" or TYPE.startswith("CCISS"): elif 'SR' == TYPE or TYPE.startswith ('IDE'):
TYPE = "RAID" TYPE = 'CDROM' # FIXME Comprobar discos IDE.
elif TYPE == "DEVICE-MAPPER": elif 'MD' == TYPE or TYPE.startswith ('CCISS'):
TYPE = "MAPPER" # FIXME Comprobar LVM y RAID. TYPE = 'RAID'
print(TYPE) elif 'DEVICE-MAPPER' == TYPE:
return 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)