How to use partname_to_device method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...36 'mount_options=%r, fs_tag=%r)' %37 (self.fstype, self.mkfs_flags,38 self.mount_options, self.fs_tag))39 return val40def partname_to_device(part):41 """ Converts a partition name to its associated device """42 return os.path.join(os.sep, 'dev', part)43def list_mount_devices():44 devices = []45 # list mounted filesystems46 for line in utils.system_output('mount').splitlines():47 devices.append(line.split()[0])48 # list mounted swap devices49 for line in utils.system_output('swapon -s').splitlines():50 if line.startswith('/'): # skip header line51 devices.append(line.split()[0])52 return devices53def list_mount_points():54 mountpoints = []55 for line in utils.system_output('mount').splitlines():56 mountpoints.append(line.split()[2])57 return mountpoints58def get_iosched_path(device_name, component):59 return '/sys/block/%s/queue/%s' % (device_name, component)60def wipe_filesystem(job, mountpoint):61 wipe_cmd = 'rm -rf %s/*' % mountpoint62 try:63 utils.system(wipe_cmd)64 except:65 job.record('FAIL', None, wipe_cmd, error.format_error())66 raise67 else:68 job.record('GOOD', None, wipe_cmd)69def is_linux_fs_type(device):70 """71 Checks if specified partition is type 8372 @param device: the device, e.g. /dev/sda373 @return: False if the supplied partition name is not type 83 linux, True74 otherwise75 """76 disk_device = device.rstrip('0123456789')77 # Parse fdisk output to get partition info. Ugly but it works.78 fdisk_fd = os.popen("/sbin/fdisk -l -u '%s'" % disk_device)79 fdisk_lines = fdisk_fd.readlines()80 fdisk_fd.close()81 for line in fdisk_lines:82 if not line.startswith(device):83 continue84 info_tuple = line.split()85 # The Id will be in one of two fields depending on if the boot flag86 # was set. Caveat: this assumes no boot partition will be 83 blocks.87 for fsinfo in info_tuple[4:6]:88 if fsinfo == '83': # hex 83 is the linux fs partition type89 return True90 return False91def get_partition_list(job, min_blocks=0, filter_func=None, exclude_swap=True,92 open_func=open):93 """94 Get a list of partition objects for all disk partitions on the system.95 Loopback devices and unnumbered (whole disk) devices are always excluded.96 @param job: The job instance to pass to the partition object97 constructor.98 @param min_blocks: The minimum number of blocks for a partition to99 be considered.100 @param filter_func: A callable that returns True if a partition is101 desired. It will be passed one parameter:102 The partition name (hdc3, etc.).103 Some useful filter functions are already defined in this module.104 @param exclude_swap: If True any partition actively in use as a swap105 device will be excluded.106 @param __open: Reserved for unit testing.107 @return: A list of L{partition} objects.108 """109 active_swap_devices = set()110 if exclude_swap:111 for swapline in open_func('/proc/swaps'):112 if swapline.startswith('/'):113 active_swap_devices.add(swapline.split()[0])114 partitions = []115 for partline in open_func('/proc/partitions').readlines():116 fields = partline.strip().split()117 if len(fields) != 4 or partline.startswith('major'):118 continue119 (major, minor, blocks, partname) = fields120 blocks = int(blocks)121 # The partition name better end with a digit, else it's not a partition122 if not partname[-1].isdigit():123 continue124 # We don't want the loopback device in the partition list125 if 'loop' in partname:126 continue127 device = partname_to_device(partname)128 if exclude_swap and device in active_swap_devices:129 logging.debug('Skipping %s - Active swap.', partname)130 continue131 if min_blocks and blocks < min_blocks:132 logging.debug('Skipping %s - Too small.', partname)133 continue134 if filter_func and not filter_func(partname):135 logging.debug('Skipping %s - Filter func.', partname)136 continue137 partitions.append(partition(job, device))138 return partitions139def get_mount_info(partition_list):140 """141 Picks up mount point information about the machine mounts. By default, we142 try to associate mount points with UUIDs, because in newer distros the143 partitions are uniquely identified using them.144 """145 mount_info = set()146 for p in partition_list:147 try:148 uuid = utils.system_output('blkid -p -s UUID -o value %s' % p.device)149 except error.CmdError:150 # fall back to using the partition151 uuid = p.device152 mount_info.add((uuid, p.get_mountpoint()))153 return mount_info154def filter_partition_list(partitions, devnames):155 """156 Pick and choose which partition to keep.157 filter_partition_list accepts a list of partition objects and a list158 of strings. If a partition has the device name of the strings it159 is returned in a list.160 @param partitions: A list of L{partition} objects161 @param devnames: A list of devnames of the form '/dev/hdc3' that162 specifies which partitions to include in the returned list.163 @return: A list of L{partition} objects specified by devnames, in the164 order devnames specified165 """166 filtered_list = []167 for p in partitions:168 for d in devnames:169 if p.device == d and p not in filtered_list:170 filtered_list.append(p)171 return filtered_list172def get_unmounted_partition_list(root_part, job=None, min_blocks=0,173 filter_func=None, exclude_swap=True,174 open_func=open):175 """176 Return a list of partition objects that are not mounted.177 @param root_part: The root device name (without the '/dev/' prefix, example178 'hda2') that will be filtered from the partition list.179 Reasoning: in Linux /proc/mounts will never directly mention the180 root partition as being mounted on / instead it will say that181 /dev/root is mounted on /. Thus require this argument to filter out182 the root_part from the ones checked to be mounted.183 @param job, min_blocks, filter_func, exclude_swap, open_func: Forwarded184 to get_partition_list().185 @return List of L{partition} objects that are not mounted.186 """187 partitions = get_partition_list(job=job, min_blocks=min_blocks,188 filter_func=filter_func, exclude_swap=exclude_swap, open_func=open_func)189 unmounted = []190 for part in partitions:191 if (part.device != partname_to_device(root_part) and192 not part.get_mountpoint(open_func=open_func)):193 unmounted.append(part)194 return unmounted195def parallel(partitions, method_name, *args, **dargs):196 """197 Run a partition method (with appropriate arguments) in parallel,198 across a list of partition objects199 """200 if not partitions:201 return202 job = partitions[0].job203 flist = []204 if (not hasattr(partition, method_name) or205 not callable(getattr(partition, method_name))):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?