-#!/bin/bash -x
+#!/bin/bash
+# Copyright (C) 2016 Ian Kelling
-# Set grub to boot into a different distro, and reboot unless -r
-# $0 [DISTRO_NAME]
-# with no argument, print available distros
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# DISTRO_NAME is based on the partition names in /boot. eg boot_debianjessie
set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
[[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
+usage() {
+ cat <<EOF
+Usage: ${0##*/} [OPTIONS] DISTRO_NAME
+Set grub to boot into a different distro, and reboot unless -r
+
+With no argument, print available distros
+DISTRO_NAME is based on the partition names in /boot.
+For example debianjessie for the partitionn boot_debianjessie.
+
+-r Do not reboot.
+-d Enable debug output.
+-h|--help Print help and exit.
+
+Note: Uses GNU getopt options parsing style
+EOF
+ exit $1
+}
+
+
+grub_extn=4
+
+###### begin command line parsing #####
reboot=true
-while [[ $1 == -* ]]; do
+temp=$(getopt -l opt o "$@") || usage 1
+eval set -- "$temp"
+while true; do
case $1 in
+ -d) set -x; shift ;;
-r) reboot=false; shift ;;
- --) break ;;
+ -h|--help) usage ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error!" ; exit 1 ;;
esac
done
distro=$1
-###### end command line parsing #####
-
if [[ ! $distro ]]; then
echo "available distros:"
- btrfs subvolume list /boot | sed -rn 's/^.*boot_(.*)/\1/p'
+ cur=$(btrfs subvol show /boot| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
+ btrfs subvolume list /boot | awk '{print $9}' | sed "s/$cur/$cur (current)/"
exit 0
fi
+###### end command line parsing #####
+
+
+#### begin initial error checking #####
+
if ! btrfs subvolume list /boot | grep "_$distro$" &>/dev/null; then
echo "$0: error: _$distro$ not found in btrfs subvolume list /boot:"
btrfs subvolume list /boot
exit 1
fi
+#### end initial error checking #####
+
e() { echo "$@"; "$@"; }
boot_dev=$(mount | sed -rn "s#^(\S+) on /boot .*#\1#p")
# https://www.gnu.org/software/grub/manual/html_node/Device-map.html
e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
+# todo, mount_point needs subvolid=0
+
+e umount $mount_point
+
+e mount $boot_disk$grub_extn $mount_point
+e grub-editenv $mount_point/grubenv set last_boot=/boot_$distro
+e grub-editenv $mount_point/grubenv set did_fai_check=true
e umount $mount_point
e rmdir $mount_point
+
+if $reboot; then
+ touch /tmp/keyscript-off
+ reboot now
+fi