]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Release version 0.1.0
[forks/vmdebootstrap.git] / vmdebootstrap
index 295bdc74f7017ef4e551c431a1f16baf6d030610..a5537b7abd6f6191ddc2cf8704470db9082184f5 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
-# Copyright 2011  Lars Wirzenius
+# Copyright 2011, 2012  Lars Wirzenius
+# Copyright 2012  Codethink Limited
 # 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -24,6 +25,9 @@ import subprocess
 import tempfile
 
 
+__version__ = '0.1.0'
+
+
 class VmDebootstrap(cliapp.Application):
 
     def add_settings(self):
@@ -36,6 +40,8 @@ class VmDebootstrap(cliapp.Application):
                                'create a disk image of size SIZE (%default)',
                                metavar='SIZE',
                                default='1G')
+        self.settings.string(['tarball'], "tar up the disk's contents in FILE",
+                             metavar='FILE')
         self.settings.string(['mirror'],
                              'use MIRROR as package source (%default)',
                              metavar='URL',
@@ -52,9 +58,12 @@ class VmDebootstrap(cliapp.Application):
                                   'install package in DEB file onto system '
                                     '(not from mirror)',
                                   metavar='DEB')
+        self.settings.boolean(['no-kernel'], 'do not install a linux package')
         self.settings.boolean(['enable-dhcp'], 'enable DHCP on eth0')
         self.settings.string(['root-password'], 'set root password',
                              metavar='PASSWORD')
+        self.settings.boolean(['lock-root-password'], 
+                              'lock root account so they cannot login?')
         self.settings.string(['customize'],
                              'run SCRIPT after setting up system',
                              metavar='SCRIPT')
@@ -67,44 +76,60 @@ class VmDebootstrap(cliapp.Application):
                                   metavar='USER/PASSWORD')
         self.settings.boolean(['serial-console'], 
                               'configure image to use a serial console')
+        self.settings.string(['serial-console-command'],
+                             'command to manage the serial console, appended '
+                               'to /etc/inittab (%default)',
+                             metavar='COMMAND',
+                             default='/sbin/getty -L ttyS0 115200 vt100')
         self.settings.boolean(['sudo'], 
                               'install sudo, and if user is created, add them '
                                 'to sudo group')
 
     def process_args(self, args):
-        if not self.settings['image']:
-            raise cliapp.AppException('You must give image filename.')
-        if not self.settings['size']:
-            raise cliapp.AppException('You must give image size.')
+        if not self.settings['image'] and not self.settings['tarball']:
+            raise cliapp.AppException('You must give disk image filename, '
+                                      'or tarball filename')
+        if self.settings['image'] and not self.settings['size']:
+            raise cliapp.AppException('If disk image is specified, '
+                                      'You must give image size.')
 
         self.remove_dirs = []
         self.mount_points = []
 
         try:
-            self.create_empty_image()
-            self.partition_image()
-            self.install_mbr()
-            rootdev = self.setup_kpartx()
-            self.mkfs(rootdev)
-            rootdir = self.mount(rootdev)
+            if self.settings['image']:
+                self.create_empty_image()
+                self.partition_image()
+                self.install_mbr()
+                rootdev = self.setup_kpartx()
+                self.mkfs(rootdev)
+                rootdir = self.mount(rootdev)
+            else:
+                rootdir = self.mkdtemp()
             self.debootstrap(rootdir)
             self.set_hostname(rootdir)
             self.create_fstab(rootdir)
             self.install_debs(rootdir)
+            self.cleanup_apt_cache(rootdir)
             self.set_root_password(rootdir)
             self.create_users(rootdir)
             self.remove_udev_persistent_rules(rootdir)
             self.setup_networking(rootdir)
-            self.install_extlinux(rootdev, rootdir)
             self.customize(rootdir)
+            if self.settings['image']:
+                self.install_extlinux(rootdev, rootdir)
+                self.optimize_image(rootdir)
+            if self.settings['tarball']:
+                self.create_tarball(rootdir)
         except BaseException, e:
             self.message('EEEK! Something bad happened...')
-            self.cleanup()
+            self.cleanup_system()
             raise
         else:
-            self.cleanup()
+            self.cleanup_system()
 
     def message(self, msg):
+        logging.info(msg)
         if self.settings['verbose']:
             print msg
 
@@ -170,22 +195,27 @@ class VmDebootstrap(cliapp.Application):
     def debootstrap(self, rootdir):
         self.message('Debootstrapping')
 
-        if self.settings['arch'] == 'i386':
-            kernel_arch = '686'
-        else:
-            kernel_arch = self.settings['arch']
-        kernel_image = 'linux-image-2.6-%s' % kernel_arch
+        necessary_packages = ['acpid']
+
+        include = self.settings['package']
+
+        if not self.settings['no-kernel']:
+            if self.settings['arch'] == 'i386':
+                kernel_arch = '486'
+            else:
+                kernel_arch = self.settings['arch']
+            kernel_image = 'linux-image-%s' % kernel_arch
+            include.append(kernel_image)
 
