+ 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)
+
+ if self.settings['sudo'] and 'sudo' not in include:
+ include.append('sudo')
+
+ args = ['debootstrap', '--arch=%s' % self.settings['arch']]
+ args.append(
+ '--include=%s' % ','.join(necessary_packages + include))
+ if self.settings['foreign']:
+ args.append('--foreign')
+ if self.settings['variant']:
+ args.append('--variant')
+ args.append(self.settings['variant'])
+ args += [self.settings['distribution'],
+ rootdir, self.settings['mirror']]
+ self.runcmd(args)
+ if self.settings['foreign']:
+ # First copy the binfmt handler over
+ shutil.copy(self.settings['foreign'], '%s/usr/bin/' % rootdir)
+ # Next, run the package install scripts etc.
+ self.runcmd(['chroot', rootdir,
+ '/debootstrap/debootstrap', '--second-stage'])
+
+ 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, 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('%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']:
+ 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 cleanup_apt_cache(self, rootdir):
+ out = self.runcmd(['chroot', rootdir, 'apt-get', 'clean'])
+ logging.debug('stdout:\n%s' % out)