X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=source%2Fsysteminfo.py;h=a0de77718cfa683f498a43aad1ebb599d1736cec;hb=26dac01fdb602d0c0db3cac2e7c822e197c6d5c7;hp=bc605de2de84732bbf55579b203d3e405683a0d0;hpb=2a0371d3e0b21c4db1c6eb7915be2d184ca76a05;p=bootmanager.git diff --git a/source/systeminfo.py b/source/systeminfo.py index bc605de..a0de777 100755 --- a/source/systeminfo.py +++ b/source/systeminfo.py @@ -34,24 +34,24 @@ a utility class for finding and returning information about block devices, memory, and other hardware on the system """ -PROC_MEMINFO_PATH= "/proc/meminfo" -PROC_PARTITIONS_PATH= "/proc/partitions" +PROC_MEMINFO_PATH = "/proc/meminfo" +PROC_PARTITIONS_PATH = "/proc/partitions" # set when the sfdisk -l trick has been done to make # all devices show up -DEVICES_SCANNED_FLAG= "/tmp/devices_scanned" +DEVICES_SCANNED_FLAG = "/tmp/devices_scanned" # a /proc/partitions block is 1024 bytes # a GB to a HDD manufacturer is 10^9 bytes BLOCKS_PER_GB = pow(10, 9) / 1024.0; -MODULE_CLASS_NETWORK= "network" -MODULE_CLASS_SCSI= "scsi" +MODULE_CLASS_NETWORK = "network" +MODULE_CLASS_SCSI = "scsi" #PCI_* is now defined in the pypci modules -#PCI_BASE_CLASS_NETWORK=0x02L -#PCI_BASE_CLASS_STORAGE=0x01L +#PCI_BASE_CLASS_NETWORK = 0x02L +#PCI_BASE_CLASS_STORAGE = 0x01L def get_total_phsyical_mem(vars = {}, log = sys.stderr): """ @@ -61,27 +61,27 @@ def get_total_phsyical_mem(vars = {}, log = sys.stderr): """ try: - meminfo_file= file(PROC_MEMINFO_PATH,"r") - except IOError, e: + meminfo_file = file(PROC_MEMINFO_PATH,"r") + except IOError as e: return - total_memory= None + total_memory = None for line in meminfo_file: try: - (fieldname,value)= string.split(line,":") - except ValueError, e: + (fieldname,value) = string.split(line,":") + except ValueError as e: # this will happen for lines that don't have two values # (like the first line on 2.4 kernels) continue - fieldname= string.strip(fieldname) - value= string.strip(value) + fieldname = string.strip(fieldname) + value = string.strip(value) if fieldname == "MemTotal": try: - (total_memory,units)= string.split(value) + total_memory, units = string.split(value) except ValueError, e: return @@ -93,7 +93,7 @@ def get_total_phsyical_mem(vars = {}, log = sys.stderr): return try: - total_memory= int(total_memory) + total_memory = int(total_memory) except ValueError, e: return @@ -119,24 +119,23 @@ def get_block_device_list(vars = {}, log = sys.stderr): # add in valid sd and hd block device names # also check for vd (virtio devices used with kvm) for blk_prefix in ('sd','hd','vd'): - for blk_num in map ( \ - lambda x: chr(x), range(ord('a'),ord('z')+1)): - devicename="%s%c" % (blk_prefix, blk_num) - valid_blk_names[devicename]=None + for blk_num in string.ascii_lowercase: + devicename = "{}{}".format(blk_prefix, blk_num) + valid_blk_names[devicename] = None # add in valid scsi raid block device names for M in range(0,1+1): for N in range(0,7+1): devicename = "cciss/c%dd%d" % (M,N) - valid_blk_names[devicename]=None + valid_blk_names[devicename] = None for devicename in valid_blk_names.keys(): # devfs under 2.4 (old boot cds) used to list partitions # in a format such as scsi/host0/bus0/target0/lun0/disc # and /dev/sda, etc. were just symlinks try: - devfsname= os.readlink( "/dev/%s" % devicename ) - valid_blk_names[devfsname]=None + devfsname = os.readlink( "/dev/%s" % devicename ) + valid_blk_names[devfsname] = None except OSError: pass @@ -150,62 +149,66 @@ def get_block_device_list(vars = {}, log = sys.stderr): # so, lets run sfdisk -l (list partitions) against # most possible block devices, that way they show # up when it comes time to do the install. + + # 27.6.2012 - Using parted instead of sfdisk, assuming + # that doing so respects the behavior mentioned above. + devicenames = valid_blk_names.keys() devicenames.sort() for devicename in devicenames: - os.system( "sfdisk -l /dev/%s > /dev/null 2>&1" % devicename ) + os.system( "parted --script --list /dev/%s > /dev/null 2>&1" % devicename ) # touch file fb = open(DEVICES_SCANNED_FLAG,"w") fb.close() - devicelist= {} + devicelist = {} - partitions_file= file(PROC_PARTITIONS_PATH,"r") - line_count= 0 + partitions_file = file(PROC_PARTITIONS_PATH,"r") + line_count = 0 for line in partitions_file: - line_count= line_count + 1 + line_count = line_count + 1 # skip the first two lines always if line_count < 2: continue - parts= string.split(line) + parts = string.split(line) if len(parts) < 4: continue - device= parts[3] + device = parts[3] # skip and ignore any partitions if not valid_blk_names.has_key(device): continue try: - major= int(parts[0]) - minor= int(parts[1]) - blocks= int(parts[2]) + major = int(parts[0]) + minor = int(parts[1]) + blocks = int(parts[2]) except ValueError, err: continue - gb_size= blocks/BLOCKS_PER_GB + gb_size = blocks/BLOCKS_PER_GB # check to see if the blk device is readonly try: # can we write to it? - dev_name= "/dev/%s" % device + dev_name = "/dev/%s" % device fb = open(dev_name,"w") fb.close() - readonly=False + readonly = False except IOError, e: # check if EROFS errno if errno.errorcode.get(e.errno,None) == 'EROFS': - readonly=True + readonly = True else: # got some other errno, pretend device is readonly - readonly=True + readonly = True - devicelist[dev_name]= (major,minor,blocks,gb_size,readonly) + devicelist[dev_name] = (major,minor,blocks,gb_size,readonly) return devicelist @@ -234,8 +237,8 @@ def get_system_modules( vars = {}, log = sys.stderr): """ if not vars.has_key("SYSIMG_PATH"): - vars["SYSIMG_PATH"]="/" - SYSIMG_PATH=vars["SYSIMG_PATH"] + vars["SYSIMG_PATH"] = "/" + SYSIMG_PATH = vars["SYSIMG_PATH"] if not vars.has_key("NODE_MODEL_OPTIONS"): vars["NODE_MODEL_OPTIONS"] = 0; @@ -245,7 +248,7 @@ def get_system_modules( vars = {}, log = sys.stderr): # get the kernel version we are assuming if kernel_version is None: try: - kernel_version= os.listdir( "%s/lib/modules/" % SYSIMG_PATH ) + kernel_version = os.listdir( "%s/lib/modules/" % SYSIMG_PATH ) except OSError, e: return @@ -255,7 +258,7 @@ def get_system_modules( vars = {}, log = sys.stderr): if len(kernel_version) > 1: print( "WARNING: We may be returning modules for the wrong kernel." ) - kernel_version= kernel_version[0] + kernel_version = kernel_version[0] print( "Using kernel version %s" % kernel_version ) @@ -269,16 +272,16 @@ def get_system_modules( vars = {}, log = sys.stderr): pcimap = pypcimap.PCIMap(modules_pcimap_path) # this is the actual data structure we return - system_mods= {} + system_mods = {} # these are the lists that will be in system_mods - network_mods= [] - scsi_mods= [] + network_mods = [] + scsi_mods = [] # XXX: this is really similar to what BootCD/conf_files/pl_hwinit does. merge? pcidevs = get_devices() - devlist=pcidevs.keys() + devlist =pcidevs.keys() devlist.sort() for slot in devlist: dev = pcidevs[slot] @@ -290,7 +293,7 @@ def get_system_modules( vars = {}, log = sys.stderr): # claims to be a Bridge, even though it is clearly a # network device if "forcedeth" in modules: - base=PCI_BASE_CLASS_NETWORK + base = PCI_BASE_CLASS_NETWORK else: continue @@ -300,8 +303,8 @@ def get_system_modules( vars = {}, log = sys.stderr): elif base == PCI_BASE_CLASS_STORAGE: scsi_mods += modules - system_mods[MODULE_CLASS_SCSI]= scsi_mods - system_mods[MODULE_CLASS_NETWORK]= network_mods + system_mods[MODULE_CLASS_SCSI] = scsi_mods + system_mods[MODULE_CLASS_NETWORK] = network_mods return system_mods @@ -309,11 +312,11 @@ def get_system_modules( vars = {}, log = sys.stderr): def getKernelVersion( vars = {} , log = sys.stderr): # make sure we have the variables we need try: - SYSIMG_PATH= vars["SYSIMG_PATH"] + SYSIMG_PATH = vars["SYSIMG_PATH"] if SYSIMG_PATH == "": raise ValueError, "SYSIMG_PATH" - NODE_MODEL_OPTIONS=vars["NODE_MODEL_OPTIONS"] + NODE_MODEL_OPTIONS = vars["NODE_MODEL_OPTIONS"] except KeyError, var: raise BootManagerException, "Missing variable in vars: %s\n" % var except ValueError, var: @@ -334,8 +337,8 @@ def getKernelVersion( vars = {} , log = sys.stderr): log.write( "WARNING: Couldn't locate smp kernel.\n") option = '' try: - initrd= os.readlink( "%s/boot/initrd-boot%s" % (SYSIMG_PATH,option) ) - kernel_version= initrd.replace("initrd-", "").replace(".img", "") + initrd = os.readlink( "%s/boot/initrd-boot%s" % (SYSIMG_PATH,option) ) + kernel_version = initrd.replace("initrd-", "").replace(".img", "") except OSError, e: initrd = None kernel_version = None @@ -344,7 +347,7 @@ def getKernelVersion( vars = {} , log = sys.stderr): if __name__ == "__main__": - devices= get_block_device_list() + devices = get_block_device_list() print "block devices detected:" if not devices: print "no devices found!" @@ -354,7 +357,7 @@ if __name__ == "__main__": print "" - memory= get_total_phsyical_mem() + memory = get_total_phsyical_mem() if not memory: print "unable to read /proc/meminfo for memory" else: @@ -367,7 +370,7 @@ if __name__ == "__main__": if len(sys.argv) > 2: kernel_version = sys.argv[1] - modules= get_system_modules() + modules = get_system_modules() if not modules: print "unable to list system modules" else: