def version(self): res, status = execWithCaptureStatus(PARTED, [PARTED, '-v']) res = res.strip() words = res.split() if len(words) != 3: raise Exception, "unable to get parted version" return words[2]
def follow_links_to_target(path, paths=[]): global cache_readlink_o global cache_readlink_s if path.startswith("/") == False: return None if path not in cache_readlink_s: o, s = execWithCaptureStatus('/bin/readlink', ['/bin/readlink', '-e', path]) cache_readlink_o[path] = o cache_readlink_s[path] = s else: s = cache_readlink_s[path] o = cache_readlink_o[path] if s == 0: word = o.strip() if word != path: paths.append(word) return follow_links_to_target(word, paths) else: return path else: return None
def __init__(self): global LVS_HAS_ALL_OPTION global LVS_HAS_MIRROR_OPTIONS out, status = execWithCaptureStatus(LVM_BIN_PATH, [LVM_BIN_PATH, 'lvs', '--all']) if status == 0: LVS_HAS_ALL_OPTION = True LVS_HAS_MIRROR_OPTIONS = LVS_HAS_ALL_OPTION self.__block_device_model = BlockDeviceModel() self.__VGs = {} self.__PVs = []
def follow_links_to_target(path, paths=[]): o, s = execWithCaptureStatus('/bin/ls', ['/bin/ls', '-l', path]) if s == 0: words = o.strip().split() if words[0][0] == 'l': target = words[len(words) - 1] paths.append(target) return follow_links_to_target(target, paths) else: return path else: return None
def get_label(self, devpath): args = ['/usr/sbin/xfs_admin'] args.append('-l') args.append(devpath) o, r = execWithCaptureStatus('/usr/sbin/xfs_admin', args) if r == 0: words = o.split() label = re.sub('\"', '', words[len(words) - 1]) if label: return label return None return None
def __init__(self): global LVS_HAS_ALL_OPTION global LVS_HAS_MIRROR_OPTIONS out, status = execWithCaptureStatus(LVM_BIN_PATH, [ LVM_BIN_PATH, 'lvs', '--config', "'log{command_names=0}'", '--all' ]) if status == 0: LVS_HAS_ALL_OPTION = True LVS_HAS_MIRROR_OPTIONS = LVS_HAS_ALL_OPTION self.__block_device_model = BlockDeviceModel() self.__VGs = {} self.__PVs = []
def getPartitions(self, devpath): sectorSize = FDisk().getDeviceGeometry(devpath)[1] parts = list() args = [PARTED, devpath] if self.version() >= '1.6.23': # parted versioned 1.6.23 and above has command "unit", # 1.6.22 and bellow displays in MBs args.append('unit') args.append('b') args.append('print') args.append('-s') res, status = execWithCaptureStatus(PARTED, args) if status != 0: msg = 'parted failed on ' + devpath print msg raise msg lines = res.splitlines() for line in lines: if not re.match('^[0-9]', line): continue words = line.split() if len(words) < 3: continue # partition num part_num = int(words[0]) # beg, end beg = self.__to_bytes(words[1]) / sectorSize end = self.__to_bytes(words[2]) / sectorSize - 1 # bootable bootable = False for word in words: if 'boot' in word: bootable = True # partition id id = ID_UNKNOWN if 'lvm' in words: id = ID_LINUX_LVM elif 'raid' in words: id = 253 else: for word in words: if 'swap' in word: id = ID_SWAPS[0] part = Partition(beg, end, id, part_num, bootable, sectorSize) parts.append(part) return parts
def getPartitions(self, devpath): sectorSize = FDisk().getDeviceGeometry(devpath)[1] parts = list() args = [PARTED, devpath] if self.version() >= '1.6.23' : # parted versioned 1.6.23 and above has command "unit", # 1.6.22 and bellow displays in MBs args.append('unit') args.append('b') args.append('print') args.append('-s') res, status = execWithCaptureStatus(PARTED, args) if status != 0: msg = 'parted failed on ' + devpath print msg raise msg lines = res.splitlines() for line in lines: if not re.match('^[0-9]', line): continue words = line.split() if len(words) < 3: continue # partition num part_num = int(words[0]) # beg, end beg = self.__to_bytes(words[1]) / sectorSize end = self.__to_bytes(words[2]) / sectorSize - 1 # bootable bootable = False for word in words: if 'boot' in word: bootable = True # partition id id = ID_UNKNOWN if 'lvm' in words: id = ID_LINUX_LVM elif 'raid' in words: id = 253 else: for word in words: if 'swap' in word: id = ID_SWAPS[0] part = Partition(beg, end, id, part_num, bootable, sectorSize) parts.append(part) return parts
def getDeviceNames(self): res = execWithCapture(SFDISK, [SFDISK, '-s']) lines = res.splitlines() devices = list() for line in lines: if not re.match('^/dev/', line): continue words = line.split(':') devname = words[0].strip() if not re.match('.*[0-9]', devname): # check if partition table is OK # out, err, ret = rhpl.executil.execWithCaptureErrorStatus(SFDISK, [SFDISK, '-V', devname]) out, ret = execWithCaptureStatus(SFDISK, [SFDISK, '-V', devname]) if ret != 0: #print 'THERE IS A PROBLEM WITH PARTITION TABLE at device ' + devname # print err pass devices.append(devname) # check if geometry can be detected for dev in devices[:]: res = execWithCapture(SFDISK, [SFDISK, '-s', dev]) if re.match('.*cannot get geometry.*', res): devices.remove(dev) # check with blkid to remove false PV (all logical volumes in /dev/mapper) for dev in devices[:]: out, ret = execWithCaptureStatus(BLKID, [BLKID, dev]) if ret == 0: devices.remove(dev) continue out, ret = execWithCaptureStatus(LVDISPLAY, [LVDISPLAY, dev]) if ret == 0: devices.remove(dev) continue return devices
def get_label(self, devpath): args = ['/sbin/tune2fs'] args.append('-l') args.append(devpath) o, r = execWithCaptureStatus('/sbin/tune2fs', args) if r == 0: lines = o.splitlines() for line in lines: if re.search('volume name', line, re.I): words = line.split() label = words[len(words) - 1] if re.match('<none>', label, re.I): return None else: return label return None
def check_mountable(self, fsname, module): global cache_mountable_fs if fsname in cache_mountable_fs: return cache_mountable_fs[fsname] mountable = False out = execWithCapture('/bin/cat', ['/bin/cat', '/proc/filesystems']) if re.search(fsname, out, re.I): mountable = True if mountable == False: out, status = execWithCaptureStatus( '/sbin/modprobe', ['/sbin/modprobe', '-n', module]) if status == 0: mountable = True cache_mountable_fs[fsname] = mountable return mountable
def getDeviceNames(self): res = execWithCapture(SFDISK, [SFDISK, '-s']) lines = res.splitlines() devices = list() for line in lines: if not re.match('^/dev/', line): continue words = line.split(':') devname = words[0].strip() if not re.match('.*[0-9]', devname): # check if partition table is OK # out, err, ret = rhpl.executil.execWithCaptureErrorStatus(SFDISK, [SFDISK, '-V', devname]) out, ret = execWithCaptureStatus(SFDISK, [SFDISK, '-V', devname]) if ret != 0: #print 'THERE IS A PROBLEM WITH PARTITION TABLE at device ' + devname # print err pass devices.append(devname) # check if geometry can be detected for dev in devices[:]: res = execWithCapture(SFDISK, [SFDISK, '-s', dev]) if re.match('.*cannot get geometry.*', res): devices.remove(dev) return devices