2006-11-23 23:22:43 +00:00
|
|
|
#! @bash@/bin/sh -e
|
|
|
|
|
2007-12-25 16:07:55 +00:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2006-12-10 22:29:44 +00:00
|
|
|
export PATH=/empty
|
|
|
|
for i in @path@; do PATH=$PATH:$i/bin; done
|
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
default=$1
|
|
|
|
if test -z "$1"; then
|
|
|
|
echo "Syntax: grub-menu-builder.sh <DEFAULT-CONFIG>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2008-01-09 05:36:40 +00:00
|
|
|
bootMount="@bootMount@"
|
2008-01-04 15:02:31 +00:00
|
|
|
if test -z "$bootMount"; then bootMount=/boot; fi
|
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
|
2009-05-18 13:54:54 +01:00
|
|
|
echo "updating the GRUB menu..."
|
2009-05-18 13:01:31 +01:00
|
|
|
|
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
target=/boot/grub/menu.lst
|
|
|
|
tmp=$target.tmp
|
|
|
|
|
|
|
|
cat > $tmp << GRUBEND
|
|
|
|
# Automatically generated. DO NOT EDIT THIS FILE!
|
2008-01-04 15:02:31 +00:00
|
|
|
default 0
|
|
|
|
timeout 5
|
2006-11-23 23:22:43 +00:00
|
|
|
GRUBEND
|
|
|
|
|
2007-02-09 16:19:29 +00:00
|
|
|
|
2008-01-04 15:02:31 +00:00
|
|
|
if test -n "@grubSplashImage@"; then
|
|
|
|
splashLocation=@grubSplashImage@
|
2009-05-18 13:01:31 +01:00
|
|
|
# Splash images in /nix/store don't seem to work, so copy them.
|
|
|
|
cp -f $splashLocation /boot/background.xpm.gz
|
|
|
|
splashLocation="$bootMount/background.xpm.gz"
|
2008-01-04 15:02:31 +00:00
|
|
|
echo "splashimage $splashLocation" >> $tmp
|
|
|
|
fi
|
|
|
|
|
2009-05-18 13:01:31 +01:00
|
|
|
|
2008-01-10 12:28:01 +00:00
|
|
|
configurationCounter=0
|
|
|
|
configurationLimit="@configurationLimit@"
|
2009-04-28 14:30:31 +01:00
|
|
|
numAlienEntries=`cat <<EOF | egrep '^[[:space:]]*title' | wc -l
|
2008-01-10 12:28:01 +00:00
|
|
|
@extraGrubEntries@
|
2009-04-28 14:30:31 +01:00
|
|
|
EOF`
|
2008-01-10 12:28:01 +00:00
|
|
|
|
|
|
|
if test $((configurationLimit+numAlienEntries)) -gt 190; then
|
2009-05-18 13:01:31 +01:00
|
|
|
configurationLimit=$((190-numAlienEntries));
|
2008-01-10 12:28:01 +00:00
|
|
|
fi
|
2007-02-09 16:19:29 +00:00
|
|
|
|
2009-05-18 13:01:31 +01: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"
|
|
|
|
local dst="/boot/kernels/$(cleanName $src)"
|
|
|
|
# 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
|
|
|
|
result=$dst
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Add an entry for a configuration to the Grub menu, and if
|
|
|
|
# appropriate, copy its kernel and initrd to /boot/kernels.
|
2006-11-23 23:22:43 +00:00
|
|
|
addEntry() {
|
2007-02-09 16:19:29 +00:00
|
|
|
local name="$1"
|
|
|
|
local path="$2"
|
2007-06-23 17:09:05 +01:00
|
|
|
local shortSuffix="$3"
|
2006-11-23 23:22:43 +00:00
|
|
|
|
2008-01-10 04:43:19 +00:00
|
|
|
configurationCounter=$((configurationCounter + 1))
|
2009-05-18 13:01:31 +01:00
|
|
|
if test $configurationCounter -gt @configurationLimit@; then
|
|
|
|
return
|
|
|
|
fi
|
2008-01-10 04:43:19 +00:00
|
|
|
|
2007-02-09 16:19:29 +00:00
|
|
|
if ! test -e $path/kernel -a -e $path/initrd; then
|
2006-12-11 17:36:57 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2007-02-09 16:19:29 +00:00
|
|
|
local kernel=$(readlink -f $path/kernel)
|
|
|
|
local initrd=$(readlink -f $path/initrd)
|
|
|
|
|
2008-01-10 04:43:19 +00:00
|
|
|
if test "$path" = "$default"; then
|
2009-05-18 13:01:31 +01:00
|
|
|
cp "$kernel" /boot/nixos-kernel
|
|
|
|
cp "$initrd" /boot/nixos-initrd
|
|
|
|
cp "$(readlink -f "$path/init")" /boot/nixos-init
|
|
|
|
cat > /boot/nixos-grub-config <<EOF
|
2008-01-10 05:50:58 +00:00
|
|
|
title Emergency boot
|
|
|
|
kernel ${bootMount:-/boot}/nixos-kernel systemConfig=$(readlink -f "$path") init=/boot/nixos-init $(cat "$path/kernel-params")
|
2008-01-10 04:43:19 +00:00
|
|
|
initrd ${bootMount:-/boot}/nixos-initrd
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
2007-02-09 16:19:29 +00:00
|
|
|
if test -n "@copyKernels@"; then
|
2009-05-18 13:01:31 +01:00
|
|
|
copyToKernelsDir $kernel; kernel=$result
|
|
|
|
copyToKernelsDir $initrd; initrd=$result
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -n "$bootMount"; then
|
|
|
|
kernel=$(echo $kernel | sed -e "s^/boot^$bootMount^")
|
|
|
|
initrd=$(echo $initrd | sed -e "s^/boot^$bootMount^")
|
2007-02-09 16:19:29 +00:00
|
|
|
fi
|
|
|
|
|
2007-06-23 17:09:05 +01:00
|
|
|
local confName=$(if test -e $path/configuration-name; then
|
|
|
|
cat $path/configuration-name;
|
2009-05-18 13:01:31 +01:00
|
|
|
fi)
|
|
|
|
if test -n "$confName"; then
|
|
|
|
name="$confName $3"
|
|
|
|
fi
|
2007-06-23 17:09:05 +01:00
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
cat >> $tmp << GRUBEND
|
2008-01-04 15:02:31 +00:00
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
title $name
|
2007-03-20 13:01:53 +00:00
|
|
|
kernel $kernel systemConfig=$(readlink -f $path) init=$(readlink -f $path/init) $(cat $path/kernel-params)
|
2007-02-09 16:19:29 +00:00
|
|
|
initrd $initrd
|
2006-11-23 23:22:43 +00:00
|
|
|
GRUBEND
|
2007-02-09 16:19:29 +00:00
|
|
|
}
|
2006-11-23 23:22:43 +00:00
|
|
|
|
|
|
|
|
2007-02-09 16:19:29 +00:00
|
|
|
if test -n "@copyKernels@"; then
|
|
|
|
mkdir -p /boot/kernels
|
|
|
|
fi
|
2006-11-23 23:22:43 +00:00
|
|
|
|
2009-05-18 13:01:31 +01:00
|
|
|
|
2007-06-15 12:40:57 +01:00
|
|
|
# Additional entries specified verbatim by the configuration.
|
2009-04-28 14:30:31 +01:00
|
|
|
extraGrubEntries=`cat <<EOF
|
2007-06-15 12:40:57 +01:00
|
|
|
@extraGrubEntries@
|
2009-04-28 14:30:31 +01:00
|
|
|
EOF`
|
|
|
|
|
2007-06-15 12:40:57 +01:00
|
|
|
|
2008-07-05 01:16:39 +01:00
|
|
|
if test -n "@extraGrubEntriesBeforeNixos@"; then
|
2008-07-23 10:10:19 +01:00
|
|
|
echo "$extraGrubEntries" >> $tmp
|
2008-07-05 01:16:39 +01:00
|
|
|
fi
|
|
|
|
|
2008-07-23 10:10:19 +01:00
|
|
|
addEntry "NixOS - Default" $default ""
|
2007-06-15 12:40:57 +01:00
|
|
|
|
2008-07-23 10:10:19 +01:00
|
|
|
if test -z "@extraGrubEntriesBeforeNixos@"; then
|
|
|
|
echo "$extraGrubEntries" >> $tmp
|
2008-07-05 01:16:39 +01:00
|
|
|
fi
|
2007-06-15 12:40:57 +01:00
|
|
|
|
2006-11-23 23:22:43 +00:00
|
|
|
# Add all generations of the system profile to the menu, in reverse
|
|
|
|
# (most recent to least recent) order.
|
2007-12-25 16:07:55 +00:00
|
|
|
for link in $((ls -d $default/fine-tune/* ) | sort -n); do
|
2007-12-20 11:36:30 +00:00
|
|
|
date=$(stat --printf="%y\n" $link | sed 's/\..*//')
|
|
|
|
addEntry "NixOS - variation" $link ""
|
|
|
|
done
|
2009-05-18 13:01:31 +01:00
|
|
|
|
2006-11-23 23:22:43 +00: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 00:00:32 +00:00
|
|
|
link=/nix/var/nix/profiles/system-$generation-link
|
|
|
|
date=$(stat --printf="%y\n" $link | sed 's/\..*//')
|
2008-10-29 13:24:00 +00:00
|
|
|
kernelVersion=$(cd $(dirname $(readlink -f $link/kernel))/lib/modules && echo *)
|
|
|
|
addEntry "NixOS - Configuration $generation ($date - $kernelVersion)" $link "$generation ($date)"
|
2006-11-23 23:22:43 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
|
2009-05-15 07:43:02 +01:00
|
|
|
# Atomically update /boot/grub/menu.lst. !!! should do an fsync()
|
|
|
|
# here on $tmp, especially on ext4.
|
|
|
|
mv $tmp $target
|
2009-05-18 13:01:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Remove obsolete files from /boot/kernels.
|
2009-05-18 13:54:54 +01:00
|
|
|
for fn in /boot/kernels/*; do
|
2009-05-18 13:01:31 +01:00
|
|
|
if ! test "${filesCopied[$fn]}" = 1; then
|
|
|
|
rm -vf -- "$fn"
|
|
|
|
fi
|
|
|
|
done
|