2013-07-10 10:49:45 +02:00
|
|
|
from base import Task
|
|
|
|
from common import phases
|
2013-09-15 13:19:45 +02:00
|
|
|
from common.tasks import apt
|
|
|
|
from common.fs.loopbackvolume import LoopbackVolume
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigureGrub(Task):
|
|
|
|
description = 'Configuring grub'
|
|
|
|
phase = phases.system_modification
|
2013-11-21 15:54:42 +01:00
|
|
|
predecessors = [apt.AptUpgrade]
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
def run(self, info):
|
2013-09-15 13:19:45 +02:00
|
|
|
import os
|
2013-08-10 16:18:48 +02:00
|
|
|
from common.tools import log_check_call
|
2013-08-01 11:19:20 +02:00
|
|
|
|
2013-09-15 13:19:45 +02:00
|
|
|
boot_dir = os.path.join(info.root, 'boot')
|
|
|
|
grub_dir = os.path.join(boot_dir, 'grub')
|
|
|
|
|
2013-09-26 22:43:36 +02:00
|
|
|
from base.fs.partitionmaps.none import NoPartitions
|
2013-10-04 21:45:19 +02:00
|
|
|
from base.fs.partitionmaps.gpt import GPTPartitionMap
|
2013-10-06 14:34:03 +02:00
|
|
|
from common.fs import remount
|
2013-10-04 21:45:19 +02:00
|
|
|
p_map = info.volume.partition_map
|
2013-09-25 23:43:02 +02:00
|
|
|
|
2013-10-06 14:34:03 +02:00
|
|
|
def mk_remount_fn(fn):
|
|
|
|
def set_device_path():
|
2013-09-25 23:43:02 +02:00
|
|
|
fn()
|
2013-10-06 14:34:03 +02:00
|
|
|
if isinstance(p_map, NoPartitions):
|
|
|
|
p_map.root.device_path = info.volume.device_path
|
|
|
|
return set_device_path
|
|
|
|
link_fn = mk_remount_fn(info.volume.link_dm_node)
|
|
|
|
unlink_fn = mk_remount_fn(info.volume.unlink_dm_node)
|
2013-09-25 23:43:02 +02:00
|
|
|
|
2013-10-06 14:34:03 +02:00
|
|
|
# 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/
|
2013-09-25 23:43:02 +02:00
|
|
|
if isinstance(info.volume, LoopbackVolume):
|
2013-10-06 14:34:03 +02:00
|
|
|
remount(info.volume, link_fn)
|
2013-09-18 00:46:58 +02:00
|
|
|
try:
|
|
|
|
[device_path] = log_check_call(['readlink', '-f', info.volume.device_path])
|
|
|
|
device_map_path = os.path.join(grub_dir, 'device.map')
|
2013-10-04 21:45:19 +02:00
|
|
|
partition_prefix = 'msdos'
|
|
|
|
if isinstance(p_map, GPTPartitionMap):
|
|
|
|
partition_prefix = 'gpt'
|
2013-09-18 00:46:58 +02:00
|
|
|
with open(device_map_path, 'w') as device_map:
|
|
|
|
device_map.write('(hd0) {device_path}\n'.format(device_path=device_path))
|
2013-10-04 21:45:19 +02:00
|
|
|
if not isinstance(p_map, NoPartitions):
|
|
|
|
for idx, partition in enumerate(info.volume.partition_map.partitions):
|
|
|
|
[partition_path] = log_check_call(['readlink', '-f', partition.device_path])
|
|
|
|
device_map.write('(hd0,{prefix}{idx}) {device_path}\n'
|
|
|
|
.format(device_path=partition_path, prefix=partition_prefix, idx=idx+1))
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2013-09-18 00:46:58 +02:00
|
|
|
# Install grub
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root,
|
|
|
|
'/usr/sbin/grub-install',
|
|
|
|
# '--root-directory=' + info.root,
|
|
|
|
# '--boot-directory=' + boot_dir,
|
|
|
|
device_path])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/usr/sbin/update-grub'])
|
|
|
|
except Exception as e:
|
|
|
|
if isinstance(info.volume, LoopbackVolume):
|
2013-10-06 14:34:03 +02:00
|
|
|
remount(info.volume, unlink_fn)
|
2013-09-18 00:46:58 +02:00
|
|
|
raise e
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
if isinstance(info.volume, LoopbackVolume):
|
2013-10-06 14:34:03 +02:00
|
|
|
remount(info.volume, unlink_fn)
|