-        include = [kernel_image] + self.settings['package']
         if self.settings['sudo'] and 'sudo' not in include:
             include.append('sudo')
 
-        self.runcmd(['debootstrap', 
-                     '--arch=%s' % self.settings['arch'],
-                     '--include=%s' % ','.join(include),
-                     self.settings['distribution'],
-                     rootdir, 
-                     self.settings['mirror']])
+        args = ['debootstrap', '--arch=%s' % self.settings['arch']]
+        args.append(
+            '--include=%s' % ','.join(necessary_packages + include))
+        args += [self.settings['distribution'],
+                 rootdir, self.settings['mirror']]
+        self.runcmd(args)
 
     def set_hostname(self, rootdir):
         hostname = self.settings['hostname']
@@ -226,13 +256,20 @@ class VmDebootstrap(cliapp.Application):
         logging.debug('stdout:\n%s' % out)
         shutil.rmtree(tmp)
 
+    def cleanup_apt_cache(self, rootdir):
+        out = self.runcmd(['chroot', rootdir, 'apt-get', 'clean'])
+        logging.debug('stdout:\n%s' % out)
+
     def set_root_password(self, rootdir):
         if self.settings['root-password']:
             self.message('Setting root password')
             self.set_password(rootdir, 'root', self.settings['root-password'])
-        else:
+        elif self.settings['lock-root-password']:
             self.message('Locking root password')
             self.runcmd(['chroot', rootdir, 'passwd', '-l', 'root'])
+        else:
+            self.message('Give root an empty password')
+            self.delete_password(rootdir, 'root')
 
     def create_users(self, rootdir):
         def create_user(user):
@@ -248,11 +285,15 @@ class VmDebootstrap(cliapp.Application):
                 self.set_password(rootdir, user, password)
             else:
                 create_user(userpass)
+                self.delete_password(rootdir, userpass)
 
     def set_password(self, rootdir, user, password):
         encrypted = crypt.crypt(password, '..')
         self.runcmd(['chroot', rootdir, 'usermod', '-p', encrypted, user])
 
+    def delete_password(self, rootdir, user):
+        self.runcmd(['chroot', rootdir, 'passwd', '-d', user])
+
     def remove_udev_persistent_rules(self, rootdir):
         self.message('Removing udev persistent cd and net rules')
         for x in ['70-persistent-cd.rules', '70-persistent-net.rules']:
@@ -272,7 +313,7 @@ class VmDebootstrap(cliapp.Application):
         
         if self.settings['enable-dhcp']:
             f.write('\n')
-            f.write('allow-hotplug eth0\n')
+            f.write('auto eth0\n')
             f.write('iface eth0 inet dhcp\n')
             
         f.close()
@@ -305,7 +346,7 @@ timeout 1
 
 label linux
 kernel %(kernel)s
-append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s
+append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
 %(extserial)s
 ''' % {
     'kernel': kernel_image,
@@ -318,24 +359,35 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s
         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:/sbin/getty -L ttyS0 115200 vt100\n')
+                f.write('\nS0:23:respawn:%s\n' % serial_command)
 
         self.runcmd(['extlinux', '--install', rootdir])
         self.runcmd(['sync'])
         import time; time.sleep(2)
-        
-    def cleanup(self):
+
+    def optimize_image(self, rootdir):
+        """
+        Filing up the image with zeros will increase its compression rate
+        """
+        zeros = os.path.join(rootdir, 'ZEROS')
+        self.runcmd_unchecked(['dd', 'if=/dev/zero', 'of=' + zeros, 'bs=1M'])
+        self.runcmd(['rm', '-f', zeros])
+
+
+    def cleanup_system(self):
         # Clean up after any errors.
 
         self.message('Cleaning up')
 
-        for mount_point in self.mount_points:
-            self.runcmd(['umount', mount_point], ignore_fail=True)
+        if self.settings['image']:
+            for mount_point in self.mount_points:
+                self.runcmd(['umount', mount_point], ignore_fail=True)
 
-        self.runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True)
+            self.runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True)
         
         for dirname in self.remove_dirs:
             shutil.rmtree(dirname)
@@ -344,9 +396,16 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s
         script = self.settings['customize']
         if script:
             self.message('Running customize script %s' % script)
-            self.runcmd([script, rootdir])
+            with open('/dev/tty', 'w') as tty:
+                cliapp.runcmd([script, rootdir], stdout=tty, stderr=tty)
+
+    def create_tarball(self, rootdir):
+        # Create a tarball of the disk's contents
+        # shell out to runcmd since it more easily handles rootdir
+        self.message('Creating tarball of disk contents')
+        self.runcmd(['tar', '-cf', self.settings['tarball'], '-C', rootdir, '.'])
 
 
 if __name__ == '__main__':
-    VmDebootstrap().run()
+    VmDebootstrap(version=__version__).run()