]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Run customize before install_extlinux
[forks/vmdebootstrap.git] / vmdebootstrap
index 05178ea36765c3a694a9df0004cd2e8d67396db7..2ffe6634bb58303aa13241f9f9736e0bb600d149 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
@@ -48,9 +49,16 @@ class VmDebootstrap(cliapp.Application):
                              metavar='NAME',
                              default='stable')
         self.settings.string_list(['package'], 'install PACKAGE onto system')
+        self.settings.string_list(['custom-package'],
+                                  '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')
@@ -61,6 +69,11 @@ class VmDebootstrap(cliapp.Application):
         self.settings.string_list(['user'],
                                   'create USER with PASSWORD',
                                   metavar='USER/PASSWORD')
+        self.settings.boolean(['serial-console'], 
+                              'configure image to use a serial console')
+        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']:
@@ -81,20 +94,22 @@ class VmDebootstrap(cliapp.Application):
             self.debootstrap(rootdir)
             self.set_hostname(rootdir)
             self.create_fstab(rootdir)
+            self.install_debs(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)
+            self.install_extlinux(rootdev, 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
 
@@ -160,13 +175,18 @@ 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
+        include = self.settings['package']
+
+        if not self.settings['no-kernel']:
+            if self.settings['arch'] == 'i386':
+                kernel_arch = '686'
+            else:
+                kernel_arch = self.settings['arch']
+            kernel_image = 'linux-image-2.6-%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'],
@@ -195,18 +215,42 @@ class VmDebootstrap(cliapp.Application):
             f.write('proc /proc proc defaults 0 0\n')
             f.write('/dev/sda1 / ext4 errors=remount-ro 0 1\n')
 
+    def install_debs(self, rootdir):
+        if not self.settings['custom-package']:
+            return
+        self.message('Installing custom packages')
+        tmp = os.path.join(rootdir, 'tmp', 'install_debs')
+        os.mkdir(tmp)
+        for deb in self.settings['custom-package']:
+            shutil.copy(deb, tmp)
+        filenames = [os.path.join('/tmp/install_debs', os.path.basename(deb))
+                     for deb in self.settings['custom-package']]
+        out, err, exit = \
+            self.runcmd_unchecked(['chroot', rootdir, 'dpkg', '-i'] + filenames)
+        logging.debug('stdout:\n%s' % out)
+        logging.debug('stderr:\n%s' % err)
+        out = self.runcmd(['chroot', rootdir, 
+                     'apt-get', '-f', '--no-remove', 'install'])
+        logging.debug('stdout:\n%s' % out)
+        shutil.rmtree(tmp)
+
     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):
             self.runcmd(['chroot', rootdir, 'adduser', '--gecos', user,
                          '--disabled-password', user])
+            if self.settings['sudo']:
+                self.runcmd(['chroot', rootdir, 'adduser', user, 'sudo'])
 
         for userpass in self.settings['user']:
             if '/' in userpass:
@@ -215,11 +259,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,19 +320,29 @@ timeout 1
 
 label linux
 kernel %(kernel)s
-append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
+append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s
+%(extserial)s
 ''' % {
     'kernel': kernel_image,
     'initrd': initrd_image,
     'uuid': uuid,
+    'kserial': 
+        'console=ttyS0,115200' if self.settings['serial-console'] else '',
+    'extserial': 'serial 0 115200' if self.settings['serial-console'] else '',
 })
         f.close()
+        
+        if self.settings['serial-console']:
+            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')
 
         self.runcmd(['extlinux', '--install', rootdir])
         self.runcmd(['sync'])
         import time; time.sleep(2)
         
-    def cleanup(self):
+    def cleanup_system(self):
         # Clean up after any errors.
 
         self.message('Cleaning up')
@@ -301,7 +359,8 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
         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)
 
 
 if __name__ == '__main__':