rename err to bash-bear
[distro-setup] / gen-amy-fstab
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: Apache-2.0
4
5 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
6 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
7 set -eE -o pipefail
8 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
9 source /a/bin/bash-bear-trap/bash-bear
10
11 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
12
13 subvol=$1 # eg ubuntubionic
14 nr=$2 # new root
15
16 if (( $# != 2 )); then
17 echo $0: error expected 2 arguments
18 exit 1
19 fi
20
21 # lots of this is taken from default.PARTITION
22
23 if (($(nproc) > 2)); then
24 mopts=,compress=zstd
25 fi
26
27 rm -f $nr/etc/{fs,crypt}tab
28 while read -r line; do
29 printf "%s none keyscript=decrypt_keyctl,discard,luks,initramfs\n" "$line" >>$nr/etc/crypttab
30 done < <(awk '{print $1,$2}' /mnt/root/root2-crypttab)
31 awk '$3 == "/dev/urandom" {print}' /etc/crypttab >>$nr/etc/crypttab
32
33
34 rootdev=$(awk '$2 == "/mnt/root2" {print $1}' /mnt/root/root2-fstab)
35 fstabstd=x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s
36 bootdev=$(awk '$2 == "/mnt/boot2" {print $1}' /mnt/root/root2-fstab)
37
38
39 cat >> $nr/etc/fstab <<EOF
40 $rootdev / btrfs $fstabstd,noatime,subvol=root_$subvol$mopts 0 0
41 $rootdev /mnt/root btrfs $fstabstd,noatime,subvolid=0$mopts 0 0
42 $bootdev /boot btrfs nofail,$fstabstd,noatime,subvol=boot_$subvol 0 0
43 $bootdev /mnt/boot btrfs nofail,$fstabstd,noatime,subvolid=0 0 0
44 tmpfs /tmp tmpfs nodev,nosuid,size=50%,mode=1777 0 0
45 EOF
46 awk '$2 == "/boot/efi" {print}' /etc/fstab >>$nr/etc/fstab
47
48
49 while read -r mdname; do
50 cat >> $nr/etc/fstab <<EOF
51 /dev/mapper/$mdname none swap nofail,$fstabstd,sw 0 0
52 EOF
53 done < <(awk '$3 == "/dev/urandom" {print $1}' /etc/crypttab)