def get(device_path, attribute=None): """Get Information about device Returns dictionary of properties """ cmd = "tune2fs -l %s" % device_path (error, stdoutdata, stderrdata) = utils.run_syscmd(cmd) if error > 0: raise Tune2fsException(stderrdata.strip()) data = {} for d in stdoutdata.strip().split('\n'): try: (key, value) = d.split(':', 1) except ValueError: continue data[key] = value.strip() if attribute: if attribute in data: data = data[attribute] else: data = None return data
def get(device_path=None, by='NAME'): """ Get information about a device Returns a dict of all attributes of a device """ if device_path is None: devices = _get_devices(by=by) return devices lsblk_options = ','.join(LSBLK_ATTR) cmd = "lsblk -b -d -i -n -P -o %s %s" % (lsblk_options, device_path) (error, stdoutdata, stderrdata) = utils.run_syscmd(cmd) if error > 0: raise ListBlockDeviceException(stderrdata.strip()) data = {} for d in shlex.split(stdoutdata.strip()): (key, value) = d.split('=', 1) if key in lsblk_options: value = value.strip() data[key] = value return data
def is_preboot(): """Check if system is running in pre-boot""" cmd = "uname -n" (error, stdoutdata, stderrdata) = utils.run_syscmd(cmd) if error > 0: return True nodename = stdoutdata.strip() if nodename == "(none)": return True return False
def dd(ifdev, ofdev, bs=None, count=None): """ Run dd on ifdev and ofdev Returns (error, stdoutdata, stderrdata) """ if bs: BS_OPTION = "bs=%s" % bs else: BS_OPTION = "" if count: COUNT_OPTION = "count %s" % count else: COUNT_OPTION = "" cmd = "dd if=%s of=%s %s %s" % (ifdev, ofdev, BS_OPTION, COUNT_OPTION) print cmd (error, stdoutdata, stderrdata) = utils.run_syscmd(cmd) return (error, stdoutdata, stderrdata)
def _get_devices(by='NAME'): """Get all devices by NAME, LABEL, UUID, or KNAME""" devices = [] if by not in ['NAME', 'LABEL', 'UUID', 'KNAME']: raise ListBlockDeviceException("Invalid type") cmd = "lsblk -b -a -l -i -n -o %s" % by (error, stdoutdata, stderrdata) = utils.run_syscmd(cmd) if error > 0: raise ListBlockDeviceException(stderrdata.strip()) for d in stdoutdata.strip().split('\n'): d = d.strip() if d == '': continue device_name = d.split()[0] d = d.split()[0] d_path1 = d_path2 = '' if by == 'UUID': d_path1 = "/dev/disk/by-uuid/%s" % d elif by == 'LABEL': d_path1 = "/dev/disk/by-label/%s" % d elif by == 'NAME': d_path1 = "/dev/%s" % d d_path2 = "/dev/mapper/%s" % d elif by == 'KNAME': d_path1 = "/dev/%s" % d d_path2 = "/dev/mapper/%s" % d else: continue if os.path.exists(d_path1): devices.append(d_path1) elif os.path.exists(d_path2): devices.append(d_path2) return devices