def mount_delete(ns, target): """ Unmount filesystem. :type target: string :param target: device path or mountpoint """ # Try to convert device name to full device path try: device_inst = common.str2device(ns, target) if device_inst: target = device_inst.Name except LmiFailed: # we did not find CIM_StorageExtent for the device, it must be non # device filesystem specification pass mnt = ns.LMI_MountedFileSystem.first_instance({'FileSystemSpec':target}) or \ ns.LMI_MountedFileSystem.first_instance({'MountPointPath':target}) if mnt is None: raise LmiFailed('Target is not mounted: %s.' % target) service = ns.LMI_MountConfigurationService.first_instance() # TODO for now # Mode 32769 == only unmount (don't remove any persistent info) (ret, _outparams, _err) = service.SyncDeleteMount(Mount=mnt, Mode=32769) if ret != 0: raise LmiFailed('Cannot delete mount: %s.' % target) LOG().info('Successfully deleted mount: %s', target)
def partition_table_show(ns, disk, human_friendly): """ Print extended information about the partition table on given disk. :type disk: LMIInstance/CIM_StorageExtent or string :param disk: Device with partition table to show. """ disk = common.str2device(ns, disk) yield ("Data Type", "Partition Table") table = disk.first_associator(AssocClass="CIM_InstalledPartitionTable") cls = ns.LMI_DiskPartitionConfigurationCapabilities if table.PartitionStyle == cls.PartitionStyleValues.MBR: yield ("Partition Table Type", "MS-DOS") else: yield ("Partition Table Type", cls.PartitionStyleValues.value_name(table.PartitionStyle)) yield ("Partition Table Size (in blocks)", table.PartitionTableSize) yield ("Largest Free Space", common.size2str(partition.get_largest_partition_size(ns, disk), human_friendly)) parts = partition.get_disk_partitions(ns, disk) partnames = [part.Name for part in parts] yield ("Partitions", " ".join(partnames))
def get_largest_partition_size(ns, device): """ Returns size of the largest free region (in blocks), which can accommodate a partition on given device. There must be partition table present on this device. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device which should be examined. :rtype: int """ device = common.str2device(ns, device) # find the partition table (=LMI_DiskPartitionConfigurationCapabilities) cap = device.first_associator( ResultClass="LMI_DiskPartitionConfigurationCapabilities") if not cap: raise LmiFailed("Cannot find partition table on %s" % device.name) (ret, outparams, err) = cap.FindPartitionLocation(Extent=device) if ret == cap.FindPartitionLocation.FindPartitionLocationValues\ .NotEnoughFreeSpace: return 0 if ret != 0: if err: LOG().warning("Cannot find largest partition size: %s." % err) else: LOG().warning("Cannot find largest partition size: %d." % ret) return 0 blocks = outparams['EndingAddress'] - outparams['StartingAddress'] size = blocks * device.BlockSize return size
def create_raid(ns, devices, level, name=None): """ Create new MD RAID device. :type devices: list of LMIInstance/CIM_StorageExtent or list of strings :param device: Devices to add to the RAID. :type level: int :param level: RAID level. :type name: string :param name: RAID name. :rtype: LMIInstance/LMI_MDRAIDStorageExtent """ devs = [common.str2device(ns, device) for device in devices] args = {'InExtents': devs, 'Level': level} if name: args['ElementName'] = name service = ns.LMI_StorageConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateOrModifyMDRAID(**args) if ret != 0: if err: raise LmiFailed("Cannot create the partition: %s." % err) values = service.CreateOrModifyMDRAID.CreateOrModifyMDRAIDValues raise LmiFailed("Cannot create the partition: %s." % (values.value_name(ret), )) return outparams['TheElement']
def partition_show(ns, part, human_friendly): """ Print extended information about the partition. :type part: LMIInstance/CIM_GenericDiskPartition or string :param part: Partition to show. """ part = common.str2device(ns, part) yield ("Type", "Partition") for line in device_show_device(ns, part, human_friendly): yield line if "PartitionType" in part.properties(): cls = ns.LMI_DiskPartition if part.PartitionType == cls.PartitionTypeValues.Primary: ptype = "primary" elif part.PartitionType == cls.PartitionTypeValues.Extended: ptype = "extended" elif part.PartitionType == cls.PartitionTypeValues.Logical: ptype = "logical" else: ptype = "unknown" else: ptype = "N/A" yield ("Partition Type", ptype) basedon = part.first_reference(ResultClass="CIM_BasedOn", Role="Dependent") yield ("Starting sector", basedon.StartingAddress) yield ("Ending sector", basedon.EndingAddress) disk = partition.get_partition_disk(ns, part) yield ("Sector Size", common.size2str(disk.BlockSize, human_friendly)) yield ("Disk", disk.Name) for line in device_show_data(ns, part, human_friendly): yield line
def device_show(ns, device, human_friendly): """ Print extended information about the device. :type device: LMIInstance/CIM_StorageExtent or string :param part: Device to show. :type human_friendly: bool :param human_friendly: If True, the device sizes are shown in human-friendly units (KB, MB, ...). """ device = common.str2device(ns, device) if device.classname == "LMI_MDRAIDStorageExtent": for line in raid_show(ns, device, human_friendly): yield line elif device.classname == "LMI_LVStorageExtent": for line in lv_show(ns, device, human_friendly): yield line elif device.classname == "LMI_GenericDiskPartition": for line in partition_show(ns, device, human_friendly): yield line elif device.classname == "LMI_DiskPartition": for line in partition_show(ns, device, human_friendly): yield line else: yield ("Type", "Generic block device") for line in device_show_device(ns, device, human_friendly): yield line for line in device_show_data(ns, device, human_friendly): yield line
def create_luks(ns, device, passphrase): """ Format given device with LUKS encryption format. All data on the device will be deleted! Encryption key and algorithm will be chosen automatically. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to format with LUKS data :type passphrase: string :param passphrase: Password to open the encrypted data. This is not the encryption key. :rtype: LMIInstance/LMI_EncryptionFormat """ device = common.str2device(ns, device) service = ns.LMI_ExtentEncryptionConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateEncryptionFormat( InExtent=device, Passphrase=passphrase) if ret != 0: if err: raise LmiFailed("Cannot create LUKS format: %s." % err) values = service.CreateEncryptionFormat.CreateEncryptionFormatValues raise LmiFailed("Cannot create LUKS format: %s." % (values.value_name(ret),)) LOG().info("Created LUKS on %s", device.Name) return outparams['Format']
def create_raid(ns, devices, level, name=None): """ Create new MD RAID device. :type devices: list of LMIInstance/CIM_StorageExtent or list of strings :param device: Devices to add to the RAID. :type level: int :param level: RAID level. :type name: string :param name: RAID name. :rtype: LMIInstance/LMI_MDRAIDStorageExtent """ devs = [common.str2device(ns, device) for device in devices] args = { 'InExtents': devs, 'Level': level} if name: args['ElementName'] = name service = ns.LMI_StorageConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateOrModifyMDRAID(**args) if ret != 0: if err: raise LmiFailed("Cannot create the partition: %s." % err) values = service.CreateOrModifyMDRAID.CreateOrModifyMDRAIDValues raise LmiFailed("Cannot create the partition: %s." % (values.value_name(ret),)) return outparams['TheElement']
def device_show_data(ns, device, human_friendly): """ Display description of data on the device. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to show. """ device = common.str2device(ns, device) fmt = fs.get_format_on_device(ns, device) if fmt: if "FormatType" in fmt.properties(): for line in format_show(ns, fmt, human_friendly): yield line elif "FileSystemType" in fmt.properties(): for line in fs_show(ns, fmt, human_friendly): yield line else: yield ("Data Format", "Unknown") else: part_table = partition.get_disk_partition_table(ns, device) if part_table: for line in partition_table_show(ns, device, human_friendly): yield line else: yield ("Data Format", "Unknown")
def create_luks(ns, device, passphrase): """ Format given device with LUKS encryption format. All data on the device will be deleted! Encryption key and algorithm will be chosen automatically. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to format with LUKS data :type passphrase: string :param passphrase: Password to open the encrypted data. This is not the encryption key. :rtype: LMIInstance/LMI_EncryptionFormat """ device = common.str2device(ns, device) service = ns.LMI_ExtentEncryptionConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateEncryptionFormat(InExtent=device, Passphrase=passphrase) if ret != 0: if err: raise LmiFailed("Cannot create LUKS format: %s." % err) values = service.CreateEncryptionFormat.CreateEncryptionFormatValues raise LmiFailed("Cannot create LUKS format: %s." % (values.value_name(ret), )) LOG().info("Created LUKS on %s", device.Name) return outparams['Format']
def create_partition_table(ns, device, table_type): """ Create new partition table on a device. The device must be empty, i.e. must not have any partitions on it. :type device: LMIInstance/CIM_StorageExtent :param device: Device where the partition table should be created. :type table_type: int :param table_type: Requested partition table type. See PARTITION_TABLE_TYPE_xxx variables. """ device = common.str2device(ns, device) query = "SELECT * FROM LMI_DiskPartitionConfigurationCapabilities WHERE " \ "PartitionStyle=%d" % table_type caps = ns.wql(query) if not caps: raise LmiFailed("Unsupported partition table type: %d" % table_type) cap = caps[0] service = ns.LMI_DiskPartitionConfigurationService.first_instance() (ret, _outparams, err) = service.SetPartitionStyle( Extent=device, PartitionStyle=cap) if ret != 0: if err: raise LmiFailed("Cannot create partition table: %s." % err) values = service.SetPartitionStyle.SetPartitionStyleValues raise LmiFailed("Cannot create partition table: %s." % (values.value_name(ret))) LOG().info("Created partition table on %s", device.Name)
def create_partition_table(ns, device, table_type): """ Create new partition table on a device. The device must be empty, i.e. must not have any partitions on it. :type device: LMIInstance/CIM_StorageExtent :param device: Device where the partition table should be created. :type table_type: int :param table_type: Requested partition table type. See PARTITION_TABLE_TYPE_xxx variables. """ device = common.str2device(ns, device) query = "SELECT * FROM LMI_DiskPartitionConfigurationCapabilities WHERE " \ "PartitionStyle=%d" % table_type caps = ns.wql(query) if not caps: raise LmiFailed("Unsupported partition table type: %d" % table_type) cap = caps[0] service = ns.LMI_DiskPartitionConfigurationService.first_instance() (ret, _outparams, err) = service.SetPartitionStyle(Extent=device, PartitionStyle=cap) if ret != 0: if err: raise LmiFailed("Cannot create partition table: %s." % err) values = service.SetPartitionStyle.SetPartitionStyleValues raise LmiFailed("Cannot create partition table: %s." % (values.value_name(ret))) LOG().info("Created partition table on %s", device.Name)
def get_format_on_device(ns, device, format_type=FORMAT_ALL): """ Return filesystem or data format, which is on given device. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to to examine. :type format_type: int :param format_type: Type of format to find. * FORMAT_ALL - return either CIM_LocalFileSystem or LMI_DataFormat. * FORMAT_FS - return only CIM_LocalFileSystem or None, if there is no filesystem on the device. * FORMAT_DATA - return only LMI_DataFormat or None, if there is no data format on the device. :rtype: LMIInstance/CIM_LocalFileSystem or LMIInstance/LMI_DataFormat """ device = common.str2device(ns, device) if format_type == FORMAT_ALL: fmt = device.first_associator(AssocClass="CIM_ResidesOnExtent", Role="Antecedent") elif format_type == FORMAT_FS: fmt = device.first_associator(AssocClass="CIM_ResidesOnExtent", Role="Antecedent", ResultClass="CIM_LocalFileSystem") elif format_type == FORMAT_DATA: fmt = device.first_associator(AssocClass="CIM_ResidesOnExtent", Role="Antecedent", ResultClass="LMI_DataFormat") if fmt: return fmt return None
def create_vg(ns, devices, name, extent_size=None): """ Create new Volume Group from given devices. :type devices: list of LMIInstance/CIM_StorageExtent or list of strings :param device: Devices to add to the Volume Group. :type name: string :param name: Name of the Volume gGoup. :type extent_size: int :param extent_size: Extent size in bytes. :rtype: LMIInstance/LMI_VGStoragePool """ devs = [common.str2device(ns, device) for device in devices] args = { 'InExtents': devs, 'ElementName': name} goal = None try: if extent_size: # create (and use) VGStorageSetting caps = ns.LMI_VGStorageCapabilities.first_instance() (ret, outparams, err) = caps.CreateVGStorageSetting( InExtents=devs) if ret != 0: if err: raise LmiFailed("Cannot create setting for the volume " \ "group: %s." % err) vals = caps.CreateVGStorageSetting.CreateVGStorageSettingValues raise LmiFailed("Cannot create setting for the volume group:" \ " %s." % (vals.value_name(ret),)) goal = outparams['Setting'] goal = goal.to_instance() goal.ExtentSize = extent_size (ret, outparams, err) = goal.push() if ret != 0: if err: raise LmiFailed("Cannot modify setting for the volume " \ "group: %s." % err) raise LmiFailed("Cannot modify setting for the volume group:" \ " %d." % ret) args['Goal'] = goal service = ns.LMI_StorageConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateOrModifyVG(**args) if ret != 0: if err: raise LmiFailed("Cannot create the volume group: %s." % err) values = service.CreateOrModifyVG.CreateOrModifyVGValues raise LmiFailed("Cannot create the volume group: %s." % (values.value_name(ret),)) finally: if goal: goal.delete() pool = outparams['Pool'].to_instance() LOG().info("Created volume group %s", pool.Name) return pool
def get_lv_vg(ns, lv): """ Return Volume Group of given Logical Volume. :type lv: LMIInstance/LMI_LVStorageExtent or string :param lv: Logical Volume to examine. :rtype: LMIInstance/LMI_VGStoragePool """ lv = common.str2device(ns, lv) return lv.first_associator(AssocClass="LMI_LVAllocatedFromStoragePool")
def create_vg(ns, devices, name, extent_size=None): """ Create new Volume Group from given devices. :type devices: list of LMIInstance/CIM_StorageExtent or list of strings :param device: Devices to add to the Volume Group. :type name: string :param name: Name of the Volume gGoup. :type extent_size: int :param extent_size: Extent size in bytes. :rtype: LMIInstance/LMI_VGStoragePool """ devs = [common.str2device(ns, device) for device in devices] args = { 'InExtents': devs, 'ElementName': name} goal = None try: if extent_size: # create (and use) VGStorageSetting caps = ns.LMI_VGStorageCapabilities.first_instance() (ret, outparams, err) = caps.CreateVGStorageSetting( InExtents=devs) if ret != 0: if err: raise LmiFailed("Cannot create setting for the volume " \ "group: %s." % err) vals = caps.CreateVGStorageSetting.CreateVGStorageSettingValues raise LmiFailed("Cannot create setting for the volume group:" \ " %s." % (vals.value_name(ret),)) goal = outparams['Setting'] goal = goal.to_instance() goal.ExtentSize = extent_size (ret, outparams, err) = goal.push() if ret != 0: if err: raise LmiFailed("Cannot modify setting for the volume " \ "group: %s." % err) raise LmiFailed("Cannot modify setting for the volume group:" \ " %d." % ret) args['Goal'] = goal service = ns.LMI_StorageConfigurationService.first_instance() (ret, outparams, err) = service.SyncCreateOrModifyVG(**args) if ret != 0: values = service.CreateOrModifyVG.CreateOrModifyVGValues raise LmiFailed("Cannot create the volume group: %s." % (values.value_name(ret),)) finally: if goal: goal.delete() pool = outparams['Pool'].to_instance() LOG().info("Created volume group %s", pool.Name) return pool
def get_raid_members(ns, raid): """ Return member devices of the RAID. :type raid: LMIInstance/LMI_MDRAIDStorageExtent :param raid: MD RAID to examine. :rtype: List of LMIInstance/CIM_StorageExtent """ raid = common.str2device(ns, raid) members = raid.associators(AssocClass="LMI_MDRAIDBasedOn", Role="Dependent") return members
def execute(self, ns, lvs=None): """ Implementation of 'lv show' command. """ if not lvs: lvs = lvm.get_lvs(ns) for lv in lvs: lv = str2device(ns, lv) cmd = fcmd.NewTableCommand(title=lv.DeviceID) yield cmd for line in show.lv_show(ns, lv, self.app.config.human_friendly): yield line
def execute(self, ns, devices=None): """ Implementation of 'device show' command. """ if not devices: devices = get_devices(ns) for dev in devices: dev = str2device(ns, dev) cmd = fcmd.NewTableCommand(title=dev.DeviceID) yield cmd for line in show.device_show(ns, dev, self.app.config.human_friendly): yield line
def execute(self, ns, devices=None): """ Implementation of 'raid show' command. """ if not devices: devices = raid.get_raids(ns) for r in devices: r = str2device(ns, r) cmd = fcmd.NewTableCommand(title=r.DeviceID) yield cmd for line in show.raid_show(ns, r, self.app.config.human_friendly): yield line
def execute(self, ns, device, size=None, _extended=None, _logical=None): """ Implementation of 'partition create' command. """ device = str2device(ns, device) if size: size = str2size(size) ptype = None if _extended: ptype = partition.PARTITION_TYPE_EXTENDED elif _logical: ptype = partition.PARTITION_TYPE_LOGICAL partition.create_partition(ns, device, size, ptype)
def execute(self, ns, devices=None): """ Implementation of 'device show' command. """ if not devices: devices = get_devices(ns) for dev in devices: dev = str2device(ns, dev) cmd = formatter.NewTableCommand(title=dev.DeviceID) yield cmd for line in show.device_show(ns, dev, self.app.config.human_friendly): yield line
def execute(self, ns, device, size=None, _extended=None, _logical=None): """ Implementation of 'partition create' command. """ device = str2device(ns, device) size = str2size(size) ptype = None if _extended: ptype = partition.PARTITION_TYPE_EXTENDED elif _logical: ptype = partition.PARTITION_TYPE_LOGICAL p = partition.create_partition(ns, device, size, ptype) print "Partition %s, with DeviceID %s created." % (p.Name, p.DeviceID)
def execute(self, ns, partitions=None): """ Implementation of 'partition show' command. """ if not partitions: partitions = partition.get_partitions(ns) for part in partitions: part = str2device(ns, part) cmd = formatter.NewTableCommand(title=part.DeviceID) yield cmd for line in show.partition_show(ns, part, self.app.config.human_friendly): yield line
def execute(self, ns, partitions=None): """ Implementation of 'partition show' command. """ if not partitions: partitions = partition.get_partitions(ns) for part in partitions: part = str2device(ns, part) cmd = fcmd.NewTableCommand(title=part.DeviceID) yield cmd for line in show.partition_show(ns, part, self.app.config.human_friendly): yield line
def modify_vg(ns, vg, add_pvs=None, remove_pvs=None): """ Modify given Volume Group. Add 'add_pvs' devices as Physical Volumes of the group. Remove 'remove_pvs' devices from the Volume Group. :type vg: LMIInstance/LMI_VGStoragePool or string :param vg: Volume Group to delete. :type add_pvs: List of LMIInstances/LMI_VGStoragePools or strings :param add_pvs: List of new devices to be added as Physical Volumes of the VG. :type remove_pvs: List of LMIInstances/LMI_VGStoragePools or strings :param remove_pvs: List of Physical Volume to be removed from the VG. """ vg = common.str2vg(ns, vg) service = ns.LMI_StorageConfigurationService.first_instance() # get list of current PVs pvs = get_vg_pvs(ns, vg) for device in add_pvs: device = common.str2device(ns, device) if device not in pvs: pvs.append(device) for device in remove_pvs: device = common.str2device(ns, device) # don't report error when removing device that is not a PV if device in pvs: pvs.remove(device) (ret, _outparams, err) = service.SyncCreateOrModifyVG(Pool=vg, InExtents = list(pvs)) if ret != 0: if err: raise LmiFailed("Cannot modify the VG: %s." % err) raise LmiFailed("Cannot modify the VG: %s." % (service.CreateOrModifyVG.CreateOrModifyVG.value_name(ret),)) LOG().info("Modified volume group %s", vg.Name)
def get_disk_partition_table(ns, device): """ Returns LMI_DiskPartitionTableCapabilities representing partition table on given disk. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device which should be examined. :rtype: LMIInstance/LMI_DiskPartitionConfigurationCapabilities. """ device = common.str2device(ns, device) table = device.first_associator(AssocClass="LMI_InstalledPartitionTable") return table
def modify_vg(ns, vg, add_pvs=None, remove_pvs=None): """ Modify given Volume Group. Add 'add_pvs' devices as Physical Volumes of the group. Remove 'remove_pvs' devices from the Volume Group. :type vg: LMIInstance/LMI_VGStoragePool or string :param vg: Volume Group to delete. :type add_pvs: List of LMIInstances/LMI_VGStoragePools or strings :param add_pvs: List of new devices to be added as Physical Volumes of the VG. :type remove_pvs: List of LMIInstances/LMI_VGStoragePools or strings :param remove_pvs: List of Physical Volume to be removed from the VG. """ vg = common.str2vg(ns, vg) service = ns.LMI_StorageConfigurationService.first_instance() # get list of current PVs pvs = get_vg_pvs(ns, vg) for device in add_pvs: device = common.str2device(ns, device) if device not in pvs: pvs.append(device) for device in remove_pvs: device = common.str2device(ns, device) # don't report error when removing device that is not a PV if device in pvs: pvs.remove(device) (ret, _outparams, err) = service.SyncCreateOrModifyVG(Pool=vg, InExtents=list(pvs)) if ret != 0: if err: raise LmiFailed("Cannot modify the VG: %s." % err) raise LmiFailed("Cannot modify the VG: %s." % (service.CreateOrModifyVG.CreateOrModifyVG.value_name(ret),)) LOG().info("Modified volume group %s", vg.Name)
def execute(self, ns, devices=None): """ Implementation of 'partition-table show' command. """ if not devices: ret = partition.get_partition_tables(ns) devices = [i[0] for i in ret] for device in devices: device = str2device(ns, device) cmd = formatter.NewTableCommand(title=device.DeviceID) yield cmd for line in show.partition_table_show( ns, device, self.app.config.human_friendly): yield line
def device_show_device(ns, device, human_friendly): """ Print basic information about storage device, common to all device types. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to show. """ device = common.str2device(ns, device) yield ("DeviceID", device.DeviceID) yield ("Name", device.Name) yield ("ElementName", device.ElementName) yield ("Total Size", common.size2str(device.NumberOfBlocks * device.BlockSize, human_friendly)) yield ("Block Size", common.size2str(device.BlockSize, human_friendly))
def delete_raid(ns, raid): """ Destroy given RAID device :type raid: LMIInstance/LMI_MDRAIDStorageExtent :param raid: MD RAID to destroy. """ raid = common.str2device(ns, raid) service = ns.LMI_StorageConfigurationService.first_instance() (ret, _outparams, err) = service.SyncDeleteMDRAID(TheElement=raid) if ret != 0: if err: raise LmiFailed("Cannot delete the MD RAID: %s." % err) raise LmiFailed("Cannot delete the raid: %s." % (service.DeleteMDRAID.DeleteMDRAIDValues.value_name(ret),))
def get_disk_partition_table(ns, device): """ Returns LMI_DiskPartitionTableCapabilities representing partition table on given disk. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device which should be examined. :rtype: LMIInstance/LMI_DiskPartitionConfigurationCapabilities. """ device = common.str2device(ns, device) table = device.first_associator( AssocClass="LMI_InstalledPartitionTable") return table
def execute(self, ns, devices=None): """ Implementation of 'partition-table show' command. """ if not devices: ret = partition.get_partition_tables(ns) devices = [i[0] for i in ret] for device in devices: device = str2device(ns, device) cmd = fcmd.NewTableCommand(title=device.DeviceID) yield cmd for line in show.partition_table_show( ns, device, self.app.config.human_friendly): yield line
def mount_create(ns, device, mountpoint, fs_type=None, options=None): """ Create a mounted filesystem. :type device: string or LMIInstance/CIM_StorageExtent :param device: Device to mount or a mount specifier (like '//server/share' for CIFS). :type mountpoint: string :param mountpoint: path where device should be mounted :type fs_type: string :param fs_type: filesystem type :type options: string :param options: comma-separated string of mount options """ # Try to convert device name to full device path try: device_inst = common.str2device(ns, device) if device_inst: device = device_inst.Name except LmiFailed: # we did not find CIM_StorageExtent for the device, it must be non # device filesystem specification pass fs_setting = ns.LMI_FileSystemSetting.first_instance( {'InstanceID': 'LMI:LMI_FileSystemSetting:' + device}) if fs_setting is None: raise LmiFailed('Wrong device: %s' % device) filesystem = fs_setting.associators()[0] if fs_type is None: fs_type = filesystem.FileSystemType service = ns.LMI_MountConfigurationService.first_instance() setting = get_setting_from_opts(ns, options) setting.push() # TODO for now # Mode 32768 == only mount (don't create any persistent info) (ret, _outparams, _err) = service.SyncCreateMount(Goal=setting.path, FileSystemType=fs_type, Mode=32768, FileSystem=filesystem.path, MountPoint=mountpoint, FileSystemSpec=device) msg = '%s on %s' % (device, mountpoint) if ret != 0: raise LmiFailed('Cannot create mount: %s: %s' % (msg, _err)) LOG().info('Successfully created mount: %s', msg)
def device_show_device(ns, device, human_friendly): """ Print basic information about storage device, common to all device types. :type device: LMIInstance/CIM_StorageExtent or string :param device: Device to show. """ device = common.str2device(ns, device) yield("DeviceID", device.DeviceID) yield("Name", device.Name) yield("ElementName", device.ElementName) yield("Total Size", common.size2str( device.NumberOfBlocks * device.BlockSize, human_friendly)) yield("Block Size", common.size2str(device.BlockSize, human_friendly))
def delete_lv(ns, lv): """ Destroy given Logical Volume. :type lv: LMIInstance/LMI_LVStorageExtent or string :param lv: Logical Volume to destroy. """ lv = common.str2device(ns, lv) service = ns.LMI_StorageConfigurationService.first_instance() (ret, _outparams, err) = service.SyncDeleteLV(TheElement=lv) if ret != 0: if err: raise LmiFailed("Cannot delete the LV: %s." % err) raise LmiFailed("Cannot delete the LV: %s." % (service.DeleteLV.DeleteLVValues.value_name(ret), ))
def delete_lv(ns, lv): """ Destroy given Logical Volume. :type lv: LMIInstance/LMI_LVStorageExtent or string :param lv: Logical Volume to destroy. """ lv = common.str2device(ns, lv) service = ns.LMI_StorageConfigurationService.first_instance() (ret, _outparams, err) = service.SyncDeleteLV(TheElement=lv) if ret != 0: if err: raise LmiFailed("Cannot delete the LV: %s." % err) raise LmiFailed("Cannot delete the LV: %s." % (service.DeleteLV.DeleteLVValues.value_name(ret),))
def mount_create(ns, device, mountpoint, fs_type=None, options=None): """ Create a mounted filesystem. :type device: string or LMIInstance/CIM_StorageExtent :param device: Device to mount or a mount specifier (like '//server/share' for CIFS). :type mountpoint: string :param mountpoint: path where device should be mounted :type fs_type: string :param fs_type: filesystem type :type options: string :param options: comma-separated string of mount options """ # Try to convert device name to full device path try: device_inst = common.str2device(ns, device) if device_inst: device = device_inst.Name except LmiFailed: # we did not find CIM_StorageExtent for the device, it must be non # device filesystem specification pass fs_setting = ns.LMI_FileSystemSetting.first_instance({'InstanceID':'LMI:LMI_FileSystemSetting:' + device}) if fs_setting is None: raise LmiFailed('Wrong device: %s' % device) filesystem = fs_setting.associators()[0] if fs_type is None: fs_type = filesystem.FileSystemType service = ns.LMI_MountConfigurationService.first_instance() setting = get_setting_from_opts(ns, options) setting.push() # TODO for now # Mode 32768 == only mount (don't create any persistent info) (ret, _outparams, _err) = service.SyncCreateMount(Goal=setting.path, FileSystemType=fs_type, Mode=32768, FileSystem=filesystem.path, MountPoint=mountpoint, FileSystemSpec=device) msg = '%s on %s' % (device, mountpoint) if ret != 0: raise LmiFailed('Cannot create mount: %s: %s' % ( msg, _err)) LOG().info('Successfully created mount: %s', msg)
def get_partition_disk(ns, partition): """ Return a device on which is located the given partition. :type partition: LMIInstance/CIM_GenericDiskPartition or string :param partition: Partition to examine. :rtype: LMIInstance/CIM_StorageExtent. """ partition = common.str2device(ns, partition) device = partition.first_associator(AssocClass="CIM_BasedOn", Role="Dependent") if "PartitionType" in device.properties(): # we got extended partition, find the disk device = device.first_associator(AssocClass="CIM_BasedOn", Role="Dependent") return device
def get_partition_disk(ns, partition): """ Return a device on which is located the given partition. :type partition: LMIInstance/CIM_GenericDiskPartition or string :param partition: Partition to examine. :rtype: LMIInstance/CIM_StorageExtent. """ partition = common.str2device(ns, partition) device = partition.first_associator( AssocClass="CIM_BasedOn", Role="Dependent") if "PartitionType" in device.properties(): # we got extended partition, find the disk device = device.first_associator( AssocClass="CIM_BasedOn", Role="Dependent") return device
def delete_raid(ns, raid): """ Destroy given RAID device :type raid: LMIInstance/LMI_MDRAIDStorageExtent :param raid: MD RAID to destroy. """ raid = common.str2device(ns, raid) service = ns.LMI_StorageConfigurationService.first_instance() (ret, _outparams, err) = service.SyncDeleteMDRAID(TheElement=raid) if ret != 0: if err: raise LmiFailed("Cannot create the partition: %s." % err) raise LmiFailed( "Cannot delete the raid: %s." % (service.DeleteMDRAID.DeleteMDRAIDValues.value_name(ret), ))
def delete_partition(ns, partition): """ Remove given partition :type partition: LMIInstance/CIM_GenericDiskPartition :param partition: Partition to delete. """ partition = common.str2device(ns, partition) service = ns.LMI_DiskPartitionConfigurationService.first_instance() (ret, _outparams, err) = service.SyncLMI_DeletePartition( Partition=partition) if ret != 0: if err: raise LmiFailed("Cannot delete the partition: %s." % err) values = service.LMI_DeletePartition.LMI_DeletePartitionValues raise LmiFailed("Cannot delete the partition: %s." % (values.value_name(ret)))
def delete_partition(ns, partition): """ Remove given partition :type partition: LMIInstance/CIM_GenericDiskPartition :param partition: Partition to delete. """ partition = common.str2device(ns, partition) service = ns.LMI_DiskPartitionConfigurationService.first_instance() (ret, _outparams, err) = service.SyncLMI_DeletePartition(Partition=partition) if ret != 0: if err: raise LmiFailed("Cannot delete the partition: %s." % err) values = service.LMI_DeletePartition.LMI_DeletePartitionValues raise LmiFailed("Cannot delete the partition: %s." % (values.value_name(ret)))
def tlv_show(ns, tlv, human_friendly): """ Print extended information about the Thin Logical Volume. :type tlv: LMIInstance/LMI_LVStorageExtent or string :param tlv: Thin Logical Volume to show. """ tlv = common.str2device(ns, tlv) yield ("Type", "Thin Logical Volume") for line in device_show_device(ns, tlv, human_friendly): yield line tp = lvm.get_lv_vg(ns, tlv) yield ("Thin Pool", tp.ElementName) vgs = lvm.get_tp_vgs(ns, tp) vgnames = [vg.Name for vg in vgs] yield ("Volume Group", " ".join(vgnames))
def lv_show(ns, lv, human_friendly): """ Print extended information about the Logical Volume. :type lv: LMIInstance/LMI_LVStorageExtent or string :param lv: Logical Volume to show. """ lv = common.str2device(ns, lv) yield ("Type", "Logical Volume") for line in device_show_device(ns, lv, human_friendly): yield line vg = lvm.get_lv_vg(ns, lv) yield ("Volume Group", vg.ElementName) yield ("Extent Size", common.size2str(vg.ExtentSize, human_friendly)) yield ("Number of Occupied Extents", lv.BlockSize * lv.NumberOfBlocks / vg.ExtentSize) for line in device_show_data(ns, lv, human_friendly): yield line
def raid_show(ns, r, human_friendly): """ Print extended information about the RAID. :type r: LMIInstance/LMI_MDRAIDStorageExtent or string :param r: RAID to show. """ r = common.str2device(ns, r) yield ("Type", "MD RAID") for line in device_show_device(ns, r, human_friendly): yield line yield ("RAID Level", r.Level) members = raid.get_raid_members(ns, r) mnames = [r.Name for r in members] yield ("RAID Members", " ".join(mnames)) for line in device_show_data(ns, r, human_friendly): yield line
def get_partitions(ns, devices=None): """ Retrieve list of partitions on given devices. If no devices are given, all partitions on all devices are returned. :type devices: List of LMIInstance/CIM_StorageExtent or list of string :param devices: Devices to list partitions on. :rtype: List of LMIInstance/CIM_GenericPartition. """ if devices: for device in devices: device = common.str2device(ns, device) LOG().debug("Getting list of partitions on %s", device.Name) parts = get_disk_partitions(ns, device) for part in parts: yield part else: # No devices supplied, list all partitions. for part in ns.CIM_GenericDiskPartition.instances(): yield part