Example #1
0
    def _create_btrfs_subvolume(self, user_input):
        actions = []
        device_name = self._pick_device_name(user_input.name, user_input.parents[0][0])

        new_btrfs = BTRFSSubVolumeDevice(device_name, parents=[i[0] for i in user_input.parents])
        new_btrfs.format = blivet.formats.get_format("btrfs", mountpoint=user_input.mountpoint)
        actions.append(blivet.deviceaction.ActionCreateDevice(new_btrfs))

        return actions
Example #2
0
    def test_generate_device_factory_request_btrfs(self, blockdev):
        dev1 = StorageDevice("dev1",
                             fmt=get_format("btrfs"),
                             size=Size("10 GiB"))

        dev2 = BTRFSVolumeDevice("dev2", data_level="single", parents=[dev1])

        dev3 = BTRFSSubVolumeDevice(
            parents=[dev2],
            fmt=get_format("btrfs", mountpoint="/boot"),
        )

        request = utils.generate_device_factory_request(self.storage, dev3)
        assert DeviceFactoryRequest.to_structure(request) == {
            "device-spec": get_variant(Str, dev3.name),
            "disks": get_variant(List[Str], []),
            "mount-point": get_variant(Str, "/boot"),
            "reformat": get_variant(Bool, True),
            "format-type": get_variant(Str, "btrfs"),
            "label": get_variant(Str, ""),
            "luks-version": get_variant(Str, ""),
            "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_BTRFS),
            "device-name": get_variant(Str, dev3.name),
            "device-size": get_variant(UInt64,
                                       Size("10 GiB").get_bytes()),
            "device-encrypted": get_variant(Bool, False),
            "device-raid-level": get_variant(Str, ""),
            "container-spec": get_variant(Str, dev2.name),
            "container-name": get_variant(Str, dev2.name),
            "container-size-policy": get_variant(Int64,
                                                 Size("10 GiB").get_bytes()),
            "container-encrypted": get_variant(Bool, False),
            "container-raid-level": get_variant(Str, "single"),
        }
Example #3
0
    def setUp(self):
        self._state_functions = {
            "currentSize":
            lambda x, m: self.assertEqual(x, Size(bytes=0), m),
            "exists":
            self.assertFalse,
            "format":
            self.assertIsNotNone,
            "formatArgs":
            lambda x, m: self.assertEqual(x, [], m),
            "fstabSpec":
            self.assertIsNotNone,
            "isDisk":
            self.assertFalse,
            "major":
            lambda x, m: self.assertEqual(x, 0, m),
            "maxSize":
            lambda x, m: self.assertEqual(x, Size(bytes=0), m),
            "mediaPresent":
            self.assertTrue,
            "minor":
            lambda x, m: self.assertEqual(x, 0, m),
            "parents":
            lambda x, m: self.assertEqual(len(x), 0, m) and self.
            assertIsInstance(x, ParentList, m),
            "partitionable":
            self.assertFalse,
            "path":
            lambda x, m: self.assertRegexpMatches(x, "^/dev", m),
            "resizable":
            lambda x, m: self.assertFalse,
            "size":
            lambda x, m: self.assertEqual(x, Size(bytes=0), m),
            "status":
            self.assertFalse,
            "sysfsPath":
            lambda x, m: self.assertEqual(x, "", m),
            "targetSize":
            lambda x, m: self.assertEqual(x, Size(bytes=0), m),
            "type":
            lambda x, m: self.assertEqual(x, "btrfs", m),
            "uuid":
            self.assertIsNone,
            "vol_id":
            lambda x, m: self.assertEqual(x, btrfs.MAIN_VOLUME_ID, m)
        }

        self.dev1 = BTRFSVolumeDevice(
            "dev1",
            parents=[
                OpticalDevice("deva", format=blivet.formats.getFormat("btrfs"))
            ])

        self.dev2 = BTRFSSubVolumeDevice("dev2", parents=[self.dev1])

        dev = StorageDevice("deva",
                            format=blivet.formats.getFormat("btrfs"),
                            size=Size(spec="32 MiB"))
        self.dev3 = BTRFSVolumeDevice("dev3", parents=[dev])
