def get_loop_devices(self):
     global disk_util
     disk_util = DiskUtil(patching=self.patching, logger=self.logger)
     if len(self.file_systems_info) == 0:
         self.file_systems_info = disk_util.get_mount_file_systems()
     self.logger.log("file_systems list : ", True)
     self.logger.log(str(self.file_systems_info), True)
     disk_loop_devices_file_systems = []
     for file_system_info in self.file_systems_info:
         if 'loop' in file_system_info[0]:
             disk_loop_devices_file_systems.append(file_system_info[0])
     return disk_loop_devices_file_systems
예제 #2
0
 def __init__(self, patching, logger):
     self.mounts = []
     added_mount_point_names = []
     disk_util = DiskUtil(patching, logger)
     # Get mount points
     mount_points, fs_types = disk_util.get_mount_points()
     # Get lsblk devices
     device_items = disk_util.get_device_items(None)
     lsblk_mounts = []
     lsblk_mount_points = []
     lsblk_fs_types = []
     # List to hold mount-points returned from lsblk command but not reurned from mount command
     lsblk_mounts_not_in_mount = []
     for device_item in device_items:
         mount = Mount(device_item.name, device_item.type,
                       device_item.file_system, device_item.mount_point)
         lsblk_mounts.append(mount)
         logger.log(
             "lsblk mount point " + str(device_item.mount_point) +
             " added with fs type " + str(device_item.file_system), True)
         lsblk_mount_points.append(device_item.mount_point)
         lsblk_fs_types.append(device_item.file_system)
         # If lsblk mount is not found in "mount command" mount-list, add it to the lsblk_mounts_not_in_mount array
         if ((device_item.mount_point not in mount_points) and
             (device_item.mount_point not in lsblk_mounts_not_in_mount)):
             lsblk_mounts_not_in_mount.append(device_item.mount_point)
     # Sort lsblk_mounts_not_in_mount array in ascending order
     lsblk_mounts_not_in_mount.sort()
     # Add the lsblk devices in the same order as they are returned in mount command output
     for mount_point, fs_type in zip(mount_points, fs_types):
         if ((mount_point in lsblk_mount_points)
                 and (mount_point not in added_mount_point_names)):
             mountObj = lsblk_mounts[lsblk_mount_points.index(mount_point)]
             if (mountObj.fstype is None or mountObj.fstype == ""
                     or mountObj.fstype == " "):
                 logger.log(
                     "fstype empty from lsblk for mount" + str(mount_point),
                     True)
                 mountObj.fstype = fs_type
             self.mounts.append(mountObj)
             added_mount_point_names.append(mount_point)
     # Append all the lsblk devices corresponding to lsblk_mounts_not_in_mount list mount-points
     for mount_point in lsblk_mounts_not_in_mount:
         if ((mount_point in lsblk_mount_points)
                 and (mount_point not in added_mount_point_names)):
             self.mounts.append(
                 lsblk_mounts[lsblk_mount_points.index(mount_point)])
             added_mount_point_names.append(mount_point)
     added_mount_point_names.reverse()
     logger.log("added_mount_point_names :" + str(added_mount_point_names),
                True)
     # Reverse the mounts list
     self.mounts.reverse()
 def get_loop_devices(self):
     global disk_util
     disk_util = DiskUtil(patching = self.patching,logger = self.logger)
     if len(self.file_systems_info) == 0 :
         self.file_systems_info = disk_util.get_mount_file_systems()
     self.logger.log("file_systems list : ",True)
     self.logger.log(str(self.file_systems_info),True)
     disk_loop_devices_file_systems = []
     for file_system_info in self.file_systems_info:
         if 'loop' in file_system_info[0]:
             disk_loop_devices_file_systems.append(file_system_info[0])
     return disk_loop_devices_file_systems
