X-Git-Url: https://git.siccegge.de//index.cgi?a=blobdiff_plain;f=vmdebootstrap;h=93935b71ad178a6a74b4c09bec5c03fbeb4bc84a;hb=3a412f8c99ce163873825a644e0c135a33ad1fb3;hp=8301bd5c6452ee333d232c412bf42e4efadebc91;hpb=41f88537a1fd6363a0b2da9796a176f5a727e279;p=forks%2Fvmdebootstrap.git diff --git a/vmdebootstrap b/vmdebootstrap index 8301bd5..93935b7 100755 --- a/vmdebootstrap +++ b/vmdebootstrap @@ -1,4 +1,4 @@ -#!/usr/bin/python +#! /usr/bin/python # Copyright 2011-2013 Lars Wirzenius # Copyright 2012 Codethink Limited # Copyright 2014 Neil Williams @@ -27,126 +27,180 @@ import tempfile import time -__version__ = '0.4' +__version__ = '0.6' +# pylint: disable=invalid-name -class VmDebootstrap(cliapp.Application): + +class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-methods + + def __init__(self, progname=None, version=__version__, description=None, epilog=None): + super(VmDebootstrap, self).__init__(progname, version, description, epilog) + self.remove_dirs = [] + self.mount_points = [] def add_settings(self): default_arch = subprocess.check_output( ["dpkg", "--print-architecture"]).strip() - 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.bytesize(['bootsize'], - 'create boot partition of size SIZE (%default)', - metavar='BOOTSIZE', - default='0%') - self.settings.string(['boottype'], - 'specify file system type for /boot/', - default='ext2') - self.settings.string(['foreign'], - '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(['extlinux'], 'install extlinux?', default=True) - 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://http.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.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') - 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.') - self.settings.boolean(['configure-apt'], - 'Create an apt source based on the distribution ' - 'and mirror selected.') - self.settings.boolean(['mbr'], - 'Run install-mbr (no longer done by default)') - self.settings.boolean(['grub'], - 'Install and configure grub2 - disables ' - 'extlinux.') - - def process_args(self, args): + 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.bytesize( + ['bootsize'], + 'create boot partition of size SIZE (%default)', + metavar='BOOTSIZE', + default='0%') + self.settings.string( + ['boottype'], + 'specify file system type for /boot/', + default='ext2') + self.settings.bytesize( + ['bootoffset'], + 'Space to leave at start of the image for bootloader', + default='0') + self.settings.string( + ['part-type'], + 'Partition type to use for this image', + default='msdos') + self.settings.string( + ['foreign'], + '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( + ['extlinux'], + 'install extlinux?', + default=True) + self.settings.string( + ['tarball'], + "tar up the disk's contents in FILE", + metavar='FILE') + self.settings.string( + ['apt-mirror'], + 'configure apt to use MIRROR', + metavar='URL') + self.settings.string( + ['mirror'], + 'use MIRROR as package source (%default)', + metavar='URL', + default='http://http.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.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') + 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.') + self.settings.boolean( + ['configure-apt'], + 'Create an apt source based on the distribution and mirror selected.') + self.settings.boolean( + ['mbr'], + 'Run install-mbr (default if extlinux used)') + self.settings.boolean( + ['grub'], + 'Install and configure grub2 - disables extlinux.') + self.settings.boolean( + ['sparse'], + 'Do not fill the image with zeros to keep a sparse disk image', + default=False) + self.settings.boolean( + ['pkglist'], + 'Create a list of package names included in the image.') + + def process_args(self, args): # pylint: disable=too-many-branches,too-many-statements if not self.settings['image'] and not self.settings['tarball']: - raise cliapp.AppException('You must give disk image filename, ' - 'or tarball filename') + 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 = [] + raise cliapp.AppException( + 'If disk image is specified, you must give image size.') + rootdir = None try: rootdev = None roottype = 'ext4' bootdev = None boottype = None - rootdir = None if self.settings['image']: self.create_empty_image() self.partition_image() - if self.settings['mbr']: + if self.settings['mbr'] or self.settings['extlinux']: self.install_mbr() (rootdev, bootdev) = self.setup_kpartx() - self.mkfs(rootdev, type=roottype) + self.mkfs(rootdev, fstype=roottype) rootdir = self.mount(rootdev) if bootdev: if self.settings['boottype']: boottype = self.settings['boottype'] else: boottype = 'ext2' - self.mkfs(bootdev, type=boottype) + self.mkfs(bootdev, fstype=boottype) bootdir = '%s/%s' % (rootdir, 'boot/') os.mkdir(bootdir) - bootdir = self.mount(bootdev, bootdir) + self.mount(bootdev, bootdir) else: rootdir = self.mkdtemp() self.debootstrap(rootdir) @@ -158,7 +212,7 @@ class VmDebootstrap(cliapp.Application): self.create_users(rootdir) self.remove_udev_persistent_rules(rootdir) self.setup_networking(rootdir) - if self.settings['configure-apt']: + if self.settings['configure-apt'] or self.settings['apt-mirror']: self.configure_apt(rootdir) self.customize(rootdir) self.update_initramfs(rootdir) @@ -172,6 +226,8 @@ class VmDebootstrap(cliapp.Application): self.optimize_image(rootdir) if self.settings['squash']: self.squash() + if self.settings['pkglist']: + self.list_installed_pkgs(rootdir) if self.settings['foreign']: os.unlink('%s/usr/bin/%s' % @@ -181,7 +237,7 @@ class VmDebootstrap(cliapp.Application): self.create_tarball(rootdir) if self.settings['owner']: - self.chown(rootdir) + self.chown() except BaseException, e: self.message('EEEK! Something bad happened...') if rootdir: @@ -200,7 +256,7 @@ class VmDebootstrap(cliapp.Application): print msg def runcmd(self, argv, stdin='', ignore_fail=False, env=None, **kwargs): - logging.debug('runcmd: %s %s %s' % (argv, env, kwargs)) + logging.debug('runcmd: %s %s %s', argv, env, kwargs) p = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, **kwargs) @@ -215,7 +271,7 @@ class VmDebootstrap(cliapp.Application): def mkdtemp(self): dirname = tempfile.mkdtemp() self.remove_dirs.append(dirname) - logging.debug('mkdir %s' % dirname) + logging.debug('mkdir %s', dirname) return dirname def mount(self, device, path=None): @@ -226,7 +282,7 @@ class VmDebootstrap(cliapp.Application): 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)) + logging.debug('mounted %s on %s', device, mount_point) return mount_point def create_empty_image(self): @@ -236,17 +292,41 @@ class VmDebootstrap(cliapp.Application): str(self.settings['size'])]) def partition_image(self): + """ + Uses fat16 (msdos) partitioning by default, use part-type to change. + If bootoffset is specified, the first actual partition + starts at that offset to allow customisation scripts to + put bootloader images into the space, e.g. u-boot. + """ self.message('Creating partitions') self.runcmd(['parted', '-s', self.settings['image'], - 'mklabel', 'msdos']) + 'mklabel', self.settings['part-type']]) + partoffset = 0 + bootsize = 0 + if self.settings['bootoffset'] and self.settings['bootoffset'] is not '0': + # turn v.small offsets into something at least possible to create. + if self.settings['bootoffset'] < 1048576: + partoffset = 1 + logging.info( + "Setting bootoffset %smib to allow for %s bytes", + partoffset, self.settings['bootoffset']) + else: + partoffset = self.settings['bootoffset'] / (1024 * 1024) + self.message("Using bootoffset: %smib %s bytes" % (partoffset, self.settings['bootoffset'])) if self.settings['bootsize'] and self.settings['bootsize'] is not '0%': - bootsize = str(self.settings['bootsize'] / (1024 * 1024)) + bootsize = self.settings['bootsize'] / (1024 * 1024) + bootsize += partoffset + self.message("Using bootsize %smib: %s bytes" % (bootsize, self.settings['bootsize'])) + logging.debug("Starting boot partition at %sMb", bootsize) self.runcmd(['parted', '-s', self.settings['image'], - 'mkpart', 'primary', 'fat16', '0', bootsize]) + 'mkpart', 'primary', 'fat16', str(partoffset), str(bootsize)]) + if partoffset == 0: + self.runcmd(['parted', '-s', self.settings['image'], + 'mkpart', 'primary', '0%', '100%']) else: - bootsize = '0%' - self.runcmd(['parted', '-s', self.settings['image'], - 'mkpart', 'primary', bootsize, '100%']) + logging.debug("Starting root partition at %sMb", partoffset) + self.runcmd(['parted', '-s', self.settings['image'], + 'mkpart', 'primary', str(bootsize), '100%']) self.runcmd(['parted', '-s', self.settings['image'], 'set', '1', 'boot', 'on']) @@ -260,8 +340,13 @@ class VmDebootstrap(cliapp.Application): if os.path.exists("/sbin/install-mbr"): self.message('Installing MBR') self.runcmd(['install-mbr', self.settings['image']]) + else: + msg = "mbr enabled but /sbin/install-mbr not found" \ + " - please install the mbr package." + raise cliapp.AppException(msg) def setup_kpartx(self): + bootindex = None out = self.runcmd(['kpartx', '-avs', self.settings['image']]) if self.settings['bootsize']: bootindex = 0 @@ -270,33 +355,34 @@ class VmDebootstrap(cliapp.Application): else: rootindex = 0 parts = 1 - boot = None + boot = None devices = [line.split()[2] for line in out.splitlines() if line.startswith('add map ')] if len(devices) != parts: - raise cliapp.AppException('Surprising number of partitions') + msg = 'Surprising number of partitions' + logging.debug("%s: devices=%s parts=%s", msg, devices, parts) + raise cliapp.AppException(msg) root = '/dev/mapper/%s' % devices[rootindex] if self.settings['bootsize']: boot = '/dev/mapper/%s' % devices[bootindex] - return (root, boot) + return root, boot - def mkfs(self, device, type): - self.message('Creating filesystem %s' % type) - self.runcmd(['mkfs', '-t', type, device]) + def mkfs(self, device, fstype): + self.message('Creating filesystem %s' % fstype) + self.runcmd(['mkfs', '-t', fstype, device]) def debootstrap(self, rootdir): - self.message('Debootstrapping') + msg = "(%s)" % self.settings['variant'] if self.settings['variant'] else '' + self.message('Debootstrapping %s %s' % (self.settings['distribution'], msg)) - if self.settings['foreign']: - necessary_packages = [] - else: - necessary_packages = ['acpid'] + include = self.settings['package'] - if self.settings['grub']: - necessary_packages.append('grub2') + if not self.settings['foreign']: + include.append('acpid') - include = self.settings['package'] + if self.settings['grub']: + include.append('grub2') if not self.settings['no-kernel']: if self.settings['arch'] == 'i386': @@ -310,9 +396,10 @@ class VmDebootstrap(cliapp.Application): include.append('sudo') args = ['debootstrap', '--arch=%s' % self.settings['arch']] - if self.settings['package'] and len(necessary_packages) > 0: + + if self.settings['package']: args.append( - '--include=%s' % ','.join(necessary_packages + include)) + '--include=%s' % ','.join(include)) if self.settings['foreign']: args.append('--foreign') if self.settings['variant']: @@ -354,10 +441,10 @@ class VmDebootstrap(cliapp.Application): if line.startswith('127.0.0.1'): line += ' %s' % hostname f.write('%s\n' % line) - except IOError, e: + except IOError: pass - def create_fstab(self, rootdir, rootdev, roottype, bootdev, boottype): + def create_fstab(self, rootdir, rootdev, roottype, bootdev, boottype): # pylint: disable=too-many-arguments def fsuuid(device): out = self.runcmd(['blkid', '-c', '/dev/null', '-o', 'value', '-s', 'UUID', device]) @@ -390,18 +477,18 @@ class VmDebootstrap(cliapp.Application): 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 = \ + out, err, _ = \ self.runcmd_unchecked(['chroot', rootdir, 'dpkg', '-i'] + filenames) - logging.debug('stdout:\n%s' % out) - logging.debug('stderr:\n%s' % err) + 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) + 'apt-get', '-f', '--no-remove', 'install']) + 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) + logging.debug('stdout:\n%s', out) def set_root_password(self, rootdir): if self.settings['root-password']: @@ -442,10 +529,10 @@ class VmDebootstrap(cliapp.Application): 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) + logging.debug('rm %s', pathname) os.remove(pathname) else: - logging.debug('not removing non-existent %s' % pathname) + logging.debug('not removing non-existent %s', pathname) def setup_networking(self, rootdir): self.message('Setting up networking') @@ -466,6 +553,7 @@ class VmDebootstrap(cliapp.Application): serial_command = self.settings['serial-console-command'] logging.debug('adding getty to serial console') inittab = os.path.join(rootdir, 'etc/inittab') + # to autologin, serial_command can contain '-a root' with open(inittab, 'a') as f: f.write('\nS0:23:respawn:%s\n' % serial_command) @@ -474,15 +562,15 @@ class VmDebootstrap(cliapp.Application): # rely on kpartx using consistent naming to map loop0p1 to loop0 install_dev = os.path.join('/dev', os.path.basename(rootdev)[:-2]) self.runcmd(['mount', '/dev', '-t', 'devfs', '-obind', - '%s' % os.path.join(rootdir, 'dev')]) + '%s' % os.path.join(rootdir, 'dev')]) self.runcmd(['mount', '/proc', '-t', 'proc', '-obind', - '%s' % os.path.join(rootdir, 'proc')]) + '%s' % os.path.join(rootdir, 'proc')]) self.runcmd(['mount', '/sys', '-t', 'sysfs', '-obind', - '%s' % os.path.join(rootdir, 'sys')]) + '%s' % os.path.join(rootdir, 'sys')]) try: self.runcmd(['chroot', rootdir, 'update-grub']) self.runcmd(['chroot', rootdir, 'grub-install', install_dev]) - except cliapp.AppException as e: + except cliapp.AppException: self.message("Failed. Is grub2-common installed? Using extlinux.") self.runcmd(['umount', os.path.join(rootdir, 'sys')]) self.runcmd(['umount', os.path.join(rootdir, 'proc')]) @@ -498,7 +586,7 @@ class VmDebootstrap(cliapp.Application): def find(pattern): dirname = os.path.join(rootdir, 'boot') basenames = os.listdir(dirname) - logging.debug('find: %s' % basenames) + logging.debug('find: %s', basenames) for basename in basenames: if re.search(pattern, basename): return os.path.join('boot', basename) @@ -509,7 +597,7 @@ class VmDebootstrap(cliapp.Application): initrd_image = find('initrd.img-.*') except cliapp.AppException as e: self.message("Unable to find kernel. Not installing extlinux.") - logging.debug("No kernel found. %s. Skipping install of extlinux." % e) + logging.debug("No kernel found. %s. Skipping install of extlinux.", e) return out = self.runcmd(['blkid', '-c', '/dev/null', '-o', 'value', @@ -517,9 +605,10 @@ class VmDebootstrap(cliapp.Application): uuid = out.splitlines()[0].strip() conf = os.path.join(rootdir, 'extlinux.conf') - logging.debug('configure extlinux %s' % conf) - f = open(conf, 'w') - f.write(''' + logging.debug('configure extlinux %s', conf) + kserial = 'console=ttyS0,115200' if self.settings['serial-console'] else '' + extserial = 'serial 0 115200' if self.settings['serial-console'] else '' + msg = ''' default linux timeout 1 @@ -528,14 +617,19 @@ 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() + 'kernel': kernel_image, # pylint: disable=bad-continuation + 'initrd': initrd_image, # pylint: disable=bad-continuation + 'uuid': uuid, # pylint: disable=bad-continuation + 'kserial': kserial, # pylint: disable=bad-continuation + 'extserial': extserial, # pylint: disable=bad-continuation + } # pylint: disable=bad-continuation + logging.debug("extlinux config:\n%s", msg) + + # python multiline string substitution is just ugly. + # use an external file or live with the mangling, no point in + # mangling the string to remove spaces just to keep it pretty in source. + f = open(conf, 'w') + f.write(msg) self.runcmd(['extlinux', '--install', rootdir]) self.runcmd(['sync']) @@ -545,9 +639,10 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s """ 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]) + if not self.settings['sparse']: + zeros = os.path.join(rootdir, 'ZEROS') + self.runcmd_unchecked(['dd', 'if=/dev/zero', 'of=' + zeros, 'bs=1M']) + self.runcmd(['rm', '-f', zeros]) def squash(self): """ @@ -571,7 +666,7 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s # Umount in the reverse mount order if self.settings['image']: - for i in xrange(len(self.mount_points) - 1, -1, -1): + for i in range(len(self.mount_points) - 1, -1, -1): mount_point = self.mount_points[i] try: self.runcmd(['umount', mount_point], ignore_fail=False) @@ -596,8 +691,9 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s return script = example self.message('Running customize script %s' % script) + logging.info("rootdir=%s image=%s", rootdir, self.settings['image']) with open('/dev/tty', 'w') as tty: - cliapp.runcmd([script, rootdir], stdout=tty, stderr=tty) + cliapp.runcmd([script, rootdir, self.settings['image']], stdout=tty, stderr=tty) def create_tarball(self, rootdir): # Create a tarball of the disk's contents @@ -605,27 +701,43 @@ 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): + def chown(self): # Change image owner after completed build + if self.settings['image']: + filename = self.settings['image'] + elif self.settings['tarball']: + filename = self.settings['tarball'] + else: + return self.message("Changing owner to %s" % self.settings["owner"]) - subprocess.call(["chown", - self.settings["owner"], - self.settings["image"]]) + subprocess.call(["chown", self.settings["owner"], filename]) + + def list_installed_pkgs(self, rootdir): + # output the list of installed packages for sources identification + self.message("Creating a list of installed binary package names") + out = self.runcmd(['chroot', rootdir, + 'dpkg-query', '-W', "-f='${Package}.deb\n'"]) + with open('dpkg.list', 'w') as dpkg: + dpkg.write(out) def configure_apt(self, rootdir): # use the distribution and mirror to create an apt source self.message("Configuring apt to use distribution and mirror") conf = os.path.join(rootdir, 'etc', 'apt', 'sources.list.d', 'base.list') - logging.debug('configure apt %s' % conf) + logging.debug('configure apt %s', conf) + mirror = self.settings['mirror'] + if self.settings['apt-mirror']: + mirror = self.settings['apt-mirror'] + self.message("Setting apt mirror to %s" % mirror) + os.unlink(os.path.join(rootdir, 'etc', 'apt', 'sources.list')) f = open(conf, 'w') - f.write(''' -deb %(mirror)s %(distribution)s main -#deb-src %(mirror)s %(distribution)s main -''' % { - 'mirror': self.settings['mirror'], - 'distribution': self.settings['distribution'] - }) + line = 'deb %s %s main\n' % (mirror, self.settings['distribution']) + f.write(line) + line = '#deb-src %s %s main\n' % (mirror, self.settings['distribution']) + f.write(line) f.close() + # ensure the apt sources have valid lists + self.runcmd(['chroot', rootdir, 'apt-get', '-qq', 'update']) if __name__ == '__main__': VmDebootstrap(version=__version__).run()