Example #4
0
    def setUp(self):
        """Create some device objects to test with.

            This sets up two disks (sda, sdb). The first partition of each
            is a biosboot partition. The second partitions comprise a RAID1
            array formatted as /boot.

            sda additionally contains a third partition formatted as ext4.
        """

        super(GRUBRaidSimpleTest, self).setUp()

        # Make some disks
        self.sda = DiskDevice(name="sda", size=Size("100 GiB"))
        self.sda.format = getFormat("disklabel")
        self.sdb = DiskDevice(name="sdb", size=Size("100 GiB"))
        self.sdb.format = getFormat("disklabel")

        # Set up biosboot partitions, an mdarray for /boot, and a btrfs array on sda + sdb.
        # Start with the partitions
        self.sda1 = PartitionDevice(name="sda1", parents=[self.sda], size=Size("1 MiB"))
        self.sda1.format = getFormat("biosboot")
        self.sda2 = PartitionDevice(name="sda2", parents=[self.sda], size=Size("500 MiB"))
        self.sda2.format = getFormat("mdmember")
        self.sda4 = PartitionDevice(name="sda4", parents=[self.sda], size=Size("500 MiB"))
        self.sda4.format = getFormat("btrfs")

        self.sdb1 = PartitionDevice(name="sdb1", parents=[self.sdb], size=Size("1 MiB"))
        self.sdb1.format = getFormat("biosboot")
        self.sdb2 = PartitionDevice(name="sdb2", parents=[self.sdb], size=Size("500 MiB"))
        self.sdb2.format = getFormat("mdmember")
        self.sdb4 = PartitionDevice(name="sdb4", parents=[self.sdb], size=Size("4 GiB"))
        self.sdb4.format = getFormat("btrfs")

        # Add an extra partition for /boot on not-RAID
        self.sda3 = PartitionDevice(name="sda3", parents=[self.sda], size=Size("500 MiB"))
        self.sda3.format = getFormat("ext4", mountpoint="/boot")

        # Pretend that the partitions are real with real parent disks
        for part in (self.sda1, self.sda2, self.sda3, self.sda4, self.sdb1, self.sdb2, self.sdb4):
            part.parents = part.req_disks

        self.boot_md = MDRaidArrayDevice(name="md1", parents=[self.sda2, self.sdb2], level=1)
        self.boot_md.format = getFormat("ext4", mountpoint="/boot")

        # Set up the btrfs raid1 volume with a subvolume for /boot
        self.btrfs_volume = BTRFSVolumeDevice(parents=[self.sda4, self.sdb4], dataLevel=RAID1)
        self.btrfs_volume.format = getFormat("btrfs")

        self.boot_btrfs = BTRFSSubVolumeDevice(parents=[self.btrfs_volume])
        self.boot_btrfs.format = getFormat("btrfs", mountpoint="/boot")

        self.grub = GRUB()
Example #5
0
    def setUp(self):
        self.dev1 = BTRFSVolumeDevice("dev1",
           parents=[StorageDevice("deva",
              fmt=blivet.formats.getFormat("btrfs"),
              size=btrfs.MIN_MEMBER_SIZE)])

        self.dev2 = BTRFSSubVolumeDevice("dev2",
           parents=[self.dev1],
           fmt=blivet.formats.getFormat("btrfs"))

        dev = StorageDevice("deva",
           fmt=blivet.formats.getFormat("btrfs"),
           size=Size("32 MiB"))
        self.dev3 = BTRFSVolumeDevice("dev3",
           parents=[dev])
