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