2012-03-13 20:27:59 +01:00
#! @bash@/bin/sh -e
shopt -s nullglob
export PATH = /empty
for i in @path@; do PATH = $PATH :$i /bin:$i /sbin; done
default = $1
if test -z " $1 " ; then
echo "Syntax: efi-boot-stub-builder.sh <DEFAULT-CONFIG>"
exit 1
fi
echo "updating the efi system partition..."
# Convert a path to a file in the Nix store such as
# /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
# Also, efi executables need the .efi extension
cleanName( ) {
local path = " $1 "
echo " $path " | sed 's|^/nix/store/||' | sed 's|/|-|g' | sed 's|@kernelFile@$|@kernelFile@.efi|'
}
# Copy a file from the Nix store to the EFI system partition
declare -A filesCopied
copyToKernelsDir( ) {
local src = " $1 "
local dst = " @efiSysMountPoint@/efi/nixos/ $( 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
}
# Copy its kernel, initrd, and startup script to the efi system partition
# Add the efibootmgr entry if requested
addEntry( ) {
local path = " $1 "
local generation = " $2 "
if ! test -e $path /kernel -a -e $path /initrd; then
return
fi
local kernel = $( readlink -f $path /kernel)
local initrd = $( readlink -f $path /initrd)
copyToKernelsDir $kernel ; kernel = $result
copyToKernelsDir $initrd ; initrd = $result
2012-03-21 03:05:52 +01:00
local startup = " @efiSysMountPoint@/efi/nixos/generation- $generation -startup.nsh "
2012-03-13 20:27:59 +01:00
if ! test -e $startup ; then
local dstTmp = $startup .tmp.$$
echo " $( echo $kernel | sed 's|@efiSysMountPoint@||' | sed 's|/|\\|g' ) systemConfig= $( readlink -f $path ) init= $( readlink -f $path /init) initrd= $( echo $initrd | sed 's|@efiSysMountPoint@||' | sed 's|/|\\|g' ) $( cat $path /kernel-params) " > $dstTmp
mv $dstTmp $startup
fi
filesCopied[ $startup ] = 1
if test -n "@runEfibootmgr@" ; then
set +e
efibootmgr -c -d "@efiDisk@" -g -l $( echo $kernel | sed 's|@efiSysMountPoint@||' | sed 's|/|\\|g' ) -L " NixOS $generation Generation " -p "@efiPartition@" \
2012-03-16 14:40:03 +01:00
-u systemConfig = $( readlink -f $path ) init = $( readlink -f $path /init) initrd = $( echo $initrd | sed 's|@efiSysMountPoint@||' | sed 's|/|\\|g' ) $( cat $path /kernel-params) > /dev/null 2>& 1
2012-03-13 20:27:59 +01:00
set -e
fi
if test $( readlink -f " $path " ) = " $default " ; then
if test -n "@runEfibootmgr@" ; then
set +e
defaultbootnum = $( efibootmgr | grep " NixOS $generation Generation " | sed 's/Boot//' | sed 's/\*.*//' )
set -e
fi
2012-03-16 03:34:07 +01:00
2012-03-13 20:27:59 +01:00
if test -n "@installStartupNsh@" ; then
sed 's|.*@kernelFile@.efi|@kernelFile@.efi|' < $startup > "@efiSysMountPoint@/startup.nsh"
cp $kernel "@efiSysMountPoint@/@kernelFile@.efi"
fi
fi
}
mkdir -p "@efiSysMountPoint@/efi/nixos/"
# Remove all old boot manager entries
if test -n "@runEfibootmgr@" ; then
set +e
2012-03-18 19:55:47 +01:00
modprobe efivars > /dev/null 2>& 1
2012-03-13 20:27:59 +01:00
for bootnum in $( efibootmgr | grep "NixOS" | grep "Generation" | sed 's/Boot//' | sed 's/\*.*//' ) ; do
2012-03-16 14:40:03 +01:00
efibootmgr -B -b " $bootnum " > /dev/null 2>& 1
2012-03-13 20:27:59 +01:00
done
2012-03-16 14:40:03 +01:00
set -e
2012-03-13 20:27:59 +01:00
fi
# Add all generations of the system profile to the system partition, in reverse
# (most recent to least recent) order.
for generation in $(
( cd /nix/var/nix/profiles && ls -d system-*-link) \
| sed 's/system-\([0-9]\+\)-link/\1/' \
| sort -n -r) ; do
link = /nix/var/nix/profiles/system-$generation -link
addEntry $link $generation
done
if test -n "@runEfibootmgr@" ; then
set +e
2012-03-16 14:40:03 +01:00
efibootmgr -o $defaultbootnum > /dev/null 2>& 1
2012-03-13 20:27:59 +01:00
set -e
fi
2012-05-05 00:16:35 +02:00
if test -n "@efiShell@" ; then
mkdir -pv "@efiSysMountPoint@" /efi/boot
cp "@efiShell@" "@efiSysMountPoint@" /efi/boot/boot"@targetArch@" .efi
fi
2012-03-13 20:27:59 +01:00
# Remove obsolete files from the EFI system partition
for fn in "@efiSysMountPoint@/efi/nixos/" *; do
if ! test " ${ filesCopied [ $fn ] } " = 1; then
rm -vf -- " $fn "
fi
done