]> git.draconx.ca Git - scripts.git/commitdiff
Add new kexec bootloader-like script.
authorNick Bowler <nbowler@draconx.ca>
Tue, 19 Aug 2014 23:55:07 +0000 (19:55 -0400)
committerNick Bowler <nbowler@draconx.ca>
Tue, 19 Aug 2014 23:55:07 +0000 (19:55 -0400)
init-kexec.sh [new file with mode: 0755]

diff --git a/init-kexec.sh b/init-kexec.sh
new file mode 100755 (executable)
index 0000000..848db62
--- /dev/null
@@ -0,0 +1,58 @@
+#!/bin/sh
+#
+# Copyright © 2014 Nick Bowler
+#
+# Very simple kexec-based "bootloader", intended to be run as init.  Allows
+# the user to select one of a list of kernels, or boot a default if no user
+# action is given within the timeout.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+
+default=vmlinuz
+cmdline='root=/dev/vda init=/sbin/init.sysv'
+timeout=10
+width=60
+
+do_dialog() {
+       dmesg --console-off >/dev/null 2>&1
+       exec 3>&1
+       eval_result=`printf 'dialog_output='
+                    command dialog --keep-tite --quoted "$@" 2>&1 1>&3
+                    printf '\ndialog_status=%d\n' $?`
+       exec 3>&-
+       dmesg --console-on >/dev/null 2>&1
+       eval "$eval_result" && (exit $dialog_status)
+}
+
+do_shell() {
+       printf 'Cancelled; dropping to shell\n' 1>&2
+       
+       test "$$" = 1 && exec /bin/sh
+       exit 1
+}
+
+if do_dialog --defaultno --ok-label Skip --pause \
+       'Booting default (Ctrl+C to interrupt)...' 8 $width $timeout
+then
+       kernel=$default
+else
+       do_dialog --no-tags --default-item "$default" --menu \
+               'Select kernel to boot, or cancel to exit to shell.' \
+               20 $width 20 \
+               'vmlinuz.old' 'vmlinuz.old' \
+               'vmlinuz' 'vmlinuz' || do_shell
+       kernel=$dialog_output
+fi
+
+printf 'Selected %s\n' "$kernel"
+
+kexec -l "/boot/$kernel" --command-line="$cmdline" || do_shell
+if test "$$" = 1; then
+       # Only execute new kernel if we are init.
+       kexec -e
+fi
+
+# Failing that, drop to shell.
+do_shell