예제 #1
0
def get_partition_information(devices):
    global disk_array;
    for device in devices:
        if device.type == 1:
            disk = parted.Disk(device)
            primary_partitions=disk.getPrimaryPartitions()
            logical_partitions=disk.getLogicalPartitions()
            extended_partition=disk.getExtendedPartition()

            for partition in primary_partitions:
                disk_array[device.path]['partitions'][partition.path] = {};
                disk_array[device.path]['partitions'][partition.path]['size'] = partition.getSize(unit="b");
                try:
                    fs=parted.probeFileSystem(partition.geometry)
                except:
                    fs='none'
                disk_array[device.path]['partitions'][partition.path]['fs'] = fs;
                disk_array[device.path]['partitions'][partition.path]['first_block'] = partition.geometry.start;
                disk_array[device.path]['partitions'][partition.path]['end_block'] = partition.geometry.end;
                disk_array[device.path]['partitions'][partition.path]['length'] = partition.geometry.end;
                disk_array[device.path]['partitions'][partition.path]['type'] = 'primary';

            if extended_partition:
                disk_array[device.path]['partitions'][extended_partition.path] = {};
                disk_array[device.path]['partitions'][extended_partition.path]['size'] = extended_partition.getSize(unit="b");
                try:
                    fs=parted.probeFileSystem(extended.partition.geometry)
                except:
                    fs='none'
                disk_array[device.path]['partitions'][extended_partition.path]['fs'] = fs;
                disk_array[device.path]['partitions'][extended_partition.path]['first_block'] = extended_partition.geometry.start;
                disk_array[device.path]['partitions'][extended_partition.path]['end_block'] = extended_partition.geometry.end;
                disk_array[device.path]['partitions'][extended_partition.path]['length'] = extended_partition.geometry.end;
                disk_array[device.path]['partitions'][extended_partition.path]['type'] = 'extended';


            for partition in logical_partitions:
                disk_array[device.path]['partitions'][partition.path] = {};
                disk_array[device.path]['partitions'][partition.path]['size'] = partition.getSize(unit="b");
                try:
                    fs=parted.probeFileSystem(partition.geometry)
                except:
                    fs='none'
                disk_array[device.path]['partitions'][partition.path]['fs'] = fs;
                disk_array[device.path]['partitions'][partition.path]['first_block'] = partition.geometry.start;
                disk_array[device.path]['partitions'][partition.path]['end_block'] = partition.geometry.end;
                disk_array[device.path]['partitions'][partition.path]['length'] = partition.geometry.end;
                disk_array[device.path]['partitions'][partition.path]['type'] = 'logical';
예제 #2
0
파일: filesystems.py 프로젝트: kidaa/arkos
def get(id=None):
    devs, mps = [], {}
    fstab = get_fstab()

    # Get mount data for all devices
    with open("/etc/mtab", "r") as f:
        for x in f.readlines():
            x = x.split()
            mps[x[0]] = x[1]

    # Get physical disks available
    for d in parted.getAllDevices():
        try:
            parts = parted.Disk(d).getPrimaryPartitions()
        except:
            continue
        for p in parts:
            if p.path.split("/")[-1].startswith("loop"):
                continue
            try:
                fstype = parted.probeFileSystem(p.geometry)
            except:
                fstype = "Unknown"
            try:
                dev = DiskPartition(id=p.path.split("/")[-1], path=p.path,
                    mountpoint=mps.get(p.path) or None, size=int(p.getSize("B")),
                    fstype=fstype, enabled=p.path in fstab, crypt=crypto.is_luks(p.path)==0)
                if id == dev.id:
                    return dev
                devs.append(dev)
            except:
                continue

    # Replace mount data for virtual disks with loopback id
    dd = losetup.get_loop_devices()
    for x in dd:
        try:
            s = dd[x].get_status()
        except:
            continue
        if "/dev/loop%s" % s.lo_number in mps:
            mps[s.lo_filename] = mps["/dev/loop%s" % s.lo_number]

    # Get virtual disks available
    for x in glob.glob(os.path.join(config.get("filesystems", "vdisk_dir"), "*")):
        if not x.endswith((".img", ".crypt")):
            continue
        dname = os.path.splitext(os.path.split(x)[1])[0]
        dev = VirtualDisk(id=dname, path=x, size=os.path.getsize(x),
            mountpoint=mps.get(x) or mps.get("/dev/mapper/%s" % dname) or None,
            enabled=x in fstab, crypt=x.endswith(".crypt"))
        if id == dev.id:
            return dev
        devs.append(dev)
    return devs if not id else None
예제 #3
0
 def __init__(self, device_path, path):
   self.dev = device_path
   self.path = path
   
   device = parted.getDevice(device_path)
   
   if path is 'swap':
     self.type = 'swap'
   else:
     geometry = parted.geometry.Geometry(device)
     self.type = parted.probeFileSystem(geometry)
     
   self.options = 'defaults'
   self.dump = 1 if path is '/' else 0
   self.pass = 1 if path is '/' else 0
