Commits

Rodrigo Bistolfi committed 6a01ac1

Added automatic partitioning option in advanced

If the user follows the "Advanced" path, offer automatic partitioning as an
option in the partitioning step

Comments (0)

Files changed (1)

vinstall/controller/advanced.py

 
 
 from vinstall.core import Application, Render, Controller, model
-from vinstall.backend import media
+from vinstall.backend import media, utils, partitioning
 import os, subprocess
 
 
                 " existing partitions")
         options = model.ExclusiveOptionList()
         options.append({"name": "I will use existing partitions"})
+        options.append({"name": "Create partitions automatically"})
         options.append({"name": "Launch a partitioning program"})
         return Render(title, intro, options)
 
         """Return next controller
 
         """
-        if self.is_display_set() or self.selection == 0:
+        if self.selection == 0:
             from vinstall.controller import mountpoints
-            return mountpoints.Mountpoint
+            next = mountpoints.Mountpoint
+        elif self.selection == 1:
+            next = AdvancedAutoPartition
         else:
-            return TUIPartitionManager
-
+            if not self.is_display_set():
+                next = TUIPartitionManager
+            else:
+                from vinstall.controller import mountpoints
+                next = mountpoints.Mountpoint
+        return next
+ 
     def previous(self):
         """Return previous step
 
 
         """
         self.selection = index
-        if index == 1:
+        if index == 2:
             if self.is_display_set():
                 command = "/usr/sbin/gparted".split()
                 subprocess.call(command)
 
         """
         return TUIPartitionManager
+
+
+class AdvancedAutoPartition(Controller):
+
+    ROOT_FS="ext3"
+
+    def init(self):
+        """Initialize some objects
+
+        """
+        self.disks = self.find_disks()
+
+    def render(self):
+        """Show available disks for user selection
+
+        """
+        title = "Select disk"
+        intro = "Select a disk for Vector. Data in the disk will be overwritten"
+        options = [ {"name": str(i)} for i in self.disks ]
+        options = model.ExclusiveOptionList(*options)
+        return Render(title, intro, options)
+
+    def process_in_background(self, diskindex):
+        """Schedule all the necessary steps for installation.
+        Use yield to return a function and a tuple with the arguments needed
+        for calling it.
+
+        """
+        disk = self.get_disk(diskindex)
+        self.partitioning(disk)
+        self.format_partitions(disk)
+
+    def previous(self):
+        """Go back to the first screen
+
+        """
+        return AdvancedMode
+
+    def next(self):
+        """Jump to the basic Setup
+
+        """
+        from vinstall.controller import mountpoints
+        return mountpoints.Mountpoint
+
+    def find_disks(self):
+        """Find the disks available in the system.
+
+        """
+        return [ i for i in media.Disk.all() ]
+
+    def get_disk(self, index):
+        """Return a disk object from index
+
+        """
+        return self.disks[index]
+
+    def partitioning(self, disk):
+        """Do automatic partitioning in disk
+
+        """
+        available_ram = int(utils.get_mem_size())
+
+        if available_ram <= 512:
+            swap_size = 2 * available_ram
+        elif available_ram < 1024:
+            swap_size = 1024
+        else:
+            swap_size = int(available_ram / 2)
+
+        disk_size = disk.size(unit="MB")
+        partitioner = partitioning.DiskPartitioner(disk)
+        if not disk.has_partition_table:
+            partitioner.create_partition_table()
+        else:
+            partitioner.delete_all_partitions()
+
+        root_size = disk_size - swap_size
+        partitioner.add_partition(size=root_size, units='MB')
+        partitioner.add_partition(size=swap_size-1, units='MB')
+        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)
+        utils.umount('/mnt/TARGET')
+        utils.format_partition(proot, self.ROOT_FS)
+        utils.format_partition(pswap, 'swap')
+        utils.activate_swap(pswap)
+