Exemplo n.º 1
0
    def resetRootDevice(self):
        """
        Formats root device (according LVM default scheme) and adjusts all
        pointers to allow reinstall the system correctly.

        Important: this method is directly related to LVM default partitioning
        scheme. It assumes that root device is under /dev/mapper/vg_root-lv_root
        path. If this default scheme changes on future, this method must be
        revisited to assure its functionality.

        @rtype: bool
        @return: True if everything is ok, False otherwise
        """
        # restart LVM volume groups to assure its functionality
        self.__logger.info('Restarting LVM...')
        lvm.stop()
        lvm.start(self.__tolerantMode)
        
        # wait for udev to handle all events
        if run(CMD_UDEV_SETTLE) != 0:
            raise RuntimeError('Error: udevadm settle failure')

        # Do not trust content from / partition.  User can screw up
        # with its / partition and try a reinstall to fix it.  Thus
        # our code cannot trust on reading content from old and dirty
        # / partition.  We can just infere /boot partition by
        # appending 2 to the detected disk.
        installed_disk = self.getPreviousInstalledDisk()
        boot_partition = self.genBootPartitionName(installed_disk)
        self.__bootDevice = boot_partition

        # check multipath
        self.__hasMultipath = self.__diskParameters[installed_disk.split('/')[-1]]['mpath']

        # as consequence, configure prep device path
        self.__prepDevice = self.__bootDevice[:-1] + "1"

        # update root, log, data and swap paths
        self.__rootDevice = '/dev/mapper/%s-%s' % (VGROOT, LVROOT)
        self.__logDevice  = '/dev/mapper/%s-%s' % (VGLOG, LVLOG)
        self.__dataDevice = '/dev/mapper/%s-%s' % (VGDATA, LVDATA)
        self.__swapDevice = '/dev/mapper/%s-%s' % (VGSWAP, LVSWAP)

        self.__logger.debug("resetRootDevice(): __prepDevice = %s" % self.__prepDevice)
        self.__logger.debug("resetRootDevice(): __bootDevice = %s" % self.__bootDevice)
        self.__logger.debug("resetRootDevice(): __rootDevice = %s" % self.__rootDevice)
        self.__logger.debug("resetRootDevice(): __logDevice = %s" % self.__logDevice)
        self.__logger.debug("resetRootDevice(): __dataDevice = %s" % self.__dataDevice)
        self.__logger.debug("resetRootDevice(): __swapDevice = %s" % self.__swapDevice)

        # format boot, root and swap devices
        formatPartition(self.__bootDevice)
        formatPartition(self.__rootDevice)
        formatSwapPartition(self.__swapDevice)

        return True
