Commits

Anonymous committed 980a619

Replaced reparted calls with pyparted API

  • Participants
  • Parent commits eeedf95

Comments (0)

Files changed (3)

File vinstall/backend/media.py

 """
 
 __author__ = "rbistolfi"
+# 2012-09-10 Modified by M0E-lnx to use the pyparted API instead of reparted
 
 
 import re
 import subprocess
 from utils import mount, umount, is_mounted, format_partition
 import partitioning
-import reparted
+import parted
+#from parted import device as pdevice
+#from parted import disk as pdisk
 
 # I think we should have a config.py and import settings from there
 DEV_DIR = '/dev'
         """Return all the partitions in the system
 
         """
-        disks = [i for i in Disk.all() if i.has_partition_table()]
-        partitions = ( i for disk in disks for i in disk._disk.partitions() )
-        for partition in partitions:
-            if partition.type in ('NORMAL', 'LOGICAL'):
-                yield cls(partition.device.path + str(partition.num))
+        disks = [ i for i in Disk.all() if i._has_partition_table ]
+        partitions = []
+        for dsk in disks:
+            partitions.extend(dsk._disk.partitions)
+        
+        for part in partitions:
+            if part.type in (parted.PARTITION_NORMAL,
+                             parted.PARTITION_LOGICAL,):
+                yield cls(part.path)
 
 
 class Disk(object):
 
         self._device = None
         self._disk = None
+        self._has_partition_table = False
 
     def __repr__(self):
 
 
     def free_space(self, units="MiB"):
         """Return the ammount of free space available in units specified"""
-        return self._disk.usable_free_space.to(units)
+        raise RuntimeError("Deprecated method")
+        #return self._disk.usable_free_space.to(units)
 
     def _get_size_from_hw(self, unit="GiB"):
         """ Returns disk size, by default in GB, unit can be GB or MB
         This method is used internally when the disk is not initialized.
         """
         pow = 2 if unit == "MiB" else 3
-        cylinders,  heads,  sectors = self._device.hw_geom
-        sector_size = self._device.sector_size
+        cylinders,  heads,  sectors = self._device.hardwareGeometry
+        sector_size = self._device.physicalSectorSize #self._device.sector_size
         size = cylinders * heads * sectors * sector_size /1000 ** pow
         return size
 
-    def _get_size_from_disk(self, unit="GiB"):
-        """Returns disk size.  Used internally by size when size is available from
-        the disk object"""
-        return self._disk.size.to(unit)
-
+    @property
     def has_partition_table(self):
         """Return True if the disk has a partition table.  False otherwise"""
-        return self._disk.type_name not in (None, "")
+        # test if the device has a partition table yet
+        return self._has_partition_table
+        try:
+            pdisk = parted.Disk(self._device)
+            del pdisk
+            return True
+        except:
+            return False
 
     def size(self, unit="GiB"):
         """ Returns disk size, by default in GB, unit can be GB or MB
 
         """
-        if self.has_partition_table():
-            return self._get_size_from_disk(unit)
-        else:
-            return self._get_size_from_hw(unit)
+        return self._device.getLength(unit)
 
     def path(self):
         """Return the node path for this device"""
         return self._device.path
 
     def is_read_only(self):
-
-        return self._device.read_only
+        """Return True if the device is read-only"""
+        return self._device.readOnly
 
     @classmethod
     def all(cls):
-        devices = ( dev for dev in reparted.device.probe_standard_devices() )
+        devices = ( dev for dev in parted.getAllDevices() if dev.readOnly is False )
         if not devices:
             raise RuntimeError("No hard drives found")
         for dev in devices:
             disk = cls()
-            disk._disk = reparted.Disk(dev)
+            try:
+                disk._disk = pdisk.Disk(dev)
+                disk._has_partition_table = True
+            except:
+                #disk._disk = parted.freshDisk(dev, 'msdos') # Create the object with an empty partition table in it ??
+                # FIXME: ^^
+                disk._has_partition_table = False                
             disk._device = dev
             yield disk

File vinstall/backend/partitioning.py

 
 """
 
-
-import reparted
 import utils
