コード例 #1
0
    def _create_efi_or_bios_partition(self, code, name, size, label, fetcher):
        '''
        code-->str
        name-->str
        size-->str
        label-->str
        fetcher-->callable: should return partition path
        '''
        existing_part = fetcher()
        if existing_part:
            print(highlight_str('Disk already has %s partition' % (name, )))
            print(self.highlight_partition(
                self.partnum_by_path(existing_part)))
            return False
        part_create_cmd = 'sgdisk --new=0:0:%s -t 0:%s -c 0:%s %s' % (
            size, code, label, self.devpath)

        subprocess.call(part_create_cmd, shell=True)
        subprocess.call('partprobe', shell=True)
        self.scan()
        existing_part = fetcher()
        if not existing_part:
            raise RuntimeError('Could not create %s partition' % (name, ))
        print(highlight_str('Created %s partition' % (name, )))
        print(self.highlight_partition(self.partnum_by_path(existing_part)))
        return True
コード例 #2
0
 def confirm_action(self, msg, n=None):
     '''
     msg-->str: Action to be confirmed
     n-->int partition number (or None)
     Returns-->boolean
     '''
     print(highlight_str(msg))
     print('')
     if n is not None:
         self.highlight_partition(n)
     else:
         print(self)
     print('')
     try:
         prompt = highlight_str('Enter "YES" to confirm:') + '\n'
         resp = input(prompt)
         return resp == 'YES'
     except KeyboardInterrupt:
         return False
コード例 #3
0
def erase_partition_table(dev_path):
    '''
    dev_path-->str: path to disk (e.g. /dev/sda)
    Destroys partition table by using dd.
    This is for cases where parted cannot read the partition table -
    e.g. when disk is an ISO (!!)
    '''
    l = get_valid_disk_paths()
    if dev_path not in l:
        raise ValueError('Invalid disk device: ' + dev_path)
    msg = ('Confirm erase of ALL data and partitions on device %s'
           '\nEnter YES to continue') % (dev_path, )
    print(highlight_str(msg))
    a = input()
    if a != 'YES':
        return False
    cmd = 'dd if=/dev/zero of=%s bs=128k count=80 1>/dev/null 2>&1' % (
        dev_path)
    subprocess.call(cmd, shell=True)
    return True
コード例 #4
0
    def highlight_partition(self, n):
        '''
        n-->int: partition number (1-based)
        Prints str representation, highlighting partition n in inverse video
        '''
        if n > len(self.partitions):
            raise ValueError('Invalid partition number: %d' % (n, ))

        ret = '\n' + self.diskinfo + '\n'

        hfmt = '\n%3s %-12s %-12s %-7s %-4s %-16s %-11s %s'
        rfmt = '\n%3d %-12s %-12s %-7s %-4s %-16s %-11s %s'
        ret += hfmt % ('Num', 'Start', 'End', 'Size', 'Code', 'Name', 'FS',
                       'Path')
        for i in range(len(self.partitions)):
            p = self.partitions[i]
            x = rfmt % (p.num, p.start, p.end, p.size, p.code, p.name, p.fs,
                        p.path)
            if i == (n - 1):
                x = highlight_str(x)
            ret += x
        print(ret)
コード例 #5
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)
コード例 #6
0
 def create_efi_partition(self,
                          bios_partition=True,
                          bios_size='+20M',
                          efi_size='+80M'):
     '''
     bios_partition-->bool: Whether to ALSO create partition of type
         BIOS (EF02) to support non-EFI grub
     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
     Creates a NEW partition of type EFI (EF00) and creates a VFAT
     filesystem on it
     '''
     # BIOS partition for non-EFI grub
     if bios_partition:
         self._create_efi_or_bios_partition(code=self.bios_part_code,
                                            name='BIOS',
                                            size=bios_size,
                                            label='BIOSGRUB',
                                            fetcher=self.get_bios_partition)
         # No filesystem required for BIOS partition
     # EFI Partition
     if self._create_efi_or_bios_partition(code=self.efi_part_code,
                                           name='EFI',
                                           size=efi_size,
                                           label='EFI',
                                           fetcher=self.get_efi_partition):
         # Create FS only if new EFI partition was created
         self.scan()
         p = self.get_efi_partition()
         fs_create_cmd = 'mkfs.vfat -F 32 -n EFI ' + p
         subprocess.call(fs_create_cmd, shell=True)
         print(highlight_str('Created VFAT FS on %s' % (p, )))
         self.highlight_partition(self.partnum_by_path(p))