Exemplo n.º 2
0
    def createDefaultLVMLayout(self, diskName, mpath=False):
        """
        Receives all required parameters to create a default LVM layout on
        selected disk.

        @type  diskName: str
        @param diskName: disk to be formatted

        @type  mpath: bool
        @param mpath: multipath flag

        @rtype: bool
        @return: True if everything is ok, False otherwise
        """
        self.__disk = diskName

        # given disk is not valid: abort
        if diskName not in self.__diskParameters.keys():
            raise PKVMError("PARTITIONER", "DISK", "NO_DISK")

        try:
            # partitioning dictionary creator
            sectors = 512
            if self.__diskParameters[diskName]['mpath']:
                slave = self.__diskParameters[diskName]['slaves'][0]
                sectors = get_sector_size(slave)

            else:
                sectors = get_sector_size(diskName)

            scheme = PartitionCommand(sectors)

            # configure parameters to create the commands
            scheme.setDisk(diskName)
            scheme.setDiskId(self.__diskParameters[diskName]['id'])
            scheme.setDiskSize(self.__diskParameters[diskName]['size'])
            scheme.setMultipathMode(self.__diskParameters[diskName]['mpath'])
            self.__hasMultipath = self.__diskParameters[diskName]['mpath']

            # partition sizes
            # 'prep': 8Mb
            # 'boot': 512Mb
            #    'swap': 8Gb
            #    'root': 20Gb
            #    'log' : 10Gb
            partitionSizes = {
                'prep': 8 * 1024,
                'boot': 512 * 1024,
                'swap': (8 * 1024 * 1024),
                'root': (20 * 1024 * 1024),
                'log' : (10 * 1024 * 1024)
            }

            # conventional commands
            prep = scheme.createPartition(None, 'Pri', 'prep', partitionSizes['prep'], None, 1)
            boot = scheme.createPartition('/boot', 'Pri', 'ext3', partitionSizes['boot'] , None, 2)
            ext  = scheme.createPartition(None, 'Ext', 'extended', 0, None, 3)
            root = scheme.createPartition(None, 'Log', 'lvm', partitionSizes['root'], VGROOT, 5)
            log  = scheme.createPartition(None, 'Log', 'lvm', partitionSizes['log'], VGLOG, 6)
            swap = scheme.createPartition(None, 'Log', 'lvm', partitionSizes['swap'], VGSWAP, 7)
            data = scheme.createPartition(None, 'Log', 'lvm', ALL_AVAILABLE, VGDATA, 8)
            self.__diskCommands = [prep, boot, ext, root, log, swap, data]

            # lvm commands
            pv_root = scheme.createPhysicalVolume(root['disk_name'], root['size'], root['name'])
            pv_log  = scheme.createPhysicalVolume(log['disk_name'],  log['size'],  log['name'])
            pv_swap = scheme.createPhysicalVolume(swap['disk_name'], swap['size'], swap['name'])
            pv_data = scheme.createPhysicalVolume(data['disk_name'], data['size'], data['name'])
            self.__lvmCommands = [pv_root, pv_log, pv_swap, pv_data]

            vg_root = scheme.createVolumeGroup([root['name']], VGROOT)
            vg_log  = scheme.createVolumeGroup([log['name']],  VGLOG)
            vg_swap = scheme.createVolumeGroup([swap['name']], VGSWAP)
            vg_data = scheme.createVolumeGroup([data['name']], VGDATA)
            self.__lvmCommands += [vg_root, vg_log, vg_swap, vg_data]

            lv_root = scheme.createLogicalVolume(root['vg'], 'ext3', LVROOT, '/', root['size'])
            lv_log  = scheme.createLogicalVolume(log['vg'], 'ext3',  LVLOG, '/var/log', log['size'])
            lv_swap = scheme.createLogicalVolume(swap['vg'], 'swap', LVSWAP, '', swap['size'])
            lv_data = scheme.createLogicalVolume(data['vg'], 'ext3', LVDATA, '/var/lib/libvirt/images', data['size'])
            self.__lvmCommands += [lv_root, lv_log, lv_swap, lv_data]

            # run partitioner instructions to clean and partition the disk
            self.__run(self.__diskParameters[diskName]['mpath'], sectors)

            # update prep, boot, root, log, data and swap paths
            path = '/dev/%s'
            if self.__diskParameters[diskName]['mpath']:
                path = '/dev/mapper/%s'
            self.__prepDevice = path % prep['name']
            self.__bootDevice = path % boot['name']
            self.__rootDevice = '/dev/mapper/%s-%s' % (vg_root['name'], lv_root['name'])
            self.__logDevice  = '/dev/mapper/%s-%s' % (vg_log['name'], lv_log['name'])
            self.__dataDevice = '/dev/mapper/%s-%s' % (vg_data['name'], lv_data['name'])
            self.__swapDevice = '/dev/mapper/%s-%s' % (vg_swap['name'], lv_swap['name'])

            # devices to be formatted as ext4
            devices = [
                self.__bootDevice,
                self.__rootDevice,
                self.__logDevice,
                self.__dataDevice
            ]

            # format devices
            for dev in devices:
                if formatPartition(dev) == False:
                    raise PKVMError("PARTITIONER", "FORMAT", "ERROR")

            # devices to be formatted as swap
            swapDevices = [self.__swapDevice]

            # format swap devices
            for dev in swapDevices:
                if formatSwapPartition(dev) == False:
                    raise PKVMError("PARTITIONER", "FORMAT", "ERROR")

        except PKVMError as e:
            raise

        except Exception as e:
            self.__logger.critical("Unexpected error")
            self.__logger.critical("EXCEPTION:" + str(type(e)))
            self.__logger.critical(str(e))
            self.__logger.critical("Stacktrace:" + str(traceback.format_exc()))
            raise PKVMError("PARTITIONER", "ERROR", "ERROR")

        return True