def mount(self, path, device='none', bind=False, mount_type=''): full_path = self.join(path) command = 'mount %s%s%s %s' % (bind and '--bind ' or '', mount_type and '-t %s ' % mount_type or '', device, full_path) run(command, raise_exception=True) self.mount_points[path]['mounted'] = True log.info('Mounted %s' % full_path)
def kexec(kernel, initrd, layout, kernel_type='bzImage', append=None): root_container = find_root(layout) log.info('Found root: %s' % root_container) root_uuid = root_container.file_system.fs_uuid append = append or [] append.append('root=UUID=%s' % root_uuid) command = 'kexec --type=%s --initrd=%s --append="%s" %s' % ( kernel_type, initrd, ' '.join(append), kernel) log.info('Kexec command: %s' % command) log.info('Goodbye cruel world') run(command)
def remove_gpt(self): """ 512 Fake MBR 512 GPT Header 16KiB Primary Table 16KiB Backup Table """ gpt_bytes = 33792 command = 'dd if=/dev/zero of=%s bs=%d count=1' % (self.device, gpt_bytes) run(command)
def create(self, device): command = self.full_command.format(command_path=self.command_path, device=device) log.info('Creating filesystem: {}'.format(command)) result = run(command) if result.returncode: raise FileSystemCreateException(self.fs_label, command, result)
def set_time(ntp_server): # TODO: Make utility function # TODO: --utc/--local or setting the hardware clock at all should be configurable time_cmds = ['ntpdate %s' % ntp_server, 'hwclock --utc --systohc'] for cmd in time_cmds: result = cli.run(cmd) if result.returncode: log.error('Failed to run %s: %s' % (cmd, result.returncode))
def test_run(self): self.mock_process.communicate = mock.Mock() self.mock_process.communicate.return_value = (b'hello', b'') self.mock_process.returncode = 0 r = cli.run('nocmd') self.assertEqual(r.stdout, 'hello') # Test dry run path r = cli.run('nocmd', dry_run=True) assert r.returncode == 0 # Make sure we raise an exception when expected with pytest.raises(cli.CLIException): self.mock_process.returncode = 99 cli.run('nocmd', raise_exception=True)
def create(self, device): command = self.command.format(**dict(command_path=self.command_path, label_option=self.label_option, device=device, uuid=self.fs_uuid)) log.info("Creating filesystem: %s" % command) result = run(command) if result.returncode: raise FileSystemCreateException(self.fs_label, command, result)
def get_volume_groups(): """ :return: A list of volume group names """ command = 'vgs --noheadings --rows -o vg_name' out = run(command, raise_exception=False) if out.returncode: return list() if not out.stdout: return list() return [vg.strip() for vg in out.splitlines()]
def create(self, device, quick_flag=True): command = self.full_command.format( command_path=self.command_path, quick_option="-f" if quick_flag else '', device=device) log.info('Creating filesystem: {}'.format(command)) result = run(command) if result.returncode: raise FileSystemCreateException(self.fs_label, command, result) self.fs_uuid = self.blkid_uuid(device)
def remove_partition(self, partition_number): """ Uses run to spawn the process and looks for the return val. """ command = self.parted + ' rm ' + str(partition_number) result = run(command) if result.returncode != 0: raise PartedException('Could not remove partition: %d' % partition_number)
def __execute(command, ignore_errors=False, quiet=False): """ Execute a shell command using subprocess, then validate the return code and log either stdout or stderror to the logger. """ log.debug('Running: %s' % command) res = run(command, ignore_error=ignore_errors, quiet=quiet) if res.returncode and not ignore_errors: log.error('stdout: %s, stderr: %s' % (res, res.stderr)) raise LVMError('LVM command returned status {}: {}, {}'.format( res.returncode, res, res.stderr)) return res
def get_physical_volumes(): """ :return: A list of physical volume names """ command = 'pvs --noheadings --rows -o pv_name' out = run(command, raise_exception=False) if out.returncode: return list() if not out.stdout: return list() return [pv.strip() for pv in out.splitlines()]
def create(self, device): command = self.full_command.format( **dict(command_path=self.command_path, uuid=self.fs_uuid, label_options=self.label_options, inode_options=self.inode_options, naming_options=self.naming_options, global_metadata_options=self.global_metadata_options, device=device)) log.info("Creating filesystem: %s" % command) result = run(command) if result.returncode: raise FileSystemCreateException(self.fs_label, command, result)
def create(self, device): command = self.full_command.format( **dict(command_path=self.command_path, superuser_reserve=self.superuser_reserve, feature_options=self.feature_options, extended_options=self.extended_options, label_options=self.label_options, device=device, uuid=self.fs_uuid)) log.info("Creating filesystem: {}".format(command)) result = run(command) if result.returncode: raise FileSystemCreateException(self.fs_label, command, result)
def run_mdadm(self, command, raise_on_error=True, ignore_error=False, quiet=False): full_command = '%s %s' % (self.mdadm, command) result = run(full_command, ignore_error=ignore_error, quiet=quiet, _input='yes') if result and raise_on_error: raise MDADMError('%d: %s : %s' % (result.returncode, result.stdout, result.stderr)) return result
def apply(self): """Lots of logging here """ # TODO: now that we have some clean up operations, determine if we still need to do this log.info('Clearing the device mapper') run('dmsetup remove_all') # Destroy any volume groups present at boot time self.destroy_volume_groups() self.clean_software_raid() self.apply_standard_partitions() # Now that we've built a partition table, destroy any resident data self.destroy_volume_groups() self.destroy_physical_volumes() # Now re-apply from scratch self.apply_software_raid() self.apply_lvm() # we've been sent to the loony bin self.committed = True
def run_parted(self, command, raise_on_error=True, ignore_error=False, quiet=False): """ parted does not use meaningful return codes. It pretty much returns 1 on any error and then prints an error message on to standard error stream. """ result = run(self.parted + command, ignore_error=ignore_error, quiet=quiet) if result and raise_on_error: raise PartedException(result.stderr) return result
print('Can do validation..') if dl.validate(): print('Checksums match up!') else: print('Checksums do NOT MATCH!') else: print("Validation can't be performed") log.info('Extracting image.') new_root = '/mnt/press' dl.extract(new_root) # Nessy adding some POST action run('cp /etc/resolv.conf %s/etc/resolv.conf' % new_root) run('mv %s/etc/fstab_rs %s/etc/fstab' % (new_root, new_root)) # A example config config = { 'auth': { 'algorythim': 'sha512', 'users': { 'rack': { 'gid': 1000, 'group': 'rack', 'home': '/home/rack', 'password': '******', 'password_options': [{ 'encrypted': False }],
expected_hash, chunk_size=1024 * 1024) dl.download(my_callback) if dl.can_validate(): print('Can do validation..') if dl.validate(): print('Checksums match up!') else: print('Checksums do NOT MATCH!') else: print("Validation can't be performed") log.info('Extracting image.') new_root = '/mnt/press' dl.extract(new_root) # Nessy adding some POST action run('mv %s/etc/fstab_rs %s/etc/fstab' % (new_root, new_root)) network = Network(new_root, config) network.apply() chroot = DebianChroot(new_root, config, l1.disks) chroot.apply() chroot.__exit__() log.info('Done!')
def zero_4k(device): log.info('Removing 4K from the beginning of the device: 1.2 metadata') command = 'dd if=/dev/zero of=%s bs=%d count=10' % (device, 4096) run(command)
def set_label(self, label='gpt'): result = run(self.parted + ' mklabel ' + label) if result.returncode != 0: raise PartedException('Could not create filesystem label')
def remove_mbr(self): mbr_bytes = 512 command = 'dd if=/dev/zero of=%s bs=%d count=1' % (self.device, mbr_bytes) run(command)
def clear_device_mapper(self): log.info('Clearing the device mapper') run('dmsetup remove_all')
def umount(self, path): full_path = self.join(path) command = 'umount %s' % full_path run(command, raise_exception=True) self.mount_points[path]['mounted'] = True log.info('Unmounted %s' % full_path)
def blkid_uuid(device): return run("blkid -s UUID -o value {}".format(device)).stdout.strip()
def get_product_name(self): res = cli.run('dmidecode -s system-product-name', raise_exception=True) for line in res.splitlines(): if line.lstrip().startswith('#'): continue return line.strip()
import logging from press.logger import setup_logging from press.chroot.debian import DebianPost from press.helpers.cli import run setup_logging() log = logging.getLogger(__name__) new_root = '/mnt/press' run('cp /etc/resolv.conf %s/etc/resolv.conf' % new_root) post = DebianPost(new_root) post.useradd('rack') post.passwd('rack', 'password') # Install Kernel post.install_packages(['linux-generic']) post.grub_install('/dev/loop') # After we complete lets delete the Post to call __exit__ function. post.__exit__()