Best Python code snippet using autotest_python
partition.py
Source:partition.py  
...682        logging.debug('Creating virtual partition')683        self.img = self._create_disk_img(file_img, file_size)684        self.loop = self._attach_img_loop(self.img)685        self._create_single_partition(self.loop)686        self.device = self._create_entries_partition(self.loop)687        logging.debug('Virtual partition successfuly created')688        logging.debug('Image disk: %s', self.img)689        logging.debug('Loopback device: %s', self.loop)690        logging.debug('Device path: %s', self.device)691    def destroy(self):692        """693        Removes the virtual partition from /dev/mapper, detaches the image file694        from the loopback device and removes the image file.695        """696        logging.debug('Removing virtual partition - device %s', self.device)697        self._remove_entries_partition()698        self._detach_img_loop()699        self._remove_disk_img()700    def _create_disk_img(self, img_path, size):701        """702        Creates a disk image using dd.703        @param img_path: Path to the desired image file.704        @param size: Size of the desired image in Bytes.705        @returns: Path of the image created.706        """707        logging.debug('Creating disk image %s, size = %d Bytes', img_path, size)708        try:709            cmd = 'dd if=/dev/zero of=%s bs=1024 count=%d' % (img_path, size)710            utils.run(cmd)711        except error.CmdError, e:712            e_msg = 'Error creating disk image %s: %s' % (img_path, e)713            raise error.AutotestError(e_msg)714        return img_path715    def _attach_img_loop(self, img_path):716        """717        Attaches a file image to a loopback device using losetup.718        @param img_path: Path of the image file that will be attached to a719                loopback device720        @returns: Path of the loopback device associated.721        """722        logging.debug('Attaching image %s to a loop device', img_path)723        try:724            cmd = 'losetup -f'725            loop_path = utils.system_output(cmd)726            cmd = 'losetup -f %s' % img_path727            utils.run(cmd)728        except error.CmdError, e:729            e_msg = ('Error attaching image %s to a loop device: %s' %730                     (img_path, e))731            raise error.AutotestError(e_msg)732        return loop_path733    def _create_single_partition(self, loop_path):734        """735        Creates a single partition encompassing the whole 'disk' using cfdisk.736        @param loop_path: Path to the loopback device.737        """738        logging.debug('Creating single partition on %s', loop_path)739        try:740            single_part_cmd = '0,,c\n'741            sfdisk_file_path = '/tmp/create_partition.sfdisk'742            sfdisk_cmd_file = open(sfdisk_file_path, 'w')743            sfdisk_cmd_file.write(single_part_cmd)744            sfdisk_cmd_file.close()745            utils.run('sfdisk %s < %s' % (loop_path, sfdisk_file_path))746        except error.CmdError, e:747            e_msg = 'Error partitioning device %s: %s' % (loop_path, e)748            raise error.AutotestError(e_msg)749    def _create_entries_partition(self, loop_path):750        """751        Takes the newly created partition table on the loopback device and752        makes all its devices available under /dev/mapper. As we previously753        have partitioned it using a single partition, only one partition754        will be returned.755        @param loop_path: Path to the loopback device.756        """757        logging.debug('Creating entries under /dev/mapper for %s loop dev',758                      loop_path)759        try:760            cmd = 'kpartx -a %s' % loop_path761            utils.run(cmd)762            l_cmd = 'kpartx -l %s | cut -f1 -d " "' % loop_path763            device = utils.system_output(l_cmd)...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