def find_ext_part(blkdev_path):
    # if root use parted, if normal user use sudo blkid
    if os.geteuid() == 0:
        device = parted.getDevice(blkdev_path)
        disk = parted.Disk(device)
        primary_partitions = disk.getPrimaryPartitions()

        count = 1
        for partition in primary_partitions:
            # the boot part must be either part 1 or 2 of the device
            if count > 2:
                return []

            print "Partition: %s" % partition.path
            try:
                fs = parted.probeFileSystem(partition.geometry)
            except:
                fs = "unknown"
            print "Filesystem: %s" % fs
            if fs == 'ext2' or fs == 'ext3':
                return partition.path
            count += 1  # increment counter
    else:
        # the boot part must be either part 1 or 2 of the device
        for partition in [1, 2]:
            output = ''
            try:
                command = 'blkid ' + blkdev_path + str(partition)
                command = add_sudo(command)
                output = subprocess.check_output(command,
                                                 stderr=subprocess.STDOUT,
                                                 shell=True)
            except:
                pass
            result = re.search(r"TYPE=(.*)\s", output)
            if result:
                fs = result.group(1).strip()
                fs = fs.split()[0]
                fs = fs.strip('"')
                if fs == 'ext2' or fs == 'ext3':
                    path = blkdev_path + str(partition)
                    return path

    # search failed
    return []
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice(self.rootfs_disk)
        disk = parted.Disk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log(
            "Original root filesystem size (sectors): {0}".format(
                original_root_fs_size))

        desired_boot_partition_size = self._size_to_sectors(
            256, 'MiB', device.sectorSize)
        self.context.logger.log(
            "Desired boot partition size (sectors): {0}".format(
                desired_boot_partition_size))

        root_partition = disk.partitions[0]

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log(
            "Original root partition start (sectors): {0}".format(
                original_root_partition_start))
        self.context.logger.log(
            "Original root partition end (sectors): {0}".format(
                original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log(
            "Desired root partition start (sectors): {0}".format(
                desired_root_partition_start))
        self.context.logger.log(
            "Desired root partition end (sectors): {0}".format(
                desired_root_partition_end))
        self.context.logger.log(
            "Desired root partition size (sectors): {0}".format(
                desired_root_partition_size))

        self.context.logger.log("Resizing root filesystem")
        desired_root_fs_size = desired_root_partition_size
        self._resize_root_fs_to_sectors(desired_root_fs_size,
                                        device.sectorSize)

        desired_root_partition_geometry = parted.Geometry(
            device=device,
            start=desired_root_partition_start,
            length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(
            exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log(
            "Desired boot partition start (sectors): {0}".format(
                desired_boot_partition_start))
        self.context.logger.log(
            "Desired boot partition end (sectors): {0}".format(
                desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(
            device=device,
            start=desired_boot_partition_start,
            length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(
            exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(
            disk=disk,
            type=parted.PARTITION_NORMAL,
            geometry=desired_boot_partition_geometry)

        disk.addPartition(partition=desired_boot_partition,
                          constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(disk.partitions[0].geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        disk.partitions[1].setFlag(parted.PARTITION_BOOT)

        disk.commit()

        self.command_executor.Execute("partprobe", False)
        self.command_executor.Execute(
            "mkfs.ext2 {0}".format(self.bootfs_block_device), True)

        boot_partition_uuid = self._get_uuid(self.bootfs_block_device)

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute(
            "mount {0} /oldroot".format(self.rootfs_block_device), True)
        self.command_executor.Execute("mkdir /oldroot/memroot", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /oldroot /oldroot/memroot",
                                      True)
        self.command_executor.ExecuteInBash(
            "for i in dev proc sys; do mount --move /memroot/$i /$i; done",
            True)
        self.command_executor.Execute("mv /boot /boot.backup", True)
        self.command_executor.Execute("mkdir /boot", True)
        self.disk_util.remove_mount_info("/boot")
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /memroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.Execute("mkdir /boot/boot", True)
        self.command_executor.ExecuteInBash(
            "shopt -s dotglob && mv /boot.backup/* /boot/boot/", True)
        self.command_executor.Execute("rmdir /boot.backup", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /memroot /memroot/oldroot",
                                      True)
        self.command_executor.Execute("rmdir /oldroot/memroot", True)
        self.command_executor.ExecuteInBash(
            "for i in dev proc sys; do mount --move /oldroot/$i /$i; done",
            True)
        self.command_executor.Execute("umount /oldroot/boot", True)

        try:
            self.command_executor.Execute("umount /oldroot", True)
        except:
            self.context.logger.log(
                "Could not unmount /oldroot, attempting to restart WALA and unmount again"
            )

            self.command_executor.Execute(
                'at -f /restart-wala.sh now + 1 minutes', True)
            self.command_executor.Execute('service waagent stop', True)

            os.unlink(
                '/var/lib/azure_disk_encryption_config/os_encryption_markers/UnmountOldrootState'
            )
            self.should_exit()

            raise
예제 #6
0
    def partitionsFind(self,mounted=None,ttype=None,ssd=None,prefix="sd",minsize=5,maxsize=5000,devbusy=None,\
            initialize=False,forceinitialize=False):
        """
        looks for disks which are know to be data disks & are formatted ext4
        return [[$partpath,$size,$free,$ssd]]
        @param ssd if None then ssd and other
        """
        import parted
        import JumpScale.grid.osis
        import psutil
        result = []
        mounteddevices = psutil.disk_partitions()

        def getpsutilpart(partname):
            for part in mounteddevices:
                if part.device == partname:
                    return part
            return None

        for dev in parted.getAllDevices():
            path = dev.path
            #ssize = dev.sectorSize;
            # size = (geom[0] * geom[1] * geom[2] * ssize) / 1000 / 1000 / 1000;
            # size2=dev.getSize()

            if devbusy == None or dev.busy == devbusy:
                if path.startswith("/dev/%s" % prefix):
                    try:
                        disk = parted.Disk(dev)
                        partitions = disk.partitions
                    except parted.DiskLabelException:
                        partitions = list()
                    for partition in partitions:
                        disko = Disk()
                        disko.model = dev.model
                        disko.path = partition.path if disk.type != 'loop' else disk.device.path
                        disko.size = round(partition.getSize(unit="mb"), 2)
                        disko.free = 0
                        print "partition:%s %s" % (disko.path, disko.size)
                        try:
                            fs = parted.probeFileSystem(partition.geometry)
                        except:
                            fs = "unknown"

                        disko.fs = fs
                        partfound = getpsutilpart(disko.path)
                        mountpoint = None
                        if partfound == None and mounted <> True:
                            mountpoint = "/mnt/tmp"
                            cmd = "mount %s /mnt/tmp" % partition.path
                            rcode, output = j.system.process.execute(
                                cmd,
                                ignoreErrorOutput=False,
                                dieOnNonZeroExitCode=False,
                            )
                            if rcode <> 0:
                                #mount did not work
                                mountpoint == None

                            disko.mountpoint = None
                            disko.mounted = False
                        elif partfound:
                            mountpoint = partfound.mountpoint
                            disko.mountpoint = mountpoint
                            disko.mounted = True

                        pathssdcheck = "/sys/block/%s/queue/rotational" % dev.path.replace(
                            "/dev/", "").strip()
                        ssd0 = int(
                            j.system.fs.fileGetContents(pathssdcheck)) == 0
                        disko.ssd = ssd0
                        result.append(disko)

                        if mountpoint <> None:
                            print "mountpoint:%s" % mountpoint
                            size, used, free, percent = psutil.disk_usage(
                                mountpoint)
                            disko.free = disko.size * float(1 - percent / 100)

                            size = disko.size / 1024
                            disko.free = int(disko.free)

                            if (ttype == None
                                    or fs == ttype) and size > minsize and (
                                        maxsize is None or size < maxsize):
                                if ssd == None or disko.ssd == ssd:
                                    # print disko
                                    hrdpath = "%s/disk.hrd" % mountpoint

                                    if j.system.fs.exists(hrdpath):
                                        hrd = j.core.hrd.getHRD(hrdpath)
                                        partnr = hrd.getInt("diskinfo.partnr")
                                        if partnr == 0 or forceinitialize:
                                            j.system.fs.remove(hrdpath)

                                    if not j.system.fs.exists(
                                            hrdpath) and initialize:
                                        C = """
diskinfo.partnr=
diskinfo.gid=
diskinfo.nid=
diskinfo.type=
diskinfo.epoch=
diskinfo.description=
"""
                                        j.system.fs.writeFile(filename=hrdpath,
                                                              contents=C)
                                        hrd = j.core.hrd.getHRD(hrdpath)
                                        hrd.set(
                                            "diskinfo.description",
                                            j.console.askString(
                                                "please give description for disk"
                                            ))
                                        hrd.set(
                                            "diskinfo.type", ",".join(
                                                j.console.askChoiceMultiple([
                                                    "BOOT", "CACHE", "TMP",
                                                    "DATA", "OTHER"
                                                ])))
                                        hrd.set("diskinfo.gid",
                                                j.application.whoAmI.gid)
                                        hrd.set("diskinfo.nid",
                                                j.application.whoAmI.nid)
                                        hrd.set("diskinfo.epoch",
                                                j.base.time.getTimeEpoch())

                                        masterip = j.application.config.get(
                                            "grid.master.ip")
                                        client = j.core.osis.getClient(
                                            masterip, user="******")
                                        client_disk = j.core.osis.getClientForCategory(
                                            client, "system", "disk")

                                        disk = client_disk.new()
                                        for key, val in disko.__dict__.iteritems(
                                        ):
                                            disk.__dict__[key] = val

                                        disk.description = hrd.get(
                                            "diskinfo.description")
                                        disk.type = hrd.get(
                                            "diskinfo.type").split(",")
                                        disk.type.sort()
                                        disk.nid = j.application.whoAmI.nid
                                        disk.gid = j.application.whoAmI.gid

                                        guid, new, changed = client_disk.set(
                                            disk)
                                        disk = client_disk.get(guid)
                                        diskid = disk.id
                                        hrd.set("diskinfo.partnr", diskid)
                                    if j.system.fs.exists(hrdpath):
                                        # hrd=j.core.hrd.getHRD(hrdpath)
                                        disko.id = hrd.get("diskinfo.partnr")
                                        disko.type = hrd.get(
                                            "diskinfo.type").split(",")
                                        disko.type.sort()
                                        disko.description = hrd.get(
                                            "diskinfo.description")
                                        print "found disk:\n%s" % (disko)
                                    cmd = "umount /mnt/tmp"
                                    j.system.process.execute(
                                        cmd, dieOnNonZeroExitCode=False)
                                    if os.path.ismount("/mnt/tmp") == True:
                                        raise RuntimeError(
                                            "/mnt/tmp should not be mounted")

        return result
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice(self.rootfs_disk)
        disk = parted.newDisk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log("Original root filesystem size (sectors): {0}".format(original_root_fs_size))

        desired_boot_partition_size = parted.sizeToSectors(256, 'MiB', device.sectorSize)
        self.context.logger.log("Desired boot partition size (sectors): {0}".format(desired_boot_partition_size))

        desired_root_fs_size = int(original_root_fs_size - desired_boot_partition_size)
        self.context.logger.log("Desired root filesystem size (sectors): {0}".format(desired_root_fs_size))

        attempt = 1
        while attempt < 10:
            resize_result = self.command_executor.Execute("resize2fs {0} {1}s".format(self.rootfs_block_device, desired_root_fs_size))

            if resize_result == 0:
                break
            else:
                self.command_executor.Execute('systemctl restart systemd-udevd')
                self.command_executor.Execute('systemctl restart systemd-timesyncd')
                self.command_executor.Execute('udevadm trigger')

                sleep(10)

                attempt += 1

        resized_root_fs_size = self._get_root_fs_size_in(device.sectorSize)

        self.context.logger.log("Resized root filesystem size (sectors): {0}".format(resized_root_fs_size))

        if not desired_root_fs_size == resized_root_fs_size:
            raise Exception("resize2fs failed, desired: {0}, resized: {1}".format(desired_root_fs_size,
                                                                                  resized_root_fs_size))

        self.context.logger.log("Root filesystem resized successfully")

        root_partition = disk.getPartitionByPath(os.path.realpath(self.rootfs_block_device))

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log("Original root partition start (sectors): {0}".format(original_root_partition_start))
        self.context.logger.log("Original root partition end (sectors): {0}".format(original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log("Desired root partition start (sectors): {0}".format(desired_root_partition_start))
        self.context.logger.log("Desired root partition end (sectors): {0}".format(desired_root_partition_end))
        self.context.logger.log("Desired root partition size (sectors): {0}".format(desired_root_partition_size))

        desired_root_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_root_partition_start,
                                                          length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log("Desired boot partition start (sectors): {0}".format(desired_boot_partition_start))
        self.context.logger.log("Desired boot partition end (sectors): {0}".format(desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_boot_partition_start,
                                                          length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(disk=disk,
                                                  type=parted.PARTITION_NORMAL,
                                                  geometry=desired_boot_partition_geometry)

        if (root_partition.getFlag(parted.PARTITION_BOOT)):
            desired_boot_partition.setFlag(parted.PARTITION_BOOT)

        disk.addPartition(partition=desired_boot_partition, constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(desired_root_partition_geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        self.command_executor.Execute("partprobe", True)
        self.command_executor.Execute("mkfs.ext2 {0}".format(self.bootfs_block_device), True)

        boot_partition_uuid = self._get_uuid(self.bootfs_block_device)

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute("mount {0} /oldroot".format(self.rootfs_block_device), True)
        self.command_executor.Execute("mkdir -p /boot", True)
        self.command_executor.Execute("cp /oldroot/etc/fstab /etc/fstab", True)
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /oldroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.ExecuteInBash("mv /oldroot/boot/* /boot/", True)
        self.command_executor.Execute("umount /boot", True)
        self.command_executor.Execute("umount /oldroot", True)
예제 #8
0
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice(self.rootfs_disk)
        disk = parted.Disk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log(
            "Original root filesystem size (sectors): {0}".format(
                original_root_fs_size))

        desired_boot_partition_size = parted.sizeToSectors(
            256, 'MiB', device.sectorSize)
        self.context.logger.log(
            "Desired boot partition size (sectors): {0}".format(
                desired_boot_partition_size))

        root_partition = disk.partitions[0]

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log(
            "Original root partition start (sectors): {0}".format(
                original_root_partition_start))
        self.context.logger.log(
            "Original root partition end (sectors): {0}".format(
                original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log(
            "Desired root partition start (sectors): {0}".format(
                desired_root_partition_start))
        self.context.logger.log(
            "Desired root partition end (sectors): {0}".format(
                desired_root_partition_end))
        self.context.logger.log(
            "Desired root partition size (sectors): {0}".format(
                desired_root_partition_size))

        self.context.logger.log("Resizing root filesystem")
        desired_root_fs_size = desired_root_partition_size
        self._resize_root_fs_to_sectors(desired_root_fs_size,
                                        device.sectorSize)

        desired_root_partition_geometry = parted.Geometry(
            device=device,
            start=desired_root_partition_start,
            length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(
            exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log(
            "Desired boot partition start (sectors): {0}".format(
                desired_boot_partition_start))
        self.context.logger.log(
            "Desired boot partition end (sectors): {0}".format(
                desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(
            device=device,
            start=desired_boot_partition_start,
            length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(
            exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(
            disk=disk,
            type=parted.PARTITION_NORMAL,
            geometry=desired_boot_partition_geometry)

        disk.addPartition(partition=desired_boot_partition,
                          constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(disk.partitions[0].geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        disk.partitions[1].setFlag(parted.PARTITION_BOOT)

        disk.commit()

        self.command_executor.Execute("partprobe", False)
        self.command_executor.Execute(
            "mkfs.ext2 {0}".format(self.bootfs_block_device), True)

        boot_partition_uuid = self._get_uuid(self.bootfs_block_device)

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute(
            "mount {0} /oldroot".format(self.rootfs_block_device), True)
        self.command_executor.Execute("mkdir -p /boot", True)
        self.command_executor.Execute("cp /oldroot/etc/fstab /etc/fstab", True)
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /oldroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.ExecuteInBash("mv /oldroot/boot/* /boot/", True)
        self.command_executor.Execute("umount /boot", True)
        self.command_executor.Execute("umount /oldroot", True)
예제 #9
0
파일: filesystems.py 프로젝트: ns408/core
def get(id=None):
    """
    Get all physical disks and virtual disk images present on the system.

    :params str id: Return only the disk/image that matches this ID.
    :returns: DiskPartition(s) and/or VirtualDisk(s)
    :rtype: DiskPartition, VirtualDisk or a list thereof
    """
    devs, mps = [], {}
    fstab = get_fstab()
    vdisk_dir = config.get("filesystems", "vdisk_dir")

    # Get mount data for all devices
    with open("/etc/mtab", "r") as f:
        for x in f.readlines():
            x = x.split()
            mps[x[0]] = x[1]

    # Get physical disks available
    for d in parted.getAllDevices():
        try:
            parts = parted.Disk(d).getPrimaryPartitions()
        except:
            continue
        for p in parts:
            if p.path.split("/")[-1].startswith("loop"):
                continue
            try:
                fstype = parted.probeFileSystem(p.geometry)
            except:
                fstype = "Unknown"
            try:
                dev = DiskPartition(id=p.path.split("/")[-1],
                                    path=p.path,
                                    mountpoint=mps.get(p.path) or None,
                                    size=int(p.getSize("B")),
                                    fstype=fstype,
                                    enabled=p.path in fstab,
                                    crypt=crypto.is_luks(p.path) == 0)
                if id == dev.id:
                    return dev
                devs.append(dev)
            except:
                continue

    # Replace mount data for virtual disks with loopback id
    dd = losetup.get_loop_devices()
    for x in dd:
        try:
            s = dd[x].get_status()
        except:
            continue
        if "/dev/loop{0}".format(s.lo_number) in mps:
            mps[s.lo_filename] = mps["/dev/loop{0}".format(s.lo_number)]

    # Get virtual disks available
    for x in glob.glob(os.path.join(vdisk_dir, "*")):
        if not x.endswith((".img", ".crypt")):
            continue
        dname = os.path.splitext(os.path.split(x)[1])[0]
        luks_point = "/dev/mapper/{0}".format(dname)
        dev = VirtualDisk(id=dname,
                          path=x,
                          size=os.path.getsize(x),
                          mountpoint=mps.get(x) or mps.get(luks_point) or None,
                          enabled=x in fstab,
                          crypt=x.endswith(".crypt"))
        if id == dev.id:
            return dev
        devs.append(dev)
    return devs if not id else None
예제 #10
0
def get_partition_information(devices):
    global disk_array
    for device in devices:
        if device.type == 1:
            disk = parted.Disk(device)
            primary_partitions = disk.getPrimaryPartitions()
            logical_partitions = disk.getLogicalPartitions()
            extended_partition = disk.getExtendedPartition()

            for partition in primary_partitions:
                disk_array[device.path]['partitions'][partition.path] = {}
                disk_array[device.path]['partitions'][
                    partition.path]['size'] = partition.getSize(unit="b")
                try:
                    fs = parted.probeFileSystem(partition.geometry)
                except:
                    fs = 'none'
                disk_array[device.path]['partitions'][
                    partition.path]['fs'] = fs
                disk_array[device.path]['partitions'][
                    partition.path]['first_block'] = partition.geometry.start
                disk_array[device.path]['partitions'][
                    partition.path]['end_block'] = partition.geometry.end
                disk_array[device.path]['partitions'][partition.path][
                    'length'] = partition.geometry.end - partition.geometry.start
                disk_array[device.path]['partitions'][
                    partition.path]['type'] = 'primary'

            if extended_partition:
                disk_array[device.path]['partitions'][
                    extended_partition.path] = {}
                disk_array[device.path]['partitions'][extended_partition.path][
                    'size'] = extended_partition.getSize(unit="b")
                try:
                    fs = parted.probeFileSystem(extended.partition.geometry)
                except:
                    fs = 'none'
                disk_array[device.path]['partitions'][
                    extended_partition.path]['fs'] = fs
                disk_array[device.path]['partitions'][extended_partition.path][
                    'first_block'] = extended_partition.geometry.start
                disk_array[device.path]['partitions'][extended_partition.path][
                    'end_block'] = extended_partition.geometry.end
                disk_array[device.path]['partitions'][extended_partition.path][
                    'length'] = extended_partition.geometry.end - extended_partition.geometry.start
                disk_array[device.path]['partitions'][
                    extended_partition.path]['type'] = 'extended'

            for partition in logical_partitions:
                disk_array[device.path]['partitions'][partition.path] = {}
                disk_array[device.path]['partitions'][
                    partition.path]['size'] = partition.getSize(unit="b")
                try:
                    fs = parted.probeFileSystem(partition.geometry)
                except:
                    fs = 'none'
                disk_array[device.path]['partitions'][
                    partition.path]['fs'] = fs
                disk_array[device.path]['partitions'][
                    partition.path]['first_block'] = partition.geometry.start
                disk_array[device.path]['partitions'][
                    partition.path]['end_block'] = partition.geometry.end
                disk_array[device.path]['partitions'][partition.path][
                    'length'] = partition.geometry.end - partition.geometry.start
                disk_array[device.path]['partitions'][
                    partition.path]['type'] = 'logical'
예제 #11
0
    def partitionsFind(self,mounted=None,ttype=None,ssd=None,prefix="sd",minsize=5,maxsize=5000,devbusy=None,\
            initialize=False,forceinitialize=False):
        """
        looks for disks which are know to be data disks & are formatted ext4
        return [[$partpath,$size,$free,$ssd]]
        @param ssd if None then ssd and other
        """
        import parted
        import JumpScale.grid.osis
        import psutil
        result=[]
        mounteddevices = psutil.disk_partitions()

        def getpsutilpart(partname):
            for part in mounteddevices:
                if part.device==partname:
                    return part
            return None

        for dev in parted.getAllDevices():
            path=dev.path
            #ssize = dev.sectorSize;
            # size = (geom[0] * geom[1] * geom[2] * ssize) / 1000 / 1000 / 1000;
            # size2=dev.getSize()

            if devbusy==None or dev.busy==devbusy:
                if path.startswith("/dev/%s"%prefix):
                    try:
                        disk = parted.Disk(dev)
                        partitions = disk.partitions
                    except parted.DiskLabelException:
                        partitions = list()
                    for partition in partitions:
                        disko=Disk()
                        disko.model = dev.model
                        disko.path=partition.path if disk.type != 'loop' else disk.device.path
                        disko.size=round(partition.getSize(unit="mb"),2)
                        disko.free = 0
                        print "partition:%s %s"%(disko.path,disko.size)
                        try:
                            fs = parted.probeFileSystem(partition.geometry)
                        except:
                            fs = "unknown"

                        disko.fs=fs
                        partfound=getpsutilpart(disko.path)
                        mountpoint=None
                        if partfound==None and mounted<>True:
                            mountpoint="/mnt/tmp"
                            cmd="mount %s /mnt/tmp"%partition.path
                            rcode,output=j.system.process.execute(cmd,ignoreErrorOutput=False,dieOnNonZeroExitCode=False,)
                            if rcode<>0:
                                #mount did not work
                                mountpoint==None

                            disko.mountpoint=None
                            disko.mounted=False
                        elif partfound:
                            mountpoint=partfound.mountpoint
                            disko.mountpoint=mountpoint
                            disko.mounted=True

                        pathssdcheck="/sys/block/%s/queue/rotational"%dev.path.replace("/dev/","").strip()
                        ssd0=int(j.system.fs.fileGetContents(pathssdcheck))==0
                        disko.ssd=ssd0   
                        result.append(disko)

                        if mountpoint<>None:
                            print "mountpoint:%s"%mountpoint
                            size, used, free, percent=psutil.disk_usage(mountpoint)
                            disko.free=disko.size*float(1-percent/100)

                            size=disko.size / 1024
                            disko.free=int(disko.free)

                            if (ttype==None or fs==ttype) and size>minsize and (maxsize is None or size<maxsize):
                                if ssd==None or disko.ssd==ssd:
                                    # print disko
                                    hrdpath="%s/disk.hrd"%mountpoint

                                    if j.system.fs.exists(hrdpath):
                                        hrd=j.core.hrd.getHRD(hrdpath)
                                        partnr=hrd.getInt("diskinfo.partnr")
                                        if partnr==0 or forceinitialize:
                                            j.system.fs.remove(hrdpath)

                                    if not j.system.fs.exists(hrdpath) and initialize:
                                        C="""
diskinfo.partnr=
diskinfo.gid=
diskinfo.nid=
diskinfo.type=
diskinfo.epoch=
diskinfo.description=
"""
                                        j.system.fs.writeFile(filename=hrdpath,contents=C)
                                        hrd=j.core.hrd.getHRD(hrdpath)
                                        hrd.set("diskinfo.description",j.console.askString("please give description for disk"))
                                        hrd.set("diskinfo.type",",".join(j.console.askChoiceMultiple(["BOOT","CACHE","TMP","DATA","OTHER"])))
                                        hrd.set("diskinfo.gid",j.application.whoAmI.gid)
                                        hrd.set("diskinfo.nid",j.application.whoAmI.nid)
                                        hrd.set("diskinfo.epoch",j.base.time.getTimeEpoch())


                                        masterip=j.application.config.get("grid.master.ip")
                                        client = j.core.osis.getClient(masterip,user="******")
                                        client_disk=j.core.osis.getClientForCategory(client,"system","disk")

                                        disk=client_disk.new()
                                        for key,val in disko.__dict__.iteritems():
                                            disk.__dict__[key]=val

                                        disk.description=hrd.get("diskinfo.description")
                                        disk.type=hrd.get("diskinfo.type").split(",")
                                        disk.type.sort()
                                        disk.nid=j.application.whoAmI.nid
                                        disk.gid=j.application.whoAmI.gid


                                        guid,new,changed=client_disk.set(disk)
                                        disk=client_disk.get(guid)
                                        diskid=disk.id
                                        hrd.set("diskinfo.partnr",diskid)
                                    if j.system.fs.exists(hrdpath):
                                        # hrd=j.core.hrd.getHRD(hrdpath)
                                        disko.id=hrd.get("diskinfo.partnr")
                                        disko.type=hrd.get("diskinfo.type").split(",")
                                        disko.type.sort()
                                        disko.description=hrd.get("diskinfo.description")
                                        print "found disk:\n%s"%(disko)
                                    cmd="umount /mnt/tmp"
                                    j.system.process.execute(cmd,dieOnNonZeroExitCode=False)
                                    if os.path.ismount("/mnt/tmp")==True:
                                        raise RuntimeError("/mnt/tmp should not be mounted")

        return result
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice('/dev/sda')
        disk = parted.newDisk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log(
            "Original root filesystem size (sectors): {0}".format(
                original_root_fs_size))

        desired_boot_partition_size = parted.sizeToSectors(
            256, 'MiB', device.sectorSize)
        self.context.logger.log(
            "Desired boot partition size (sectors): {0}".format(
                desired_boot_partition_size))

        desired_root_fs_size = original_root_fs_size - desired_boot_partition_size
        self.context.logger.log(
            "Desired root filesystem size (sectors): {0}".format(
                desired_root_fs_size))

        self.command_executor.Execute(
            "resize2fs /dev/sda1 {0}s".format(desired_root_fs_size), True)

        resized_root_fs_size = self._get_root_fs_size_in(device.sectorSize)

        self.context.logger.log(
            "Resized root filesystem size (sectors): {0}".format(
                resized_root_fs_size))

        if not desired_root_fs_size == resized_root_fs_size:
            raise Exception(
                "resize2fs failed, desired: {0}, resized: {1}".format(
                    desired_root_fs_size, resized_root_fs_size))

        self.context.logger.log("Root filesystem resized successfully")

        root_partition = disk.partitions[0]

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log(
            "Original root partition start (sectors): {0}".format(
                original_root_partition_start))
        self.context.logger.log(
            "Original root partition end (sectors): {0}".format(
                original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log(
            "Desired root partition start (sectors): {0}".format(
                desired_root_partition_start))
        self.context.logger.log(
            "Desired root partition end (sectors): {0}".format(
                desired_root_partition_end))
        self.context.logger.log(
            "Desired root partition size (sectors): {0}".format(
                desired_root_partition_size))

        desired_root_partition_geometry = parted.Geometry(
            device=device,
            start=desired_root_partition_start,
            length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(
            exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log(
            "Desired boot partition start (sectors): {0}".format(
                desired_boot_partition_start))
        self.context.logger.log(
            "Desired boot partition end (sectors): {0}".format(
                desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(
            device=device,
            start=desired_boot_partition_start,
            length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(
            exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(
            disk=disk,
            type=parted.PARTITION_NORMAL,
            geometry=desired_boot_partition_geometry)

        disk.addPartition(partition=desired_boot_partition,
                          constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(disk.partitions[0].geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        disk.partitions[1].setFlag(parted.PARTITION_BOOT)

        disk.commit()

        self.command_executor.Execute("partprobe", True)
        self.command_executor.Execute("mkfs.ext2 /dev/sda2", True)

        boot_partition_uuid = self._get_uuid("/dev/sda2")

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute("mount /dev/sda1 /oldroot", True)
        self.command_executor.Execute("mkdir /oldroot/memroot", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /oldroot /oldroot/memroot",
                                      True)
        self.command_executor.ExecuteInBash(
            "for i in dev proc sys; do mount --move /memroot/$i /$i; done",
            True)
        self.command_executor.Execute("mv /boot /boot.backup", True)
        self.command_executor.Execute("mkdir /boot", True)
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /memroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.ExecuteInBash("mv /boot.backup/* /boot/", True)
        self.command_executor.Execute("rmdir /boot.backup", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /memroot /memroot/oldroot",
                                      True)
        self.command_executor.Execute("rmdir /oldroot/memroot", True)
        self.command_executor.ExecuteInBash(
            "for i in dev proc sys; do mount --move /oldroot/$i /$i; done",
            True)
        self.command_executor.Execute("systemctl restart rsyslog", True)
        self.command_executor.Execute("systemctl restart systemd-udevd", True)
        self.command_executor.Execute("systemctl restart walinuxagent", True)
        self.command_executor.Execute("umount /oldroot/boot", True)
        self.command_executor.Execute("umount /oldroot", True)
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice(self.rootfs_disk)
        disk = parted.Disk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log("Original root filesystem size (sectors): {0}".format(original_root_fs_size))

        desired_boot_partition_size = self._size_to_sectors(256, 'MiB', device.sectorSize)
        self.context.logger.log("Desired boot partition size (sectors): {0}".format(desired_boot_partition_size))
        
        root_partition = disk.partitions[0]

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log("Original root partition start (sectors): {0}".format(original_root_partition_start))
        self.context.logger.log("Original root partition end (sectors): {0}".format(original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log("Desired root partition start (sectors): {0}".format(desired_root_partition_start))
        self.context.logger.log("Desired root partition end (sectors): {0}".format(desired_root_partition_end))
        self.context.logger.log("Desired root partition size (sectors): {0}".format(desired_root_partition_size))
        
        self.context.logger.log("Resizing root filesystem")
        desired_root_fs_size = desired_root_partition_size
        self._resize_root_fs_to_sectors(desired_root_fs_size, device.sectorSize)

        desired_root_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_root_partition_start,
                                                          length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log("Desired boot partition start (sectors): {0}".format(desired_boot_partition_start))
        self.context.logger.log("Desired boot partition end (sectors): {0}".format(desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_boot_partition_start,
                                                          length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(disk=disk,
                                                  type=parted.PARTITION_NORMAL,
                                                  geometry=desired_boot_partition_geometry)

        disk.addPartition(partition=desired_boot_partition, constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(disk.partitions[0].geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        disk.partitions[1].setFlag(parted.PARTITION_BOOT)

        disk.commit()
        
        self.command_executor.Execute("partprobe", False)

        retry_counter = 0
        while not os.path.exists(self.bootfs_block_device) and retry_counter < 10:
            sleep(5)
            self.command_executor.Execute("partprobe", False)
            retry_counter += 1

        self.command_executor.Execute("mkfs.ext2 {0}".format(self.bootfs_block_device), True)
        
        boot_partition_uuid = self._get_uuid(self.bootfs_block_device)

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute("mount {0} /oldroot".format(self.rootfs_block_device), True)
        self.command_executor.Execute("mkdir /oldroot/memroot", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /oldroot /oldroot/memroot", True)
        self.command_executor.ExecuteInBash("for i in dev proc sys; do mount --move /memroot/$i /$i; done", True)
        self.command_executor.Execute("mv /boot /boot.backup", True)
        self.command_executor.Execute("mkdir /boot", True)
        self.disk_util.remove_mount_info("/boot")
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /memroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.Execute("mkdir /boot/boot", True)
        self.command_executor.ExecuteInBash("shopt -s dotglob && mv /boot.backup/* /boot/boot/", True)
        self.command_executor.Execute("rmdir /boot.backup", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /memroot /memroot/oldroot", True)
        self.command_executor.Execute("rmdir /oldroot/memroot", True)
        self.command_executor.ExecuteInBash("for i in dev proc sys; do mount --move /oldroot/$i /$i; done", True)
        self.command_executor.Execute("umount /oldroot/boot", True)

        try:
            self.command_executor.Execute("umount /oldroot", True)
        except:
            self.context.logger.log("Could not unmount /oldroot, attempting to restart WALA and unmount again")

            self.command_executor.Execute('at -f /restart-wala.sh now + 1 minutes', True)
            self.command_executor.Execute('service waagent stop', True)

            os.unlink('/var/lib/azure_disk_encryption_config/os_encryption_markers/UnmountOldrootState')
            self.should_exit()

            raise
    def enter(self):
        if not self.should_enter():
            return

        self.context.logger.log("Entering split_root_partition state")

        device = parted.getDevice('/dev/sda')
        disk = parted.newDisk(device)

        original_root_fs_size = self._get_root_fs_size_in(device.sectorSize)
        self.context.logger.log("Original root filesystem size (sectors): {0}".format(original_root_fs_size))

        desired_boot_partition_size = parted.sizeToSectors(256, 'MiB', device.sectorSize)
        self.context.logger.log("Desired boot partition size (sectors): {0}".format(desired_boot_partition_size))
      
        desired_root_fs_size = original_root_fs_size - desired_boot_partition_size
        self.context.logger.log("Desired root filesystem size (sectors): {0}".format(desired_root_fs_size))

        self.command_executor.Execute("resize2fs /dev/sda1 {0}s".format(desired_root_fs_size), True)

        resized_root_fs_size = self._get_root_fs_size_in(device.sectorSize)

        self.context.logger.log("Resized root filesystem size (sectors): {0}".format(resized_root_fs_size))

        if not desired_root_fs_size == resized_root_fs_size:
            raise Exception("resize2fs failed, desired: {0}, resized: {1}".format(desired_root_fs_size,
                                                                                  resized_root_fs_size))

        self.context.logger.log("Root filesystem resized successfully")

        root_partition = disk.partitions[0]

        original_root_partition_start = root_partition.geometry.start
        original_root_partition_end = root_partition.geometry.end

        self.context.logger.log("Original root partition start (sectors): {0}".format(original_root_partition_start))
        self.context.logger.log("Original root partition end (sectors): {0}".format(original_root_partition_end))

        desired_root_partition_start = original_root_partition_start
        desired_root_partition_end = original_root_partition_end - desired_boot_partition_size
        desired_root_partition_size = desired_root_partition_end - desired_root_partition_start

        self.context.logger.log("Desired root partition start (sectors): {0}".format(desired_root_partition_start))
        self.context.logger.log("Desired root partition end (sectors): {0}".format(desired_root_partition_end))
        self.context.logger.log("Desired root partition size (sectors): {0}".format(desired_root_partition_size))

        desired_root_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_root_partition_start,
                                                          length=desired_root_partition_size)
        root_partition_constraint = parted.Constraint(exactGeom=desired_root_partition_geometry)
        disk.setPartitionGeometry(partition=root_partition,
                                  constraint=root_partition_constraint,
                                  start=desired_root_partition_start,
                                  end=desired_root_partition_end)

        desired_boot_partition_start = disk.getFreeSpaceRegions()[1].start
        desired_boot_partition_end = disk.getFreeSpaceRegions()[1].end
        desired_boot_partition_size = disk.getFreeSpaceRegions()[1].length

        self.context.logger.log("Desired boot partition start (sectors): {0}".format(desired_boot_partition_start))
        self.context.logger.log("Desired boot partition end (sectors): {0}".format(desired_boot_partition_end))

        desired_boot_partition_geometry = parted.Geometry(device=device,
                                                          start=desired_boot_partition_start,
                                                          length=desired_boot_partition_size)
        boot_partition_constraint = parted.Constraint(exactGeom=desired_boot_partition_geometry)
        desired_boot_partition = parted.Partition(disk=disk,
                                                  type=parted.PARTITION_NORMAL,
                                                  geometry=desired_boot_partition_geometry)

        disk.addPartition(partition=desired_boot_partition, constraint=boot_partition_constraint)

        disk.commit()

        probed_root_fs = parted.probeFileSystem(disk.partitions[0].geometry)
        if not probed_root_fs == 'ext4':
            raise Exception("Probed root fs is not ext4")

        disk.partitions[1].setFlag(parted.PARTITION_BOOT)

        disk.commit()
        
        self.command_executor.Execute("partprobe", True)
        self.command_executor.Execute("mkfs.ext2 /dev/sda2", True)
        
        boot_partition_uuid = self._get_uuid("/dev/sda2")

        # Move stuff from /oldroot/boot to new partition, make new partition mountable at the same spot
        self.command_executor.Execute("mount /dev/sda1 /oldroot", True)
        self.command_executor.Execute("mkdir /oldroot/memroot", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /oldroot /oldroot/memroot", True)
        self.command_executor.ExecuteInBash("for i in dev proc sys; do mount --move /memroot/$i /$i; done", True)
        self.command_executor.Execute("mv /boot /boot.backup", True)
        self.command_executor.Execute("mkdir /boot", True)
        self._append_boot_partition_uuid_to_fstab(boot_partition_uuid)
        self.command_executor.Execute("cp /etc/fstab /memroot/etc/fstab", True)
        self.command_executor.Execute("mount /boot", True)
        self.command_executor.ExecuteInBash("mv /boot.backup/* /boot/", True)
        self.command_executor.Execute("rmdir /boot.backup", True)
        self.command_executor.Execute("mount --make-rprivate /", True)
        self.command_executor.Execute("pivot_root /memroot /memroot/oldroot", True)
        self.command_executor.Execute("rmdir /oldroot/memroot", True)
        self.command_executor.ExecuteInBash("for i in dev proc sys; do mount --move /oldroot/$i /$i; done", True)
        self.command_executor.Execute("systemctl restart rsyslog", True)
        self.command_executor.Execute("systemctl restart systemd-udevd", True)
        self.command_executor.Execute("systemctl restart walinuxagent", True)
        self.command_executor.Execute("umount /oldroot/boot", True)
        self.command_executor.Execute("umount /oldroot", True)