def get_container_size_policy_by_number(number): """Get a container size policy by the given number.""" if number <= 0: return number return Size(number)
def __init__(self, methodName='run_test'): super(LUKSTestCase, self).__init__(methodName=methodName, device_spec=[Size("100 MiB")]) self.fmt = LUKS(passphrase="password")
def __init__(self, *args, **kwargs): # these are all absolutely required. not getting them is fatal. self._disks = kwargs.pop("disks") free = kwargs.pop("free") self.selected = kwargs.pop("selected")[:] self.name = kwargs.pop("name") or "" # make sure it's a string self.device_type = kwargs.pop("device_type") self.storage = kwargs.pop("storage") # these are less critical self.raid_level = kwargs.pop("raid_level", None) or None # not "" self.encrypted = kwargs.pop("encrypted", False) self.exists = kwargs.pop("exists", False) self.size_policy = kwargs.pop("size_policy", SIZE_POLICY_AUTO) self.size = kwargs.pop("size", Size(0)) self._error = None GUIObject.__init__(self, *args, **kwargs) self._grabObjects() GUIDialogInputCheckHandler.__init__(self, self._save_button) # set up the dialog labels with device-type-specific text container_type = get_container_type(self.device_type) title_text = _(CONTAINER_DIALOG_TITLE) % { "container_type": _(container_type.name).upper() } self._title_label.set_text(title_text) dialog_text = _(CONTAINER_DIALOG_TEXT) % { "container_type": _(container_type.name).lower() } self._dialog_label.set_text(dialog_text) # populate the dialog widgets self._name_entry.set_text(self.name) # populate the store for disk in self._disks: self._store.append([ disk.description, str(disk.size), str(free[disk.name][0]), disk.name, disk.id ]) model = self._treeview.get_model() itr = model.get_iter_first() selected_ids = [d.id for d in self.selected] selection = self._treeview.get_selection() while itr: disk_id = model.get_value(itr, 4) if disk_id in selected_ids: selection.select_iter(itr) itr = model.iter_next(itr) # XXX how will this be related to the device encryption setting? self._encryptCheckbutton.set_active(self.encrypted) # set up the raid level combo # XXX how will this be related to the device raid level setting? self._raidStoreFilter.set_visible_func(self._raid_level_visible) self._raidStoreFilter.refilter() self._populate_raid() self._original_size = self.size self._original_size_text = self.size.human_readable(max_places=2) self._sizeEntry.set_text(self._original_size_text) if self.size_policy == SIZE_POLICY_AUTO: self._sizeCombo.set_active(0) elif self.size_policy == SIZE_POLICY_MAX: self._sizeCombo.set_active(1) else: self._sizeCombo.set_active(2) if self.exists: fancy_set_sensitive(self._name_entry, False) self._treeview.set_sensitive(False) fancy_set_sensitive(self._encryptCheckbutton, False) fancy_set_sensitive(self._sizeCombo, False) self._sizeEntry.set_sensitive(False) # Check that the container name configured is valid self.add_check(self._name_entry, self._checkNameEntry)
def testRoundToNearest(self): self.assertEqual(size.ROUND_DEFAULT, size.ROUND_HALF_UP) s = Size("10.3 GiB") self.assertEqual(s.roundToNearest(GiB), Size("10 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DEFAULT), Size("10 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DOWN), Size("10 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_UP), Size("11 GiB")) # >>> Size("10.3 GiB").convertTo(MiB) # Decimal('10547.19999980926513671875') self.assertEqual(s.roundToNearest(MiB), Size("10547 MiB")) self.assertEqual(s.roundToNearest(MiB, rounding=size.ROUND_UP), Size("10548 MiB")) self.assertIsInstance(s.roundToNearest(MiB), Size) with self.assertRaises(ValueError): s.roundToNearest(MiB, rounding='abc') # arbitrary decimal rounding constants are not allowed from decimal import ROUND_HALF_DOWN with self.assertRaises(ValueError): s.roundToNearest(MiB, rounding=ROUND_HALF_DOWN) s = Size("10.51 GiB") self.assertEqual(s.roundToNearest(GiB), Size("11 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DEFAULT), Size("11 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DOWN), Size("10 GiB")) self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_UP), Size("11 GiB")) s = Size("513 GiB") self.assertEqual(s.roundToNearest(GiB), s) self.assertEqual(s.roundToNearest(TiB), Size("1 TiB")) self.assertEqual(s.roundToNearest(TiB, rounding=size.ROUND_DOWN), Size(0)) # test Size parameters self.assertEqual(s.roundToNearest(Size("128 GiB")), Size("512 GiB")) self.assertEqual(s.roundToNearest(Size("1 KiB")), Size("513 GiB")) self.assertEqual(s.roundToNearest(Size("1 TiB")), Size("1 TiB")) self.assertEqual(s.roundToNearest(Size("1 TiB"), rounding=size.ROUND_DOWN), Size(0)) self.assertEqual(s.roundToNearest(Size(0)), Size(0)) self.assertEqual(s.roundToNearest(Size("13 GiB")), Size("507 GiB")) with self.assertRaises(ValueError): s.roundToNearest(Size("-1 B"))
def spaceRequired(self): return Size(iutil.getDirSize("/") * 1024)
def testNegative(self): s = Size("-500MiB") self.assertEqual(s.humanReadable(), "-500 MiB") self.assertEqual(s.convertTo(B), -524288000)
def testNoUnitsInString(self): self.assertEqual(Size("1024"), Size("1 KiB"))
def _set_up_storage(self): self.blivet.factoryDevice(devicefactory.DEVICE_TYPE_LVM_THINP, Size("1 GiB"), label="ROOT", disks=self.blivet.disks[:])
def _set_up_storage(self): # This isn't exactly autopart, but it should be plenty close to it. self.blivet.factoryDevice(devicefactory.DEVICE_TYPE_LVM, Size("500 MiB"), disks=self.blivet.disks[:], container_size=devicefactory.SIZE_POLICY_MAX)
def populate(self, disks): totalDisks = 0 totalReclaimableSpace = Size(0) self._initialFreeSpace = Size(0) self._selectedReclaimableSpace = Size(0) canShrinkSomething = False for disk in disks: # First add the disk itself. editable = not disk.protected if disk.partitioned and disk.format.supported: fstype = "" diskReclaimableSpace = Size(0) else: fstype = disk.format.name diskReclaimableSpace = disk.size itr = self._diskStore.append(None, [ disk.id, "%s %s" % (disk.size.human_readable(max_places=1), disk.description), fstype, "<span foreground='grey' style='italic'>%s total</span>", _(PRESERVE), editable, TY_NORMAL, self._get_tooltip(disk), int(disk.size), disk.name ]) if disk.partitioned and disk.format.supported: # Then add all its partitions. for dev in disk.children: if dev.is_extended and disk.format.logical_partitions: continue # Devices that are not resizable are still deletable. if dev.resizable: freeSize = dev.size - dev.min_size resizeString = _("%(freeSize)s of %(devSize)s") \ % {"freeSize": freeSize.human_readable(max_places=1), "devSize": dev.size.human_readable(max_places=1)} if not dev.protected: canShrinkSomething = True else: freeSize = dev.size resizeString = "<span foreground='grey'>%s</span>" % \ escape_markup(_("Not resizeable")) if dev.protected: ty = TY_PROTECTED else: ty = TY_NORMAL self._diskStore.append(itr, [ dev.id, self._description(dev), dev.format.name, resizeString, _(PRESERVE), not dev.protected, ty, self._get_tooltip(dev), int(dev.size), dev.name ]) diskReclaimableSpace += freeSize # And then add another uneditable line that lists how much space is # already free in the disk. diskFree = self.storage.get_disk_free_space([disk]) if diskFree >= Size("1MiB"): freeSpaceString = "<span foreground='grey' style='italic'>%s</span>" % \ escape_markup(_("Free space")) self._diskStore.append(itr, [ disk.id, freeSpaceString, "", "<span foreground='grey' style='italic'>%s</span>" % escape_markup(diskFree.human_readable(max_places=1)), NOTHING, False, TY_FREE_SPACE, self._get_tooltip(disk), diskFree, "" ]) self._initialFreeSpace += diskFree # And then go back and fill in the total reclaimable space for the # disk, now that we know what each partition has reclaimable. self._diskStore[itr][RECLAIMABLE_COL] = self._diskStore[itr][ RECLAIMABLE_COL] % diskReclaimableSpace totalDisks += 1 totalReclaimableSpace += diskReclaimableSpace self._update_labels(totalDisks, totalReclaimableSpace, 0) description = _( "You can remove existing file systems you no longer need to free up space " "for this installation. Removing a file system will permanently delete all " "of the data it contains.") if canShrinkSomething: description += "\n\n" description += _( "There is also free space available in pre-existing file systems. " "While it's risky and we recommend you back up your data first, you " "can recover that free disk space and make it available for this " "installation below.") self._reclaimDescLabel.set_text(description) self._update_reclaim_button(Size(0))
def resize_slider_format(self, scale, value): # This makes the value displayed under the slider prettier than just a # single number. return str(Size(value))
def start(self, total_files, total_size): self.total_files = total_files self.total_size = Size(total_size)
def __init__(self): self.downloads = collections.defaultdict(int) self.last_time = time.time() self.total_files = 0 self.total_size = Size(0)
def on_resize_slider_format(self, scale, value): # This makes the value displayed under the slider prettier than just a # single number. return Size(value).human_readable(max_places=1)
def spaceRequired(self): # We don't have this data with OSTree at the moment return Size("500 MB")
class BootLoader(object): """A base class for boot loaders.""" name = "Generic Bootloader" packages = [] config_file = None config_file_mode = 0o600 can_dual_boot = False keep_boot_order = False keep_mbr = False image_label_attr = "label" encryption_support = False stage2_is_valid_stage1 = False # requirements for stage2 devices stage2_device = None stage2_device_types = [] stage2_raid_levels = [] stage2_raid_metadata = [] stage2_raid_member_types = [] stage2_mountpoints = ["/boot", "/"] stage2_bootable = False stage2_must_be_primary = True stage2_description = N_("/boot file system") stage2_max_end = Size("2 TiB") @property def stage2_format_types(self): return ["ext4", "ext3", "ext2"] def __init__(self): super().__init__() self.boot_args = Arguments() self.dracut_args = Arguments() # the device the bootloader will be installed on self.stage1_device = None # the "boot disk", meaning the disk stage1 _will_ go on self.stage1_disk = None self.stage2_is_preferred_stage1 = False self.disks = [] self._disk_order = [] # timeout in seconds self._timeout = None self.password = None # console/serial stuff self.console = "" self.console_options = "" self._set_console() # list of BootLoaderImage instances representing bootable OSs self.linux_images = [] self.chain_images = [] # default image self._default_image = None self.skip_bootloader = False self.use_bls = True self.errors = [] self.warnings = [] def reset(self): """ Reset stage1 and stage2 values """ self.stage1_device = None self.stage1_disk = None self.stage2_device = None self.stage2_is_preferred_stage1 = False self.disks = [] self.errors = [] self.warnings = [] # # disk list access # @property def disk_order(self): """Potentially partial order for disks.""" return self._disk_order @disk_order.setter def disk_order(self, order): log.debug("new disk order: %s", order) self._disk_order = order if self.disks: self._sort_disks() def _sort_disks(self): """Sort the internal disk list.""" for name in reversed(self.disk_order): try: idx = [d.name for d in self.disks].index(name) except ValueError: log.error("bios order specified unknown disk %s", name) continue self.disks.insert(0, self.disks.pop(idx)) def set_disk_list(self, disks): self.disks = disks[:] self._sort_disks() log.debug("new disk list: %s", self.disks) # # image list access # @property def default(self): """The default image.""" if not self._default_image and self.linux_images: self._default_image = self.linux_images[0] return self._default_image @default.setter def default(self, image): if image not in self.images: raise ValueError("new default image not in image list") log.debug("new default image: %s", image) self._default_image = image @property def images(self): """ List of OS images that will be included in the configuration. """ all_images = self.linux_images all_images.extend(i for i in self.chain_images if i.label) return all_images def clear_images(self): """Empty out the image list.""" self.linux_images = [] self.chain_images = [] def add_image(self, image): """Add a BootLoaderImage instance to the image list.""" if isinstance(image, LinuxBootLoaderImage): self.linux_images.append(image) else: self.chain_images.append(image) def image_label(self, image): """Return the appropriate image label for this bootloader.""" return getattr(image, self.image_label_attr) # # platform-specific data access # @property def disklabel_types(self): return DiskLabel.get_platform_label_types() @property def device_descriptions(self): return platform.platform.boot_stage1_constraint_dict["descriptions"] # # constraint checking for target devices # def _is_valid_md(self, device, raid_levels=None, metadata=None, member_types=None, desc=""): ret = True if device.type != "mdarray": return ret if raid_levels and device.level not in raid_levels: levels_str = ",".join("%s" % l for l in raid_levels) self.errors.append( _("RAID sets that contain '%(desc)s' must have one " "of the following raid levels: %(raid_level)s.") % { "desc": desc, "raid_level": levels_str }) ret = False # new arrays will be created with an appropriate metadata format if device.exists and \ metadata and device.metadata_version not in metadata: self.errors.append( _("RAID sets that contain '%(desc)s' must have one " "of the following metadata versions: %(metadata_versions)s.") % { "desc": desc, "metadata_versions": ",".join(metadata) }) ret = False if member_types: for member in device.members: if not self._device_type_match(member, member_types): self.errors.append( _("RAID sets that contain '%(desc)s' must " "have one of the following device " "types: %(types)s.") % { "desc": desc, "types": ",".join(member_types) }) ret = False log.debug("_is_valid_md(%s) returning %s", device.name, ret) return ret def _is_valid_disklabel(self, device, disklabel_types=None): ret = True if self.disklabel_types: for disk in device.disks: label_type = getattr(disk.format, "label_type", None) if not label_type or label_type not in self.disklabel_types: types_str = ",".join(disklabel_types) self.errors.append( _("%(name)s must have one of the following " "disklabel types: %(types)s.") % { "name": device.name, "types": types_str }) ret = False log.debug("_is_valid_disklabel(%s) returning %s", device.name, ret) return ret def _is_valid_format(self, device, format_types=None, mountpoints=None, desc=""): ret = True if format_types and device.format.type not in format_types: self.errors.append( _("%(desc)s cannot be of type %(type)s.") % { "desc": desc, "type": device.format.type }) ret = False if mountpoints and hasattr(device.format, "mountpoint") \ and device.format.mountpoint not in mountpoints: self.errors.append( _("%(desc)s must be mounted on one of %(mountpoints)s.") % { "desc": desc, "mountpoints": ", ".join(mountpoints) }) ret = False log.debug("_is_valid_format(%s) returning %s", device.name, ret) return ret def _is_valid_size(self, device, desc=""): ret = True msg = None errors = [] if device.format.min_size and device.format.max_size: msg = ( _("%(desc)s must be between %(min)d and %(max)d MB in size") % { "desc": desc, "min": device.format.min_size, "max": device.format.max_size }) if device.format.min_size and device.size < device.format.min_size: if msg is None: errors.append( _("%(desc)s must not be smaller than %(min)dMB.") % { "desc": desc, "min": device.format.min_size }) else: errors.append(msg) ret = False if device.format.max_size and device.size > device.format.max_size: if msg is None: errors.append( _("%(desc)s must not be larger than %(max)dMB.") % { "desc": desc, "max": device.format.max_size }) elif msg not in errors: # don't add the same error string twice errors.append(msg) ret = False log.debug("_is_valid_size(%s) returning %s", device.name, ret) return ret def _is_valid_location(self, device, max_end=None, desc=""): ret = True if max_end and device.type == "partition" and device.parted_partition: end_sector = device.parted_partition.geometry.end sector_size = device.parted_partition.disk.device.sectorSize end = Size(sector_size * end_sector) if end > max_end: self.errors.append( _("%(desc)s must be within the first %(max_end)s of " "the disk.") % { "desc": desc, "max_end": max_end }) ret = False log.debug("_is_valid_location(%s) returning %s", device.name, ret) return ret def _is_valid_partition(self, device, primary=None, desc=""): ret = True if device.type == "partition" and primary and not device.is_primary: self.errors.append(_("%s must be on a primary partition.") % desc) ret = False log.debug("_is_valid_partition(%s) returning %s", device.name, ret) return ret # # target/stage1 device access # def _device_type_index(self, device, types): """ Return the index of the matching type in types to device's type. Return None if no match is found. """ index = None try: index = types.index(device.type) except ValueError: if "disk" in types and device.is_disk: index = types.index("disk") return index def _device_type_match(self, device, types): """ Return True if device is of one of the types in the list types. """ return self._device_type_index(device, types) is not None def device_description(self, device): device_types = list(self.device_descriptions.keys()) idx = self._device_type_index(device, device_types) if idx is None: raise ValueError("No description available for %s" % device.type) # this looks unnecessarily complicated, but it handles the various # device types that we treat as disks return self.device_descriptions[device_types[idx]] def set_preferred_stage1_type(self, preferred): """ Set a preferred type of stage1 device. """ if not self.stage2_is_valid_stage1: # "partition" means first sector of stage2 and is only meaningful # for bootloaders that can use stage2 as stage1 return if preferred == "mbr": # "mbr" is already the default return # partition means "use the stage2 device for a stage1 device" self.stage2_is_preferred_stage1 = True def is_valid_stage1_device(self, device, early=False): """ Return True if the device is a valid stage1 target device. Also collect lists of errors and warnings. The criteria for being a valid stage1 target device vary from platform to platform. On some platforms a disk with an msdos disklabel is a valid stage1 target, while some platforms require a special device. Some examples of these special devices are EFI system partitions on EFI machines, PReP boot partitions on iSeries, and Apple bootstrap partitions on Mac. The 'early' keyword argument is a boolean flag indicating whether or not this check is being performed at a point where the mountpoint cannot be expected to be set for things like EFI system partitions. """ self.errors = [] self.warnings = [] valid = True constraint = platform.platform.boot_stage1_constraint_dict if device is None: return False if not self._device_type_match(device, constraint["device_types"]): log.debug("stage1 device cannot be of type %s", device.type) return False if _is_on_sw_iscsi(device): if not _is_on_ibft(device): if conf.bootloader.nonibft_iscsi_boot: log.debug("stage1 device on non-iBFT iSCSI disk allowed " "by boot option inst.iscsi.nonibftboot") else: log.debug( "stage1 device cannot be on an non-iBFT iSCSI disk") self.errors.append( _("Boot loader stage1 device cannot be on " "an iSCSI disk which is not configured in iBFT.")) return False description = self.device_description(device) if self.stage2_is_valid_stage1 and device == self.stage2_device: # special case valid = (self.stage2_is_preferred_stage1 and self.is_valid_stage2_device(device)) # we'll be checking stage2 separately so don't duplicate messages self.warnings = [] return valid if device.protected: valid = False if not self._is_valid_disklabel(device, disklabel_types=self.disklabel_types): valid = False if not self._is_valid_size(device, desc=description): valid = False if not self._is_valid_location( device, max_end=constraint["max_end"], desc=description): valid = False if not self._is_valid_md(device, raid_levels=constraint["raid_levels"], metadata=constraint["raid_metadata"], member_types=constraint["raid_member_types"], desc=description): valid = False if not self.stage2_bootable and not getattr(device, "bootable", True): log.warning("%s not bootable", device.name) # XXX does this need to be here? if getattr(device.format, "label", None) in ("ANACONDA", "LIVE"): log.info("ignoring anaconda boot disk") valid = False if early: mountpoints = [] else: mountpoints = constraint["mountpoints"] if not self._is_valid_format(device, format_types=constraint["format_types"], mountpoints=mountpoints, desc=description): valid = False if not self.encryption_support and device.encrypted: self.errors.append( _("%s cannot be on an encrypted block " "device.") % description) valid = False log.debug("is_valid_stage1_device(%s) returning %s", device.name, valid) return valid def set_stage1_device(self, devices): self.stage1_device = None if not self.stage1_disk: self.reset() raise BootLoaderError("need stage1 disk to set stage1 device") if self.stage2_is_preferred_stage1: self.stage1_device = self.stage2_device return # Track the errors set by validity check in case no device would be found. errors = [] for device in devices: if self.stage1_disk not in device.disks: continue if self.is_valid_stage1_device(device): if conf.target.is_image and device.is_disk: # GRUB2 will install to /dev/loop0 but not to # /dev/mapper/<image_name> self.stage1_device = device.parents[0] else: self.stage1_device = device break errors.extend(self.errors) if not self.stage1_device: self.reset() msg = "Failed to find a suitable stage1 device" if errors: msg = msg + ": " + "; ".join(errors) raise BootLoaderError(msg) # # boot/stage2 device access # def is_valid_stage2_device(self, device, linux=True, non_linux=False): """ Return True if the device is suitable as a stage2 target device. Also collect lists of errors and warnings. """ self.errors = [] self.warnings = [] valid = True if device is None: return False if device.protected: valid = False if _is_on_sw_iscsi(device): if not _is_on_ibft(device): if conf.bootloader.nonibft_iscsi_boot: log.info( "%s on non-iBFT iSCSI disk allowed by boot option inst.nonibftiscsiboot", self.stage2_description) else: self.errors.append( _("%(bootloader_stage2_description)s cannot be on " "an iSCSI disk which is not configured in iBFT.") % { "bootloader_stage2_description": self.stage2_description }) valid = False if not self._device_type_match(device, self.stage2_device_types): self.errors.append( _("%(desc)s cannot be of type %(type)s") % { "desc": _(self.stage2_description), "type": device.type }) valid = False if not self._is_valid_disklabel(device, disklabel_types=self.disklabel_types): valid = False if not self._is_valid_size(device, desc=_(self.stage2_description)): valid = False if self.stage2_max_end and not self._is_valid_location( device, max_end=self.stage2_max_end, desc=_(self.stage2_description)): valid = False if not self._is_valid_partition(device, primary=self.stage2_must_be_primary): valid = False if not self._is_valid_md(device, raid_levels=self.stage2_raid_levels, metadata=self.stage2_raid_metadata, member_types=self.stage2_raid_member_types, desc=_(self.stage2_description)): valid = False if linux and \ not self._is_valid_format(device, format_types=self.stage2_format_types, mountpoints=self.stage2_mountpoints, desc=_(self.stage2_description)): valid = False non_linux_format_types = platform.platform._non_linux_format_types if non_linux and \ not self._is_valid_format(device, format_types=non_linux_format_types): valid = False if not self.encryption_support and device.encrypted: self.errors.append( _("%s cannot be on an encrypted block " "device.") % _(self.stage2_description)) valid = False log.debug("is_valid_stage2_device(%s) returning %s", device.name, valid) return valid # # miscellaneous # def has_windows(self, devices): return False @property def timeout(self): """Bootloader timeout in seconds.""" if self._timeout is not None: t = self._timeout else: t = 5 return t def check(self): """ Run additional bootloader checks """ return True @timeout.setter def timeout(self, seconds): self._timeout = seconds def set_boot_args(self, storage): """Set up the boot command line.""" self._set_extra_boot_args() self._set_storage_boot_args(storage) self._preserve_some_boot_args() self._set_graphical_boot_args() def _set_extra_boot_args(self): """Set the extra boot args.""" bootloader_proxy = STORAGE.get_proxy(BOOTLOADER) self.boot_args.update(bootloader_proxy.ExtraArguments) def _set_storage_boot_args(self, storage): """Set the storage boot args.""" fcoe_proxy = STORAGE.get_proxy(FCOE) iscsi_proxy = STORAGE.get_proxy(ISCSI) # FIPS boot_device = storage.mountpoints.get("/boot") if kernel_arguments.get("fips") == "1" and boot_device: self.boot_args.add("boot=%s" % self.stage2_device.fstab_spec) # Storage dracut_devices = [storage.root_device] if self.stage2_device != storage.root_device: dracut_devices.append(self.stage2_device) swap_devices = storage.fsset.swap_devices dracut_devices.extend(swap_devices) # Add resume= option to enable hibernation on x86. # Choose the largest swap device for that. if blivet.arch.is_x86() and swap_devices: resume_device = max(swap_devices, key=lambda x: x.size) self.boot_args.add("resume=%s" % resume_device.fstab_spec) # Does /usr have its own device? If so, we need to tell dracut usr_device = storage.mountpoints.get("/usr") if usr_device: dracut_devices.extend([usr_device]) netdevs = [d for d in storage.devices \ if (getattr(d, "complete", True) and isinstance(d, NetworkStorageDevice))] rootdev = storage.root_device if any(rootdev.depends_on(netdev) for netdev in netdevs): dracut_devices = set(dracut_devices) # By this time this thread should be the only one running, and also # mountpoints is a property function that returns a new dict every # time, so iterating over the values is safe. for dev in storage.mountpoints.values(): if any(dev.depends_on(netdev) for netdev in netdevs): dracut_devices.add(dev) done = [] for device in dracut_devices: for dep in storage.devices: if dep in done: continue if device != dep and not device.depends_on(dep): continue if isinstance(dep, blivet.devices.FcoeDiskDevice): setup_args = fcoe_proxy.GetDracutArguments(dep.nic) elif isinstance(dep, blivet.devices.iScsiDiskDevice): # (partial) offload devices do not need setup in dracut if not dep.offload: node = _get_iscsi_node_from_device(dep) setup_args = iscsi_proxy.GetDracutArguments( Node.to_structure(node)) else: setup_args = dep.dracut_setup_args() if not setup_args: continue self.boot_args.update(setup_args) self.dracut_args.update(setup_args) done.append(dep) # network configuration arguments if isinstance(dep, NetworkStorageDevice): network_proxy = NETWORK.get_proxy() network_args = [] ibft = False nic = "" if isinstance(dep, blivet.devices.iScsiDiskDevice): if dep.iface == "default" or ":" in dep.iface: node = _get_iscsi_node_from_device(dep) if iscsi_proxy.IsNodeFromIbft( Node.to_structure(node)): ibft = True else: nic = iface_for_host_ip(dep.host_address) else: nic = iscsi_proxy.GetInterface(dep.iface) else: nic = dep.nic if nic or ibft: network_args = network_proxy.GetDracutArguments( nic, dep.host_address, "", ibft) self.boot_args.update(network_args) self.dracut_args.update(network_args) # This is needed for FCoE, bug #743784. The case: # We discover LUN on an iface which is part of multipath setup. # If the iface is disconnected after discovery anaconda doesn't # write dracut ifname argument for the disconnected iface path # (in NETWORK.GetDracutArguments). # Dracut needs the explicit ifname= because biosdevname # fails to rename the iface (because of BFS booting from it). for nic in fcoe_proxy.GetNics(): hwaddr = get_interface_hw_address(nic) if hwaddr: self.boot_args.add("ifname=%s:%s" % (nic, hwaddr.lower())) # Add rd.iscsi.firmware to trigger dracut running iscsistart # See rhbz#1099603 and rhbz#1185792 if len(glob("/sys/firmware/iscsi_boot*")) > 0: self.boot_args.add("rd.iscsi.firmware") def _preserve_some_boot_args(self): """Preserve some of the boot args.""" for opt in conf.bootloader.preserved_arguments: if opt not in kernel_arguments: continue arg = kernel_arguments.get(opt) new_arg = opt if arg: new_arg += "=%s" % arg self.boot_args.add(new_arg) def _set_graphical_boot_args(self): """Set up the graphical boot.""" args = [] try: import rpm except ImportError: pass else: util.resetRpmDb() ts = rpm.TransactionSet(conf.target.system_root) # Only add "rhgb quiet" on non-s390, non-serial installs. if util.isConsoleOnVirtualTerminal() \ and (ts.dbMatch('provides', 'rhgb').count() or ts.dbMatch('provides', 'plymouth').count()): args = ["rhgb", "quiet"] self.boot_args.update(args) self.dracut_args.update(args) # # configuration # @property def boot_prefix(self): """ Prefix, if any, to paths in /boot. """ if self.stage2_device.format.mountpoint == "/": prefix = "/boot" else: prefix = "" return prefix def _set_console(self): """ Set console options based on boot arguments. """ console = kernel_arguments.get("console", "") console = os.path.basename(console) self.console, _x, self.console_options = console.partition(",") def write_config_console(self, config): """Write console-related configuration lines.""" pass def write_config_password(self, config): """Write password-related configuration lines.""" pass def write_config_header(self, config): """Write global configuration lines.""" self.write_config_console(config) self.write_config_password(config) def write_config_images(self, config): """Write image configuration entries.""" raise NotImplementedError() def write_config_post(self): pass def write_config(self): """ Write the bootloader configuration. """ if not self.config_file: raise BootLoaderError( "no config file defined for this boot loader") config_path = os.path.normpath(conf.target.system_root + self.config_file) if os.access(config_path, os.R_OK): os.rename(config_path, config_path + ".anacbak") config = util.open_with_perm(config_path, "w", self.config_file_mode) self.write_config_header(config) self.write_config_images(config) config.close() self.write_config_post() # # installation # def write(self): """ Write the bootloader configuration and install the bootloader. """ if self.skip_bootloader: return self.write_config() os.sync() self.stage2_device.format.sync(root=conf.target.physical_root) self.install() def install(self, args=None): raise NotImplementedError()
def testConvertToPrecision(self): s = Size(1835008) self.assertEqual(s.convertTo(None), 1835008) self.assertEqual(s.convertTo(B), 1835008) self.assertEqual(s.convertTo(KiB), 1792) self.assertEqual(s.convertTo(MiB), Decimal("1.75"))
def _calculate_free_space(self): """Calculate the available space.""" return Size(self.storage.file_system_free_space)
def testPartialBytes(self): self.assertEqual(Size("1024.6"), Size(1024)) self.assertEqual(Size("%s KiB" % (1/1025.0,)), Size(0)) self.assertEqual(Size("%s KiB" % (1/1023.0,)), Size(1))
def _calculate_free_space(self): """Calculate the available space.""" stat = os.statvfs(util.getSysroot()) return Size(stat.f_bsize * stat.f_bfree)
def testPickling(self): s = Size("10 MiB") self.assertEqual(s, cPickle.loads(cPickle.dumps(s)))
def get_disks_for_implicit_partitions_test(self): """Test the get_disks_for_implicit_partitions function.""" # The /boot partition always requires a slot. requests = [ PartSpec(mountpoint="/boot", size=Size("1GiB")), PartSpec(mountpoint="/", size=Size("2GiB"), max_size=Size("15GiB"), grow=True, btr=True, lv=True, thin=True, encrypted=True), PartSpec(fstype="swap", grow=False, lv=True, encrypted=True) ] # No implicit partitions to schedule. disk_1 = Mock() disk_2 = Mock() parted_disk_1 = disk_1.format.parted_disk parted_disk_2 = disk_2.format.parted_disk self.assertEqual( get_disks_for_implicit_partitions(scheme=AUTOPART_TYPE_PLAIN, disks=[disk_1, disk_2], requests=requests), []) # Extended partitions are supported by the first disk. parted_disk_1.supportsFeature.return_value = True parted_disk_1.maxPrimaryPartitionCount = 3 parted_disk_1.primaryPartitionCount = 3 parted_disk_2.supportsFeature.return_value = False parted_disk_2.maxPrimaryPartitionCount = 3 parted_disk_2.primaryPartitionCount = 2 self.assertEqual( get_disks_for_implicit_partitions(scheme=AUTOPART_TYPE_LVM_THINP, disks=[disk_1, disk_2], requests=requests), [disk_1, disk_2]) # Extended partitions are not supported by the first disk. parted_disk_1.supportsFeature.return_value = False parted_disk_1.maxPrimaryPartitionCount = 3 parted_disk_1.primaryPartitionCount = 2 self.assertEqual( get_disks_for_implicit_partitions(scheme=AUTOPART_TYPE_LVM_THINP, disks=[disk_1, disk_2], requests=requests), [disk_2]) # Not empty slots for implicit partitions. parted_disk_1.supportsFeature.return_value = False parted_disk_1.maxPrimaryPartitionCount = 3 parted_disk_1.primaryPartitionCount = 3 self.assertEqual( get_disks_for_implicit_partitions(scheme=AUTOPART_TYPE_LVM_THINP, disks=[disk_1, disk_2], requests=requests), [])
def testHumanReadable(self): s = Size(58929971) self.assertEqual(s.humanReadable(), "56.2 MiB") s = Size(478360371) self.assertEqual(s.humanReadable(), "456.2 MiB") # humanReable output should be the same as input for big enough sizes # and enough places and integer values s = Size("12.68 TiB") self.assertEqual(s.humanReadable(max_places=2), "12.68 TiB") s = Size("26.55 MiB") self.assertEqual(s.humanReadable(max_places=2), "26.55 MiB") s = Size("300 MiB") self.assertEqual(s.humanReadable(max_places=2), "300 MiB") # when min_value is 10 and single digit on left of decimal, display # with smaller unit. s = Size("9.68 TiB") self.assertEqual(s.humanReadable(max_places=2, min_value=10), "9912.32 GiB") s = Size("4.29 MiB") self.assertEqual(s.humanReadable(max_places=2, min_value=10), "4392.96 KiB") s = Size("7.18 KiB") self.assertEqual(s.humanReadable(max_places=2, min_value=10), "7352 B") # rounding should work with max_places limitted s = Size("12.687 TiB") self.assertEqual(s.humanReadable(max_places=2), "12.69 TiB") s = Size("23.7874 TiB") self.assertEqual(s.humanReadable(max_places=3), "23.787 TiB") s = Size("12.6998 TiB") self.assertEqual(s.humanReadable(max_places=2), "12.7 TiB") # byte values close to multiples of 2 are shown without trailing zeros s = Size(0xff) self.assertEqual(s.humanReadable(max_places=2), "255 B") s = Size(8193) self.assertEqual(s.humanReadable(max_places=2, min_value=10), "8193 B") # a fractional quantity is shown if the value deviates # from the whole number of units by more than 1% s = Size(16384 - (1024/100 + 1)) self.assertEqual(s.humanReadable(max_places=2), "15.99 KiB") # if max_places is set to None, all digits are displayed s = Size(0xfffffffffffff) self.assertEqual(s.humanReadable(max_places=None), "3.9999999999999991118215803 PiB") s = Size(0x10000) self.assertEqual(s.humanReadable(max_places=None), "64 KiB") s = Size(0x10001) self.assertEqual(s.humanReadable(max_places=None), "64.0009765625 KiB") # test a very large quantity with no associated abbreviation or prefix s = Size(1024**9) self.assertEqual(s.humanReadable(max_places=2), "1024 YiB") s = Size(1024**9 - 1) self.assertEqual(s.humanReadable(max_places=2), "1024 YiB") s = Size(1024**9 + 1) self.assertEqual(s.humanReadable(max_places=2, strip=False), "1024.00 YiB") s = Size(1024**10) self.assertEqual(s.humanReadable(max_places=2), "1048576 YiB")
from blivet.size import Size from pyanaconda.modules.storage.partitioning.automatic.utils import get_default_partitioning from pyanaconda.modules.storage.partitioning.specification import PartSpec from pyanaconda.core.configuration.anaconda import AnacondaConfiguration from pyanaconda.core.configuration.base import ConfigurationError, create_parser, read_config from pyanaconda.core.configuration.product import ProductLoader from pyanaconda.product import trim_product_version_for_ui PRODUCT_DIR = os.path.join(os.environ.get("ANACONDA_DATA"), "product.d") SERVER_PARTITIONING = [ PartSpec( mountpoint="/", size=Size("2GiB"), max_size=Size("15GiB"), grow=True, btr=True, lv=True, thin=True, encrypted=True, ) ] WORKSTATION_PARTITIONING = [ PartSpec( mountpoint="/", size=Size("1GiB"), max_size=Size("70GiB"), grow=True,
def test_pick_mount_points(self): """Test the _pick_mount_points function.""" mount_points = { "/": Size("1 G"), "/home": Size("1 G"), "/var/tmp": Size("1 G"), "/mnt/sysroot": Size("1 G"), "/mnt/sysroot/home": Size("1 G"), "/mnt/sysroot/tmp": Size("1 G"), "/mnt/sysroot/var": Size("1 G"), "/mnt/sysroot/usr": Size("1 G"), } # All mount points are big enough. # Choose all suitable mount points. sufficient = _pick_mount_points(mount_points, download_size=Size("0.5 G"), install_size=Size("0.5 G")) assert sufficient == { "/var/tmp", "/mnt/sysroot", "/mnt/sysroot/home", "/mnt/sysroot/tmp", "/mnt/sysroot/var" } # No mount point is big enough for installation. # Choose non-sysroot mount points for download. sufficient = _pick_mount_points(mount_points, download_size=Size("0.5 G"), install_size=Size("1.5 G")) assert sufficient == { "/var/tmp", } # No mount point is big enough for installation or download. sufficient = _pick_mount_points(mount_points, download_size=Size("1.5 G"), install_size=Size("1.5 G")) assert sufficient == set()
def space_required(self): return super().space_required + Size( self._flatpak_payload.get_required_size())
class ContainerDialog(GUIObject, GUIDialogInputCheckHandler): builderObjects = [ "container_dialog", "disk_store", "container_disk_view", "containerRaidStoreFiltered", "containerRaidLevelLabel", "containerRaidLevelCombo", "raidLevelStore", "containerSizeCombo", "containerSizeEntry", "containerSizeLabel", "containerEncryptedCheckbox" ] mainWidgetName = "container_dialog" uiFile = "spokes/lib/custom_storage_helpers.glade" # If the user enters a smaller size, the GUI changes it to this value MIN_SIZE_ENTRY = Size("1 MiB") def __init__(self, *args, **kwargs): # these are all absolutely required. not getting them is fatal. self._disks = kwargs.pop("disks") free = kwargs.pop("free") self.selected = kwargs.pop("selected")[:] self.name = kwargs.pop("name") or "" # make sure it's a string self.device_type = kwargs.pop("device_type") self.storage = kwargs.pop("storage") # these are less critical self.raid_level = kwargs.pop("raid_level", None) or None # not "" self.encrypted = kwargs.pop("encrypted", False) self.exists = kwargs.pop("exists", False) self.size_policy = kwargs.pop("size_policy", SIZE_POLICY_AUTO) self.size = kwargs.pop("size", Size(0)) self._error = None GUIObject.__init__(self, *args, **kwargs) self._grabObjects() GUIDialogInputCheckHandler.__init__(self, self._save_button) # set up the dialog labels with device-type-specific text container_type = get_container_type(self.device_type) title_text = _(CONTAINER_DIALOG_TITLE) % { "container_type": _(container_type.name).upper() } self._title_label.set_text(title_text) dialog_text = _(CONTAINER_DIALOG_TEXT) % { "container_type": _(container_type.name).lower() } self._dialog_label.set_text(dialog_text) # populate the dialog widgets self._name_entry.set_text(self.name) # populate the store for disk in self._disks: self._store.append([ disk.description, str(disk.size), str(free[disk.name][0]), disk.name, disk.id ]) model = self._treeview.get_model() itr = model.get_iter_first() selected_ids = [d.id for d in self.selected] selection = self._treeview.get_selection() while itr: disk_id = model.get_value(itr, 4) if disk_id in selected_ids: selection.select_iter(itr) itr = model.iter_next(itr) # XXX how will this be related to the device encryption setting? self._encryptCheckbutton.set_active(self.encrypted) # set up the raid level combo # XXX how will this be related to the device raid level setting? self._raidStoreFilter.set_visible_func(self._raid_level_visible) self._raidStoreFilter.refilter() self._populate_raid() self._original_size = self.size self._original_size_text = self.size.human_readable(max_places=2) self._sizeEntry.set_text(self._original_size_text) if self.size_policy == SIZE_POLICY_AUTO: self._sizeCombo.set_active(0) elif self.size_policy == SIZE_POLICY_MAX: self._sizeCombo.set_active(1) else: self._sizeCombo.set_active(2) if self.exists: fancy_set_sensitive(self._name_entry, False) self._treeview.set_sensitive(False) fancy_set_sensitive(self._encryptCheckbutton, False) fancy_set_sensitive(self._sizeCombo, False) self._sizeEntry.set_sensitive(False) # Check that the container name configured is valid self.add_check(self._name_entry, self._checkNameEntry) def _grabObjects(self): self._title_label = self.builder.get_object( "container_dialog_title_label") self._dialog_label = self.builder.get_object("container_dialog_label") self._error_label = self.builder.get_object("containerErrorLabel") self._name_entry = self.builder.get_object("container_name_entry") self._encryptCheckbutton = self.builder.get_object( "containerEncryptedCheckbox") self._raidStoreFilter = self.builder.get_object( "containerRaidStoreFiltered") self._store = self.builder.get_object("disk_store") self._treeview = self.builder.get_object("container_disk_view") self._sizeCombo = self.builder.get_object("containerSizeCombo") self._sizeEntry = self.builder.get_object("containerSizeEntry") self._raidLevelCombo = self.builder.get_object( "containerRaidLevelCombo") self._raidLevelLabel = self.builder.get_object( "containerRaidLevelLabel") self._save_button = self.builder.get_object("container_save_button") def _get_disk_by_id(self, disk_id): for disk in self._disks: if disk.id == disk_id: return disk def _save_clicked(self): if self.exists: return model, paths = self._treeview.get_selection().get_selected_rows() raid_level = selectedRaidLevel(self._raidLevelCombo) if raid_level: min_disks = raid_level.min_members if len(paths) < min_disks: self._error = (_(RAID_NOT_ENOUGH_DISKS) % { "level": raid_level, "min": min_disks, "count": len(paths) }) self._error_label.set_text(self._error) self.window.show_all() return idx = self._sizeCombo.get_active() if idx == 0: size = SIZE_POLICY_AUTO elif idx == 1: size = SIZE_POLICY_MAX elif idx == 2: if self._original_size_text != self._sizeEntry.get_text(): size = size_from_entry(self._sizeEntry, lower_bound=self.MIN_SIZE_ENTRY, units=SIZE_UNITS_DEFAULT) if size is None: size = SIZE_POLICY_MAX else: size = self._original_size # now save the changes self.selected = [] for path in paths: itr = model.get_iter(path) disk_id = model.get_value(itr, 4) self.selected.append(self._get_disk_by_id(disk_id)) self.name = self._name_entry.get_text().strip() self.raid_level = raid_level self.encrypted = self._encryptCheckbutton.get_active() self.size_policy = size self._error_label.set_text("") def run(self): while True: self._error = None rc = self.window.run() if rc == 1: # Save clicked and input validation passed, try saving it if self.on_ok_clicked(): self._save_clicked() # If that failed, try again if self._error: continue else: break # Save clicked with invalid input, try again else: continue else: # Cancel or something similar, just exit break self.window.destroy() return rc def on_size_changed(self, combo): active_index = combo.get_active() if active_index == 0: self._sizeEntry.set_sensitive(False) elif active_index == 1: self._sizeEntry.set_sensitive(False) else: self._sizeEntry.set_sensitive(True) def _raid_level_visible(self, model, itr, user_data): raid_level_str = model[itr][1] raid_level = raid.get_raid_level( raid_level_str) if raid_level_str != "none" else None return raid_level in containerRaidLevelsSupported(self.device_type) def _populate_raid(self): """ Set up the raid-specific portion of the device details. Hide the RAID level menu if this device type does not support RAID. Choose a default RAID level. """ if not containerRaidLevelsSupported(self.device_type): for widget in [self._raidLevelLabel, self._raidLevelCombo]: really_hide(widget) return raid_level = self.raid_level or defaultContainerRaidLevel( self.device_type) raid_level_name = raidLevelSelection(raid_level) # Set a default RAID level in the combo. for (i, row) in enumerate(self._raidLevelCombo.get_model()): log.debug("container dialog: raid level %s", row[1]) if row[1] == raid_level_name: self._raidLevelCombo.set_active(i) break for widget in [self._raidLevelLabel, self._raidLevelCombo]: really_show(widget) fancy_set_sensitive(self._raidLevelCombo, not self.exists) def _checkNameEntry(self, inputcheck): container_name = self.get_input(inputcheck.input_obj).strip() # Check that the container name is valid safename = self.storage.safe_device_name(container_name) if container_name != safename: return _("Invalid container name") return InputCheck.CHECK_OK
# from blivet.size import Size from pyanaconda.core.configuration.anaconda import conf from pyanaconda.core.configuration.storage import PartitioningType from pyanaconda.core.constants import STORAGE_SWAP_IS_RECOMMENDED from pyanaconda.modules.common.constants.objects import AUTO_PARTITIONING from pyanaconda.modules.common.constants.services import STORAGE from pyanaconda.storage.partspec import PartSpec # Partitioning requirements for servers. SERVER_PARTITIONING = [ PartSpec( mountpoint="/", size=Size("2GiB"), max_size=Size("15GiB"), grow=True, btr=True, lv=True, thin=True, encrypted=True ), PartSpec( fstype="swap", grow=False, lv=True, encrypted=True ) ]
def set_platform_boot_partition(self): """Return the default /boot partition for this platform.""" return [PartSpec(mountpoint="/boot", size=Size("1GiB"))]
def destroy_device(storage, device): """Destroy the given device in the storage model. :param storage: an instance of Blivet :param device: an instance of a device """ log.debug("Destroy device: %s", device.name) # Remove the device. if device.is_disk and device.partitioned and not device.format.supported: storage.recursive_remove(device) elif device.direct and not device.isleaf: # We shouldn't call this method for with non-leaf devices # except for those which are also directly accessible like # lvm snapshot origins and btrfs subvolumes that contain # other subvolumes. storage.recursive_remove(device) else: storage.destroy_device(device) # Initialize the disk. if device.is_disk: storage.initialize_disk(device) # Remove empty extended partitions. if getattr(device, "is_logical", False): storage.remove_empty_extended_partitions() # If we've just removed the last partition and the disk label # is preexisting, reinitialize the disk. if device.type == "partition" and device.exists and device.disk.format.exists: config = DiskInitializationConfig() if config.can_initialize(storage, device.disk): storage.initialize_disk(device.disk) # Get the device container. if hasattr(device, "vg"): container = device.vg device_type = devicefactory.get_device_type(device) elif hasattr(device, "volume"): container = device.volume device_type = devicefactory.DEVICE_TYPE_BTRFS else: container = None device_type = None # Adjust container to size of remaining devices, if auto-sized. if (container and not container.exists and container.children and container.size_policy == devicefactory.SIZE_POLICY_AUTO): # Create the device factory. factory = devicefactory.get_device_factory( storage, device_type=device_type, size=Size(0), disks=container.disks, container_name=container.name, container_encrypted=container.encrypted, container_raid_level=get_device_raid_level(container), container_size=container.size_policy, ) # Configure the factory's devices. factory.configure() # Finally, remove empty parents of the device. for parent in device.parents: if not parent.children and not parent.is_disk: destroy_device(storage, parent)