]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Add squashfs support
[forks/vmdebootstrap.git] / vmdebootstrap
index 5f01f5a37e1788862b36ecbf4ff66d5f9662c8b9..e28b98d3d7aa5945d9a997d4728537ba9b8a3c34 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/python
-# Copyright 2011, 2012  Lars Wirzenius
+# Copyright 2011-2013  Lars Wirzenius
 # Copyright 2012  Codethink Limited
 # 
 # This program is free software: you can redistribute it and/or modify
@@ -23,9 +23,10 @@ import re
 import shutil
 import subprocess
 import tempfile
+import time
 
 
-__version__ = '0.1.0'
+__version__ = '0.3'
 
 
 class VmDebootstrap(cliapp.Application):
@@ -51,7 +52,10 @@ class VmDebootstrap(cliapp.Application):
                              'set up foreign debootstrap environment using provided program (ie binfmt handler)')
         self.settings.string(['variant'],
                              'select debootstrap variant it not using the default')
-        self.settings.boolean(['no-extlinux'], 'do not install extlinux')
+        self.settings.boolean(
+            ['extlinux'],
+            'install extlinux?',
+            default=True)
         self.settings.string(['tarball'], "tar up the disk's contents in FILE",
                              metavar='FILE')
         self.settings.string(['mirror'],
@@ -96,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']:
@@ -109,19 +118,23 @@ class VmDebootstrap(cliapp.Application):
         self.mount_points = []
 
         try:
+            rootdev = None
+            roottype = 'ext4'
+            bootdev = None
+            boottype = None
             if self.settings['image']:
                 self.create_empty_image()
                 self.partition_image()
                 self.install_mbr()
                 (rootdev,bootdev) = self.setup_kpartx()
-                self.mkfs(rootdev)
+                self.mkfs(rootdev, type=roottype)
                 rootdir = self.mount(rootdev)
                 if bootdev:
                     if self.settings['boottype']:
-                        fstype = self.settings['boottype']
+                        boottype = self.settings['boottype']
                     else:
-                        fstype = 'ext2'
-                    self.mkfs(bootdev, type=fstype)
+                        boottype = 'ext2'
+                    self.mkfs(bootdev, type=boottype)
                     bootdir = '%s/%s' % (rootdir, 'boot/')
                     os.mkdir(bootdir)
                     bootdir = self.mount(bootdev, bootdir)
@@ -129,7 +142,7 @@ class VmDebootstrap(cliapp.Application):
                 rootdir = self.mkdtemp()
             self.debootstrap(rootdir)
             self.set_hostname(rootdir)
-            self.create_fstab(rootdir)
+            self.create_fstab(rootdir, rootdev, roottype, bootdev, boottype)
             self.install_debs(rootdir)
             self.cleanup_apt_cache(rootdir)
             self.set_root_password(rootdir)
@@ -138,9 +151,12 @@ class VmDebootstrap(cliapp.Application):
             self.setup_networking(rootdir)
             self.customize(rootdir)
             if self.settings['image']:
-                if not self.settings['no-extlinux']:
+                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' %
@@ -148,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:
@@ -180,11 +200,11 @@ class VmDebootstrap(cliapp.Application):
         return dirname
     
     def mount(self, device, path=None):
-        self.message('Mounting %s' % device)
         if not path:
             mount_point = self.mkdtemp()
         else:
             mount_point = path
+        self.message('Mounting %s on %s' % (device,mount_point))
         self.runcmd(['mount', device, mount_point])
         self.mount_points.append(mount_point)
         logging.debug('mounted %s on %s' % (device, mount_point))
@@ -216,7 +236,7 @@ class VmDebootstrap(cliapp.Application):
         self.runcmd(['install-mbr', self.settings['image']])
 
     def setup_kpartx(self):
-        out = self.runcmd(['kpartx', '-av', self.settings['image']])
+        out = self.runcmd(['kpartx', '-avs', self.settings['image']])
         if self.settings['bootsize']:
             bootindex = 0
             rootindex = 1
@@ -235,8 +255,8 @@ class VmDebootstrap(cliapp.Application):
             boot = '/dev/mapper/%s' % devices[bootindex]
         return (root,boot)
 
-    def mkfs(self, device, type='ext2'):
-        self.message('Creating filesystem')
+    def mkfs(self, device, type):
+        self.message('Creating filesystem %s' % type)
         self.runcmd(['mkfs', '-t', type, device])
     
     def debootstrap(self, rootdir):
@@ -284,19 +304,39 @@ 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)
-
-    def create_fstab(self, rootdir):
+        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):
+            out = self.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
+                               '-s', 'UUID', device])
+            return out.splitlines()[0].strip()
+
+        if rootdev:
+            rootdevstr = 'UUID=%s' % fsuuid(rootdev)
+        else:
+            rootdevstr = '/dev/sda1'
+
+        if bootdev:
+            bootdevstr = 'UUID=%s' % fsuuid(bootdev)
+        else:
+            bootdevstr = None
+
         fstab = os.path.join(rootdir, 'etc', 'fstab')
         with open(fstab, 'w') as f:
             f.write('proc /proc proc defaults 0 0\n')
-            f.write('/dev/sda1 / ext4 errors=remount-ro 0 1\n')
+            f.write('%s / %s errors=remount-ro 0 1\n' % (rootdevstr, roottype))
+            if bootdevstr:
+                f.write('%s /boot %s errors=remount-ro 0 2\n' % (bootdevstr, boottype))
 
     def install_debs(self, rootdir):
         if not self.settings['custom-package']:
@@ -379,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')
 
@@ -418,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'])
@@ -438,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.
@@ -448,10 +503,15 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
         if self.settings['image']:
             for i in xrange(len(self.mount_points) - 1, -1, -1):
                 mount_point = self.mount_points[i]
-                self.runcmd(['umount', mount_point], ignore_fail=True)
+                try:
+                    self.runcmd(['umount', mount_point], ignore_fail=False)
+                except cliapp.AppException:
+                    logging.debug("umount failed, sleeping and trying again")
+                    time.sleep(5)
+                    self.runcmd(['umount', mount_point], ignore_fail=False)
 
             self.runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True)
-        
+
         for dirname in self.remove_dirs:
             shutil.rmtree(dirname)
 
@@ -468,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()