+# This file is part of VASM.
+# VASM is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License v3 as published by
+# the Free Software Foundation.
+# VASM is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with VASM. If not, see <http://www.gnu.org/licenses/>.
+""" Interact with system disk drives """
+# Some constants to help identify device types
+ def __init__(self, devpath=None):
+ """ Class representing a device drive on the system.
+ path - path to the device
+ size - Device size in MB
+ model - Device model information
+ type - Device type (one of DEVICE_HARD_DISK or DEVICE_OPTICAL)
+ assert devpath not in (None, ''), "Invalid device path"
+ self._pdev = self._get_parted_device()
+ self.size = self._pdev.getSize()
+ self.model = self._pdev.model
+ self.type = self._get_device_type()
+ if self.type in (DEVICE_HARD_DISK,) and int(self.size) > 0:
+ # Reading the label will tell us if the drive is ready to
+ # accept partitions or if we need to create a partition table
+ # first with ` mklabel msdos|gpt|etc ` in parted
+ self.tabletype = self._get_partition_table_type()
+ if self.type == DEVICE_HARD_DISK and self.tabletype is not None:
+ dsk = parted.Disk(self._pdev)
+ self.partitions = dsk.partitions
+ def _get_partition_table_type(self):
+ """ When an unpartitioned disk is manipulated in parted/gparted,
+ it needs to have a 'label' on it. parted will throw an exception if
+ the disk has no label, so we need to try it and find out. """
+ mydisk = parted.Disk(self._pdev)
+ # Set the return value to whatever .type reports. This would be
+ # 'msdos' or 'GPT' or something along those lines.
+ except parted.DiskLabelException:
+ # This is definitely an unpartitioned drive. We must return
+ # None to expose this condition
+ def _get_parted_device(self):
+ """ Return a parted device object that we can interact with """
+ for dev in parted.getAllDevices():
+ if dev.path == self.path:
+ def _get_device_type(self):
+ """ Check if the device is listed by the kernel as a cdrom. If true,
+ then this method will return DEVICE_OPTICAL, otherwise, it will
+ return DEVICE_HARD_DISK"""
+ datafile = "/proc/sys/dev/cdrom/info"
+ f = open(datafile, 'r')
+ data = [line for line in f.readlines() if line.startswith('drive name:')]
+ shortpath = os.path.split(self.path)[-1].strip()
+ thisdev = line.split(':')[-1].strip()
+ if thisdev == shortpath:
+ return DEVICE_HARD_DISK
+ """ Return a list of Device objects """
+ for drive in parted.getAllDevices():
+ dev = Device(drive.path)
+ """ Return a list of all system partitions """
+ for dev in listSystemDrives():
+ if dev.type == DEVICE_HARD_DISK and dev.tabletype not in (None, ''):
+ for part in dev.partitions:
+ if part.type != parted.PARTITION_EXTENDED: