#!/usr/bin/python # Copyright 2011, 2012, 2013 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 # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import cliapp import crypt import logging import os import re import shutil import subprocess import tempfile class VmDebootstrap(cliapp.Application): def add_settings(self): default_arch = 'amd64' self.settings.boolean(['verbose'], 'report what is going on') self.settings.string(['image'], 'put created disk image in FILE', metavar='FILE') self.settings.bytesize(['size'], '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', default='http://cdn.debian.net/debian/') self.settings.string(['arch'], 'architecture to use (%default)', metavar='ARCH', default=default_arch) self.settings.string(['distribution'], 'release to use (%default)', 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') self.settings.string(['hostname'], 'set name to HOSTNAME (%default)', metavar='HOSTNAME', default='debian') 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'] 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: if self.settings['image']: self.create_empty_image() self.partition_image() 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.set_root_password(rootdir) self.create_users(rootdir) self.remove_udev_persistent_rules(rootdir) self.setup_networking(rootdir) self.customize(rootdir) if self.settings['image']: self.install_extlinux(rootdev, rootdir) if self.settings['tarball']: self.create_tarball(rootdir) except BaseException, e: self.message('EEEK! Something bad happened...') self.cleanup_system() raise else: self.cleanup_system() def message(self, msg): logging.info(msg) if self.settings['verbose']: print msg def runcmd(self, argv, stdin='', ignore_fail=False, **kwargs): logging.debug('runcmd: %s %s' % (argv, kwargs)) p = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) out, err = p.communicate(stdin) if p.returncode != 0: msg = 'command failed: %s\n%s\n%s' % (argv, out, err) logging.error(msg) if not ignore_fail: raise cliapp.AppException(msg) return out def mkdtemp(self): dirname = tempfile.mkdtemp() self.remove_dirs.append(dirname) logging.debug('mkdir %s' % dirname) return dirname def mount(self, device): self.message('Mounting %s' % device) mount_point = self.mkdtemp() self.runcmd(['mount', device, mount_point]) self.mount_points.append(mount_point) logging.debug('mounted %s on %s' % (device, mount_point)) return mount_point def create_empty_image(self): self.message('Creating disk image') self.runcmd(['qemu-img', 'create', '-f', 'raw', self.settings['image'], str(self.settings['size'])]) def partition_image(self): self.message('Creating partitions') self.runcmd(['parted', '-s', self.settings['image'], 'mklabel', 'msdos']) self.runcmd(['parted', '-s', self.settings['image'], 'mkpart', 'primary', '0%', '100%']) self.runcmd(['parted', '-s', self.settings['image'], 'set', '1', 'boot', 'on']) def setup_kpartx(self): out = self.runcmd(['kpartx', '-av', self.settings['image']]) devices = [line.split()[2] for line in out.splitlines() if line.startswith('add map ')] if len(devices) != 1: raise cliapp.AppException('Surprising number of partitions') return '/dev/mapper/%s' % devices[0] def mkfs(self, device): self.message('Creating filesystem') self.runcmd(['mkfs', '-t', 'ext2', device]) def debootstrap(self, rootdir): self.message('Debootstrapping') 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) if self.settings['sudo'] and 'sudo' not in include: include.append('sudo') args = ['debootstrap', '--arch=%s' % self.settings['arch']] if include: args.append('--include=%s' % ','.join(include)) args += [self.settings['distribution'], rootdir, self.settings['mirror']] self.runcmd(args) def set_hostname(self, rootdir): hostname = self.settings['hostname'] with open(os.path.join(rootdir, 'etc', 'hostname'), 'w') as f: 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): 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') 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']) 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: user, password = userpass.split('/', 1) create_user(user) 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']: pathname = os.path.join(rootdir, 'etc', 'udev', 'rules.d', x) if os.path.exists(pathname): logging.debug('rm %s' % pathname) os.remove(pathname) else: logging.debug('not removing non-existent %s' % pathname) def setup_networking(self, rootdir): self.message('Setting up networking') f = open(os.path.join(rootdir, 'etc', 'network', 'interfaces'), 'w') f.write('auto lo\n') f.write('iface lo inet loopback\n') if self.settings['enable-dhcp']: f.write('\n') f.write('allow-hotplug eth0\n') f.write('iface eth0 inet dhcp\n') f.close() def install_extlinux(self, rootdev, rootdir): self.message('Installing extlinux') def find(pattern): dirname = os.path.join(rootdir, 'boot') basenames = os.listdir(dirname) logging.debug('find: %s' % basenames) for basename in basenames: if re.search(pattern, basename): return os.path.join('boot', basename) raise cliapp.AppException('Cannot find match: %s' % pattern) kernel_image = find('vmlinuz-.*') initrd_image = find('initrd.img-.*') out = self.runcmd(['blkid', '-c', '/dev/null', '-o', 'value', '-s', 'UUID', rootdev]) uuid = out.splitlines()[0].strip() conf = os.path.join(rootdir, 'extlinux.conf') logging.debug('configure extlinux %s' % conf) f = open(conf, 'w') f.write(''' default linux timeout 1 label linux kernel %(kernel)s append initrd=%(initrd)s root=UUID=%(uuid)s ro %(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_system(self): # Clean up after any errors. self.message('Cleaning up') 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) for dirname in self.remove_dirs: shutil.rmtree(dirname) def customize(self, rootdir): script = self.settings['customize'] if script: self.message('Running customize script %s' % script) 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()