1#! @bash@/bin/sh -e
2
3shopt -s nullglob
4
5export PATH=/empty
6for i in @path@; do PATH=$PATH:$i/bin; done
7
8default=$1
9if test -z "$1"; then
10 echo "Syntax: builder.sh <DEFAULT-CONFIG>"
11 exit 1
12fi
13
14echo "updating the boot generations directory..."
15
16mkdir -p /boot/old
17
18# Convert a path to a file in the Nix store such as
19# /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
20cleanName() {
21 local path="$1"
22 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
23}
24
25# Copy a file from the Nix store to /boot/kernels.
26declare -A filesCopied
27
28copyToKernelsDir() {
29 local src="$1"
30 local dst="/boot/old/$(cleanName $src)"
31 # Don't copy the file if $dst already exists. This means that we
32 # have to create $dst atomically to prevent partially copied
33 # kernels or initrd if this script is ever interrupted.
34 if ! test -e $dst; then
35 local dstTmp=$dst.tmp.$$
36 cp $src $dstTmp
37 mv $dstTmp $dst
38 fi
39 filesCopied[$dst]=1
40 result=$dst
41}
42
43copyForced() {
44 local src="$1"
45 local dst="$2"
46 cp $src $dst.tmp
47 mv $dst.tmp $dst
48}
49
50outdir=/boot/old
51mkdir -p $outdir || true
52
53# Copy its kernel and initrd to /boot/kernels.
54addEntry() {
55 local path="$1"
56 local generation="$2"
57
58 if ! test -e $path/kernel -a -e $path/initrd; then
59 return
60 fi
61
62 local kernel=$(readlink -f $path/kernel)
63 local initrd=$(readlink -f $path/initrd)
64
65 if test -n "@copyKernels@"; then
66 copyToKernelsDir $kernel; kernel=$result
67 copyToKernelsDir $initrd; initrd=$result
68 fi
69
70 echo $(readlink -f $path) > $outdir/$generation-system
71 echo $(readlink -f $path/init) > $outdir/$generation-init
72 cp $path/kernel-params $outdir/$generation-cmdline.txt
73 echo $initrd > $outdir/$generation-initrd
74 echo $kernel > $outdir/$generation-kernel
75
76 if test $(readlink -f "$path") = "$default"; then
77 if [ @version@ -eq 1 ]; then
78 copyForced $kernel /boot/kernel.img
79 else
80 copyForced $kernel /boot/kernel7.img
81 fi
82 copyForced $initrd /boot/initrd
83 cp "$(readlink -f "$path/init")" /boot/nixos-init
84 echo "`cat $path/kernel-params` init=$path/init" >/boot/cmdline.txt
85
86 echo "$2" > /boot/defaultgeneration
87 fi
88}
89
90# Add all generations of the system profile to the menu, in reverse
91# (most recent to least recent) order.
92for generation in $(
93 (cd /nix/var/nix/profiles && ls -d system-*-link) \
94 | sed 's/system-\([0-9]\+\)-link/\1/' \
95 | sort -n -r); do
96 link=/nix/var/nix/profiles/system-$generation-link
97 addEntry $link $generation
98done
99
100# Add the firmware files
101fwdir=@firmware@/share/raspberrypi/boot/
102copyForced $fwdir/bootcode.bin /boot/bootcode.bin
103copyForced $fwdir/fixup.dat /boot/fixup.dat
104copyForced $fwdir/fixup_cd.dat /boot/fixup_cd.dat
105copyForced $fwdir/fixup_db.dat /boot/fixup_db.dat
106copyForced $fwdir/start.elf /boot/start.elf
107copyForced $fwdir/start_cd.elf /boot/start_cd.elf
108copyForced $fwdir/start_db.elf /boot/start_db.elf
109copyForced $fwdir/start_x.elf /boot/start_x.elf
110
111# Remove obsolete files from /boot/old.
112for fn in /boot/old/*linux* /boot/old/*initrd*; do
113 if ! test "${filesCopied[$fn]}" = 1; then
114 rm -vf -- "$fn"
115 fi
116done