2006-11-24 00:22:43 +01:00
|
|
|
#! @bash@/bin/sh -e
|
|
|
|
|
2007-12-25 17:07:55 +01:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2006-12-10 23:29:44 +01:00
|
|
|
export PATH=/empty
|
|
|
|
for i in @path@; do PATH=$PATH:$i/bin; done
|
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
if test $# -ne 1; then
|
|
|
|
echo "Usage: grub-menu-builder.sh DEFAULT-CONFIG"
|
2006-11-24 00:22:43 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
grubVersion="@version@"
|
|
|
|
defaultConfig="$1"
|
2008-01-04 16:02:31 +01:00
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
case "$grubVersion" in
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2009-12-16 19:57:02 +01:00
|
|
|
# Discover whether /boot is on the same filesystem as /. If not, then
|
|
|
|
# all kernels and initrds must be copied to /boot, and all paths in
|
|
|
|
# the GRUB config file must be relative to the root of the /boot
|
|
|
|
# filesystem. `$bootRoot' is the path to be prepended to paths under
|
|
|
|
# /boot.
|
2010-06-20 22:54:31 +02:00
|
|
|
if [ "$(stat -c '%D' /.)" = "$(stat -c '%D' /boot/.)" ]; then
|
2009-12-16 19:57:02 +01:00
|
|
|
bootRoot=/boot
|
|
|
|
copyKernels="@copyKernels@" # user can override in the NixOS config
|
|
|
|
else
|
|
|
|
bootRoot=
|
|
|
|
copyKernels=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
prologue() {
|
|
|
|
case "$grubVersion" in
|
|
|
|
1)
|
2009-12-15 22:11:39 +01:00
|
|
|
cp -f "@splashImage@" /boot/background.xpm.gz
|
2009-10-13 23:39:18 +02:00
|
|
|
cat > "$1" << GRUBEND
|
2006-11-24 00:22:43 +01:00
|
|
|
# Automatically generated. DO NOT EDIT THIS FILE!
|
2009-12-11 01:51:07 +01:00
|
|
|
default @default@
|
|
|
|
timeout @timeout@
|
2006-11-24 00:22:43 +01:00
|
|
|
GRUBEND
|
2009-10-13 23:39:18 +02:00
|
|
|
if test -n "@splashImage@"; then
|
2009-12-16 19:57:02 +01:00
|
|
|
echo "splashimage $bootRoot/background.xpm.gz" >> "$1"
|
2009-10-13 23:39:18 +02:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
2)
|
2009-12-15 22:11:39 +01:00
|
|
|
cp -f @grub@/share/grub/unicode.pf2 /boot/grub/unicode.pf2
|
2009-10-13 23:39:18 +02:00
|
|
|
cat > "$1" <<EOF
|
|
|
|
# Automatically generated. DO NOT EDIT THIS FILE!
|
2009-12-11 01:51:07 +01:00
|
|
|
set default=@default@
|
|
|
|
set timeout=@timeout@
|
2009-12-16 19:57:02 +01:00
|
|
|
if loadfont $bootRoot/grub/unicode.pf2; then
|
2009-10-13 23:39:18 +02:00
|
|
|
set gfxmode=640x480
|
|
|
|
insmod gfxterm
|
|
|
|
insmod vbe
|
|
|
|
terminal_output gfxterm
|
|
|
|
fi
|
|
|
|
EOF
|
|
|
|
if test -n "@splashImage@"; then
|
2009-12-15 22:11:39 +01:00
|
|
|
cp -f "@splashImage@" /boot/background.png
|
2009-10-13 23:39:18 +02:00
|
|
|
# FIXME: GRUB 1.97 doesn't resize the background image
|
|
|
|
# if it doesn't match the video resolution.
|
|
|
|
cat >> "$1" <<EOF
|
|
|
|
insmod png
|
2009-12-16 19:57:02 +01:00
|
|
|
if background_image $bootRoot/background.png; then
|
2009-12-15 22:11:39 +01:00
|
|
|
set color_normal=white/black
|
|
|
|
set color_highlight=black/white
|
2009-10-13 23:39:18 +02:00
|
|
|
else
|
|
|
|
set menu_color_normal=cyan/blue
|
|
|
|
set menu_color_highlight=white/blue
|
|
|
|
fi
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2006-11-24 00:22:43 +01:00
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
case "$grubVersion" in
|
|
|
|
1) target="/boot/grub/menu.lst";;
|
|
|
|
2) target="/boot/grub/grub.cfg";;
|
|
|
|
esac
|
2007-02-09 17:19:29 +01:00
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
tmp="$target.tmp"
|
|
|
|
|
|
|
|
prologue "$tmp"
|
2008-01-04 16:02:31 +01:00
|
|
|
|
2009-05-18 14:01:31 +02:00
|
|
|
|
2008-01-10 13:28:01 +01:00
|
|
|
configurationCounter=0
|
|
|
|
configurationLimit="@configurationLimit@"
|
2009-04-28 15:30:31 +02:00
|
|
|
numAlienEntries=`cat <<EOF | egrep '^[[:space:]]*title' | wc -l
|
2009-09-29 11:50:38 +02:00
|
|
|
@extraEntries@
|
2009-04-28 15:30:31 +02:00
|
|
|
EOF`
|
2008-01-10 13:28:01 +01:00
|
|
|
|
|
|
|
if test $((configurationLimit+numAlienEntries)) -gt 190; then
|
2009-05-18 14:01:31 +02:00
|
|
|
configurationLimit=$((190-numAlienEntries));
|
2008-01-10 13:28:01 +01:00
|
|
|
fi
|
2007-02-09 17:19:29 +01:00
|
|
|
|
2009-05-18 14:01:31 +02:00
|
|
|
|
|
|
|
# Convert a path to a file in the Nix store such as
|
|
|
|
# /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
|
|
|
|
cleanName() {
|
|
|
|
local path="$1"
|
|
|
|
echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Copy a file from the Nix store to /boot/kernels.
|
|
|
|
declare -A filesCopied
|
|
|
|
|
|
|
|
copyToKernelsDir() {
|
|
|
|
local src="$1"
|
2009-12-16 19:57:02 +01:00
|
|
|
local p="kernels/$(cleanName $src)"
|
|
|
|
local dst="/boot/$p"
|
2009-05-18 14:01:31 +02:00
|
|
|
# Don't copy the file if $dst already exists. This means that we
|
|
|
|
# have to create $dst atomically to prevent partially copied
|
|
|
|
# kernels or initrd if this script is ever interrupted.
|
|
|
|
if ! test -e $dst; then
|
|
|
|
local dstTmp=$dst.tmp.$$
|
|
|
|
cp $src $dstTmp
|
|
|
|
mv $dstTmp $dst
|
|
|
|
fi
|
|
|
|
filesCopied[$dst]=1
|
2009-12-16 19:57:02 +01:00
|
|
|
result="$bootRoot/$p"
|
2009-05-18 14:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Add an entry for a configuration to the Grub menu, and if
|
|
|
|
# appropriate, copy its kernel and initrd to /boot/kernels.
|
2006-11-24 00:22:43 +01:00
|
|
|
addEntry() {
|
2007-02-09 17:19:29 +01:00
|
|
|
local name="$1"
|
|
|
|
local path="$2"
|
2007-06-23 18:09:05 +02:00
|
|
|
local shortSuffix="$3"
|
2006-11-24 00:22:43 +01:00
|
|
|
|
2008-01-10 05:43:19 +01:00
|
|
|
configurationCounter=$((configurationCounter + 1))
|
2009-05-18 14:01:31 +02:00
|
|
|
if test $configurationCounter -gt @configurationLimit@; then
|
|
|
|
return
|
|
|
|
fi
|
2008-01-10 05:43:19 +01:00
|
|
|
|
2007-02-09 17:19:29 +01:00
|
|
|
if ! test -e $path/kernel -a -e $path/initrd; then
|
2006-12-11 18:36:57 +01:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2007-02-09 17:19:29 +01:00
|
|
|
local kernel=$(readlink -f $path/kernel)
|
|
|
|
local initrd=$(readlink -f $path/initrd)
|
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
if test "$path" = "$defaultConfig"; then
|
2009-05-18 14:01:31 +02:00
|
|
|
cp "$kernel" /boot/nixos-kernel
|
|
|
|
cp "$initrd" /boot/nixos-initrd
|
|
|
|
cp "$(readlink -f "$path/init")" /boot/nixos-init
|
2009-10-13 23:39:18 +02:00
|
|
|
case "$grubVersion" in
|
|
|
|
1)
|
|
|
|
cat > /boot/nixos-grub-config <<EOF
|
2010-07-22 16:40:29 +02:00
|
|
|
title Emergency boot
|
|
|
|
kernel $bootRoot/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params")
|
|
|
|
initrd $bootRoot/nixos-initrd
|
2008-01-10 05:43:19 +01:00
|
|
|
EOF
|
2009-10-13 23:39:18 +02:00
|
|
|
;;
|
|
|
|
2)
|
|
|
|
cat > /boot/nixos-grub-config <<EOF
|
2010-07-22 16:40:29 +02:00
|
|
|
menuentry "Emergency boot" {
|
|
|
|
linux $bootRoot/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params")
|
|
|
|
initrd $bootRoot/initrd
|
|
|
|
}
|
2009-10-13 23:39:18 +02:00
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
esac
|
2008-01-10 05:43:19 +01:00
|
|
|
fi
|
|
|
|
|
2009-12-16 19:57:02 +01:00
|
|
|
if test -n "$copyKernels"; then
|
2009-05-18 14:01:31 +02:00
|
|
|
copyToKernelsDir $kernel; kernel=$result
|
|
|
|
copyToKernelsDir $initrd; initrd=$result
|
|
|
|
fi
|
|
|
|
|
2009-12-16 19:58:34 +01:00
|
|
|
local confName=$(cat $path/configuration-name 2>/dev/null || true)
|
2009-05-18 14:01:31 +02:00
|
|
|
if test -n "$confName"; then
|
|
|
|
name="$confName $3"
|
|
|
|
fi
|
2007-06-23 18:09:05 +02:00
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
case "$grubVersion" in
|
|
|
|
1)
|
|
|
|
cat >> "$tmp" << GRUBEND
|
2006-11-24 00:22:43 +01:00
|
|
|
title $name
|
2010-07-22 16:40:29 +02:00
|
|
|
@extraPerEntryConfig@
|
2007-03-20 14:01:53 +01:00
|
|
|
kernel $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params)
|
2007-02-09 17:19:29 +01:00
|
|
|
initrd $initrd
|
2006-11-24 00:22:43 +01:00
|
|
|
GRUBEND
|
2009-10-13 23:39:18 +02:00
|
|
|
;;
|
|
|
|
2)
|
|
|
|
cat >> "$tmp" << GRUBEND
|
|
|
|
menuentry "$name" {
|
2010-07-22 16:40:29 +02:00
|
|
|
@extraPerEntryConfig@
|
2009-10-13 23:39:18 +02:00
|
|
|
linux $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params)
|
|
|
|
initrd $initrd
|
|
|
|
}
|
|
|
|
GRUBEND
|
|
|
|
;;
|
|
|
|
esac
|
2007-02-09 17:19:29 +01:00
|
|
|
}
|
2006-11-24 00:22:43 +01:00
|
|
|
|
|
|
|
|
2009-12-16 19:57:02 +01:00
|
|
|
if test -n "$copyKernels"; then
|
2007-02-09 17:19:29 +01:00
|
|
|
mkdir -p /boot/kernels
|
|
|
|
fi
|
2006-11-24 00:22:43 +01:00
|
|
|
|
2009-05-18 14:01:31 +02:00
|
|
|
|
2007-06-15 13:40:57 +02:00
|
|
|
# Additional entries specified verbatim by the configuration.
|
2009-09-29 11:50:38 +02:00
|
|
|
extraEntries=`cat <<EOF
|
|
|
|
@extraEntries@
|
2009-04-28 15:30:31 +02:00
|
|
|
EOF`
|
|
|
|
|
2007-06-15 13:40:57 +02:00
|
|
|
|
2010-06-17 00:18:26 +02:00
|
|
|
cat >> $tmp <<EOF
|
|
|
|
@extraConfig@
|
|
|
|
EOF
|
|
|
|
|
2009-09-29 11:50:38 +02:00
|
|
|
if test -n "@extraEntriesBeforeNixOS@"; then
|
|
|
|
echo "$extraEntries" >> $tmp
|
2008-07-05 02:16:39 +02:00
|
|
|
fi
|
|
|
|
|
2009-10-13 23:39:18 +02:00
|
|
|
addEntry "NixOS - Default" $defaultConfig ""
|
2007-06-15 13:40:57 +02:00
|
|
|
|
2009-09-29 11:50:38 +02:00
|
|
|
if test -z "@extraEntriesBeforeNixOS@"; then
|
|
|
|
echo "$extraEntries" >> $tmp
|
2008-07-05 02:16:39 +02:00
|
|
|
fi
|
2007-06-15 13:40:57 +02:00
|
|
|
|
2006-11-24 00:22:43 +01:00
|
|
|
# Add all generations of the system profile to the menu, in reverse
|
|
|
|
# (most recent to least recent) order.
|
2009-10-13 23:39:18 +02:00
|
|
|
for link in $((ls -d $defaultConfig/fine-tune/* ) | sort -n); do
|
2007-12-20 12:36:30 +01:00
|
|
|
date=$(stat --printf="%y\n" $link | sed 's/\..*//')
|
|
|
|
addEntry "NixOS - variation" $link ""
|
|
|
|
done
|
2009-05-18 14:01:31 +02:00
|
|
|
|
2006-11-24 00:22:43 +01:00
|
|
|
for generation in $(
|
|
|
|
(cd /nix/var/nix/profiles && ls -d system-*-link) \
|
|
|
|
| sed 's/system-\([0-9]\+\)-link/\1/' \
|
|
|
|
| sort -n -r); do
|
2006-11-24 01:00:32 +01:00
|
|
|
link=/nix/var/nix/profiles/system-$generation-link
|
|
|
|
date=$(stat --printf="%y\n" $link | sed 's/\..*//')
|
2008-10-29 14:24:00 +01:00
|
|
|
kernelVersion=$(cd $(dirname $(readlink -f $link/kernel))/lib/modules && echo *)
|
|
|
|
addEntry "NixOS - Configuration $generation ($date - $kernelVersion)" $link "$generation ($date)"
|
2006-11-24 00:22:43 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
|
2009-12-15 22:11:39 +01:00
|
|
|
# Atomically update the GRUB configuration file.
|
2009-05-15 08:43:02 +02:00
|
|
|
mv $tmp $target
|
2009-05-18 14:01:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Remove obsolete files from /boot/kernels.
|
2009-05-18 14:54:54 +02:00
|
|
|
for fn in /boot/kernels/*; do
|
2009-05-18 14:01:31 +02:00
|
|
|
if ! test "${filesCopied[$fn]}" = 1; then
|
|
|
|
rm -vf -- "$fn"
|
|
|
|
fi
|
|
|
|
done
|