Beispiel #1
0
 def _get_percent_free(self, device):
     ''' return the device free space and percentage
         of the whole device'''
     percent = "%d" % (int((1.0 - (device.usedspace / device.size)) * 100))
     free = _humanize_size(device.freespace)
     rounded = "{}{}".format(int(float(free[:-1])), free[-1])
     return (rounded, percent)
Beispiel #2
0
    def __init__(self, model, signal, selected_disk):
        log.debug('AddPartitionView: selected_disk=[{}]'.format(selected_disk))
        self.model = model
        self.signal = signal
        self.selected_disk = selected_disk
        self.disk_obj = self.model.get_disk(selected_disk)

        self.partnum = IntegerEditor(caption="",
                                     default=self.disk_obj.lastpartnumber + 1)
        self.size_str = _humanize_size(self.disk_obj.freespace)
        self.size = StringEditor(caption="".format(self.size_str))
        self.mountpoint = MountEditor(caption="", edit_text="/")
        self.fstype = Selector(opts=self.model.supported_filesystems)
        body = [
            Columns([
                ("weight", 0.2,
                 Text("Adding partition to {}".format(self.disk_obj.devpath),
                      align="right")), ("weight", 0.3, Text(""))
            ]),
            Padding.line_break(""),
            self._container(),
            Padding.line_break(""),
            Padding.fixed_10(self._build_buttons())
        ]
        partition_box = Padding.center_50(ListBox(body))
        super().__init__(partition_box)
Beispiel #3
0
    def _build_disk_selection(self):
        log.debug('lvm: _build_disk_selection')
        items = [Text("DISK SELECTION")]

        # lvm can use empty whole disks, or empty partitions
        avail_disks = self.model.get_empty_disk_names()
        avail_parts = self.model.get_empty_partition_names()
        avail_devs = sorted(avail_disks + avail_parts)
        if len(avail_devs) == 0:
            return items.append(
                [Color.info_minor(Text("No available disks."))])

        for dname in avail_devs:
            device = self.model.get_disk(dname)
            if device.path != dname:
                # we've got a partition
                lvmdev = device.get_partition(dname)
            else:
                lvmdev = device

            disk_sz = _humanize_size(lvmdev.size)
            disk_string = "{}     {},     {}".format(dname, disk_sz,
                                                     device.model)
            log.debug('lvm: disk_string={}'.format(disk_string))
            self.selected_disks.append(CheckBox(disk_string))

        items += self.selected_disks

        return Pile(items)
Beispiel #4
0
    def _build_model_inputs(self):
        partitioned_disks = []

        for mnt, size, fstype, path in self.disk_obj.get_fs_table():
            mnt = Text(mnt)
            size = Text("{}".format(_humanize_size(size)))
            fstype = Text(fstype) if fstype else '-'
            path = Text(path) if path else '-'
            partition_column = Columns([(15, path), size, fstype, mnt], 4)
            partitioned_disks.append(partition_column)
        free_space = _humanize_size(self.disk_obj.freespace)
        partitioned_disks.append(
            Columns([(15, Text("FREE SPACE")),
                     Text(free_space),
                     Text(""),
                     Text("")], 4))

        return BoxAdapter(SimpleList(partitioned_disks, is_selectable=False),
                          height=len(partitioned_disks))
Beispiel #5
0
    def add_partition_w(self):
        """ Handles presenting the add partition widget button
        depending on if partitions exist already or not.
        """
        text = "Add first GPT partition"
        if len(self.disk_obj.partitions) > 0:
            text = "Add partition (max size {})".format(
                _humanize_size(self.disk_obj.freespace))

        if self.disk_obj.available and \
           self.disk_obj.blocktype not in self.model.no_partition_blocktypes:
            return Color.menu_button(menu_btn(label=text,
                                              on_press=self.add_partition),
                                     focus_map='menu_button focus')
