Add GRUB 2 support.

svn path=/nixos/trunk/; revision=17792
This commit is contained in:
Ludovic Courtès 2009-10-13 21:39:18 +00:00
parent ccd2a0b617
commit f9541e0d12
4 changed files with 137 additions and 31 deletions

View File

@ -5,36 +5,87 @@ shopt -s nullglob
export PATH=/empty export PATH=/empty
for i in @path@; do PATH=$PATH:$i/bin; done for i in @path@; do PATH=$PATH:$i/bin; done
default=$1 if test $# -ne 1; then
if test -z "$1"; then echo "Usage: grub-menu-builder.sh DEFAULT-CONFIG"
echo "Syntax: grub-menu-builder.sh <DEFAULT-CONFIG>"
exit 1 exit 1
fi fi
grubVersion="@version@"
defaultConfig="$1"
bootDevice="@bootDevice@" bootDevice="@bootDevice@"
if test -z "$bootDevice"; then bootDevice=/boot; fi if test -z "$bootDevice"; then bootDevice=/boot; fi
case "$grubVersion" in
echo "updating the GRUB menu..." 1|2)
echo "updating GRUB $grubVersion menu..."
;;
*)
echo "Unsupported GRUB version \`$grubVersion'" >&2
echo "Supported versions are \`1' (GRUB Legacy) and \`2' (GRUB 1.9x)." >&2
exit 1
;;
esac
target=/boot/grub/menu.lst prologue() {
tmp=$target.tmp if test -n "@splashImage@"; then
splashLocation="@splashImage@"
if test "$grubVersion" -eq 1 ; then
# Splash images in /nix/store don't seem to work, so copy them.
cp -f $splashLocation /boot/background.xpm.gz
splashLocation="$bootDevice/background.xpm.gz"
fi
fi
cat > $tmp << GRUBEND case "$grubVersion" in
1)
cat > "$1" << GRUBEND
# Automatically generated. DO NOT EDIT THIS FILE! # Automatically generated. DO NOT EDIT THIS FILE!
default 0 default 0
timeout 5 timeout 5
GRUBEND GRUBEND
if test -n "@splashImage@"; then
echo "splashimage $splashLocation" >> "$1"
if test -n "@splashImage@"; then fi
splashLocation=@splashImage@ ;;
# Splash images in /nix/store don't seem to work, so copy them. 2)
cp -f $splashLocation /boot/background.xpm.gz cat > "$1" <<EOF
splashLocation="$bootDevice/background.xpm.gz" # Automatically generated. DO NOT EDIT THIS FILE!
echo "splashimage $splashLocation" >> $tmp set default=0
set timeout=5
if loadfont @grub@/share/grub/unicode.pf2 ; then
set gfxmode=640x480
insmod gfxterm
insmod vbe
terminal_output gfxterm
fi fi
EOF
if test -n "@splashImage@"; then
# FIXME: GRUB 1.97 doesn't resize the background image
# if it doesn't match the video resolution.
cat >> "$1" <<EOF
insmod png
if background_image $splashLocation ; then
set color_normal=black/black
set color_highlight=magenta/black
else
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue
fi
EOF
fi
;;
esac
}
case "$grubVersion" in
1) target="/boot/grub/menu.lst";;
2) target="/boot/grub/grub.cfg";;
esac
tmp="$target.tmp"
prologue "$tmp"
configurationCounter=0 configurationCounter=0
@ -94,15 +145,27 @@ addEntry() {
local kernel=$(readlink -f $path/kernel) local kernel=$(readlink -f $path/kernel)
local initrd=$(readlink -f $path/initrd) local initrd=$(readlink -f $path/initrd)
if test "$path" = "$default"; then if test "$path" = "$defaultConfig"; then
cp "$kernel" /boot/nixos-kernel cp "$kernel" /boot/nixos-kernel
cp "$initrd" /boot/nixos-initrd cp "$initrd" /boot/nixos-initrd
cp "$(readlink -f "$path/init")" /boot/nixos-init cp "$(readlink -f "$path/init")" /boot/nixos-init
cat > /boot/nixos-grub-config <<EOF case "$grubVersion" in
1)
cat > /boot/nixos-grub-config <<EOF
title Emergency boot title Emergency boot
kernel ${bootDevice:-/boot}/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params") kernel ${bootDevice:-/boot}/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params")
initrd ${bootDevice:-/boot}/nixos-initrd initrd ${bootDevice:-/boot}/nixos-initrd
EOF EOF
;;
2)
cat > /boot/nixos-grub-config <<EOF
menuentry "Emergency boot" {
linux ${bootDevice:-/boot}/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params")
initrd ${bootDevice:-/boot}/initrd
}
EOF
;;
esac
fi fi
if test -n "@copyKernels@"; then if test -n "@copyKernels@"; then
@ -122,12 +185,23 @@ EOF
name="$confName $3" name="$confName $3"
fi fi
cat >> $tmp << GRUBEND case "$grubVersion" in
1)
cat >> "$tmp" << GRUBEND
title $name title $name
kernel $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params) kernel $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params)
initrd $initrd initrd $initrd
GRUBEND GRUBEND
;;
2)
cat >> "$tmp" << GRUBEND
menuentry "$name" {
linux $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params)
initrd $initrd
}
GRUBEND
;;
esac
} }
@ -146,7 +220,7 @@ if test -n "@extraEntriesBeforeNixOS@"; then
echo "$extraEntries" >> $tmp echo "$extraEntries" >> $tmp
fi fi
addEntry "NixOS - Default" $default "" addEntry "NixOS - Default" $defaultConfig ""
if test -z "@extraEntriesBeforeNixOS@"; then if test -z "@extraEntriesBeforeNixOS@"; then
echo "$extraEntries" >> $tmp echo "$extraEntries" >> $tmp
@ -154,7 +228,7 @@ fi
# Add all generations of the system profile to the menu, in reverse # Add all generations of the system profile to the menu, in reverse
# (most recent to least recent) order. # (most recent to least recent) order.
for link in $((ls -d $default/fine-tune/* ) | sort -n); do for link in $((ls -d $defaultConfig/fine-tune/* ) | sort -n); do
date=$(stat --printf="%y\n" $link | sed 's/\..*//') date=$(stat --printf="%y\n" $link | sed 's/\..*//')
addEntry "NixOS - variation" $link "" addEntry "NixOS - variation" $link ""
done done

View File

@ -7,10 +7,13 @@ let
grubMenuBuilder = pkgs.substituteAll { grubMenuBuilder = pkgs.substituteAll {
src = ./grub-menu-builder.sh; src = ./grub-menu-builder.sh;
isExecutable = true; isExecutable = true;
grub = if config.boot.loader.grub.version == 1
then pkgs.grub
else pkgs.grub2;
inherit (pkgs) bash; inherit (pkgs) bash;
path = [pkgs.coreutils pkgs.gnused pkgs.gnugrep]; path = [pkgs.coreutils pkgs.gnused pkgs.gnugrep];
inherit (config.boot.loader.grub) copyKernels extraEntries extraEntriesBeforeNixOS inherit (config.boot.loader.grub) copyKernels extraEntries extraEntriesBeforeNixOS
splashImage bootDevice configurationLimit; splashImage bootDevice configurationLimit version;
}; };
in in
@ -30,6 +33,15 @@ in
''; '';
}; };
version = mkOption {
default = 1;
example = 2;
description = ''
The version of GRUB to use: <literal>1</literal> for GRUB Legacy
(versions 0.9x), or <literal>2</literal> for GRUB 2.
'';
};
device = mkOption { device = mkOption {
default = ""; default = "";
example = "/dev/hda"; example = "/dev/hda";
@ -78,10 +90,17 @@ in
}; };
splashImage = mkOption { splashImage = mkOption {
default = pkgs.fetchurl { default =
url = http://www.gnome-look.org/CONTENT/content-files/36909-soft-tux.xpm.gz; if config.boot.loader.grub.version == 1
sha256 = "14kqdx2lfqvh40h6fjjzqgff1mwk74dmbjvmqphi6azzra7z8d59"; then pkgs.fetchurl {
}; url = http://www.gnome-look.org/CONTENT/content-files/36909-soft-tux.xpm.gz;
sha256 = "14kqdx2lfqvh40h6fjjzqgff1mwk74dmbjvmqphi6azzra7z8d59";
}
# GRUB 1.97 doesn't support gzipped XPMs.
else pkgs.fetchurl {
url = http://www.gnu.org/graphics/winkler-gnu-blue.png;
sha256 = "0y8fvxalwxyid4k438k7c21bnv728qjsb92rqfapsmpv2bcj7f6k";
};
example = null; example = null;
description = " description = "
Background image used for Grub. It must be a 640x480, Background image used for Grub. It must be a 640x480,
@ -124,9 +143,16 @@ in
# set at once. # set at once.
system.boot.loader.id = "grub"; system.boot.loader.id = "grub";
system.boot.loader.kernelFile = "vmlinuz"; system.boot.loader.kernelFile = "vmlinuz";
environment.systemPackages = mkIf config.boot.loader.grub.enable [ pkgs.grub ]; environment.systemPackages =
mkIf config.boot.loader.grub.enable
(let version = config.boot.loader.grub.version; in
assert version != 1 -> version == 2;
if version == 1
then [ pkgs.grub ]
else [ pkgs.grub2 ]);
# and many other things that have to be moved inside this file. # and many other things that have to be moved inside this file.
}; };

View File

@ -27,6 +27,7 @@ if test "$action" = "switch" -o "$action" = "boot"; then
mkdir -m 0700 -p /boot/grub mkdir -m 0700 -p /boot/grub
@menuBuilder@ @out@ @menuBuilder@ @out@
if test "$NIXOS_INSTALL_GRUB" = 1; then if test "$NIXOS_INSTALL_GRUB" = 1; then
echo "running \`@grub@/sbin/grub-install'..."
@grub@/sbin/grub-install "@grubDevice@" --no-floppy --recheck @grub@/sbin/grub-install "@grubDevice@" --no-floppy --recheck
fi fi
else else

View File

@ -110,7 +110,12 @@ let
# Boot loaders # Boot loaders
bootLoader = config.system.boot.loader.id; bootLoader = config.system.boot.loader.id;
grub = if config.boot.loader.grub.enable then pkgs.grub else null; grub =
if config.boot.loader.grub.enable
then (if config.boot.loader.grub.version == 1
then pkgs.grub
else pkgs.grub2)
else null;
grubDevice = config.boot.loader.grub.device; grubDevice = config.boot.loader.grub.device;
configurationName = config.boot.loader.grub.configurationName; configurationName = config.boot.loader.grub.configurationName;
}; };