]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Add squashfs support
[forks/vmdebootstrap.git] / vmdebootstrap
index 3344a59b41308e2d2cbcbf3a36f0b2293cd01974..e28b98d3d7aa5945d9a997d4728537ba9b8a3c34 100755 (executable)
@@ -26,7 +26,7 @@ import tempfile
 import time
 
 
-__version__ = '0.2'
+__version__ = '0.3'
 
 
 class VmDebootstrap(cliapp.Application):
@@ -100,6 +100,11 @@ class VmDebootstrap(cliapp.Application):
         self.settings.boolean(['sudo'], 
                               'install sudo, and if user is created, add them '
                                 'to sudo group')
+        self.settings.string(['owner'],
+                             'the user who will own the image when the build '
+                               'is complete.')
+        self.settings.boolean(['squash'],
+                              'use squashfs on the final image.')
 
     def process_args(self, args):
         if not self.settings['image'] and not self.settings['tarball']:
@@ -148,7 +153,10 @@ class VmDebootstrap(cliapp.Application):
             if self.settings['image']:
                 if self.settings['extlinux']:
                     self.install_extlinux(rootdev, rootdir)
+                self.append_serial_console(rootdir)
                 self.optimize_image(rootdir)
+                if self.settings['squash']:
+                    self.squash()
 
             if self.settings['foreign']:
                 os.unlink('%s/usr/bin/%s' %
@@ -156,8 +164,12 @@ class VmDebootstrap(cliapp.Application):
 
             if self.settings['tarball']:
                 self.create_tarball(rootdir)
+
+            if self.settings['owner']:
+                self.chown(rootdir)
         except BaseException, e:
             self.message('EEEK! Something bad happened...')
+            self.message(e)
             self.cleanup_system()
             raise
         else:
@@ -292,13 +304,16 @@ class VmDebootstrap(cliapp.Application):
             f.write('%s\n' % hostname)
             
         etc_hosts = os.path.join(rootdir, 'etc', 'hosts')
-        with open(etc_hosts, 'r') as f:
-            data = f.read()
-        with open(etc_hosts, 'w') as f:
-            for line in data.splitlines():
-                if line.startswith('127.0.0.1'):
-                    line += ' %s' % hostname
-                f.write('%s\n' % line)
+        try:
+            with open(etc_hosts, 'r') as f:
+                data = f.read()
+            with open(etc_hosts, 'w') as f:
+                for line in data.splitlines():
+                    if line.startswith('127.0.0.1'):
+                        line += ' %s' % hostname
+                    f.write('%s\n' % line)
+        except IOError, e:
+            pass
 
     def create_fstab(self, rootdir, rootdev, roottype, bootdev, boottype):
         def fsuuid(device):
@@ -404,6 +419,14 @@ class VmDebootstrap(cliapp.Application):
             
         f.close()
 
+    def append_serial_console(self, rootdir):
+        if self.settings['serial-console']:
+            serial_command = self.settings['serial-console-command']
+            logging.debug('adding getty to serial console')
+            inittab = os.path.join(rootdir, 'etc/inittab')
+            with open(inittab, 'a') as f:
+                f.write('\nS0:23:respawn:%s\n' % serial_command)
+
     def install_extlinux(self, rootdev, rootdir):
         self.message('Installing extlinux')
 
@@ -443,13 +466,6 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
     'extserial': 'serial 0 115200' if self.settings['serial-console'] else '',
 })
         f.close()
-        
-        if self.settings['serial-console']:
-            serial_command = self.settings['serial-console-command']
-            logging.debug('adding getty to serial console')
-            inittab = os.path.join(rootdir, 'etc/inittab')
-            with open(inittab, 'a') as f:
-                f.write('\nS0:23:respawn:%s\n' % serial_command)
 
         self.runcmd(['extlinux', '--install', rootdir])
         self.runcmd(['sync'])
@@ -463,6 +479,20 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
         self.runcmd_unchecked(['dd', 'if=/dev/zero', 'of=' + zeros, 'bs=1M'])
         self.runcmd(['rm', '-f', zeros])
 
+    def squash(self):
+        """
+        Run squashfs on the image.
+        """
+        if not os.path.exists('/usr/bin/mksquashfs'):
+            logging.warning("Squash selected but mksquashfs not found!")
+            return
+        self.message("Running mksquashfs")
+        suffixed = "%s.squashfs" % self.settings['image']
+        self.runcmd(['mksquashfs', self.settings['image'],
+            suffixed,
+            '-no-progress', '-comp', 'xz'], ignore_fail=False)
+        os.unlink(self.settings['image'])
+        self.settings['image'] = suffixed
 
     def cleanup_system(self):
         # Clean up after any errors.
@@ -498,6 +528,13 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
         self.message('Creating tarball of disk contents')
         self.runcmd(['tar', '-cf', self.settings['tarball'], '-C', rootdir, '.'])
 
+    def chown(self, rootdir):
+        # Change image owner after completed build
+        self.message("Changing owner to %s" % self.settings["owner"])
+        subprocess.call(["chown",
+                         self.settings["owner"],
+                         self.settings["image"]])            
+
 
 if __name__ == '__main__':
     VmDebootstrap(version=__version__).run()