def __init__(self, *args, **kwargs): super(DmObject, self).__init__(*args, **kwargs) self.type = 'crypt' self.mounts = misc.get_mounts('{0}/mapper'.format(DM_DEV_DIR)) self.default_pool_name = SSM_CRYPT_DEFAULT_POOL if not misc.check_binary('dmsetup') or \ not misc.check_binary('cryptsetup'): return
def __init__(self, *args, **kwargs): super(MdRaid, self).__init__(*args, **kwargs) self.type = 'dm' self._vol = {} self._pool = {} self._dev = {} self.hostname = socket.gethostname() self._binary = misc.check_binary(MDADM) self.default_pool_name = SSM_DM_DEFAULT_POOL self.attrs = [ 'dev_name', 'pool_name', 'dev_free', 'dev_used', 'dev_size' ] if not self._binary: return self.mounts = misc.get_mounts('/dev/md') self.swaps = misc.get_swaps() mdnumber = misc.get_dmnumber("md") for line in misc.get_partitions(): devname = line[3] devsize = int(line[2]) if line[0] == mdnumber: self._vol[devname] = self.get_volume_data(devname) for dev in misc.get_slaves(os.path.basename(devname)): self._dev[dev] = self.get_device_data(dev, devsize)
def __init__(self, *args, **kwargs): super(LvmInfo, self).__init__(*args, **kwargs) self.type = 'lvm' self.attrs = [] self.binary = misc.check_binary('lvm') self.default_pool_name = SSM_LVM_DEFAULT_POOL self.init_local_problem_set()
def __init__(self, options, data=None): self.type = 'dm' self.data = data or {} self._vol = {} self._pool = {} self._dev = {} self.options = options self.hostname = socket.gethostname() self._binary = misc.check_binary(MDADM) self.default_pool_name = SSM_DM_DEFAULT_POOL self.attrs = [ 'dev_name', 'pool_name', 'dev_free', 'dev_used', 'dev_size' ] if not self._binary: return self.problem = problem.ProblemSet(options) self.mounts = misc.get_mounts('/dev/md') mdnumber = misc.get_dmnumber("md") for line in misc.get_partitions(): devname = '/dev/' + line[3] devsize = int(line[2]) if line[0] == mdnumber: self._vol[devname] = self.get_volume_data(devname) for dev in misc.get_slaves(os.path.basename(devname)): self._dev[dev] = self.get_device_data(dev, devsize)
def _create_filesystem(self, pool, name, devs, size=None, raid=None): if not devs: raise Exception("To create btrfs volume, some devices must be " + "provided") self._binary = misc.check_binary('mkfs.btrfs') if not self._binary: self.problem.check(self.problem.TOOL_MISSING, 'mkfs.btrfs') command = ['mkfs.btrfs', '-L', name] if raid: if raid['level'] == '0': command.extend(['-m', 'raid0', '-d', 'raid0']) elif raid['level'] == '1': command.extend(['-m', 'raid1', '-d', 'raid1']) elif raid['level'] == '10': command.extend(['-m', 'raid10', '-d', 'raid10']) else: raise Exception("Btrfs backed currently does not support " + "RAID level {0}".format(raid['level'])) if size: command.extend(['-b', "{0}".format(int(float(size) * 1024))]) # This might seem weird, but btrfs is mostly broken when it comes to # checking existing signatures because it will for example check for # backup superblocks as well, which is wrong. Also we have check for # existing file system signatures in the ssm itself. Other things # than file system should be covered by the backend and we should # have tried to remove the device from the respective pool already. # So at this point there should not be any useful signatures to # speak of. However as I mentioned btrfs is broken, so force it. command.extend(['-f']) command.extend(devs) misc.run(command, stdout=True) misc.send_udev_event(devs[0], "change") return name
def run_bash_tests(names): cur = os.getcwd() os.chdir('./tests/bashtests') command = ['ls', '-m'] if os.access('.coverage', os.R_OK): os.remove('.coverage') failed = [] passed = [] count = 0 misc.run('./set.sh', stdout=False) output = misc.run(command, stdout=False)[1] t0 = time.time() for script in output.split(","): script = script.strip() if not re.match("^\d\d\d-.*\.sh$", script): continue if names and script not in names: continue count += 1 sys.stdout.write("{0:<29}".format(script) + " ") sys.stdout.flush() bad_file = re.sub("\.sh$",".bad", script) if os.access(bad_file, os.R_OK): os.remove(bad_file) ret, out = misc.run(['./' + script], stdout=False, can_fail=True) if ret: print("\033[91m[FAILED]\033[0m") failed.append(script) with open(bad_file, 'w') as f: f.write(out) elif re.search("Traceback", out): # There should be no tracebacks in the output out += "\nWARNING: Traceback in the output!\n" print("\033[93m[WARNING]\033[0m") with open(bad_file, 'w') as f: f.write(out) else: print("\033[92m[PASSED]\033[0m") passed.append(script) if count == 0 and names: print("[+] No bash test matches the name(s)") return 0 t1 = time.time() - t0 print("Ran {0} tests in {1} seconds.".format(count, round(t1, 2))) print("{0} tests PASSED: {1}".format(len(passed), ", ".join(passed))) ret = 0 if len(failed) > 0: print("{0} tests FAILED: {1}".format(len(failed), ", ".join(failed))) print("See files with \"bad\" extension for output") ret = 1 # Show coverage report output if possible if misc.check_binary('coverage'): print("[+] Coverage") misc.run(['coverage', 'report'], stdout=True, can_fail=True) os.chdir(cur) return ret
def run_cryptsetup(self, command, stdout=True, password=None): if not misc.check_binary('cryptsetup'): self.problem.check(self.problem.TOOL_MISSING, 'cryptsetup') command.insert(0, "cryptsetup") if password != None: return misc.run(command, stdout=stdout, stdin_data=password) else: return misc.run(command, stdout=stdout)
def __init__(self, options, data=None): self.type = 'lvm' self.data = data or {} self.attrs = [] self.output = None self.options = options self.binary = misc.check_binary('lvm') self.default_pool_name = SSM_LVM_DEFAULT_POOL self.problem = problem.ProblemSet(options)
def __init__(self, options, data=None): self.type = 'crypt' self.data = data or {} self.output = None self.options = options self.mounts = misc.get_mounts('{0}/mapper'.format(DM_DEV_DIR)) self.default_pool_name = SSM_CRYPT_DEFAULT_POOL self.problem = problem.ProblemSet(options) if not misc.check_binary('dmsetup') or \ not misc.check_binary('cryptsetup'): return command = ['dmsetup', 'table'] self.output = misc.run(command, stderr=False)[1] for line in self.output.split("\n"): if not line or line == "No devices found": break dm = {} array = line.split() dm['type'] = array[3] if dm['type'] != 'crypt': continue dm['vol_size'] = str(int(array[2]) / 2.0) devname = re.sub(":$", "", "{0}/mapper/{1}".format(DM_DEV_DIR, array[0])) dm['dm_name'] = devname dm['pool_name'] = 'dm-crypt' dm['dev_name'] = misc.get_real_device(devname) dm['real_dev'] = dm['dev_name'] if dm['real_dev'] in self.mounts: dm['mount'] = self.mounts[dm['real_dev']]['mp'] # Check if the device really exists in the system. In some cases # (tests) DM_DEV_DIR can lie to us, if that is the case, simple # ignore the device. if not os.path.exists(devname): continue command = ['cryptsetup', 'status', devname] self._parse_cryptsetup(command, dm) self.data[dm['dev_name']] = dm
def __init__(self, *args, **kwargs): super(Btrfs, self).__init__(*args, **kwargs) self.type = 'btrfs' self.default_pool_name = SSM_BTRFS_DEFAULT_POOL self._vol = {} self._pool = {} self._dev = {} self._snap = {} self._subvolumes = {} self._binary = misc.check_binary('btrfs') self.modified_list_version = True if not self._binary: return self.mounts = misc.get_mounts('btrfs') command = ['btrfs', 'filesystem', 'show'] self.output = misc.run(command, stderr=False)[1] vol = {} pool = {} dev = {} partitions = {} fs_size = pool_size = fs_used = 0 pool_name = '' for line in misc.get_partitions(): partitions[line[3]] = line for line in self.output.strip().split("\n"): if not line: continue array = line.split() if array[0] == 'Label:': if len(vol) > 0: self._store_data(vol, pool, fs_used, fs_size, pool_size, pool_name) vol = {} pool = {} fs_size = pool_size = 0 pool_name = '' label = array[1].strip("'") uuid = array[3] pool['uuid'] = vol['uuid'] = uuid try: vol['real_dev'] = misc.get_device_by_uuid(uuid) if vol['real_dev'] in self.mounts: pool['mount'] = self.mounts[vol['real_dev']]['mp'] vol['mount'] = self.mounts[vol['real_dev']]['mp'] else: for dev_i in self.mounts: found = re.findall( r'{0}:/.*'.format(vol['real_dev']), dev_i) if found: pool['mount'] = self.mounts[found[0]]['mp'] break except OSError: # udev is "hard-to-work-with" sometimes so this is fallback vol['real_dev'] = "" if label != 'none': vol['label'] = label vol['ID'] = 0 elif array[0] == 'Total': pool['dev_count'] = array[2] fs_used = float(misc.get_real_size(array[6])) elif array[0] == 'devid': # This is ugly hack to fix a problem with test suite and btrfs # where ?sometimes? btrfs prints out device name in the path # of the test suite rather than path in the real '/dev/' # directory. This should cover that without any impact on # real usage if not os.path.islink(array[7]): array[7] = re.sub(r'.*/dev/', '/dev/', array[7]) dev['dev_name'] = misc.get_real_device(array[7]) if not pool_name: pool_name = self._find_uniq_pool_name(label, array[7]) dev['pool_name'] = pool_name # Fallback in case we could not find real_dev by uuid if 'mount' not in pool: if dev['dev_name'] in self.mounts: pool['mount'] = self.mounts[dev['dev_name']]['mp'] vol['real_dev'] = dev['dev_name'] if 'root' in self.mounts[dev['dev_name']]: if self.mounts[dev['dev_name']]['root'] == '/': vol['mount'] = self.mounts[ dev['dev_name']]['mp'] else: for dev_i in self.mounts: found = re.findall( r'{0}:/.*'.format(dev['dev_name']), dev_i) if found: pool['mount'] = self.mounts[found[0]]['mp'] vol['real_dev'] = found[0].split(':')[0] break dev_used = float(misc.get_real_size(array[5])) dev['dev_used'] = str(dev_used) fs_size += float(misc.get_real_size(array[3])) dev_size = \ int(partitions[dev['dev_name']][2]) pool_size += dev_size dev['dev_free'] = dev_size - dev_used dev['hide'] = False self._dev[dev['dev_name']] = dev dev = {} if len(vol) > 0: self._store_data(vol, pool, fs_used, fs_size, pool_size, pool_name)
def __init__(self, options, data=None): self.type = 'btrfs' self.data = data or {} self.options = options self.default_pool_name = SSM_BTRFS_DEFAULT_POOL self._vol = {} self._pool = {} self._dev = {} self._snap = {} self._subvolumes = {} self._binary = misc.check_binary('btrfs') self.problem = problem.ProblemSet(options) self.modified_list_version = True if not self._binary: return self.mounts = misc.get_mounts('/dev/') command = ['btrfs', 'filesystem', 'show'] self.output = misc.run(command, stderr=False)[1] vol = {} pool = {} dev = {} partitions = {} fs_size = pool_size = fs_used = 0 pool_name = '' for line in misc.get_partitions(): partitions[line[3]] = line for line in self.output.strip().split("\n"): if not line: continue array = line.split() if array[0] == 'Label:': if len(vol) > 0: self._store_data(vol, pool, fs_used, fs_size, pool_size, pool_name) vol = {} pool = {} fs_size = pool_size = 0 pool_name = '' label = array[1].strip("'") uuid = array[3] pool['uuid'] = vol['uuid'] = uuid try: fallback = False vol['real_dev'] = misc.get_device_by_uuid(uuid) if vol['real_dev'] in self.mounts: pool['mount'] = self.mounts[vol['real_dev']]['mp'] vol['mount'] = self.mounts[vol['real_dev']]['mp'] else: for dev_i in self.mounts: found = re.findall( r'{0}:/.*'.format(vol['real_dev']), dev_i) if found: pool['mount'] = self.mounts[found[0]]['mp'] break except OSError: # udev is "hard-to-work-with" sometimes so this is fallback fallback = True vol['real_dev'] = "" if label != 'none': vol['label'] = label vol['ID'] = 0 elif array[0] == 'Total': pool['dev_count'] = array[2] fs_used = get_real_number(array[6]) elif array[0] == 'devid': dev['dev_name'] = array[7] if not pool_name: pool_name = self._find_uniq_pool_name(label, array[7]) dev['pool_name'] = pool_name # Fallback in case we could not find real_dev by uuid if fallback and 'mount' not in pool: if dev['dev_name'] in self.mounts: pool['mount'] = self.mounts[dev['dev_name']]['mp'] vol['real_dev'] = dev['dev_name'] if 'root' in self.mounts[dev['dev_name']]: if self.mounts[dev['dev_name']]['root'] == '/': vol['mount'] = self.mounts[ dev['dev_name']]['mp'] else: for dev_i in self.mounts: found = re.findall( r'{0}:/.*'.format(dev['dev_name']), dev_i) if found: pool['mount'] = self.mounts[found[0]]['mp'] break dev_used = get_real_number(array[5]) dev['dev_used'] = str(dev_used) fs_size += get_real_number(array[3]) dev_size = \ int(partitions[dev['dev_name'].rpartition("/")[-1]][2]) pool_size += dev_size dev['dev_free'] = dev_size - dev_used self._dev[dev['dev_name']] = dev dev = {} if len(vol) > 0: self._store_data(vol, pool, fs_used, fs_size, pool_size, pool_name)