fix bugs, support multiple distros via subvolumes
[automated-distro-installer] / chboot
1 #!/bin/bash -x
2
3 # Set grub to boot into a different distro, and reboot unless -r
4 # $0 DISTRO_NAME
5
6 # DISTRO_NAME is based on the partition names in /boot. eg boot_debianjessie
7
8 set -eE -o pipefail
9 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
10
11 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
12
13 reboot=true
14 while [[ $1 == -* ]]; do
15 case $1 in
16 -r) reboot=false; shift ;;
17 --) break ;;
18 esac
19 done
20
21
22 distro=$1
23
24 ###### end command line parsing #####
25
26 if ! btrfs subvolume list /boot | grep "_$distro$" &>/dev/null; then
27 echo "$0: error: _$distro$ not found in btrfs subvolume list /boot:"
28 btrfs subvolume list /boot
29 exit 1
30 fi
31
32 e() { echo "$@"; "$@"; }
33
34 boot_dev=$(mount | sed -rn "s#^(\S+) on /boot .*#\1#p")
35
36 mount_point=$(mktemp -d)
37
38 e mount -o subvol=boot_$distro $boot_dev $mount_point
39
40 boot_disk=${boot_dev%%[0-9]*}
41
42 # arch doesn't have $mount_point/grub/device.map, accoring to the grub manual,
43 # it just generates one if the file doesn't exist.
44 # https://www.gnu.org/software/grub/manual/html_node/Device-map.html
45 e grub-bios-setup -d $mount_point/grub/i386-pc -s -m $mount_point/grub/device.map $boot_disk
46
47 e umount $mount_point
48 e rmdir $mount_point