-
+import parted
 
 __author__ = "Moises Henriquez"
 __email__ = "moc.liamg@xnl.e0m"[::-1]
                 disk - a media.Disk object
 
         """
-        self.disk = disk._disk
+        #self.disk = disk._disk
+        self.disk = disk
 
     def create_partition_table(self, table_type="msdos"):
         """ Create the partition table on the disk.
         never been partitioned before.
 
         """
-        assert self.disk.type_name is None, "Disk already has a partition table"
-        self.disk.set_label(table_type)
+        assert self.disk._disk is None, "Partition table already exists?"
+#        assert self.disk._disk._has_partition_table is False, "Partition table already exists?"
+#        assert not hasattr(self.disk, "_disk"), ".disk attribute already here.  Do we have a partition table already?"
+#        assert self.disk._disk is not None, "We already have .disk attribute.  Do we have a partition table already?"
+#        assert self.disk.type is None, "Disk already has a partition table"
+        self.disk._disk = parted.freshDisk(self.disk._device, "msdos")
+        self.disk._disk._has_partition_table = True
+
 
     def delete_all_partitions(self):
         """ Delete all partitions from drive
 
         """
-        self.disk.delete_all()
+        assert self.disk._has_partition_table is True, "No partition table on disk."
+        self.disk.deleteAllPartitions()
 
     def add_partition(self,  size=0, units='MiB'):
         """Add a partition to the disk.  
         Args:  size = partition size
+               units = partition units (MB, GB, MiB, GiB), (defaults to MiB)
 
         """
+        assert self.disk.has_partition_table is False, "No partition table on disk.  Cannot add partitions"
+        mypsize = parted.sizeToSectors(size, units, self.disk._device.sectorSize)
+        # first partition starts at sector 1
+        # find out where the free space is
+        myconst = parted.Constraint(device = self.disk._device)
+        for i in self.disk._disk.getFreeSpaceRegions():
+            if i > mypsize:  # partition fits here
+                start_marker = i.start
+        print "Creating new partition @ block %s and extending to %s"% (start_marker, mypsize)
+        mygeom = parted.Geometry(device = self.disk._device,
+                                 start = start_marker,
+                                 length = mypsize)
+        myfs = parted.FileSystem(type='ext2', geometry=mygeom)
+        mypartition = parted.Partition(disk=self.disk._disk,
+                                       fs = myfs,
+                                       type=parted.PARTITION_NORMAL,
+                                       geometry = mygeom)
+        myconst = parted.Constraint(exactGeom = mygeom)
+        # Add the partition to the disk
+        self.disk._disk.addPartition(partition = mypartition,
+                                     constraint = myconst)
+
+        
+
+        return
+        # OLD CODE ----
         if self.disk.type_name is None:
             self.create_partition_table()
 
         """Finalize changes to the disk.  This needs to be called
         after creating partitions or deleting partitions to make sure
         the changes are actually written to the disk."""
-        return self.disk.commit()
+        self.disk._disk.commit()
 
 

File vinstall/controller/automatic.py

 
         disk_size = disk.size(unit="MiB")
         partitioner = partitioning.DiskPartitioner(disk)
-        if not disk.has_partition_table():
+        if not disk.has_partition_table:
+            print "Creating partition table"
             partitioner.create_partition_table()
         else:
             partitioner.delete_all_partitions()
-
-        root_size = disk.free_space("MiB") - swap_size
+        
+        root_size = disk_size - swap_size
         partitioner.add_partition(size=root_size, units='MiB')
-        swap_size = disk.free_space("MiB")
-        partitioner.add_partition(size=swap_size, units='MiB')
+        partitioner.add_partition(size=swap_size-1, units='MiB')
         partitioner.write_changes()
 
+
     def format_partitions(self, disk):
         """Format partitions in disk with the default filesystem
 
 
         proot = "%s%s" % (disk.path(), 1)
         pswap = "%s%s" % (disk.path(), 2)
+        # make sure our partitions are not mounted before we attempt to
+        # format them.  Especially the target's /
+        utils.umount('/mnt/TARGET')
         utils.format_partition(proot, self.ROOT_FS)
         utils.format_partition(pswap, 'swap')
         utils.activate_swap(pswap)