From 3e114313539e0696d861fb62cc5e7525d48ea0f2 Mon Sep 17 00:00:00 2001 From: Olivier Sallou Date: Tue, 13 Aug 2013 08:55:12 +0200 Subject: [PATCH] fix grub/boot --- common/tasks/parted.py | 14 +-- providers/virtualbox/assets/grub.d/00_header | 4 + providers/virtualbox/assets/grub.d/10_linux | 93 ++++++++++++++++++++ providers/virtualbox/tasks/boot.py | 28 ++++-- 4 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 providers/virtualbox/assets/grub.d/00_header create mode 100644 providers/virtualbox/assets/grub.d/10_linux diff --git a/common/tasks/parted.py b/common/tasks/parted.py index c8d1190..caade0a 100644 --- a/common/tasks/parted.py +++ b/common/tasks/parted.py @@ -25,13 +25,9 @@ class MapPartitions(Task): after = [PartitionVolume] def run(self, info): - log_check_call(['kpartx', '-a', '-v', info.bootstrap_device['path']]) - root_partition_path = info.bootstrap_device['path'].replace('/dev', '/dev/mapper')+'p1' - - [root_loopback_path] = log_check_call(['/sbin/losetup', '--find']) - log_check_call(['/sbin/losetup', root_loopback_path, root_partition_path]) - - info.bootstrap_device['partitions'] = {'root_path': root_loopback_path} + root_partition_path = info.bootstrap_device['path'].replace('/dev', '/dev/mapper')+'p1' + log_check_call(['kpartx', '-a', '-v', info.bootstrap_device['path']]) + info.bootstrap_device['partitions'] = {'root_path': root_partition_path} class FormatPartitions(Task): @@ -52,7 +48,5 @@ class UnmapPartitions(Task): after = [filesystem.UnmountVolume] def run(self, info): - log_check_call(['/sbin/losetup', '-d', info.bootstrap_device['partitions']['root_path']]) - del info.bootstrap_device['partitions']['root_path'] - log_check_call(['kpartx', '-d', info.bootstrap_device['path']]) + del info.bootstrap_device['partitions']['root_path'] diff --git a/providers/virtualbox/assets/grub.d/00_header b/providers/virtualbox/assets/grub.d/00_header new file mode 100644 index 0000000..43927bc --- /dev/null +++ b/providers/virtualbox/assets/grub.d/00_header @@ -0,0 +1,4 @@ +#! /bin/sh +set -e + +# nothing to do, skip grub mkconfig for this diff --git a/providers/virtualbox/assets/grub.d/10_linux b/providers/virtualbox/assets/grub.d/10_linux new file mode 100644 index 0000000..7e944d2 --- /dev/null +++ b/providers/virtualbox/assets/grub.d/10_linux @@ -0,0 +1,93 @@ +#!/bin/sh + +# This file generates the old menu.lst configuration with grub2 +# It was copied from tomheadys github repo: +# https://github.com/tomheady/ec2debian/blob/master/src/root/etc/grub.d/40_custom + +prefix=/usr +exec_prefix=${prefix} +bindir=${exec_prefix}/bin +libdir=${exec_prefix}/lib +. ${libdir}/grub/grub-mkconfig_lib + +export TEXTDOMAIN=grub +export TEXTDOMAINDIR=${prefix}/share/locale + +GRUB_DEVICE=/dev/sda1 + + +cat << EOF +set default=${GRUB_DEFAULT} +set timeout=${GRUB_TIMEOUT} +insmod part_msdos +insmod ext2 +insmod gettext +set menu_color_normal=cyan/blue +set menu_color_highlight=white/blue +set root='(hd0,msdos1)' +EOF + +if ${GRUB_HIDDEN_TIMEOUT:-false}; then + printf "hiddenmenu\n" +fi + +linux_entry () +{ + os="$1" + version="$2" + args="$4" + + title="$(gettext_quoted "%s, with Linux %s")" + + cat << EOF +menuentry 'Debian GNU/Linux for OpenNebula, ${version}' --class debian --class gnu-linux --class os { + insmod part_msdos + insmod ext2 + set timeout=${GRUB_TIMEOUT} + set root='(hd0,msdos1)' + echo 'Loading Linux ${version}' + linux ${rel_dirname}/${basename} root=${GRUB_DEVICE} ro ${args} + echo 'Loading initial ramdisk ...' + initrd ${rel_dirname}/${initrd} +} +EOF +} + +list=`for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* ; do + if grub_file_is_not_garbage "$i" ; then echo -n "$i " ; fi + done` +prepare_boot_cache= + +while [ "x$list" != "x" ] ; do + linux=`version_find_latest $list` + basename=`basename $linux` + dirname=`dirname $linux` + rel_dirname=`make_system_path_relative_to_its_root $dirname` + version=`echo $basename | sed -e "s,^[^0-9]*-,,g"` + alt_version=`echo $version | sed -e "s,\.old$,,g"` + linux_root_device_thisversion="${LINUX_ROOT_DEVICE}" + + initrd= + for i in "initrd.img-${version}" "initrd-${version}.img" \ + "initrd-${version}" "initramfs-${version}.img" \ + "initrd.img-${alt_version}" "initrd-${alt_version}.img" \ + "initrd-${alt_version}" "initramfs-${alt_version}.img"; do + if test -e "${dirname}/${i}" ; then + initrd="$i" + break + fi + done + + initramfs= + for i in "config-${version}" "config-${alt_version}"; do + if test -e "${dirname}/${i}" ; then + initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${dirname}/${i}" | cut -f2 -d= | tr -d \"` + break + fi + done + + linux_entry "${OS}" "${version}" \ + "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" + + list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '` +done diff --git a/providers/virtualbox/tasks/boot.py b/providers/virtualbox/tasks/boot.py index fcd25f9..e8abc78 100644 --- a/providers/virtualbox/tasks/boot.py +++ b/providers/virtualbox/tasks/boot.py @@ -7,19 +7,29 @@ class ConfigureGrub(Task): phase = phases.system_modification def run(self, info): + import stat + rwxr_xr_x = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | + stat.S_IRGRP | stat.S_IXGRP | + stat.S_IROTH | stat.S_IXOTH) + x_all = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH import os.path device_map_path = os.path.join(info.root, 'boot/grub/device.map') with open(device_map_path, 'w') as device_map: - device_map.write(('(hd0) {dev_path}\n' - '(hd0,1) {root_path}' - .format(dev_path=info.bootstrap_device['path'], - root_path=info.bootstrap_device['partitions']['root_path']))) + device_map.write('(hd0) /dev/sda\n') from common.tools import log_check_call + + from shutil import copy + script_src = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets/grub.d/10_linux')) + script_dst = os.path.join(info.root, 'etc/grub.d/10_linux') + copy(script_src, script_dst) + os.chmod(script_dst, rwxr_xr_x) + script_src = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets/grub.d/00_header')) + script_dst = os.path.join(info.root, 'etc/grub.d/00_header') + copy(script_src, script_dst) + os.chmod(script_dst, rwxr_xr_x) log_check_call(['/usr/sbin/chroot', info.root, 'update-initramfs', '-u']) + # Install grub in mbr + log_check_call(['grub-install', '--boot-directory='+info.root+"/boot/", info.bootstrap_device['path']]) - log_check_call(['/usr/sbin/chroot', info.root, - '/usr/sbin/grub-mkconfig', '-o', '/boot/grub/grub.cfg']) - - log_check_call(['/usr/sbin/chroot', info.root, - '/usr/sbin/grub-install', info.bootstrap_device['path']]) + log_check_call(['/usr/sbin/chroot', info.root, '/usr/sbin/update-grub'])