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.
19 x
="$(readlink -f "$BASH_SOURCE")"
20 f
="${x%/*}/bash-trace"
24 source ${x%/*}/..
/bash-trace
/DEFAULT
27 [[ $EUID == 0 ]] ||
exec sudo
"$BASH_SOURCE" "$@"
31 Usage: ${0##*/} [OPTIONS] DISTRO_NAME
32 Set grub to boot into a different distro, and reboot unless -r
34 With no argument, print available distros
35 DISTRO_NAME is based on the partition names in /boot.
36 For example, boot_debianjessie.
38 For a system without libreboot, which is failing completely to
39 boot on one distro, here is how I did a chboot for it:
40 # arch-pxe had been run previously
41 pxe-server some_hostname arch
42 # reboot some_hostname into arch live env
43 pxe-server # disable pxe server
44 ssh root@some_hostname
45 lsblk # identify boot dev. if boot dev is a raid, this could be repeated on all boot devs.
47 mount_point=/mnt/boot_debiantesting # the subvol i want to chboot to
49 grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
52 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
53 for non-libreboot systems
56 -d Enable debug output.
57 -h|--help Print help and exit.
59 Note: Uses GNU getopt options parsing style
67 ###### begin command line parsing #####
69 temp
=$
(getopt
-l help hdr
"$@") || usage
1
74 -r) reboot
=false
; shift ;;
77 *) echo "$0: Internal error!" ; exit 1 ;;
85 if ! mountpoint
$mnt &>/dev
/null
; then
89 if [[ ! $distro ]]; then
90 echo "available distros:"
91 cur
=$
(btrfs subvol show
$mnt|
sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
92 btrfs subvolume list
$mnt |
awk '{print $9}' |
sed "s/$cur/$cur (current)/"
96 ###### end command line parsing #####
99 #### begin initial error checking #####
101 if ! btrfs subvolume list
$mnt |
grep "$distro$" &>/dev
/null
; then
102 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
103 btrfs subvolume list
$mnt
107 #### end initial error checking #####
109 e
() { echo "$@"; "$@"; }
111 boot_dev
=$
(mount |
sed -rn "s#^(\S+) on $mnt .*#\1#p")
113 mount_point
=$
(mktemp
-d)
115 e mount
-o subvol
=$distro $boot_dev $mount_point
117 boot_disk
=${boot_dev%%[0-9]*}
119 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
120 # it just generates one if the file doesn't exist.
121 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
122 e grub-bios-setup
-d $mount_point/grub
/i386-pc
-s -m $mount_point/grub
/device.map
$boot_disk
124 e umount
$mount_point
126 e mount
$boot_disk$grub_extn $mount_point
127 e grub-editenv
$mount_point/grubenv
set last_boot
=/$distro
128 e grub-editenv
$mount_point/grubenv
set did_fai_check
=true
129 e umount
$mount_point
133 touch /tmp
/keyscript-off