Example #1
0
    def bootdisk_populate_update_cfg(self, boot_partition, boot_dir=None):
        '''
        boot_partition-->str: EXT4 partition on boot_disk
        boot_dir--.str: Path to dir with boot files (vmlinuz, initrd etc.)
            If set, these files will be copied
        '''
        with MountedDirTemp(
                src=boot_partition,
                parent_dir=TMP_MOUNT_DIR,
        ) as boot:
            if not os.path.isdir(os.path.join(boot, 'boot/grub')):
                os.makedirs(os.path.join(boot, 'boot/grub'), mode=def_mode)
            if not os.path.isdir(os.path.join(boot, 'boot/efi')):
                os.makedirs(os.path.join(boot, 'boot/efi'), mode=owner_mode)
            if boot_dir:
                if os.path.isdir(boot_dir):
                    cmd = 'cp -rPxv %s/. %s/.' % (boot_dir,
                                                  os.path.join(boot, 'boot'))
                    subprocess.call(cmd, shell=True)

        # Update grub.cfg by calling external script
        script_dir = os.path.realpath(os.path.dirname(__file__))
        chroot_script = os.path.join(script_dir, BOOTDISK_MKCONFIG_SCRIPT)
        cmd = ' '.join([chroot_script, boot_partition])
        sys.stderr.write('Executing: %s\n' % (cmd, ))
        subprocess.call(cmd, shell=True)
Example #2
0
    def multiboot_erase_create(self, bios_size='+20M', efi_size='-0'):
        '''
        Will erase all partitions and data (DESTRUCTIVE)
        bios_size-->str: size of BIOS partition in sgdisk dialect
            Examples:
                +80M : 80 MB starting at default start sector
                -0   : Use all available space
        efi_size-->str: size of EFI partition in sgdisk dialect
            See bios_part_size for examples
        Will create a BIOS partition (for grub-mbr) of size bios_size
        Will create an EFI partition of size efi_size
            -0 means use all remaining space
        '''
        need_bios_partition = True
        self.erase_disk()
        self.create_efi_partition(bios_partition=need_bios_partition,
                                  bios_size=bios_size,
                                  efi_size=efi_size)
        print(self)

        efi_partition = self.get_efi_partition()
        with MountedDirTemp(src=efi_partition,
                            parent_dir=TMP_MOUNT_DIR,
                            opts='') as temp_dir:
            iso_dir = os.path.join(temp_dir, 'iso')
            efi_boot_dir = os.path.join(temp_dir, 'EFI/BOOT')
            os.mkdir(iso_dir)
            os.makedirs(efi_boot_dir)

        self.multiboot_install_grub()
Example #3
0
    def bootdisk_erase_create(self, bios_size='+20M', efi_size='+80M'):
        '''
        Will erase all partitions and data (DESTRUCTIVE)
        bios_size-->str: size of BIOS partition in sgdisk dialect
            Examples:
                +80M : 80 MB starting at default start sector
                -0   : Use all available space
        efi_size-->str: size of EFI partition in sgdisk dialect
            See bios_part_size for examples
        Will create a BIOS partition (for grub-mbr) of size bios_size
        Will create an EFI partition of size efi_size
            -0 means use all remaining space
        '''
        need_bios_partition = True
        self.erase_disk()
        self.create_efi_partition(bios_partition=need_bios_partition,
                                  bios_size=bios_size,
                                  efi_size=efi_size)
        print(self)
        self.install_grub_efi(get_partition_uuid(self.get_efi_partition()))

        efi_partition = self.get_efi_partition()
        with MountedDirTemp(src=efi_partition,
                            parent_dir=TMP_MOUNT_DIR,
                            opts='') as temp_dir:
            efi_boot_dir = os.path.join(temp_dir, 'EFI/BOOT')
            if not os.path.isdir(efi_boot_dir):
                os.makedirs(efi_boot_dir)
            self.create_ext4_part_and_fs()
            bootfs_uuid = get_partition_uuid(self.partitions[-1].path)
            with open(os.path.join(temp_dir, GRUB_FS_UUID_FILE), 'w') as f:
                f.write('set grub_fs_uuid="%s"\n' % bootfs_uuid)
                f.flush()
