vuls: init at 0.27.0
[NixPkgs.git] / nixos / modules / system / boot / loader / generic-extlinux-compatible / extlinux-conf-builder.sh
blobf2b281d23292bae4f94f0b037d6e3fcf2c402a1b
1 #! @bash@/bin/sh -e
3 shopt -s nullglob
5 export PATH=/empty
6 for i in @path@; do PATH=$PATH:$i/bin; done
8 usage() {
9 echo "usage: $0 -t <timeout> -c <path-to-default-configuration> [-d <boot-dir>] [-g <num-generations>] [-n <dtbName>] [-r]" >&2
10 exit 1
13 timeout= # Timeout in centiseconds
14 default= # Default configuration
15 target=/boot # Target directory
16 numGenerations=0 # Number of other generations to include in the menu
18 while getopts "t:c:d:g:n:r" opt; do
19 case "$opt" in
20 t) # U-Boot interprets '0' as infinite and negative as instant boot
21 if [ "$OPTARG" -lt 0 ]; then
22 timeout=0
23 elif [ "$OPTARG" = 0 ]; then
24 timeout=-10
25 else
26 timeout=$((OPTARG * 10))
29 c) default="$OPTARG" ;;
30 d) target="$OPTARG" ;;
31 g) numGenerations="$OPTARG" ;;
32 n) dtbName="$OPTARG" ;;
33 r) noDeviceTree=1 ;;
34 \?) usage ;;
35 esac
36 done
38 [ "$timeout" = "" -o "$default" = "" ] && usage
40 mkdir -p $target/nixos
41 mkdir -p $target/extlinux
43 # Convert a path to a file in the Nix store such as
44 # /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
45 cleanName() {
46 local path="$1"
47 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
50 # Copy a file from the Nix store to $target/nixos.
51 declare -A filesCopied
53 copyToKernelsDir() {
54 local src=$(readlink -f "$1")
55 local dst="$target/nixos/$(cleanName $src)"
56 # Don't copy the file if $dst already exists. This means that we
57 # have to create $dst atomically to prevent partially copied
58 # kernels or initrd if this script is ever interrupted.
59 if ! test -e $dst; then
60 local dstTmp=$dst.tmp.$$
61 cp -r $src $dstTmp
62 mv $dstTmp $dst
64 filesCopied[$dst]=1
65 result=$dst
68 # Copy its kernel, initrd and dtbs to $target/nixos, and echo out an
69 # extlinux menu entry
70 addEntry() {
71 local path=$(readlink -f "$1")
72 local tag="$2" # Generation number or 'default'
74 if ! test -e $path/kernel -a -e $path/initrd; then
75 return
78 copyToKernelsDir "$path/kernel"; kernel=$result
79 copyToKernelsDir "$path/initrd"; initrd=$result
80 dtbDir=$(readlink -m "$path/dtbs")
81 if [ -e "$dtbDir" ]; then
82 copyToKernelsDir "$dtbDir"; dtbs=$result
85 timestampEpoch=$(stat -L -c '%Z' $path)
87 timestamp=$(date "+%Y-%m-%d %H:%M" -d @$timestampEpoch)
88 nixosLabel="$(cat $path/nixos-version)"
89 extraParams="$(cat $path/kernel-params)"
91 echo
92 echo "LABEL nixos-$tag"
93 if [ "$tag" = "default" ]; then
94 echo " MENU LABEL NixOS - Default"
95 else
96 echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)"
98 echo " LINUX ../nixos/$(basename $kernel)"
99 echo " INITRD ../nixos/$(basename $initrd)"
100 echo " APPEND init=$path/init $extraParams"
102 if [ -n "$noDeviceTree" ]; then
103 return
106 if [ -d "$dtbDir" ]; then
107 # if a dtbName was specified explicitly, use that, else use FDTDIR
108 if [ -n "$dtbName" ]; then
109 echo " FDT ../nixos/$(basename $dtbs)/${dtbName}"
110 else
111 echo " FDTDIR ../nixos/$(basename $dtbs)"
113 else
114 if [ -n "$dtbName" ]; then
115 echo "Explicitly requested dtbName $dtbName, but there's no FDTDIR - bailing out." >&2
116 exit 1
121 tmpFile="$target/extlinux/extlinux.conf.tmp.$$"
123 cat > $tmpFile <<EOF
124 # Generated file, all changes will be lost on nixos-rebuild!
126 # Change this to e.g. nixos-42 to temporarily boot to an older configuration.
127 DEFAULT nixos-default
129 MENU TITLE ------------------------------------------------------------
130 TIMEOUT $timeout
133 addEntry $default default >> $tmpFile
135 if [ "$numGenerations" -gt 0 ]; then
136 # Add up to $numGenerations generations of the system profile to the menu,
137 # in reverse (most recent to least recent) order.
138 for generation in $(
139 (cd /nix/var/nix/profiles && ls -d system-*-link) \
140 | sed 's/system-\([0-9]\+\)-link/\1/' \
141 | sort -n -r \
142 | head -n $numGenerations); do
143 link=/nix/var/nix/profiles/system-$generation-link
144 addEntry $link "${generation}-default"
145 for specialisation in $(
146 ls /nix/var/nix/profiles/system-$generation-link/specialisation \
147 | sort -n -r); do
148 link=/nix/var/nix/profiles/system-$generation-link/specialisation/$specialisation
149 addEntry $link "${generation}-${specialisation}"
150 done
151 done >> $tmpFile
154 mv -f $tmpFile $target/extlinux/extlinux.conf
156 # Remove obsolete files from $target/nixos.
157 for fn in $target/nixos/*; do
158 if ! test "${filesCopied[$fn]}" = 1; then
159 echo "Removing no longer needed boot file: $fn"
160 chmod +w -- "$fn"
161 rm -rf -- "$fn"
163 done