예제 #4
0
 def __init__(self,patching,logger):
     self.mounts = []
     added_mount_point_names = [] 
     disk_util = DiskUtil(patching,logger)
     # Get mount points 
     mount_points, fs_types = disk_util.get_mount_points() 
     # Get lsblk devices 
     device_items = disk_util.get_device_items(None)
     lsblk_mounts = [] 
     lsblk_mount_points = [] 
     lsblk_fs_types = []
     # List to hold mount-points returned from lsblk command but not reurned from mount command 
     lsblk_mounts_not_in_mount = [] 
     for device_item in device_items:
         mount = Mount(device_item.name, device_item.type, device_item.file_system, device_item.mount_point)
         lsblk_mounts.append(mount)
         logger.log("lsblk mount point "+str(device_item.mount_point)+" added with fs type "+str(device_item.file_system), True)
         lsblk_mount_points.append(device_item.mount_point)
         lsblk_fs_types.append(device_item.file_system)
         # If lsblk mount is not found in "mount command" mount-list, add it to the lsblk_mounts_not_in_mount array
         if((device_item.mount_point not in mount_points) and (device_item.mount_point not in lsblk_mounts_not_in_mount)):
             lsblk_mounts_not_in_mount.append(device_item.mount_point)
     # Sort lsblk_mounts_not_in_mount array in ascending order
     lsblk_mounts_not_in_mount.sort()
     # Add the lsblk devices in the same order as they are returned in mount command output
     for mount_point, fs_type in zip(mount_points, fs_types):
         if((mount_point in lsblk_mount_points) and (mount_point not in added_mount_point_names)):
             mountObj = lsblk_mounts[lsblk_mount_points.index(mount_point)]
             if(mountObj.fstype is None or mountObj.fstype == "" or mountObj.fstype == " "):
                 logger.log("fstype empty from lsblk for mount" + str(mount_point), True)
                 mountObj.fstype = fs_type
             self.mounts.append(mountObj)
             added_mount_point_names.append(mount_point)
     # Append all the lsblk devices corresponding to lsblk_mounts_not_in_mount list mount-points
     for mount_point in lsblk_mounts_not_in_mount:
         if((mount_point in lsblk_mount_points) and (mount_point not in added_mount_point_names)):
             self.mounts.append(lsblk_mounts[lsblk_mount_points.index(mount_point)])
             added_mount_point_names.append(mount_point)
     added_mount_point_names.reverse()
     logger.log("added_mount_point_names :" + str(added_mount_point_names), True)
     # Reverse the mounts list
     self.mounts.reverse()
 def __init__(self, patching, logger):
     self.logger = logger
     self.disk_util = DiskUtil.get_instance(patching, logger)
예제 #6
0
 def __init__(self, patching, logger):
     self.logger = logger
     self.disk_util = DiskUtil(patching, logger)
