python312Packages.tencentcloud-sdk-python: 3.0.1291 -> 3.0.1292 (#369707)
[NixPkgs.git] / nixos / modules / system / boot / loader / generic-extlinux-compatible / extlinux-conf-builder.sh
blobb50eae024122cf7a31b5724a56d306fb315f610d
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 menu=1 # Enable menu by default
15 default= # Default configuration
16 target=/boot # Target directory
17 numGenerations=0 # Number of other generations to include in the menu
19 while getopts "t:c:d:g:n:r" opt; do
20 case "$opt" in
21 t) # U-Boot interprets '0' as infinite
22 if [ "$OPTARG" -lt 0 ]; then
23 # When negative (or null coerced to -1), disable timeout which means that we wait forever for input
24 timeout=0
25 elif [ "$OPTARG" = 0 ]; then
26 # When zero, which means disabled in Nix module, disable menu which results in instant boot of the default item
27 # .. timeout is actually ignored by u-Boot but set here for the rest of the script
28 timeout=1
29 menu=0
30 else
31 # Positive results in centi-seconds of timeout, which when passed with no input results in boot of the default item
32 timeout=$((OPTARG * 10))
35 c) default="$OPTARG" ;;
36 d) target="$OPTARG" ;;
37 g) numGenerations="$OPTARG" ;;
38 n) dtbName="$OPTARG" ;;
39 r) noDeviceTree=1 ;;
40 \?) usage ;;
41 esac
42 done
44 [ "$timeout" = "" -o "$default" = "" ] && usage
46 mkdir -p $target/nixos
47 mkdir -p $target/extlinux
49 # Convert a path to a file in the Nix store such as
50 # /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
51 cleanName() {
52 local path="$1"
53 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
56 # Copy a file from the Nix store to $target/nixos.
57 declare -A filesCopied
59 copyToKernelsDir() {
60 local src=$(readlink -f "$1")
61 local dst="$target/nixos/$(cleanName $src)"
62 # Don't copy the file if $dst already exists. This means that we
63 # have to create $dst atomically to prevent partially copied
64 # kernels or initrd if this script is ever interrupted.
65 if ! test -e $dst; then
66 local dstTmp=$dst.tmp.$$
67 cp -r $src $dstTmp
68 mv $dstTmp $dst
70 filesCopied[$dst]=1
71 result=$dst
74 # Copy its kernel, initrd and dtbs to $target/nixos, and echo out an
75 # extlinux menu entry
76 addEntry() {
77 local path=$(readlink -f "$1")
78 local tag="$2" # Generation number or 'default'
80 if ! test -e $path/kernel -a -e $path/initrd; then
81 return
84 copyToKernelsDir "$path/kernel"; kernel=$result
85 copyToKernelsDir "$path/initrd"; initrd=$result
86 dtbDir=$(readlink -m "$path/dtbs")
87 if [ -e "$dtbDir" ]; then
88 copyToKernelsDir "$dtbDir"; dtbs=$result
91 timestampEpoch=$(stat -L -c '%Z' $path)
93 timestamp=$(date "+%Y-%m-%d %H:%M" -d @$timestampEpoch)
94 nixosLabel="$(cat $path/nixos-version)"
95 extraParams="$(cat $path/kernel-params)"
97 echo
98 echo "LABEL nixos-$tag"
99 if [ "$tag" = "default" ]; then
100 echo " MENU LABEL NixOS - Default"
101 else
102 echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)"
104 echo " LINUX ../nixos/$(basename $kernel)"
105 echo " INITRD ../nixos/$(basename $initrd)"
106 echo " APPEND init=$path/init $extraParams"
108 if [ -n "$noDeviceTree" ]; then
109 return
112 if [ -d "$dtbDir" ]; then
113 # if a dtbName was specified explicitly, use that, else use FDTDIR
114 if [ -n "$dtbName" ]; then
115 echo " FDT ../nixos/$(basename $dtbs)/${dtbName}"
116 else
117 echo " FDTDIR ../nixos/$(basename $dtbs)"
119 else
120 if [ -n "$dtbName" ]; then
121 echo "Explicitly requested dtbName $dtbName, but there's no FDTDIR - bailing out." >&2
122 exit 1
127 tmpFile="$target/extlinux/extlinux.conf.tmp.$$"
129 cat > $tmpFile <<EOF
130 # Generated file, all changes will be lost on nixos-rebuild!
132 # Change this to e.g. nixos-42 to temporarily boot to an older configuration.
133 DEFAULT nixos-default
135 TIMEOUT $timeout
138 [ "$menu" == "1" ] \
139 && echo "MENU TITLE ------------------------------------------------------------" >> $tmpFile
141 addEntry $default default >> $tmpFile
143 if [ "$numGenerations" -gt 0 ]; then
144 # Add up to $numGenerations generations of the system profile to the menu,
145 # in reverse (most recent to least recent) order.
146 for generation in $(
147 (cd /nix/var/nix/profiles && ls -d system-*-link) \
148 | sed 's/system-\([0-9]\+\)-link/\1/' \
149 | sort -n -r \
150 | head -n $numGenerations); do
151 link=/nix/var/nix/profiles/system-$generation-link
152 addEntry $link "${generation}-default"
153 for specialisation in $(
154 ls /nix/var/nix/profiles/system-$generation-link/specialisation \
155 | sort -n -r); do
156 link=/nix/var/nix/profiles/system-$generation-link/specialisation/$specialisation
157 addEntry $link "${generation}-${specialisation}"
158 done
159 done >> $tmpFile
162 mv -f $tmpFile $target/extlinux/extlinux.conf
164 # Remove obsolete files from $target/nixos.
165 for fn in $target/nixos/*; do
166 if ! test "${filesCopied[$fn]}" = 1; then
167 echo "Removing no longer needed boot file: $fn"
168 chmod +w -- "$fn"
169 rm -rf -- "$fn"
171 done