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