예제 #7
0
class ResourceDiskUtil(object):
    def __init__(self, patching, logger):
        self.logger = logger
        self.disk_util = DiskUtil(patching, logger)

    @staticmethod
    def _enumerate_device_id():
        """
		Enumerate all storage device IDs.
		Args:
		None
		Returns:
		Iterator[Tuple[str, str]]: VmBus and storage devices.
		"""

        if os.path.exists(STORAGE_DEVICE_PATH):
            for vmbus in os.listdir(STORAGE_DEVICE_PATH):
                deviceid = read_file(filepath=os.path.join(
                    STORAGE_DEVICE_PATH, vmbus, "device_id"))
                guid = deviceid.strip('{}\n')
                yield vmbus, guid

    @staticmethod
    def search_for_resource_disk(gen1_device_prefix, gen2_device_id):
        """
		Search the filesystem for a device by ID or prefix.
		Args:
		gen1_device_prefix (str): Gen1 resource disk prefix.
		gen2_device_id (str): Gen2 resource device ID.
		Returns:
		str: The found device.
		"""

        device = None
        # We have to try device IDs for both Gen1 and Gen2 VMs.
        #ResourceDiskUtil.logger.log('Searching gen1 prefix {0} or gen2 {1}'.format(gen1_device_prefix, gen2_device_id),True)
        try:  # pylint: disable=R1702
            for vmbus, guid in ResourceDiskUtil._enumerate_device_id():
                if guid.startswith(
                        gen1_device_prefix) or guid == gen2_device_id:
                    for root, dirs, files in os.walk(STORAGE_DEVICE_PATH +
                                                     vmbus):  # pylint: disable=W0612
                        root_path_parts = root.split('/')
                        # For Gen1 VMs we only have to check for the block dir in the
                        # current device. But for Gen2 VMs all of the disks (sda, sdb,
                        # sr0) are presented in this device on the same SCSI controller.
                        # Because of that we need to also read the LUN. It will be:
                        #   0 - OS disk
                        #   1 - Resource disk
                        #   2 - CDROM
                        if root_path_parts[-1] == 'block' and (  # pylint: disable=R1705
                                guid != gen2_device_id
                                or root_path_parts[-2].split(':')[-1] == '1'):
                            device = dirs[0]
                            return device
                        else:
                            # older distros
                            for d in dirs:  # pylint: disable=C0103
                                if ':' in d and "block" == d.split(':')[0]:
                                    device = d.split(':')[1]
                                    return device
        except (OSError, IOError) as exc:
            err_msg = 'Error getting device for %s or %s: %s , Stack Trace: %s' % (
                gen1_device_prefix, gen2_device_id, str(exc),
                traceback.format_exc())
        return None

    def device_for_ide_port(self):
        """
		Return device name attached to ide port 'n'.
		gen1 device prefix is the prefix of the file name in which the resource disk partition is stored eg sdb
		gen1 is for new distros
		In old distros the directory name which contains resource disk partition is assigned to gen2 device id
		"""
        g0 = "00000000"
        gen1_device_prefix = '{0}-0001'.format(g0)
        self.logger.log(
            'Searching gen1 prefix {0} or gen2 {1}'.format(
                gen1_device_prefix, GEN2_DEVICE_ID), True)
        device = self.search_for_resource_disk(
            gen1_device_prefix=gen1_device_prefix,
            gen2_device_id=GEN2_DEVICE_ID)
        self.logger.log('Found device: {0}'.format(device), True)
        return device

    def get_mount_point(self, mountlist, device):
        """
		Example of mountlist:
			/dev/sda1 on / type ext4 (rw)
			proc on /proc type proc (rw)
			sysfs on /sys type sysfs (rw)
			devpts on /dev/pts type devpts (rw,gid=5,mode=620)
			tmpfs on /dev/shm type tmpfs
			(rw,rootcontext="system_u:object_r:tmpfs_t:s0")
			none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
			/dev/sdb1 on /mnt/resource type ext4 (rw)
		"""
        if (mountlist and device):
            for entry in mountlist.split('\n'):
                if (re.search(device, entry)):
                    tokens = entry.split()
                    #Return the 3rd column of this line
                    return tokens[2] if len(tokens) > 2 else None
        return None

    def get_resource_disk_mount_point(self, option=1):  # pylint: disable=R0912,R0914
        try:
            """
			if option = 0 then partition will be returned eg sdb1
			if option = 1 then mount point will be returned eg /mnt/resource
			"""
            device = self.device_for_ide_port()
            if device is None:
                self.logger.log('unable to detect disk topology', True,
                                'Error')

            if device is not None:
                partition = "{0}{1}".format(
                    device, "1")  #assuming only one resourde disk partition
            else:
                partition = ""

            self.logger.log(("Resource disk partition: {0} ", partition), True)
            if (option == 0):
                return partition

            #p = Popen("mount", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            #mount_list, err = p.communicate()
            mount_list = self.disk_util.get_mount_output()

            if (mount_list is not None):
                mount_point = self.get_mount_point(mountlist=mount_list,
                                                   device=device)
                self.logger.log(("Resource disk [{0}] is mounted [{1}]",
                                 partition, mount_point), True)
                if mount_point:
                    return mount_point
            return None
        except Exception as e:
            err_msg = 'Cannot get Resource disk partition, Exception %s, stack trace: %s' % (
                str(e), traceback.format_exc())
            self.logger.log(err_msg, True, 'Error')
            return None
