OLD | NEW |
1 """ | 1 """ |
2 APIs to write tests and control files that handle partition creation, deletion | 2 APIs to write tests and control files that handle partition creation, deletion |
3 and formatting. | 3 and formatting. |
4 | 4 |
5 @copyright: Google 2006-2008 | 5 @copyright: Google 2006-2008 |
6 @author: Martin Bligh (mbligh@google.com) | 6 @author: Martin Bligh (mbligh@google.com) |
7 """ | 7 """ |
8 | 8 |
9 import os, re, string, sys, fcntl, logging | 9 import os, re, string, sys, fcntl, logging |
10 from autotest_lib.client.bin import os_dep, utils | 10 from autotest_lib.client.bin import os_dep, utils |
(...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 """ | 827 """ |
828 logging.debug('Sanity check before attempting to create virtual ' | 828 logging.debug('Sanity check before attempting to create virtual ' |
829 'partition') | 829 'partition') |
830 try: | 830 try: |
831 os_dep.commands('sfdisk', 'losetup', 'kpartx') | 831 os_dep.commands('sfdisk', 'losetup', 'kpartx') |
832 except ValueError, e: | 832 except ValueError, e: |
833 e_msg = 'Unable to create virtual partition: %s' % e | 833 e_msg = 'Unable to create virtual partition: %s' % e |
834 raise error.AutotestError(e_msg) | 834 raise error.AutotestError(e_msg) |
835 | 835 |
836 logging.debug('Creating virtual partition') | 836 logging.debug('Creating virtual partition') |
837 self.img = self.__create_disk_img(file_img, file_size) | 837 self.img = self._create_disk_img(file_img, file_size) |
838 self.loop = self.__attach_img_loop(self.img) | 838 self.loop = self._attach_img_loop(self.img) |
839 self.__create_single_partition(self.loop) | 839 self._create_single_partition(self.loop) |
840 self.device = self.__create_entries_partition(self.loop) | 840 self.device = self._create_entries_partition(self.loop) |
841 logging.debug('Virtual partition successfuly created') | 841 logging.debug('Virtual partition successfuly created') |
842 logging.debug('Image disk: %s', self.img) | 842 logging.debug('Image disk: %s', self.img) |
843 logging.debug('Loopback device: %s', self.loop) | 843 logging.debug('Loopback device: %s', self.loop) |
844 logging.debug('Device path: %s', self.device) | 844 logging.debug('Device path: %s', self.device) |
845 | 845 |
846 | 846 |
847 def destroy(self): | 847 def destroy(self): |
848 """ | 848 """ |
849 Removes the virtual partition from /dev/mapper, detaches the image file | 849 Removes the virtual partition from /dev/mapper, detaches the image file |
850 from the loopback device and removes the image file. | 850 from the loopback device and removes the image file. |
851 """ | 851 """ |
852 logging.debug('Removing virtual partition - device %s', self.device) | 852 logging.debug('Removing virtual partition - device %s', self.device) |
853 self.__remove_entries_partition() | 853 self._remove_entries_partition() |
854 self.__detach_img_loop() | 854 self._detach_img_loop() |
855 self.__remove_disk_img() | 855 self._remove_disk_img() |
856 | 856 |
857 | 857 |
858 def __create_disk_img(self, img_path, size): | 858 def _create_disk_img(self, img_path, size): |
859 """ | 859 """ |
860 Creates a disk image using dd. | 860 Creates a disk image using dd. |
861 | 861 |
862 @param img_path: Path to the desired image file. | 862 @param img_path: Path to the desired image file. |
863 @param size: Size of the desired image in Bytes. | 863 @param size: Size of the desired image in Bytes. |
864 @returns: Path of the image created. | 864 @returns: Path of the image created. |
865 """ | 865 """ |
866 logging.debug('Creating disk image %s, size = %d Bytes', img_path, size) | 866 logging.debug('Creating disk image %s, size = %d Bytes', img_path, size) |
867 try: | 867 try: |
868 cmd = 'dd if=/dev/zero of=%s bs=1024 count=%d' % (img_path, size) | 868 cmd = 'dd if=/dev/zero of=%s bs=1024 count=%d' % (img_path, size) |
869 utils.system(cmd) | 869 utils.system(cmd) |
870 except error.CmdError, e: | 870 except error.CmdError, e: |
871 e_msg = 'Error creating disk image %s: %s' % (img_path, e) | 871 e_msg = 'Error creating disk image %s: %s' % (img_path, e) |
872 raise error.AutotestError(e_msg) | 872 raise error.AutotestError(e_msg) |
873 return img_path | 873 return img_path |
874 | 874 |
875 | 875 |
876 def __attach_img_loop(self, img_path): | 876 def _attach_img_loop(self, img_path): |
877 """ | 877 """ |
878 Attaches a file image to a loopback device using losetup. | 878 Attaches a file image to a loopback device using losetup. |
879 | 879 |
880 @param img_path: Path of the image file that will be attached to a | 880 @param img_path: Path of the image file that will be attached to a |
881 loopback device | 881 loopback device |
882 @returns: Path of the loopback device associated. | 882 @returns: Path of the loopback device associated. |
883 """ | 883 """ |
884 logging.debug('Attaching image %s to a loop device', img_path) | 884 logging.debug('Attaching image %s to a loop device', img_path) |
885 try: | 885 try: |
886 cmd = 'losetup -f' | 886 cmd = 'losetup -f' |
887 loop_path = utils.system_output(cmd) | 887 loop_path = utils.system_output(cmd) |
888 cmd = 'losetup -f %s' % img_path | 888 cmd = 'losetup -f %s' % img_path |
889 utils.system(cmd) | 889 utils.system(cmd) |
890 except error.CmdError, e: | 890 except error.CmdError, e: |
891 e_msg = 'Error attaching image %s to a loop device: %s' % \ | 891 e_msg = 'Error attaching image %s to a loop device: %s' % \ |
892 (img_path, e) | 892 (img_path, e) |
893 raise error.AutotestError(e_msg) | 893 raise error.AutotestError(e_msg) |
894 return loop_path | 894 return loop_path |
895 | 895 |
896 | 896 |
897 def __create_single_partition(self, loop_path): | 897 def _create_single_partition(self, loop_path): |
898 """ | 898 """ |
899 Creates a single partition encompassing the whole 'disk' using cfdisk. | 899 Creates a single partition encompassing the whole 'disk' using cfdisk. |
900 | 900 |
901 @param loop_path: Path to the loopback device. | 901 @param loop_path: Path to the loopback device. |
902 """ | 902 """ |
903 logging.debug('Creating single partition on %s', loop_path) | 903 logging.debug('Creating single partition on %s', loop_path) |
904 try: | 904 try: |
905 single_part_cmd = '0,,c\n' | 905 single_part_cmd = '0,,c\n' |
906 sfdisk_file_path = '/tmp/create_partition.sfdisk' | 906 sfdisk_file_path = '/tmp/create_partition.sfdisk' |
907 sfdisk_cmd_file = open(sfdisk_file_path, 'w') | 907 sfdisk_cmd_file = open(sfdisk_file_path, 'w') |
908 sfdisk_cmd_file.write(single_part_cmd) | 908 sfdisk_cmd_file.write(single_part_cmd) |
909 sfdisk_cmd_file.close() | 909 sfdisk_cmd_file.close() |
910 utils.system('sfdisk %s < %s' % (loop_path, sfdisk_file_path)) | 910 utils.system('sfdisk %s < %s' % (loop_path, sfdisk_file_path)) |
911 except error.CmdError, e: | 911 except error.CmdError, e: |
912 e_msg = 'Error partitioning device %s: %s' % (loop_path, e) | 912 e_msg = 'Error partitioning device %s: %s' % (loop_path, e) |
913 raise error.AutotestError(e_msg) | 913 raise error.AutotestError(e_msg) |
914 | 914 |
915 | 915 |
916 def __create_entries_partition(self, loop_path): | 916 def _create_entries_partition(self, loop_path): |
917 """ | 917 """ |
918 Takes the newly created partition table on the loopback device and | 918 Takes the newly created partition table on the loopback device and |
919 makes all its devices available under /dev/mapper. As we previously | 919 makes all its devices available under /dev/mapper. As we previously |
920 have partitioned it using a single partition, only one partition | 920 have partitioned it using a single partition, only one partition |
921 will be returned. | 921 will be returned. |
922 | 922 |
923 @param loop_path: Path to the loopback device. | 923 @param loop_path: Path to the loopback device. |
924 """ | 924 """ |
925 logging.debug('Creating entries under /dev/mapper for %s loop dev', | 925 logging.debug('Creating entries under /dev/mapper for %s loop dev', |
926 loop_path) | 926 loop_path) |
927 try: | 927 try: |
928 cmd = 'kpartx -a %s' % loop_path | 928 cmd = 'kpartx -a %s' % loop_path |
929 utils.system(cmd) | 929 utils.system(cmd) |
930 l_cmd = 'kpartx -l %s | cut -f1 -d " "' % loop_path | 930 l_cmd = 'kpartx -l %s | cut -f1 -d " "' % loop_path |
931 device = utils.system_output(l_cmd) | 931 device = utils.system_output(l_cmd) |
932 except error.CmdError, e: | 932 except error.CmdError, e: |
933 e_msg = 'Error creating entries for %s: %s' % (loop_path, e) | 933 e_msg = 'Error creating entries for %s: %s' % (loop_path, e) |
934 raise error.AutotestError(e_msg) | 934 raise error.AutotestError(e_msg) |
935 return os.path.join('/dev/mapper', device) | 935 return os.path.join('/dev/mapper', device) |
936 | 936 |
937 | 937 |
938 def __remove_entries_partition(self): | 938 def _remove_entries_partition(self): |
939 """ | 939 """ |
940 Removes the entries under /dev/mapper for the partition associated | 940 Removes the entries under /dev/mapper for the partition associated |
941 to the loopback device. | 941 to the loopback device. |
942 """ | 942 """ |
943 logging.debug('Removing the entry on /dev/mapper for %s loop dev', | 943 logging.debug('Removing the entry on /dev/mapper for %s loop dev', |
944 self.loop) | 944 self.loop) |
945 try: | 945 try: |
946 cmd = 'kpartx -d %s' % self.loop | 946 cmd = 'kpartx -d %s' % self.loop |
947 utils.system(cmd) | 947 utils.system(cmd) |
948 except error.CmdError, e: | 948 except error.CmdError, e: |
949 e_msg = 'Error removing entries for loop %s: %s' % (self.loop, e) | 949 e_msg = 'Error removing entries for loop %s: %s' % (self.loop, e) |
950 raise error.AutotestError(e_msg) | 950 raise error.AutotestError(e_msg) |
951 | 951 |
952 | 952 |
953 def __detach_img_loop(self): | 953 def _detach_img_loop(self): |
954 """ | 954 """ |
955 Detaches the image file from the loopback device. | 955 Detaches the image file from the loopback device. |
956 """ | 956 """ |
957 logging.debug('Detaching image %s from loop device %s', self.img, | 957 logging.debug('Detaching image %s from loop device %s', self.img, |
958 self.loop) | 958 self.loop) |
959 try: | 959 try: |
960 cmd = 'losetup -d %s' % self.loop | 960 cmd = 'losetup -d %s' % self.loop |
961 utils.system(cmd) | 961 utils.system(cmd) |
962 except error.CmdError, e: | 962 except error.CmdError, e: |
963 e_msg = ('Error detaching image %s from loop device %s: %s' % | 963 e_msg = ('Error detaching image %s from loop device %s: %s' % |
964 (self.loop, e)) | 964 (self.loop, e)) |
965 raise error.AutotestError(e_msg) | 965 raise error.AutotestError(e_msg) |
966 | 966 |
967 | 967 |
968 def __remove_disk_img(self): | 968 def _remove_disk_img(self): |
969 """ | 969 """ |
970 Removes the disk image. | 970 Removes the disk image. |
971 """ | 971 """ |
972 logging.debug('Removing disk image %s', self.img) | 972 logging.debug('Removing disk image %s', self.img) |
973 try: | 973 try: |
974 os.remove(self.img) | 974 os.remove(self.img) |
975 except: | 975 except: |
976 e_msg = 'Error removing image file %s' % self.img | 976 e_msg = 'Error removing image file %s' % self.img |
977 raise error.AutotestError(e_msg) | 977 raise error.AutotestError(e_msg) |
OLD | NEW |