for traci different pass and a home subvolume
[automated-distro-installer] / fai / config / hooks / partition.DEFAULT
1 #!/bin/bash -x
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
5
6 # # fai's setup-storage won't do btrfs on luks,
7 # # so we do it ourself :)
8
9 partition=false
10
11
12 letters=(a)
13
14 if ifclass VM; then
15 d=/dev/vd
16 else
17 d=/dev/sd
18 fi
19
20 if ifclass TWO_DISK; then
21 skiptask partition
22 devs=(${d}{a,b})
23 [[ -e /dev/md127 ]] || partition=true
24 elif ifclass ONE_DISK; then
25 skiptask partition
26 devs=(${d}a)
27 else
28 exit
29 fi
30
31 # somewhat crude detection of wehter to partition
32 for dev in ${devs[@]}; do
33 for part in ${dev}{1,2,3,4}; do
34 [[ -e $part ]] || partition=true
35 done
36 done
37
38 # keyfiles generated like:
39 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
40 luks_dir=/var/lib/fai/config/distro-install-common/luks
41 if ifclass tp; then
42 lukspw=$(cat $luks_dir/traci)
43 else
44 lukspw=$(cat $luks_dir/ian)
45 fi
46
47 boot_end=504
48
49 ! ifclass tp || letters=(a b)
50
51 md() { ((${#letters[@]} > 1)); }
52
53 if md; then
54 # if partition with md0, then reboot into the installer,
55 # it becomes md127. So might as well start with 127 for simplicity.
56 crypt=md127
57 else
58 crypt=${d##/dev/}a3
59 fi
60
61
62
63 # 1.5 x based on https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide/sect-disk-partitioning-setup-x86.html#sect-custom-partitioning-x86
64 swap_end=$(( $(grep ^MemTotal: /proc/meminfo| awk '{print $2}') * 3/(${#letters[@]} * 2 ) / 1000 + boot_end ))MiB
65
66 create_subvols() {
67 cd /mnt
68 for x in a home root; do
69 btrfs subvolume list . | grep "$x$" >/dev/null || btrfs subvolume create $x
70 done
71 btrfs subvolume set-default \
72 $(btrfs subvolume list . | grep 'root$' | awk '{print $2}') .
73 cd /
74 umount /mnt
75 }
76
77 shopt -s nullglob
78 if $partition; then
79 mkdir -p /tmp/fai
80 for dev in ${devs[@]}; do
81 for x in /dev/md*; do [[ -d $x ]] || mdadm --stop $x; done
82 for x in $dev[0-9]; do wipefs -a $x; done
83 parted -s $dev mklabel gpt
84 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB. ehh, i'll do 4.
85 # also, using MB instead of MiB causes complains about alignment.
86 parted -s $dev mkpart primary "ext3" 4MB ${boot_end}MiB
87 parted -s $dev set 1 boot on
88 parted -s $dev mkpart primary "linux-swap" ${boot_end}MiB $swap_end
89 parted -s -- $dev mkpart primary "" $swap_end -0
90 parted -s $dev set 3 raid on
91 parted -s $dev mkpart primary "" 1MiB 4MiB
92 parted -s $dev set 4 bios_grub on
93 # the mkfs failed randomly on a vm, so I threw a sleep in here.
94 sleep .1
95 mkfs.ext4 -F ${dev}1
96 done
97 if md; then
98 yes | mdadm --create /dev/$crypt --level=raid0 --force --run \
99 --raid-devices=${#devs[@]} ${devs[@]/%/3} || [[ $? == 141 ]]
100 fi
101
102 yes YES | cryptsetup luksFormat /dev/$crypt $luks_dir/host-$HOSTNAME \
103 -c aes-cbc-essiv:sha256 -s 256 || [[ $? == 141 ]]
104 yes "$lukspw" cryptsetup luksAddKey --key-file \
105 $luks_dir/host-$HOSTNAME /dev/$crypt || [[ $? == 141 ]]
106 # this would remove the keyfile. we will do that manually later.
107 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
108 # /key/file || [[ $? == 141 ]]
109 cryptsetup luksOpen /dev/$crypt crypt_dev_$crypt --key-file \
110 $luks_dir/host-$HOSTNAME
111 parted ${devs[0]} set 1 boot on
112 mkfs.btrfs -f /dev/mapper/crypt_dev_$crypt
113 mount /dev/mapper/crypt_dev_$crypt /mnt
114 create_subvols
115 else
116 for dev in ${devs[@]}; do
117 mkfs.ext4 -F ${dev}1
118 done
119 yes "$lukspw" | \
120 cryptsetup luksOpen /dev/$crypt crypt_dev_$crypt || [[ $? == 141 ]]
121 sleep 1
122 mount -o subvolid=0 /dev/mapper/crypt_dev_$crypt /mnt
123 # systemd creates subvolumes we want to delete.
124 s=($(btrfs subvolume list --sort=-path /mnt |
125 sed -rn 's#^.*path\s*(root/\S+)\s*$#\1#p'))
126 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
127 btrfs subvolume set-default 0 /mnt
128 btrfs subvolume delete /mnt/root
129 create_subvols
130 fi
131
132 cat > /tmp/fai/crypttab <<EOF
133 crypt_dev_$crypt /dev/$crypt none keyscript=/root/keyscript,discard,luks
134 EOF
135
136 for dev in ${devs[@]}; do
137 cat >> /tmp/fai/crypttab <<EOF
138 swap ${dev}2 /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
139 EOF
140 done
141
142 # this is duplicated in arch-init
143 cat > /tmp/fai/fstab <<EOF
144 /dev/mapper/crypt_dev_$crypt / btrfs noatime,subvol=/root 0 0
145 /dev/mapper/crypt_dev_$crypt /a btrfs noatime,subvol=/a 0 0
146 /dev/mapper/crypt_dev_$crypt /home btrfs noatime,subvol=/home 0 0
147 ${devs[0]}1 /boot ext4 noatime 0 2
148 EOF
149
150
151 cat >/tmp/fai/disk_var.sh <<EOF
152 ROOT_PARTITION=\${ROOT_PARTITION:-/dev/mapper/crypt_dev_$crypt}
153 BOOT_PARTITION=\${BOOT_PARTITION:-${devs[0]}1}
154 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
155 SWAPLIST=\${SWAPLIST:-"${devs[@]/%/2}"}
156 EOF