add rescue, fix default pxe, ubuntu locale
[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, 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 mnt=/boot
80 if ! mountpoint $mnt &>/dev/null; then
81 mnt=/
82 fi
83
84 if [[ ! $distro ]]; then
85 echo "available distros:"
86 cur=$(btrfs subvol show $mnt| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
87 btrfs subvolume list $mnt | awk '{print $9}' | sed "s/$cur/$cur (current)/"
88 exit 0
89 fi
90
91 ###### end command line parsing #####
92
93
94 #### begin initial error checking #####
95
96 if ! btrfs subvolume list $mnt | grep "$distro$" &>/dev/null; then
97 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
98 btrfs subvolume list $mnt
99 exit 1
100 fi
101
102 #### end initial error checking #####
103
104 e() { echo "$@"; "$@"; }
105
106 boot_dev=$(mount | sed -rn "s#^(\S+) on $mnt .*#\1#p")
107
108 mount_point=$(mktemp -d)
109
110 e mount -o subvol=$distro $boot_dev $mount_point
111
112 boot_disk=${boot_dev%%[0-9]*}
113
114 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
115 # it just generates one if the file doesn't exist.
116 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
117 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
118
119 e umount $mount_point
120
121 e mount $boot_disk$grub_extn $mount_point
122 e grub-editenv $mount_point/grubenv set last_boot=/$distro
123 e grub-editenv $mount_point/grubenv set did_fai_check=true
124 e umount $mount_point
125 e rmdir $mount_point
126
127 if $reboot; then
128 touch /tmp/keyscript-off
129 reboot now
130 fi