Example #4
0
def update_fstab_boot_efi(boot_disk, root_partition):
    '''
    boot_disk-->str: disk device path (e.g. /dev/sda)
    root_partition-->str: full path to root partition (e.g. /dev/sda3)
    '''
    if boot_disk not in get_valid_disk_paths():
        raise ValueError('Invalid disk path: ' + boot_disk)
    if not is_valid_partition(root_partition):
        raise ValueError('Invalid partition ' + root_partition)

    d = DiskDetails(boot_disk)
    efi_partition = d.get_efi_partition()
    if not efi_partition:
        raise ValueError('%s does not have an EFI partition' % (boot_disk, ))
    efi_uuid = get_partition_uuid(efi_partition)
    if not efi_uuid:
        print('No UUID found, cannot update fstab')
        return
    with MountedDirTemp(src=root_partition, parent_dir=TMP_MOUNT_DIR,
                        opts='') as temp_dir:
        fstab_path = os.path.join(temp_dir, 'etc/fstab')
        if not os.path.exists(fstab_path):
            sys.stderr.write('fstab not found: %s\n' % (fstab_path, ))
            print(os.listdir(os.path.dirname(fstab_path)))
            return
        fstab = FSTab(fstab_path=fstab_path)
        l = fstab.find_first(mountpoint='/boot/efi')
        if l is None:
            sys.stderr.write('/boot/efi not mounted in %s\n' % (fstab_path, ))
            return
        fstab.update_line(l[0], src='UUID=%s' % (efi_uuid, ))
        with open(fstab_path, 'w') as f:
            f.write(str(fstab).encode(DECODE_LANG))
            f.flush()
Example #5
0
 def multiboot_update_config(self):
     '''
     Updates /boot/grub/grub.cfg on EFI partition
     Does not destroy partitions or other data
     '''
     with MountedDirTemp(src=self.get_efi_partition(),
                         parent_dir=TMP_MOUNT_DIR,
                         opts='') as temp_dir:
         cfg_file = os.path.join(temp_dir, 'boot/grub/grub.cfg')
         iso_dir = os.path.join(temp_dir, 'iso')
         g = GrubMenu(cfg_file=cfg_file, iso_dir=iso_dir)
         g.write()
Example #6
0
 def install_grub_mbr(self, root_partition):
     '''
     root_partition-->str: root partition device path
     Installs grub-mbr to MBR
     Does not destroy partitions or other data
     '''
     if self.get_bios_partition():
         with MountedDirTemp(src=root_partition,
                             parent_dir=TMP_MOUNT_DIR,
                             opts='') as temp_dir:
             boot_dir = os.path.join(temp_dir, 'boot')
             if not os.path.isdir(boot_dir):
                 os.makedirs(boot_dir, mode=def_mode)
             cmd = ('grub-install --target=i386-pc '
                    ' --boot-directory=%s %s' % (boot_dir, self.devpath))
             subprocess.call(cmd, shell=True)
Example #7
0
    def install_grub_efi(self, embed_uuid):
        '''
        embed_uuid-->str: FS UUID to embed in grub-efi
        Installs grub-efi under /EFI/BOOT on EFI partition
        Does not destroy partitions or other data
        '''
        efi_partition = self.get_efi_partition()
        if not efi_partition:
            raise ValueError('Disk does not have EFI partition')

        cfg_dir = os.path.realpath(os.path.dirname(__file__))
        with MountedDirTemp(src=self.get_efi_partition(),
                            parent_dir=TMP_MOUNT_DIR,
                            opts='') as temp_dir:

            efi_boot_dir = os.path.join(temp_dir, 'EFI/BOOT')
            if not os.path.isdir(efi_boot_dir):
                os.makedirs(efi_boot_dir, mode=def_mode)
            self.write_efi_files(cfg_dir=cfg_dir,
                                 efi_boot_dir=efi_boot_dir,
                                 embed_uuid=embed_uuid)
Example #8
0
 def create_ext4_part_and_fs(self, ext4_size='-0', label='BOOT'):
     '''
     ext4_size-->str: size of EXT4 partition in sgdisk dialect
         Examples:
             +1G : 1 GB starting at default start sector
             -0   : Use all available space
     '''
     part_create_cmd = 'sgdisk --new=0:0:%s -t 0:%s -c 0:%s %s' % (
         ext4_size, self.linux_part_code, label, self.devpath)
     subprocess.call(part_create_cmd, shell=True)
     subprocess.call('partprobe', shell=True)
     self.scan()
     p = self.partitions[-1].path
     fs_create_cmd = 'mkfs.ext4 -F -m 1 -L %s %s' % (label, p)
     subprocess.call(fs_create_cmd, shell=True)
     subprocess.call('partprobe', shell=True)
     self.scan()
     print(highlight_str('Created EXT4 FS on %s' % (p, )))
     self.highlight_partition(self.partnum_by_path(p))
     # Create required dirs on boot partition
     with MountedDirTemp(src=p, parent_dir=TMP_MOUNT_DIR) as temp_dir:
         os.makedirs(os.path.join(temp_dir, 'boot/grub'), mode=def_mode)
         os.makedirs(os.path.join(temp_dir, 'boot/efi'), mode=owner_mode)