adfbe1c31d71661d685b64abca31137f6bc0617a
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 [[ $EUID == 0 ]] ||
exec sudo
"$BASH_SOURCE" "$@"
22 x
="$(readlink -f "$BASH_SOURCE")"
23 f
="${x%/*}/bash-trace"
27 source ${x%/*}/..
/bash-trace
/DEFAULT
33 Usage: ${0##*/} [OPTIONS] DISTRO_NAME
34 Set grub to boot into a different distro, and reboot unless -r
36 With no argument, print available distros
37 DISTRO_NAME is based on the partition names in /boot.
38 For example, boot_debianjessie.
40 For a system without libreboot, which is failing completely to
41 boot on one distro, here is how I did a chboot for it:
42 # arch-pxe had been run previously
43 pxe-server some_hostname arch
44 # reboot some_hostname into arch live env
45 pxe-server # disable pxe server
46 ssh root@some_hostname
47 lsblk # identify boot dev. if boot dev is a raid, this could be repeated on all boot devs.
49 mp=/mnt/boot_debiantesting # the subvol i want to chboot to
51 grub-bios-setup -d $mp/grub/i386-pc -s -m $mp/grub/device.map $boot_disk
54 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
55 for non-libreboot systems
58 -d Enable debug output.
59 -h|--help Print help and exit.
61 Note: Uses GNU getopt options parsing style
68 ###### begin command line parsing #####
70 temp
=$
(getopt
-l help hdr
"$@") || usage
1
75 -r) reboot
=false
; shift ;;
78 *) echo "$0: Internal error!" ; exit 1 ;;
86 if ! mountpoint
$mnt &>/dev
/null
; then
90 if [[ ! $distro ]]; then
91 echo "available distros:"
92 cur
=$
(btrfs subvol show
$mnt|
sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
93 btrfs subvolume list
$mnt |
awk '{print $9}' |
sed "s/$cur/$cur (current)/"
97 ###### end command line parsing #####
100 #### begin initial error checking #####
102 if ! btrfs subvolume list
$mnt |
grep "$distro$" &>/dev
/null
; then
103 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
104 btrfs subvolume list
$mnt
108 #### end initial error checking #####
110 e
() { echo "$@"; "$@"; }
112 for boot_dev
in $
(btrfs fil show
$mnt |
sed -nr 's#.*path\s+(\S+)$#\1#p'); do
114 mount_point
=$
(mktemp
-d)
116 e mount
-o subvol
=$distro $boot_dev $mount_point
118 boot_disk
=${boot_dev%%[0-9]*}
120 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
121 # it just generates one if the file doesn't exist.
122 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
123 e grub-bios-setup
-d $mount_point/grub
/i386-pc
-s -m $mount_point/grub
/device.map
$boot_disk
124 e umount
$mount_point
127 if [[ $
(blockdev
--getsize64 ${boot_disk}4) == 8388608 ]]; then
128 # old partition scheme
129 grub_dev
=${boot_disk}4
130 elif [[ $
(blockdev
--getsize64 ${boot_disk}5) == 8388608 ]]; then
131 grub_dev
=${boot_disk}5
133 grub_dev
=${boot_disk}7
136 e mount
$grub_dev $mount_point
137 e grub-editenv
$mount_point/grubenv
set last_boot
=/$distro
138 e grub-editenv
$mount_point/grubenv
set did_fai_check
=true
139 e umount
$mount_point
141 touch /tmp
/keyscript-off