Example #6
0
    def test_btrfsdevice_init(self):
        """Tests the state of a BTRFSDevice after initialization.
           For some combinations of arguments the initializer will throw
           an exception.
        """

        self.state_check(
            self.dev1,
            current_size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            data_level=xform(self.assertIsNone),
            isleaf=xform(self.assertFalse),
            max_size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            metadata_level=xform(self.assertIsNone),
            parents=xform(lambda x, m: self.assertEqual(len(x), 1, m)),
            size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            type=xform(lambda x, m: self.assertEqual(x, "btrfs volume", m)))

        self.state_check(
            self.dev2,
            target_size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            current_size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            max_size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            parents=xform(lambda x, m: self.assertEqual(len(x), 1, m)),
            size=xform(
                lambda x, m: self.assertEqual(x, BTRFS_MIN_MEMBER_SIZE, m)),
            type=xform(lambda x, m: self.assertEqual(x, "btrfs subvolume", m)),
            vol_id=xform(self.assertIsNone))

        self.state_check(
            self.dev3,
            current_size=xform(
                lambda x, m: self.assertEqual(x, Size("500 MiB"), m)),
            data_level=xform(self.assertIsNone),
            max_size=xform(
                lambda x, m: self.assertEqual(x, Size("500 MiB"), m)),
            metadata_level=xform(self.assertIsNone),
            parents=xform(lambda x, m: self.assertEqual(len(x), 1, m)),
            size=xform(lambda x, m: self.assertEqual(x, Size("500 MiB"), m)),
            type=xform(lambda x, m: self.assertEqual(x, "btrfs volume", m)))

        with six.assertRaisesRegex(
                self, ValueError,
                "BTRFSDevice.*must have at least one parent"):
            BTRFSVolumeDevice("dev")

        with six.assertRaisesRegex(self, ValueError, "format"):
            BTRFSVolumeDevice(
                "dev",
                parents=[StorageDevice("deva", size=BTRFS_MIN_MEMBER_SIZE)])

        with six.assertRaisesRegex(self, DeviceError,
                                   "btrfs subvolume.*must be a btrfs volume"):
            fmt = blivet.formats.get_format("btrfs")
            device = StorageDevice("deva", fmt=fmt, size=BTRFS_MIN_MEMBER_SIZE)
            BTRFSSubVolumeDevice("dev1", parents=[device])

        deva = OpticalDevice("deva",
                             fmt=blivet.formats.get_format("btrfs",
                                                           exists=True),
                             exists=True)
        with six.assertRaisesRegex(self, BTRFSValueError, "at least"):
            BTRFSVolumeDevice("dev1", data_level="raid1", parents=[deva])

        deva = StorageDevice("deva",
                             fmt=blivet.formats.get_format("btrfs"),
                             size=BTRFS_MIN_MEMBER_SIZE)
        self.assertIsNotNone(
            BTRFSVolumeDevice("dev1", metadata_level="dup", parents=[deva]))

        deva = StorageDevice("deva",
                             fmt=blivet.formats.get_format("btrfs"),
                             size=BTRFS_MIN_MEMBER_SIZE)
        with six.assertRaisesRegex(self, BTRFSValueError, "invalid"):
            BTRFSVolumeDevice("dev1", data_level="dup", parents=[deva])

        self.assertEqual(self.dev1.isleaf, False)
        self.assertEqual(self.dev1.direct, True)
        self.assertEqual(self.dev2.isleaf, True)
        self.assertEqual(self.dev2.direct, True)

        member = self.dev1.parents[0]
        self.assertEqual(member.isleaf, False)
        self.assertEqual(member.direct, False)
