Add remove_swap_entry sysprep in linux
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 16 May 2012 12:23:34 +0000 (15:23 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 16 May 2012 12:23:34 +0000 (15:23 +0300)
This action will remove the swap entry from the fstab.

image_creator/os_type/__init__.py
image_creator/os_type/linux.py

index b19d573..052a3e3 100644 (file)
@@ -120,6 +120,7 @@ class OSBase(object):
         wrapper = textwrap.TextWrapper()
         wrapper.subsequent_indent = '\t'
         wrapper.initial_indent = '\t'
+        wrapper.width = 72
 
         output("Enabled system preperation operations:")
         if len(enabled) == 0:
@@ -127,7 +128,7 @@ class OSBase(object):
         else:
             for sysprep in enabled:
                 name = sysprep.__name__.replace('_', '-')
-                descr = wrapper.fill(sysprep.__doc__)
+                descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
                 output('    %s:\n%s\n' % (name, descr))
 
         output("Disabled system preperation operations:")
@@ -136,7 +137,7 @@ class OSBase(object):
         else:
             for sysprep in disabled:
                 name = sysprep.__name__.replace('_', '-')
-                descr = wrapper.fill(sysprep.__doc__)
+                descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
                 output('    %s:\n%s\n' % (name, descr))
 
     @add_prefix
index de82e7d..e128396 100644 (file)
@@ -126,6 +126,29 @@ class Linux(Unix):
             self.g.rm(rule_file)
 
     @sysprep()
+    def remove_swap_entry(self, print_header=True):
+        """Remove swap entry from /etc/fstab. If swap is the last partition
+        then the partition will be removed when shrinking is performed. If the
+        swap partition is not the last partition in the disk or if you are not
+        going to shrink the image you should probably disable this.
+        """
+
+        if print_header:
+            output('Removing swap entry from fstab')
+
+        new_fstab = ""
+        fstab = self.g.cat('/etc/fstab')
+        for line in fstab.splitlines():
+
+            entry = line.split('#')[0].strip().split()
+            if len(entry) == 6 and entry[2] == 'swap':
+                continue
+
+            new_fstab += "%s\n" % line
+
+        self.g.write('/etc/fstab', new_fstab)
+
+    @sysprep()
     def persistent_devs(self, print_header=True):
         """Scan fstab & grub configuration files and replace all non-persistent
         device appearences with UUIDs.