enable ecne and noble
[automated-distro-installer] / fai / config / files / boot / chboot / DEFAULT
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
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.
8
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.
13
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.
17
18
19 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21
22 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
23 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
24 set -eE -o pipefail
25 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
26
27 usage() {
28 cat <<'EOF'
29 Usage: chboot [OPTIONS] DISTRO_NAME
30 Set grub to boot into a different distro, and reboot unless -r
31
32 With no argument, print available distros
33 DISTRO_NAME is based on the partition names in /boot.
34 For example, boot_debianjessie.
35
36 For a system without libreboot, which is failing completely to
37 boot on one distro, here is how I did a chboot for it:
38 # arch-pxe had been run previously
39 pxe-server some_hostname arch
40 # reboot some_hostname into arch live env
41 pxe-server # disable pxe server
42 ssh root@some_hostname
43 lsblk # identify boot dev. if boot dev is a raid, this could be repeated on all boot devs.
44 mount /dev/sdd3 /mnt
45 mp=/mnt/boot_debiantesting # the subvol i want to chboot to
46 boot_disk=/dev/sdd
47 grub-bios-setup -d $mp/grub/i386-pc -s -m $mp/grub/device.map $boot_disk
48 reboot
49
50 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
51 for non-libreboot systems
52
53 -r Do not reboot.
54 -d Enable debug output.
55 -h|--help Print help and exit.
56
57 Note: Uses GNU getopt options parsing style
58 EOF
59 exit $1
60 }
61
62
63
64 ###### begin command line parsing #####
65 reboot=true
66 temp=$(getopt -l help hdr "$@") || usage 1
67 eval set -- "$temp"
68 while true; do
69 case $1 in
70 -d) set -x; shift ;;
71 -r) reboot=false; shift ;;
72 -h|--help) usage 0 ;;
73 --) shift; break ;;
74 *) echo "$0: Internal error!" ; exit 1 ;;
75 esac
76 done
77
78
79 distro=$1
80
81 mnt=/boot
82 if ! mountpoint $mnt &>/dev/null; then
83 mnt=/
84 fi
85
86 if [[ ! $distro ]]; then
87 echo "available distros:"
88 cur=$(btrfs subvol show $mnt| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
89 btrfs subvolume list $mnt | awk '{print $9}' | sed "s/$cur/$cur (current)/"
90 exit 0
91 fi
92
93 ###### end command line parsing #####
94
95
96 #### begin initial error checking #####
97
98 if ! btrfs subvolume list $mnt | grep "$distro$" &>/dev/null; then
99 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
100 btrfs subvolume list $mnt
101 exit 1
102 fi
103
104 #### end initial error checking #####
105
106 e() { echo "$@"; "$@"; }
107
108 for boot_dev in $(btrfs fil show $mnt | sed -nr 's#.*path\s+(\S+)$#\1#p'); do
109
110 mount_point=$(mktemp -d)
111
112 e mount -o subvol=$distro $boot_dev $mount_point
113
114 boot_disk=${boot_dev%%[0-9]*}
115
116 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
117 # it just generates one if the file doesn't exist.
118 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
119 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
120 e umount $mount_point
121 done
122
123 if [[ $(blockdev --getsize64 ${boot_disk}4) == 8388608 ]]; then
124 # old partition scheme
125 grub_dev=${boot_disk}4
126 elif [[ $(blockdev --getsize64 ${boot_disk}5) == 8388608 ]]; then
127 grub_dev=${boot_disk}5
128 else
129 grub_dev=${boot_disk}7
130 fi
131
132 e mount $grub_dev $mount_point
133 e grub-editenv $mount_point/grubenv set last_boot=/$distro
134 e grub-editenv $mount_point/grubenv set did_fai_check=true
135 e umount $mount_point
136 if $reboot; then
137 touch /tmp/keyscript-off
138 reboot now
139 fi