Example #7
0
    def add_device(self, user_input):
        """ Create new device

            :param user_input: selected parameters from AddDialog
            :type user_input: class UserInput
            :returns: new device name
            :rtype: str

        """

        actions = []

        if user_input.device_type == "partition":

            if user_input.encrypt:
                dev = PartitionDevice(
                    name="req%d" % self.storage.nextID,
                    size=user_input.size,
                    parents=[i[0] for i in user_input.parents])
                actions.append(blivet.deviceaction.ActionCreateDevice(dev))

                fmt = blivet.formats.getFormat(
                    fmt_type="luks",
                    passphrase=user_input.passphrase,
                    device=dev.path)
                actions.append(blivet.deviceaction.ActionCreateFormat(
                    dev, fmt))

                luks_dev = LUKSDevice("luks-%s" % dev.name,
                                      fmt=blivet.formats.getFormat(
                                          user_input.filesystem,
                                          device=dev.path,
                                          mountpoint=user_input.mountpoint),
                                      size=dev.size,
                                      parents=[dev])

                actions.append(
                    blivet.deviceaction.ActionCreateDevice(luks_dev))

            else:
                new_part = PartitionDevice(
                    name="req%d" % self.storage.nextID,
                    size=user_input.size,
                    parents=[i[0] for i in user_input.parents],
                    partType=PARTITION_TYPE[user_input.advanced["parttype"]])

                actions.append(
                    blivet.deviceaction.ActionCreateDevice(new_part))

                if user_input.advanced["parttype"] != "extended":
                    new_fmt = blivet.formats.getFormat(
                        fmt_type=user_input.filesystem,
                        label=user_input.label,
                        mountpoint=user_input.mountpoint)

                    actions.append(
                        blivet.deviceaction.ActionCreateFormat(
                            new_part, new_fmt))

        elif user_input.device_type == "lvm" and not user_input.encrypt:

            device_name = self._pick_device_name(user_input.name)

            pvs = []

            # exact total size of newly created pvs (future parents)
            total_size = blivet.size.Size("0 MiB")

            for parent, size in user_input.parents:

                dev = PartitionDevice(name="req%d" % self.storage.nextID,
                                      size=size,
                                      parents=parent)
                ac_part = blivet.deviceaction.ActionCreateDevice(dev)

                fmt = blivet.formats.getFormat(fmt_type="lvmpv")
                ac_fmt = blivet.deviceaction.ActionCreateFormat(dev, fmt)

                actions.extend([ac_part, ac_fmt])

                total_size += dev.size

                # we need to try to create pvs immediately, if something
                # fails, fail now
                try:
                    for ac in (ac_part, ac_fmt):
                        self.storage.devicetree.registerAction(ac)

                except blivet.errors.PartitioningError as e:
                    return ReturnList(success=False,
                                      actions=None,
                                      message=None,
                                      exception=e,
                                      traceback=sys.exc_info()[2])

                pvs.append(dev)

            new_vg = LVMVolumeGroupDevice(size=total_size,
                                          parents=pvs,
                                          name=device_name,
                                          peSize=user_input.advanced["pesize"])

            actions.append(blivet.deviceaction.ActionCreateDevice(new_vg))

        elif user_input.device_type == "lvm" and user_input.encrypt:

            device_name = self._pick_device_name(user_input.name)

            lukses = []

            # exact total size of newly created pvs (future parents)
            total_size = blivet.size.Size("0 MiB")

            for parent, size in user_input.parents:
                dev = PartitionDevice(name="req%d" % self.storage.nextID,
                                      size=user_input.size,
                                      parents=[parent])
                ac_part = blivet.deviceaction.ActionCreateDevice(dev)

                fmt = blivet.formats.getFormat(
                    fmt_type="luks",
                    passphrase=user_input.passphrase,
                    device=dev.path)
                ac_fmt = blivet.deviceaction.ActionCreateFormat(dev, fmt)

                luks_dev = LUKSDevice("luks-%s" % dev.name,
                                      fmt=blivet.formats.getFormat(
                                          "lvmpv", device=dev.path),
                                      size=dev.size,
                                      parents=[dev])
                ac_luks = blivet.deviceaction.ActionCreateDevice(luks_dev)

                actions.extend([ac_part, ac_fmt, ac_luks])

                total_size += luks_dev.size

                # we need to try to create pvs immediately, if something
                # fails, fail now
                try:
                    for ac in (ac_part, ac_fmt, ac_luks):
                        self.storage.devicetree.registerAction(ac)

                except blivet.errors.PartitioningError as e:
                    return ReturnList(success=False,
                                      actions=None,
                                      message=None,
                                      exception=e,
                                      traceback=sys.exc_info()[2])

                lukses.append(luks_dev)

            new_vg = LVMVolumeGroupDevice(size=total_size,
                                          parents=lukses,
                                          name=device_name,
                                          peSize=user_input.advanced["pesize"])

            actions.append(blivet.deviceaction.ActionCreateDevice(new_vg))

        elif user_input.device_type == "lvmlv":

            device_name = self._pick_device_name(user_input.name,
                                                 user_input.parents[0][0])

            new_part = LVMLogicalVolumeDevice(
                name=device_name,
                size=user_input.size,
                parents=[i[0] for i in user_input.parents])

            actions.append(blivet.deviceaction.ActionCreateDevice(new_part))

            new_fmt = blivet.formats.getFormat(
                fmt_type=user_input.filesystem,
                mountpoint=user_input.mountpoint)

            actions.append(
                blivet.deviceaction.ActionCreateFormat(new_part, new_fmt))

        elif user_input.device_type == "lvmvg":

            device_name = self._pick_device_name(user_input.name)

            new_vg = LVMVolumeGroupDevice(
                size=user_input.size,
                name=device_name,
                parents=[i[0] for i in user_input.parents],
                peSize=user_input.advanced["pesize"])

            actions.append(blivet.deviceaction.ActionCreateDevice(new_vg))

        elif user_input.device_type == "lvmpv":

            if user_input.encrypt:

                dev = PartitionDevice(
                    name="req%d" % self.storage.nextID,
                    size=user_input.size,
                    parents=[i[0] for i in user_input.parents])
                actions.append(blivet.deviceaction.ActionCreateDevice(dev))

                fmt = blivet.formats.getFormat(
                    fmt_type="luks",
                    passphrase=user_input.passphrase,
                    device=dev.path)
                actions.append(blivet.deviceaction.ActionCreateFormat(
                    dev, fmt))

                luks_dev = LUKSDevice("luks-%s" % dev.name,
                                      fmt=blivet.formats.getFormat(
                                          "lvmpv", device=dev.path),
                                      size=dev.size,
                                      parents=[dev])
                actions.append(
                    blivet.deviceaction.ActionCreateDevice(luks_dev))

            else:
                dev = PartitionDevice(
                    name="req%d" % self.storage.nextID,
                    size=user_input.size,
                    parents=[i[0] for i in user_input.parents])
                actions.append(blivet.deviceaction.ActionCreateDevice(dev))

                fmt = blivet.formats.getFormat(fmt_type="lvmpv")
                actions.append(blivet.deviceaction.ActionCreateFormat(
                    dev, fmt))

        elif user_input.device_type == "btrfs volume":

            device_name = self._pick_device_name(user_input.name)

            # for btrfs we need to create parents first -- currently selected "parents" are
            # disks but "real parents" for subvolume are btrfs formatted partitions
            btrfs_parents = []

            # exact total size of newly created partitions (future parents)
            total_size = blivet.size.Size("0 MiB")

            for parent, size in user_input.parents:

                if user_input.btrfs_type == "disks":
                    assert parent.isDisk

                    fmt = blivet.formats.getFormat(fmt_type="btrfs")
                    ac_fmt = blivet.deviceaction.ActionCreateFormat(
                        parent, fmt)

                    actions.append(ac_fmt)

                    try:
                        self.storage.devicetree.registerAction(ac_fmt)

                    except Exception as e:  # pylint: disable=broad-except
                        return ReturnList(success=False,
                                          actions=None,
                                          message=None,
                                          exception=e,
                                          traceback=sys.exc_info()[2])

                    total_size += size
                    btrfs_parents.append(parent)

                else:

                    dev = PartitionDevice(name="req%d" % self.storage.nextID,
                                          size=size,
                                          parents=[parent])
                    ac_part = blivet.deviceaction.ActionCreateDevice(dev)

                    fmt = blivet.formats.getFormat(fmt_type="btrfs")
                    ac_fmt = blivet.deviceaction.ActionCreateFormat(dev, fmt)

                    actions.extend([ac_part, ac_fmt])

                    total_size += dev.size

                    # we need to try to create partitions immediately, if something
                    # fails, fail now
                    try:
                        for ac in (ac_part, ac_fmt):
                            self.storage.devicetree.registerAction(ac)

                    except blivet.errors.PartitioningError as e:
                        return ReturnList(success=False,
                                          actions=None,
                                          message=None,
                                          exception=e,
                                          traceback=sys.exc_info()[2])

                    btrfs_parents.append(dev)

            new_btrfs = BTRFSVolumeDevice(device_name,
                                          size=total_size,
                                          parents=btrfs_parents)
            new_btrfs.format = blivet.formats.getFormat(
                "btrfs", label=device_name, mountpoint=user_input.mountpoint)
            actions.append(blivet.deviceaction.ActionCreateDevice(new_btrfs))

        elif user_input.device_type == "btrfs subvolume":

            device_name = self._pick_device_name(user_input.name,
                                                 user_input.parents[0][0])

            new_btrfs = BTRFSSubVolumeDevice(
                device_name, parents=[i[0] for i in user_input.parents])
            new_btrfs.format = blivet.formats.getFormat(
                "btrfs", mountpoint=user_input.mountpoint)
            actions.append(blivet.deviceaction.ActionCreateDevice(new_btrfs))

        elif user_input.device_type == "mdraid":
            device_name = self._pick_device_name(user_input.name)

            parts = []

            # exact total size of newly created pvs (future parents)
            total_size = blivet.size.Size("0 MiB")

            for parent, size in user_input.parents:

                dev = PartitionDevice(name="req%d" % self.storage.nextID,
                                      size=size,
                                      parents=[parent])
                ac_part = blivet.deviceaction.ActionCreateDevice(dev)

                fmt = blivet.formats.getFormat(fmt_type="mdmember")
                ac_fmt = blivet.deviceaction.ActionCreateFormat(dev, fmt)

                actions.extend([ac_part, ac_fmt])

                total_size += dev.size

                # we need to try to create pvs immediately, if something
                # fails, fail now
                try:
                    for ac in (ac_part, ac_fmt):
                        self.storage.devicetree.registerAction(ac)

                except blivet.errors.PartitioningError as e:
                    return ReturnList(success=False,
                                      actions=None,
                                      message=None,
                                      exception=e,
                                      traceback=sys.exc_info()[2])

                parts.append(dev)

            new_md = MDRaidArrayDevice(size=total_size,
                                       parents=parts,
                                       name=device_name,
                                       level=user_input.raid_level,
                                       memberDevices=len(parts),
                                       totalDevices=len(parts))
            actions.append(blivet.deviceaction.ActionCreateDevice(new_md))

            fmt = blivet.formats.getFormat(fmt_type=user_input.filesystem)
            actions.append(blivet.deviceaction.ActionCreateFormat(new_md, fmt))

        try:
            for ac in actions:
                if not ac._applied:
                    self.storage.devicetree.registerAction(ac)

            blivet.partitioning.doPartitioning(self.storage)

        except Exception as e:  # pylint: disable=broad-except
            return ReturnList(success=False,
                              actions=None,
                              message=None,
                              exception=e,
                              traceback=sys.exc_info()[2])

        return ReturnList(success=True,
                          actions=actions,
                          message=None,
                          exception=None,
                          traceback=None)