2 # Copyright (C) 2016 Ian Kelling
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
22 [[ $EUID == 0 ]] ||
exec sudo
"$BASH_SOURCE" "$@"
26 Usage: ${0##*/} [OPTIONS] DISTRO_NAME
27 Set grub to boot into a different distro, and reboot unless -r
29 With no argument, print available distros
30 DISTRO_NAME is based on the partition names in /boot.
31 For example, boot_debianjessie.
33 For a system without libreboot, which is failing completely to
34 boot on one distro, here is how I did a chboot for it:
35 # arch-pxe had been run previously
36 pxe-server treetowl arch
37 # reboot treetowl into arch live env
38 pxe-server # disable pxe server
40 lsblk # identify boot dev. if boot dev is a raid, this could be repeated on all boot devs.
42 mount_point=/mnt/boot_debiantesting # the subvol i want to chboot to
44 grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
47 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
48 for non-libreboot systems
51 -d Enable debug output.
52 -h|--help Print help and exit.
54 Note: Uses GNU getopt options parsing style
62 ###### begin command line parsing #####
64 temp
=$
(getopt
-l help hdr
"$@") || usage
1
69 -r) reboot
=false
; shift ;;
72 *) echo "$0: Internal error!" ; exit 1 ;;
80 if ! mountpoint
$mnt &>/dev
/null
; then
84 if [[ ! $distro ]]; then
85 echo "available distros:"
86 cur
=$
(btrfs subvol show
$mnt|
sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
87 btrfs subvolume list
$mnt |
awk '{print $9}' |
sed "s/$cur/$cur (current)/"
91 ###### end command line parsing #####
94 #### begin initial error checking #####
96 if ! btrfs subvolume list
$mnt |
grep "$distro$" &>/dev
/null
; then
97 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
98 btrfs subvolume list
$mnt
102 #### end initial error checking #####
104 e
() { echo "$@"; "$@"; }
106 boot_dev
=$
(mount |
sed -rn "s#^(\S+) on $mnt .*#\1#p")
108 mount_point
=$
(mktemp
-d)
110 e mount
-o subvol
=$distro $boot_dev $mount_point
112 boot_disk
=${boot_dev%%[0-9]*}
114 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
115 # it just generates one if the file doesn't exist.
116 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
117 e grub-bios-setup
-d $mount_point/grub
/i386-pc
-s -m $mount_point/grub
/device.map
$boot_disk
119 e umount
$mount_point
121 e mount
$boot_disk$grub_extn $mount_point
122 e grub-editenv
$mount_point/grubenv
set last_boot
=/$distro
123 e grub-editenv
$mount_point/grubenv
set did_fai_check
=true
124 e umount
$mount_point
128 touch /tmp
/keyscript-off