| 1 | #!/bin/sh |
| 2 | |
| 3 | version= |
| 4 | default=0 |
| 5 | timeout=5 |
| 6 | verbose=0 |
| 7 | |
| 8 | conf=/boot/extlinux.conf |
| 9 | myconf=/etc/update-extlinux.conf |
| 10 | |
| 11 | # read in extlinux settings |
| 12 | if [ -f "$myconf" ]; then |
| 13 | . $myconf |
| 14 | fi |
| 15 | |
| 16 | everbose() { |
| 17 | if [ "$verbose" = "0" ]; then |
| 18 | return |
| 19 | fi |
| 20 | |
| 21 | echo $* |
| 22 | } |
| 23 | |
| 24 | ewarn() { |
| 25 | echo "WARNING:" $@ >&2 |
| 26 | } |
| 27 | |
| 28 | eerror() { |
| 29 | echo "ERROR:" $@ >&2 |
| 30 | return 1 |
| 31 | } |
| 32 | |
| 33 | usage() { |
| 34 | echo "usage: $0 [-v|--verbose] [--warn-only]" |
| 35 | } |
| 36 | |
| 37 | while [ $# -gt 0 ]; do |
| 38 | opt="$1" |
| 39 | shift |
| 40 | case "$opt" in |
| 41 | -v|--verbose) |
| 42 | verbose=1 |
| 43 | ;; |
| 44 | --warn-only) |
| 45 | warn_only=1 |
| 46 | ;; |
| 47 | --) |
| 48 | break |
| 49 | ;; |
| 50 | -*) |
| 51 | usage |
| 52 | exit 1 |
| 53 | ;; |
| 54 | esac |
| 55 | done |
| 56 | |
| 57 | everbose "Updating extlinux configuration." |
| 58 | |
| 59 | if [ "x$root" = "x" ]; then |
| 60 | ewarn "Root device is not specified in $myconf." |
| 61 | blkid_export=$(blkid -o export /dev/root) |
| 62 | if [ -n "$blkid_export" ]; then |
| 63 | export $blkid_export |
| 64 | fi |
| 65 | if [ -z "$UUID" ]; then |
| 66 | # try parse /proc/mount for mounted / |
| 67 | dev=$(awk '$2 == "/" {dev=$1} END {print dev}' /proc/mounts) |
| 68 | if [ -n "$dev" ]; then |
| 69 | blkid_export=$(blkid -o export $dev) |
| 70 | if [ -n "$blkid_export" ]; then |
| 71 | export "$blkid_export" |
| 72 | fi |
| 73 | fi |
| 74 | fi |
| 75 | if [ -z "$UUID" ]; then |
| 76 | if [ -z "$dev" ]; then |
| 77 | if [ -n "$warn_only" ]; then |
| 78 | ewarn "Failed to detect root device. extlinux.conf is not updated" |
| 79 | exit 0 |
| 80 | else |
| 81 | eerror "Failed to detect root device" |
| 82 | exit 1 |
| 83 | fi |
| 84 | else |
| 85 | root=$dev |
| 86 | fi |
| 87 | else |
| 88 | root=UUID=$UUID |
| 89 | fi |
| 90 | everbose "Root device is: $root" |
| 91 | fi |
| 92 | |
| 93 | if [ -z "${timeout##*[!0-9]*}" ]; then |
| 94 | if [ -n "$warn_only" ]; then |
| 95 | ewarn "timeout ($timeout) must be an integer; flooring." |
| 96 | timeout=$(echo $timeout | cut -f1 -d.) |
| 97 | else |
| 98 | eerror "timeout ($timeout) must be an integer." |
| 99 | exit 1 |
| 100 | fi |
| 101 | fi |
| 102 | rtimeout=$(( $timeout * 10 )) |
| 103 | syslinux_menu=menu.c32 |
| 104 | menu_hidden= |
| 105 | |
| 106 | # vesa menu has been requested? |
| 107 | if [ "$vesa_menu" = "1" ]; then |
| 108 | syslinux_menu=vesamenu.c32 |
| 109 | fi |
| 110 | |
| 111 | umask 0022 |
| 112 | rm -f $conf.new |
| 113 | echo "# Generated by update-extlinux $version" > $conf.new |
| 114 | if [ -n "$serial_port" ]; then |
| 115 | echo "SERIAL $serial_port ${serial_baud:-115200}" >> $conf.new |
| 116 | fi |
| 117 | echo "DEFAULT $syslinux_menu" >> $conf.new |
| 118 | echo "PROMPT 0" >> $conf.new |
| 119 | echo "MENU TITLE Alpine/$(uname -s) Boot Menu" >> $conf.new |
| 120 | if [ "$hidden" = "1" ]; then |
| 121 | echo "MENU HIDDEN" >> $conf.new |
| 122 | fi |
| 123 | echo "MENU AUTOBOOT Alpine will be booted automatically in # seconds." >> $conf.new |
| 124 | echo "TIMEOUT $rtimeout" >> $conf.new |
| 125 | |
| 126 | lst=0 |
| 127 | if [ -f "/boot/xen.gz" ]; then |
| 128 | for kernel in $(find /boot -name "vmlinuz-*" -type f); do |
| 129 | tag=$(basename $kernel | cut -b9-) |
| 130 | everbose "Found Xen hypervisor: /boot/xen.gz, kernel: $kernel" |
| 131 | |
| 132 | if [ -f "/boot/initramfs-$tag" ]; then |
| 133 | everbose "Found initramfs: /boot/initramfs-$tag" |
| 134 | initramfs="initramfs-$tag" |
| 135 | else |
| 136 | initramfs= |
| 137 | fi |
| 138 | initramfs_stage="${initramfs:+---} $initramfs" |
| 139 | |
| 140 | if [ -f "/boot/amd-ucode.img" ]; then |
| 141 | everbose "Found microcode for AMD CPUs: /boot/amd-ucode.img" |
| 142 | ucode_image_amd="amd-ucode.img" |
| 143 | case "$xen_opts" in |
| 144 | *ucode=scan* ) ;; |
| 145 | * ) xen_opts="$xen_opts ucode=scan" ;; |
| 146 | esac |
| 147 | fi |
| 148 | if [ -f "/boot/intel-ucode.img" ]; then |
| 149 | everbose "Found microcode for Intel CPUs: /boot/intel-ucode.img" |
| 150 | ucode_image_intel="intel-ucode.img" |
| 151 | case "$xen_opts" in |
| 152 | *ucode=scan* ) ;; |
| 153 | * ) xen_opts="$xen_opts ucode=scan" ;; |
| 154 | esac |
| 155 | fi |
| 156 | ucode_image_stage="${ucode_image_amd:+---} $ucode_image_amd ${ucode_image_intel:+---} $ucode_image_intel" |
| 157 | |
| 158 | label=xen-$(grep -w -l $tag /usr/share/kernel/*/kernel.release \ |
| 159 | | cut -d/ -f5) |
| 160 | if [ "$label" = "xen-" ]; then |
| 161 | label=xen-$lst |
| 162 | fi |
| 163 | |
| 164 | dom0_kernel_stage="--- $(basename $kernel) root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts" |
| 165 | |
| 166 | echo "LABEL $label" >> $conf.new |
| 167 | if [ "$label" = "$default" ]; then |
| 168 | echo " MENU DEFAULT" >> $conf.new |
| 169 | fi |
| 170 | echo " MENU LABEL Xen + Linux $tag" >> $conf.new |
| 171 | echo " COM32 mboot.c32" >> $conf.new |
| 172 | echo " APPEND xen.gz $xen_opts $dom0_kernel_stage $initramfs_stage $ucode_image_stage" >> $conf.new |
| 173 | echo "" >> $conf.new |
| 174 | lst=$(($lst + 1)) |
| 175 | done |
| 176 | fi |
| 177 | |
| 178 | for kernel in $(find /boot -name "vmlinuz*" -type f); do |
| 179 | case $kernel in |
| 180 | *vmlinuz) tag=vanilla;; |
| 181 | *vmlinuz-*) tag=$(basename $kernel | cut -b9-);; |
| 182 | *) continue;; |
| 183 | esac |
| 184 | everbose "Found kernel: $kernel" |
| 185 | label=$(grep -w -l $tag /usr/share/kernel/*/kernel.release | cut -d/ -f5) |
| 186 | if [ -z "$label" ]; then |
| 187 | if [ "$tag" = vanilla ]; then |
| 188 | label="vanilla" |
| 189 | else |
| 190 | label=$lst |
| 191 | fi |
| 192 | fi |
| 193 | echo "LABEL $label" >> $conf.new |
| 194 | if [ "$label" = "$default" ]; then |
| 195 | echo " MENU DEFAULT" >> $conf.new |
| 196 | fi |
| 197 | echo " MENU LABEL Linux $tag" >> $conf.new |
| 198 | echo " LINUX $(basename $kernel)" >> $conf.new |
| 199 | initrd= |
| 200 | if [ -f "/boot/initramfs-$tag" ]; then |
| 201 | everbose "Found initramfs: /boot/initramfs-$tag" |
| 202 | initrd="initramfs-$tag" |
| 203 | fi |
| 204 | if [ -f "/boot/amd-ucode.img" ]; then |
| 205 | everbose "Found microcode for AMD CPUs: /boot/amd-ucode.img" |
| 206 | initrd="amd-ucode.img${initrd:+,}${initrd}" |
| 207 | fi |
| 208 | if [ -f "/boot/intel-ucode.img" ]; then |
| 209 | everbose "Found microcode for Intel CPUs: /boot/intel-ucode.img" |
| 210 | initrd="intel-ucode.img${initrd:+,}${initrd}" |
| 211 | fi |
| 212 | echo " INITRD ${initrd}" >> $conf.new |
| 213 | echo " APPEND root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts" >> $conf.new |
| 214 | echo "" >> $conf.new |
| 215 | lst=$(($lst + 1)) |
| 216 | done |
| 217 | |
| 218 | if [ -n "$password" ]; then |
| 219 | echo "NOESCAPE 1" >> $conf.new |
| 220 | echo "MENU MASTER PASSWD $password" >> $conf.new |
| 221 | echo "" >> $conf.new |
| 222 | chmod o-r $conf.new |
| 223 | fi |
| 224 | |
| 225 | everbose "$lst entries found." |
| 226 | |
| 227 | for entry in /etc/update-extlinux.d/*; do |
| 228 | [ -f "$entry" ] && { cat $entry; echo ""; } >> $conf.new |
| 229 | done |
| 230 | |
| 231 | echo "MENU SEPARATOR" >> $conf.new |
| 232 | echo "" >> $conf.new |
| 233 | |
| 234 | if [ -f "/boot/hdt.c32" ]; then |
| 235 | everbose "Found Hardware Detection Tool: /boot/hdt.c32" |
| 236 | echo "LABEL hdt" >> $conf.new |
| 237 | echo " MENU LABEL Hardware info" >> $conf.new |
| 238 | if [ -n "$password" ]; then |
| 239 | echo " MENU PASSWD" >> $conf.new |
| 240 | fi |
| 241 | echo " COM32 hdt.c32" >> $conf.new |
| 242 | if [ -f "/boot/memtest" ]; then |
| 243 | everbose "Found memtest86+: /boot/memtest" |
| 244 | echo " APPEND memtest=memtest" >> $conf.new |
| 245 | fi |
| 246 | echo "" >> $conf.new |
| 247 | elif [ -f "/boot/memtest" ]; then |
| 248 | everbose "Found memtest86+: /boot/memtest" |
| 249 | echo "LABEL memtest" >> $conf.new |
| 250 | echo " MENU LABEL Memtest86+" >> $conf.new |
| 251 | echo " KERNEL memtest" >> $conf.new |
| 252 | echo "" >> $conf.new |
| 253 | fi |
| 254 | |
| 255 | for i in reboot poweroff; do |
| 256 | [ -f "/boot/$i.c32" ] || continue |
| 257 | everbose "Found $i" |
| 258 | # make first char capital |
| 259 | cap=$( echo $i | awk '{sub(".", substr(toupper($0),1,1), $0); print}' ) |
| 260 | echo "LABEL $i" >> $conf.new |
| 261 | echo " MENU LABEL $cap" >> $conf.new |
| 262 | echo " COM32 $i.c32" >> $conf.new |
| 263 | echo "" >> $conf.new |
| 264 | done |
| 265 | |
| 266 | if cmp -s $conf.new $conf; then |
| 267 | everbose "Configuration unchanged." |
| 268 | rm $conf.new |
| 269 | fi |
| 270 | |
| 271 | if [ "$overwrite" != "1" ]; then |
| 272 | exit 0 |
| 273 | elif [ -f "$conf.new" ]; then |
| 274 | # keep a backup just in case |
| 275 | if [ -f "$conf" ]; then |
| 276 | mv $conf $conf.old |
| 277 | fi |
| 278 | |
| 279 | mv $conf.new $conf |
| 280 | fi |
| 281 | |
| 282 | everbose "Installing libutil.c32 libcom32.c32 mboot.c32 menu.c32 vesamenu.c32 to /boot." |
| 283 | cp /usr/share/syslinux/libutil.c32 \ |
| 284 | /usr/share/syslinux/libcom32.c32 \ |
| 285 | /usr/share/syslinux/mboot.c32 \ |
| 286 | /usr/share/syslinux/menu.c32 \ |
| 287 | /usr/share/syslinux/vesamenu.c32 \ |
| 288 | /boot |
| 289 | |
| 290 | case "$(stat -f -c '%T' /boot)" in |
| 291 | ext*) extlinux --update /boot || [ -n "$warn_only" ];; |
| 292 | esac |
| 293 | |