fix chboot options
[automated-distro-installer] / chboot
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 debianjessie for the partitionn boot_debianjessie.
32
33 -r Do not reboot.
34 -d Enable debug output.
35 -h|--help Print help and exit.
36
37 Note: Uses GNU getopt options parsing style
38 EOF
39 exit $1
40 }
41
42
43 grub_extn=4
44
45 ###### begin command line parsing #####
46 reboot=true
47 temp=$(getopt -l help hdr "$@") || usage 1
48 eval set -- "$temp"
49 while true; do
50 case $1 in
51 -d) set -x; shift ;;
52 -r) reboot=false; shift ;;
53 -h|--help) usage ;;
54 --) shift; break ;;
55 *) echo "$0: Internal error!" ; exit 1 ;;
56 esac
57 done
58
59
60 distro=$1
61
62 if [[ ! $distro ]]; then
63 echo "available distros:"
64 cur=$(btrfs subvol show /boot| sed -rn 's/^.*Name:\s*(\S*).*/\1/p')
65 btrfs subvolume list /boot | awk '{print $9}' | sed "s/$cur/$cur (current)/"
66 exit 0
67 fi
68
69 ###### end command line parsing #####
70
71
72 #### begin initial error checking #####
73
74 if ! btrfs subvolume list /boot | grep "_$distro$" &>/dev/null; then
75 echo "$0: error: _$distro$ not found in btrfs subvolume list /boot:"
76 btrfs subvolume list /boot
77 exit 1
78 fi
79
80 #### end initial error checking #####
81
82 e() { echo "$@"; "$@"; }
83
84 boot_dev=$(mount | sed -rn "s#^(\S+) on /boot .*#\1#p")
85
86 mount_point=$(mktemp -d)
87
88 e mount -o subvol=boot_$distro $boot_dev $mount_point
89
90 boot_disk=${boot_dev%%[0-9]*}
91
92 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
93 # it just generates one if the file doesn't exist.
94 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
95 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
96
97 # todo, mount_point needs subvolid=0
98
99 e umount $mount_point
100
101 e mount $boot_disk$grub_extn $mount_point
102 e grub-editenv $mount_point/grubenv set last_boot=/boot_$distro
103 e grub-editenv $mount_point/grubenv set did_fai_check=true
104 e umount $mount_point
105 e rmdir $mount_point
106
107 if $reboot; then
108 touch /tmp/keyscript-off
109 reboot now
110 fi