021c74d42d4764d1391bc75f19c1a4c3c695134b
[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 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
23
24 usage() {
25 cat <<EOF
26 Usage: ${0##*/} [OPTIONS] DISTRO_NAME
27 Set grub to boot into a different distro, and reboot unless -r
28
29 With no argument, print available distros
30 DISTRO_NAME is based on the partition names in /boot.
31 For example debianjessie for the partitionn boot_debianjessie.
32
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
39 ssh root@treetowl
40 lsblk # identify boot dev. if boot dev is a raid, this could be repeated on all boot devs.
41 mount /dev/sdd3 /mnt
42 mount_point=/mnt/boot_debiantesting # the subvol i want to chboot to
43 boot_disk=/dev/sdd
44 grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
45 reboot
46
47 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
48 for non-libreboot systems
49
50 -r Do not reboot.
51 -d Enable debug output.
52 -h|--help Print help and exit.
53
54 Note: Uses GNU getopt options parsing style
55 EOF
56 exit $1
57 }
58
59
60 grub_extn=4
61
62 ###### begin command line parsing #####
63 reboot=true
64 temp=$(getopt -l help hdr "$@") || usage 1
65 eval set -- "$temp"
66 while true; do
67 case $1 in
68 -d) set -x; shift ;;
69 -r) reboot=false; shift ;;
70 -h|--help) usage ;;
71 --) shift; break ;;
72 *) echo "$0: Internal error!" ; exit 1 ;;
73 esac
74 done
75
76
77 distro=$1
78
79 if [[ ! $distro ]]; then
80 echo "available distros:"
81 cur=$(btrfs subvol show /boot| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
82 btrfs subvolume list /boot | awk '{print $9}' | sed "s/$cur/$cur (current)/"
83 exit 0
84 fi
85
86 ###### end command line parsing #####
87
88
89 #### begin initial error checking #####
90
91 if ! btrfs subvolume list /boot | grep "_$distro$" &>/dev/null; then
92 echo "$0: error: _$distro$ not found in btrfs subvolume list /boot:"
93 btrfs subvolume list /boot
94 exit 1
95 fi
96
97 #### end initial error checking #####
98
99 e() { echo "$@"; "$@"; }
100
101 boot_dev=$(mount | sed -rn "s#^(\S+) on /boot .*#\1#p")
102
103 mount_point=$(mktemp -d)
104
105 e mount -o subvol=boot_$distro $boot_dev $mount_point
106
107 boot_disk=${boot_dev%%[0-9]*}
108
109 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
110 # it just generates one if the file doesn't exist.
111 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
112 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
113
114 e umount $mount_point
115
116 # i don't change the default subvolid, so the arg here is just being cautious
117 e mount -o subvolid=0 $boot_disk$grub_extn $mount_point
118 e grub-editenv $mount_point/grubenv set last_boot=/boot_$distro
119 e grub-editenv $mount_point/grubenv set did_fai_check=true
120 e umount $mount_point
121 e rmdir $mount_point
122
123 if $reboot; then
124 touch /tmp/keyscript-off
125 reboot now
126 fi