884b2a55f80478fb6f0f0d47c489efc1b8bde5ab
3 # Set grub to boot into a different distro, and reboot unless -r
5 # with no argument, print available distros
7 # DISTRO_NAME is based on the partition names in /boot. eg boot_debianjessie
10 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
12 [[ $EUID == 0 ]] ||
exec sudo
"$BASH_SOURCE" "$@"
15 ###### begin command line parsing #####
17 while [[ $1 == -* ]]; do
20 -r) reboot
=false
; shift ;;
28 if [[ ! $distro ]]; then
29 echo "available distros:"
30 btrfs subvolume list
/boot |
sed -rn 's/^.*boot_(.*)/\1/p'
34 ###### end command line parsing #####
37 #### begin initial error checking #####
39 if ! btrfs subvolume list
/boot |
grep "_$distro$" &>/dev
/null
; then
40 echo "$0: error: _$distro$ not found in btrfs subvolume list /boot:"
41 btrfs subvolume list
/boot
45 #### end initial error checking #####
47 e
() { echo "$@"; "$@"; }
49 boot_dev
=$
(mount |
sed -rn "s#^(\S+) on /boot .*#\1#p")
51 mount_point
=$
(mktemp
-d)
53 e mount
-o subvol
=boot_
$distro $boot_dev $mount_point
55 boot_disk
=${boot_dev%%[0-9]*}
57 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
58 # it just generates one if the file doesn't exist.
59 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
60 e grub-bios-setup
-d $mount_point/grub
/i386-pc
-s -m $mount_point/grub
/device.map
$boot_disk
66 touch /tmp
/keyscript-off