bootstrap-vz/bootstrapvz/common/tasks/boot.py

188 lines
5.8 KiB
Python
Raw Normal View History

from bootstrapvz.base import Task
from .. import phases
from ..tools import log_check_call
import apt
import filesystem
from bootstrapvz.base.fs import partitionmaps
2013-12-01 23:50:32 +01:00
import os.path
Blacklist floppy module to speed up boot by several seconds. Add UpdateInitramfs task which is needed for this to take effect. Enable both tasks for GCE. Console output before ===================== [ 1.877142] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.880163] sd 0:0:1:0: Attached scsi generic sg0 type 0 [ 2.684132] tsc: Refined TSC clocksource calibration: 2500.000 MHz [ 4.824081] floppy0: no floppy controllers found [ 5.103671] work still pending Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 5.313107] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 7.751955] alg: No test for crc32 (crc32-pclmul) [ 10.728078] floppy0: no floppy controllers found [ 11.006680] work still pending [....] Activating swap... done [ 11.258954] EXT4-fs (sda1): re-mounted. Opts: (null) Console output after ==================== [ 1.829785] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.832806] sd 0:0:1:0: Attached scsi generic sg0 type 0 Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 1.969862] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 2.878920] alg: No test for crc32 (crc32-pclmul) [....] Activating swap... done [ 2.986642] EXT4-fs (sda1): re-mounted. Opts: (null) Delint. Delint
2015-01-24 08:17:34 +00:00
class UpdateInitramfs(Task):
description = 'Updating initramfs'
phase = phases.system_modification
@classmethod
def run(cls, info):
from ..tools import log_check_call
log_check_call(['chroot', info.root, 'update-initramfs', '-u'])
class BlackListModules(Task):
description = 'Blacklisting kernel modules'
phase = phases.system_modification
Blacklist floppy module to speed up boot by several seconds. Add UpdateInitramfs task which is needed for this to take effect. Enable both tasks for GCE. Console output before ===================== [ 1.877142] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.880163] sd 0:0:1:0: Attached scsi generic sg0 type 0 [ 2.684132] tsc: Refined TSC clocksource calibration: 2500.000 MHz [ 4.824081] floppy0: no floppy controllers found [ 5.103671] work still pending Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 5.313107] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 7.751955] alg: No test for crc32 (crc32-pclmul) [ 10.728078] floppy0: no floppy controllers found [ 11.006680] work still pending [....] Activating swap... done [ 11.258954] EXT4-fs (sda1): re-mounted. Opts: (null) Console output after ==================== [ 1.829785] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.832806] sd 0:0:1:0: Attached scsi generic sg0 type 0 Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 1.969862] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 2.878920] alg: No test for crc32 (crc32-pclmul) [....] Activating swap... done [ 2.986642] EXT4-fs (sda1): re-mounted. Opts: (null) Delint. Delint
2015-01-24 08:17:34 +00:00
successors = [UpdateInitramfs]
@classmethod
def run(cls, info):
blacklist_path = os.path.join(info.root, 'etc/modprobe.d/blacklist.conf')
with open(blacklist_path, 'a') as blacklist:
Blacklist floppy module to speed up boot by several seconds. Add UpdateInitramfs task which is needed for this to take effect. Enable both tasks for GCE. Console output before ===================== [ 1.877142] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.880163] sd 0:0:1:0: Attached scsi generic sg0 type 0 [ 2.684132] tsc: Refined TSC clocksource calibration: 2500.000 MHz [ 4.824081] floppy0: no floppy controllers found [ 5.103671] work still pending Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 5.313107] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 7.751955] alg: No test for crc32 (crc32-pclmul) [ 10.728078] floppy0: no floppy controllers found [ 11.006680] work still pending [....] Activating swap... done [ 11.258954] EXT4-fs (sda1): re-mounted. Opts: (null) Console output after ==================== [ 1.829785] sd 0:0:1:0: [sda] Attached SCSI disk [ 1.832806] sd 0:0:1:0: Attached scsi generic sg0 type 0 Begin: Loading essential drivers ... done. Begin: Running /scripts/init-premount ... done. Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done. Begin: Running /scripts/local-premount ... done. [ 1.969862] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) ... [ 2.878920] alg: No test for crc32 (crc32-pclmul) [....] Activating swap... done [ 2.986642] EXT4-fs (sda1): re-mounted. Opts: (null) Delint. Delint
2015-01-24 08:17:34 +00:00
blacklist.write(('# disable pc speaker and floppy\n'
'blacklist pcspkr\n'
'blacklist floppy\n'))
class DisableGetTTYs(Task):
description = 'Disabling getty processes'
phase = phases.system_modification
@classmethod
def run(cls, info):
from ..tools import sed_i
inittab_path = os.path.join(info.root, 'etc/inittab')
tty1 = '1:2345:respawn:/sbin/getty 38400 tty1'
2013-08-17 17:28:46 +02:00
sed_i(inittab_path, '^' + tty1, '#' + tty1)
ttyx = ':23:respawn:/sbin/getty 38400 tty'
for i in range(2, 7):
i = str(i)
2013-08-17 17:28:46 +02:00
sed_i(inittab_path, '^' + i + ttyx + i, '#' + i + ttyx + i)
2013-12-01 23:50:32 +01:00
class AddGrubPackage(Task):
description = 'Adding grub package'
phase = phases.preparation
predecessors = [apt.AddDefaultSources]
@classmethod
def run(cls, info):
info.packages.add('grub-pc')
class ConfigureGrub(Task):
description = 'Configuring grub'
phase = phases.system_modification
predecessors = [filesystem.FStab]
@classmethod
def run(cls, info):
from bootstrapvz.common.tools import sed_i
grub_def = os.path.join(info.root, 'etc/default/grub')
sed_i(grub_def, '^#GRUB_TERMINAL=console', 'GRUB_TERMINAL=console')
sed_i(grub_def, '^GRUB_CMDLINE_LINUX_DEFAULT="quiet"',
'GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"')
class InstallGrub_1_99(Task):
description = 'Installing grub 1.99'
2013-12-01 23:50:32 +01:00
phase = phases.system_modification
predecessors = [filesystem.FStab]
2013-12-01 23:50:32 +01:00
@classmethod
def run(cls, info):
2013-12-01 23:50:32 +01:00
from ..fs import remount
2013-12-01 23:50:32 +01:00
p_map = info.volume.partition_map
def link_fn():
info.volume.link_dm_node()
2014-01-19 01:02:29 +01:00
if isinstance(p_map, partitionmaps.none.NoPartitions):
2013-12-01 23:50:32 +01:00
p_map.root.device_path = info.volume.device_path
def unlink_fn():
info.volume.unlink_dm_node()
2014-01-19 01:02:29 +01:00
if isinstance(p_map, partitionmaps.none.NoPartitions):
2013-12-01 23:50:32 +01:00
p_map.root.device_path = info.volume.device_path
# GRUB cannot deal with installing to loopback devices
# so we fake a real harddisk with dmsetup.
# Guide here: http://ebroder.net/2009/08/04/installing-grub-onto-a-disk-image/
from ..fs.loopbackvolume import LoopbackVolume
2013-12-01 23:50:32 +01:00
if isinstance(info.volume, LoopbackVolume):
remount(info.volume, link_fn)
try:
[device_path] = log_check_call(['readlink', '-f', info.volume.device_path])
device_map_path = os.path.join(info.root, 'boot/grub/device.map')
2013-12-01 23:50:32 +01:00
partition_prefix = 'msdos'
2014-01-19 01:02:29 +01:00
if isinstance(p_map, partitionmaps.gpt.GPTPartitionMap):
2013-12-01 23:50:32 +01:00
partition_prefix = 'gpt'
with open(device_map_path, 'w') as device_map:
device_map.write('(hd0) {device_path}\n'.format(device_path=device_path))
2014-01-19 01:02:29 +01:00
if not isinstance(p_map, partitionmaps.none.NoPartitions):
2013-12-01 23:50:32 +01:00
for idx, partition in enumerate(info.volume.partition_map.partitions):
device_map.write('(hd0,{prefix}{idx}) {device_path}\n'
.format(device_path=partition.device_path,
prefix=partition_prefix,
2013-12-14 19:37:23 +01:00
idx=idx + 1))
2013-12-01 23:50:32 +01:00
# Install grub
log_check_call(['chroot', info.root, 'grub-install', device_path])
log_check_call(['chroot', info.root, 'update-grub'])
2013-12-01 23:50:32 +01:00
except Exception as e:
if isinstance(info.volume, LoopbackVolume):
remount(info.volume, unlink_fn)
raise e
if isinstance(info.volume, LoopbackVolume):
remount(info.volume, unlink_fn)
class InstallGrub_2(Task):
description = 'Installing grub 2'
phase = phases.system_modification
predecessors = [filesystem.FStab]
@classmethod
def run(cls, info):
log_check_call(['chroot', info.root, 'grub-install', info.volume.device_path])
log_check_call(['chroot', info.root, 'update-grub'])
class AddExtlinuxPackage(Task):
description = 'Adding extlinux package'
phase = phases.preparation
predecessors = [apt.AddDefaultSources]
@classmethod
def run(cls, info):
info.packages.add('extlinux')
2014-01-19 01:02:29 +01:00
if isinstance(info.volume.partition_map, partitionmaps.gpt.GPTPartitionMap):
info.packages.add('syslinux-common')
class ConfigureExtlinux(Task):
description = 'Configuring extlinux'
phase = phases.system_modification
predecessors = [filesystem.FStab]
@classmethod
def run(cls, info):
2014-12-31 13:39:40 +01:00
if info.release_codename == 'squeeze':
# On squeeze /etc/default/extlinux is generated when running extlinux-update
log_check_call(['chroot', info.root,
'extlinux-update'])
from bootstrapvz.common.tools import sed_i
extlinux_def = os.path.join(info.root, 'etc/default/extlinux')
sed_i(extlinux_def, r'^EXTLINUX_PARAMETERS="([^"]+)"$',
r'EXTLINUX_PARAMETERS="\1 console=ttyS0"')
class InstallExtLinux(Task):
description = 'Installing extlinux'
phase = phases.system_modification
predecessors = [filesystem.FStab, ConfigureExtlinux]
@classmethod
def run(cls, info):
2014-01-19 01:02:29 +01:00
if isinstance(info.volume.partition_map, partitionmaps.gpt.GPTPartitionMap):
bootloader = '/usr/lib/syslinux/gptmbr.bin'
else:
bootloader = '/usr/lib/extlinux/mbr.bin'
log_check_call(['chroot', info.root,
'dd', 'bs=440', 'count=1',
2014-01-19 01:02:29 +01:00
'if=' + bootloader,
'of=' + info.volume.device_path])
log_check_call(['chroot', info.root,
'extlinux',
2014-01-19 01:02:29 +01:00
'--install', '/boot/extlinux'])
log_check_call(['chroot', info.root,
'extlinux-update'])