mostly fixes
[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 # alternatively, using https://iankelling.org/git/?p=errhandle;a=tree
10 # source /path/errhandle/err
11 # on my machine
12 source /a/bin/errhandle/err
13
14 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
15
16 subvol=$1 # eg ubuntubionic
17 nr=$2 # new root
18
19 if (( $# != 2 )); then
20 echo $0: error expected 2 arguments
21 exit 1
22 fi
23
24 # lots of this is taken from default.PARTITION
25
26 if (($(nproc) > 2)); then
27 mopts=,compress=zstd
28 fi
29
30 rm -f $nr/etc/{fs,crypt}tab
31 while read -r line; do
32 printf "%s none keyscript=decrypt_keyctl,discard,luks,initramfs\n" "$line" >>$nr/etc/crypttab
33 done < <(awk '{print $1,$2}' /mnt/root/root2-crypttab)
34 awk '$3 == "/dev/urandom" {print}' /etc/crypttab >>$nr/etc/crypttab
35
36
37 rootdev=$(awk '$2 == "/mnt/root2" {print $1}' /mnt/root/root2-fstab)
38 fstabstd=x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s
39 bootdev=$(awk '$2 == "/mnt/boot2" {print $1}' /mnt/root/root2-fstab)
40
41
42 cat >> $nr/etc/fstab <<EOF
43 $rootdev / btrfs $fstabstd,noatime,subvol=root_$subvol$mopts 0 0
44 $rootdev /mnt/root btrfs $fstabstd,noatime,subvolid=0$mopts 0 0
45 $bootdev /boot btrfs nofail,$fstabstd,noatime,subvol=boot_$subvol 0 0
46 $bootdev /mnt/boot btrfs nofail,$fstabstd,noatime,subvolid=0 0 0
47 tmpfs /tmp tmpfs nodev,nosuid,size=50%,mode=1777 0 0
48 EOF
49 awk '$2 == "/boot/efi" {print}' /etc/fstab >>$nr/etc/fstab
50
51
52 while read -r mdname; do
53 cat >> $nr/etc/fstab <<EOF
54 /dev/mapper/$mdname none swap nofail,$fstabstd,sw 0 0
55 EOF
56 done < <(awk '$3 == "/dev/urandom" {print $1}' /etc/crypttab)