bunch of minor updates
[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 "$BASH_SOURCE" "$@"
20
21
22 x="$(readlink -f "$BASH_SOURCE")"
23 f="${x%/*}/bash-trace"
24 if [[ -e $f ]]; then
25 source $f
26 else
27 source ${x%/*}/../bash-trace/DEFAULT
28 fi
29
30
31 usage() {
32 cat <<EOF
33 Usage: ${0##*/} [OPTIONS] DISTRO_NAME
34 Set grub to boot into a different distro, and reboot unless -r
35
36 With no argument, print available distros
37 DISTRO_NAME is based on the partition names in /boot.
38 For example, boot_debianjessie.
39
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.
48 mount /dev/sdd3 /mnt
49 mp=/mnt/boot_debiantesting # the subvol i want to chboot to
50 boot_disk=/dev/sdd
51 grub-bios-setup -d $mp/grub/i386-pc -s -m $mp/grub/device.map $boot_disk
52 reboot
53
54 todo: figure out if it's possible to make a multi-distro grub like I have with libreboot
55 for non-libreboot systems
56
57 -r Do not reboot.
58 -d Enable debug output.
59 -h|--help Print help and exit.
60
61 Note: Uses GNU getopt options parsing style
62 EOF
63 exit $1
64 }
65
66
67 grub_extn=4
68
69 ###### begin command line parsing #####
70 reboot=true
71 temp=$(getopt -l help hdr "$@") || usage 1
72 eval set -- "$temp"
73 while true; do
74 case $1 in
75 -d) set -x; shift ;;
76 -r) reboot=false; shift ;;
77 -h|--help) usage ;;
78 --) shift; break ;;
79 *) echo "$0: Internal error!" ; exit 1 ;;
80 esac
81 done
82
83
84 distro=$1
85
86 mnt=/boot
87 if ! mountpoint $mnt &>/dev/null; then
88 mnt=/
89 fi
90
91 if [[ ! $distro ]]; then
92 echo "available distros:"
93 cur=$(btrfs subvol show $mnt| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
94 btrfs subvolume list $mnt | awk '{print $9}' | sed "s/$cur/$cur (current)/"
95 exit 0
96 fi
97
98 ###### end command line parsing #####
99
100
101 #### begin initial error checking #####
102
103 if ! btrfs subvolume list $mnt | grep "$distro$" &>/dev/null; then
104 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
105 btrfs subvolume list $mnt
106 exit 1
107 fi
108
109 #### end initial error checking #####
110
111 e() { echo "$@"; "$@"; }
112
113 for boot_dev in $(btrfs fil show $mnt | sed -nr 's#.*path\s+(\S+)$#\1#p'); do
114
115 mount_point=$(mktemp -d)
116
117 e mount -o subvol=$distro $boot_dev $mount_point
118
119 boot_disk=${boot_dev%%[0-9]*}
120
121 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
122 # it just generates one if the file doesn't exist.
123 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
124 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
125 e umount $mount_point
126 done
127
128 e mount $boot_disk$grub_extn $mount_point
129 e grub-editenv $mount_point/grubenv set last_boot=/$distro
130 e grub-editenv $mount_point/grubenv set did_fai_check=true
131 e umount $mount_point
132 if $reboot; then
133 touch /tmp/keyscript-off
134 reboot now
135 fi