use latest btrfs-progs to match linux-libre
[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
68 ###### begin command line parsing #####
69 reboot=true
70 temp=$(getopt -l help hdr "$@") || usage 1
71 eval set -- "$temp"
72 while true; do
73 case $1 in
74 -d) set -x; shift ;;
75 -r) reboot=false; shift ;;
76 -h|--help) usage ;;
77 --) shift; break ;;
78 *) echo "$0: Internal error!" ; exit 1 ;;
79 esac
80 done
81
82
83 distro=$1
84
85 mnt=/boot
86 if ! mountpoint $mnt &>/dev/null; then
87 mnt=/
88 fi
89
90 if [[ ! $distro ]]; then
91 echo "available distros:"
92 cur=$(btrfs subvol show $mnt| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
93 btrfs subvolume list $mnt | awk '{print $9}' | sed "s/$cur/$cur (current)/"
94 exit 0
95 fi
96
97 ###### end command line parsing #####
98
99
100 #### begin initial error checking #####
101
102 if ! btrfs subvolume list $mnt | grep "$distro$" &>/dev/null; then
103 echo "$0: error: $distro not found in btrfs subvolume list $mnt:"
104 btrfs subvolume list $mnt
105 exit 1
106 fi
107
108 #### end initial error checking #####
109
110 e() { echo "$@"; "$@"; }
111
112 for boot_dev in $(btrfs fil show $mnt | sed -nr 's#.*path\s+(\S+)$#\1#p'); do
113
114 mount_point=$(mktemp -d)
115
116 e mount -o subvol=$distro $boot_dev $mount_point
117
118 boot_disk=${boot_dev%%[0-9]*}
119
120 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
121 # it just generates one if the file doesn't exist.
122 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
123 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
124 e umount $mount_point
125 done
126
127 if [[ $(blockdev --getsize64 ${boot_disk}4) == 8388608 ]]; then
128 # old partition scheme
129 grub_dev=${boot_disk}4
130 elif [[ $(blockdev --getsize64 ${boot_disk}5) == 8388608 ]]; then
131 grub_dev=${boot_disk}5
132 else
133 grub_dev=${boot_disk}7
134 fi
135
136 e mount $grub_dev $mount_point
137 e grub-editenv $mount_point/grubenv set last_boot=/$distro
138 e grub-editenv $mount_point/grubenv set did_fai_check=true
139 e umount $mount_point
140 if $reboot; then
141 touch /tmp/keyscript-off
142 reboot now
143 fi