Beispiel #6
0
    def show_disk_information(self, device):
        """ Show disk information, requires sudo/root
        """
        disk_info = self.model.get_disk_info(device)
        disk = self.model.get_disk(device)

        bus = disk_info.raw.get('ID_BUS', None)
        major = disk_info.raw.get('MAJOR', None)
        if bus is None and major == '253':
            bus = 'virtio'

        devpath = disk_info.raw.get('DEVPATH', disk.devpath)
        rotational = '1'
        try:
            dev = os.path.basename(devpath)
            rfile = '/sys/class/block/{}/queue/rotational'.format(dev)
            rotational = open(rfile, 'r').read().strip()
        except (PermissionError, FileNotFoundError, IOError):
            log.exception('WARNING: Failed to read file {}'.format(rfile))
            pass

        dinfo = {
            'bus': bus,
            'devname': disk.devpath,
            'devpath': devpath,
            'model': disk.model,
            'serial': disk.serial,
            'size': disk.size,
            'humansize': _humanize_size(disk.size),
            'vendor': disk_info.vendor,
            'rotational': 'true' if rotational == '1' else 'false',
        }

        template = """\n
{devname}:\n
 Vendor: {vendor}
 Model: {model}
 SerialNo: {serial}
 Size: {humansize} ({size}B)
 Bus: {bus}
 Rotational: {rotational}
 Path: {devpath}
"""
        result = template.format(**dinfo)
        log.debug('calling DiskInfoView()')
        disk_info_view = DiskInfoView(self.model, self.signal, device, result)
        footer = ('Select next or previous disks with n and p')
        self.ui.set_footer(footer, 30)
        self.ui.set_body(disk_info_view)
Beispiel #7
0
    def _build_model_inputs(self):
        log.debug('FileSystemView: building model inputs')
        col_1 = []
        col_2 = []

        avail_disks = self.model.get_available_disk_names()
        if len(avail_disks) == 0:
            return Pile([Color.info_minor(Text("No available disks."))])

        for dname in avail_disks:
            disk = self.model.get_disk_info(dname)
            device = self.model.get_disk(dname)
            btn = menu_btn(label=disk.name,
                           on_press=self.show_disk_partition_view)

            col_1.append(Color.menu_button(btn, focus_map='menu_button focus'))
            disk_sz = _humanize_size(disk.size)
            log.debug('device partitions: {}'.format(len(device.partitions)))
            # if we've consumed some of the device, show
            # the remaining space and percentage of the whole
            if len(device.partitions) > 0:
                free, percent = self._get_percent_free(device)
                disk_sz = "{} ({}%) free".format(free, percent)
            col_2.append(Text(disk_sz))
            for partname in device.available_partitions:
                part = device.get_partition(partname)
                btn = menu_btn(label=partname,
                               on_press=self.show_disk_partition_view)
                col_1.append(
                    Color.menu_button(btn, focus_map='menu_button focus'))
                col_2.append(Text(_humanize_size(part.size)))

        col_1 = BoxAdapter(SimpleList(col_1), height=len(col_1))
        col_2 = BoxAdapter(SimpleList(col_2, is_selectable=False),
                           height=len(col_2))
        return Columns([(16, col_1), col_2], 2)
Beispiel #8
0
 def _build_partition_list(self):
     log.debug('FileSystemView: building part list')
     pl = []
     nr_parts = len(self.model.get_partitions())
     nr_fs = len(self.model.get_filesystems())
     if nr_parts == 0 and nr_fs == 0:
         pl.append(Color.info_minor(Text("No disks or partitions mounted")))
         log.debug('FileSystemView: no partitions')
         return Pile(pl)
     log.debug('FileSystemView: weve got partitions!')
     for dev in self.model.devices.values():
         for mnt, size, fstype, path in dev.get_fs_table():
             mnt = Text(mnt)
             size = Text("{}".format(_humanize_size(size)))
             fstype = Text(fstype) if fstype else '-'
             path = Text(path) if path else '-'
             partition_column = Columns([(15, path), size, fstype, mnt], 4)
             pl.append(partition_column)
     log.debug('FileSystemView: build-part-list done')
     return Pile(pl)