+
+ # unconditionally write for wheezy (which became oldstable on 04/25/2015)
+ if self.was_oldstable(datetime.date(2015, 4, 26)):
+ with open(os.path.join(rootdir, 'etc', 'network', 'interfaces'), 'w') as netfile:
+ netfile.write('source /etc/network/interfaces.d/*\n')
+ os.mkdir(os.path.join(rootdir, 'etc', 'network', 'interfaces.d'))
+
+ elif not os.path.exists(os.path.join(rootdir, 'etc', 'network', 'interfaces')):
+ iface_path = os.path.join(rootdir, 'etc', 'network', 'interfaces')
+ with open(iface_path, 'w') as netfile:
+ netfile.write('source-directory /etc/network/interfaces.d\n')
+ ethpath = os.path.join(rootdir, 'etc', 'network', 'interfaces.d', 'setup')
+ with open(ethpath, 'w') as eth:
+ eth.write('auto lo\n')
+ eth.write('iface lo inet loopback\n')
+
+ if self.settings['enable-dhcp']:
+ eth.write('\n')
+ eth.write('auto eth0\n')
+ eth.write('iface eth0 inet dhcp\n')
+
+ def append_serial_console(self, rootdir):
+ 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')
+ # to autologin, serial_command can contain '-a root'
+ with open(inittab, 'a') as f:
+ f.write('\nS0:23:respawn:%s\n' % serial_command)
+
+ # pylint: disable=no-self-use
+ def _grub_serial_console(self, rootdir):
+ cmdline = 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=tty1 console=ttyS0,38400n8"'
+ terminal = 'GRUB_TERMINAL="serial gfxterm"'
+ command = 'GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --parity=no --stop=1"'
+ grub_cfg = os.path.join(rootdir, 'etc', 'default', 'grub')
+ logging.debug("Allowing serial output in grub config %s", grub_cfg)
+ with open(grub_cfg, 'a+') as cfg:
+ cfg.write("# %s serial support\n" % os.path.basename(__file__))
+ cfg.write("%s\n" % cmdline)
+ cfg.write("%s\n" % terminal)
+ cfg.write("%s\n" % command)
+
+ def _mount_wrapper(self, rootdir):
+ self.runcmd(['mount', '/dev', '-t', 'devfs', '-obind',
+ '%s' % os.path.join(rootdir, 'dev')])
+ self.runcmd(['mount', '/proc', '-t', 'proc', '-obind',
+ '%s' % os.path.join(rootdir, 'proc')])
+ self.runcmd(['mount', '/sys', '-t', 'sysfs', '-obind',
+ '%s' % os.path.join(rootdir, 'sys')])
+
+ def _umount_wrapper(self, rootdir):
+ self.runcmd(['umount', os.path.join(rootdir, 'sys')])
+ self.runcmd(['umount', os.path.join(rootdir, 'proc')])
+ self.runcmd(['umount', os.path.join(rootdir, 'dev')])
+
+ def install_grub_uefi(self, rootdir):
+ self.message("Configuring grub-uefi")
+ target = self.efi_arch_table[self.settings['arch']]['target']
+ grub_opts = "--target=%s" % target
+ logging.debug("Running grub-install with options: %s", grub_opts)
+ self._mount_wrapper(rootdir)
+ try:
+ self.runcmd(['chroot', rootdir, 'update-grub'])
+ self.runcmd(['chroot', rootdir, 'grub-install', grub_opts])
+ except cliapp.AppException as exc:
+ logging.warning(exc)
+ self.message(
+ "Failed to configure grub-uefi for %s" %
+ self.settings['arch'])
+ self._umount_wrapper(rootdir)
+ self.configure_efi()
+ extra = str(self.efi_arch_table[self.settings['arch']]['extra'])
+ if extra:
+ target = self.efi_arch_table[extra]['target']
+ grub_opts = "--target=%s" % target
+ try:
+ self.runcmd(['chroot', rootdir, 'update-grub'])
+ self.runcmd(['chroot', rootdir, 'grub-install', grub_opts])
+ except cliapp.AppException as exc:
+ logging.warning(exc)
+ self.message(
+ "Failed to configure grub-uefi for %s" % extra)
+ self.configure_extra_efi()
+ self._umount_wrapper(rootdir)
+
+ def install_grub2(self, rootdev, rootdir):
+ self.message("Configuring grub2")
+ # rely on kpartx using consistent naming to map loop0p1 to loop0
+ grub_opts = os.path.join('/dev', os.path.basename(rootdev)[:-2])
+ if self.settings['serial-console']:
+ self._grub_serial_console(rootdir)
+ logging.debug("Running grub-install with options: %s", grub_opts)
+ self._mount_wrapper(rootdir)
+ try:
+ self.runcmd(['chroot', rootdir, 'update-grub'])
+ self.runcmd(['chroot', rootdir, 'grub-install', grub_opts])
+ except cliapp.AppException as exc:
+ logging.warning(exc)
+ self.message("Failed. Is grub2-common installed? Using extlinux.")
+ self.install_extlinux(rootdev, rootdir)
+ self._umount_wrapper(rootdir)