예제 #8
0
 def __init__(self, patching, logger):
     self.mounts = []
     added_mount_point_names = []
     disk_util = DiskUtil(patching, logger)
     # Get mount points
     mount_points, mount_points_info = disk_util.get_mount_points()
     # Get lsblk devices
     device_items = disk_util.get_device_items(None)
     lsblk_mounts = []
     lsblk_mount_points = []
     lsblk_unique_names = []
     lsblk_fs_types = []
     # List to hold mount-points returned from lsblk command but not reurned from mount command
     lsblk_mounts_not_in_mount = []
     for device_item in device_items:
         mount = Mount(device_item.name, device_item.type,
                       device_item.file_system, device_item.mount_point)
         lsblk_mounts.append(mount)
         logger.log(
             "lsblk mount point " + str(mount.mount_point) +
             " added with device-name " + str(mount.name) +
             " and fs type " + str(mount.fstype) + ", unique-name " +
             str(mount.unique_name), True)
         lsblk_mount_points.append(device_item.mount_point)
         lsblk_unique_names.append(mount.unique_name)
         lsblk_fs_types.append(device_item.file_system)
         # If lsblk mount is not found in "mount command" mount-list, add it to the lsblk_mounts_not_in_mount array
         if ((device_item.mount_point not in mount_points) and
             (device_item.mount_point not in lsblk_mounts_not_in_mount)):
             lsblk_mounts_not_in_mount.append(device_item.mount_point)
     # Sort lsblk_mounts_not_in_mount array in ascending order
     lsblk_mounts_not_in_mount.sort()
     # Add the lsblk devices in the same order as they are returned in mount command output
     for mount_point_info in mount_points_info:
         mountPoint = mount_point_info[0]
         deviceNameParts = mount_point_info[1].split("/")
         uniqueName = str(mountPoint) + "_" + str(
             deviceNameParts[len(deviceNameParts) - 1])
         fsType = mount_point_info[2]
         if ((mountPoint in lsblk_mount_points)
                 and (mountPoint not in added_mount_point_names)):
             lsblk_mounts_index = 0
             try:
                 lsblk_mounts_index = lsblk_unique_names.index(uniqueName)
             except ValueError as e:
                 logger.log(
                     "######## UniqueName not found in lsblk list :" +
                     str(uniqueName), True)
                 lsblk_mounts_index = lsblk_mount_points.index(mountPoint)
             mountObj = lsblk_mounts[lsblk_mounts_index]
             if (mountObj.fstype is None or mountObj.fstype == ""
                     or mountObj.fstype == " "):
                 logger.log(
                     "fstype empty from lsblk for mount" + str(mountPoint),
                     True)
                 mountObj.fstype = fsType
             self.mounts.append(mountObj)
             added_mount_point_names.append(mountPoint)
             logger.log(
                 "mounts list item added, mount point " +
                 str(mountObj.mount_point) + ", device-name " +
                 str(mountObj.name) + ", fs-type " + str(mountObj.fstype) +
                 ", unique-name " + str(mountObj.unique_name), True)
     # Append all the lsblk devices corresponding to lsblk_mounts_not_in_mount list mount-points
     for mount_point in lsblk_mounts_not_in_mount:
         if ((mount_point in lsblk_mount_points)
                 and (mount_point not in added_mount_point_names)):
             self.mounts.append(
                 lsblk_mounts[lsblk_mount_points.index(mount_point)])
             added_mount_point_names.append(mount_point)
             logger.log(
                 "mounts list item added from lsblk_mounts_not_in_mount, mount point "
                 + str(mount_point), True)
     added_mount_point_names.reverse()
     logger.log("added_mount_point_names :" + str(added_mount_point_names),
                True)
     # Reverse the mounts list
     self.mounts.reverse()