initial suppport for raid 10
[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 skiptask partition ||: # for running out of fai
10
11 #### begin configuration
12
13 bootn=1
14 swapn=2
15 rootn=3
16 bios_grubn=4
17 boot_end=804
18 lastn=$bios_grubn
19
20 if ifclass VM; then
21 d=vd
22 else
23 d=sd
24 fi
25
26 letters=()
27 if ifclass TWO_DISK; then
28 letters=(a b)
29 elif ifclass ONE_DISK; then
30 letters=(a)
31 elif ifclass MANY_DISK; then
32 for dev in /dev/${d}?; do letters+=(${dev#/dev/${d}}); done
33 else
34 exit 1
35 fi
36 ##### end configuration
37
38
39 bpart() {
40 dev_n=$1
41 case ${#@} in
42 [1-3]) mkfs.btrfs -f $@ ;;
43 [4-9]*|[1-3]?*) mkfs.btrfs -f -m raid10 -d raid10 $@ ;;
44 esac
45 }
46
47 devs=(${letters[@]/#//dev/${d}})
48 crypt_devs=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
49
50 partition=true
51
52 # somewhat crude detection of whether to partition
53 for dev in ${devs[@]}; do
54 x=($dev[0-9])
55 [[ ${#x[@]} == ${lastn} ]] || partition=true
56 for (( i=1; i <= $lastn; i++ )); do
57 [[ -e ${dev}$i ]] || partition=true
58 done
59 for part in $dev$rootn $dev$bootn; do
60 # type tells us it's not totally blank
61 blkid | grep "^${part}:.*TYPE=" &>/dev/null || partition=true
62 done
63 done
64
65 #partition=true # for temporarily override
66
67 # keyfiles generated like:
68 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
69 luks_dir=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
70 if ifclass tp; then
71 lukspw=$(cat $luks_dir/traci)
72 else
73 lukspw=$(cat $luks_dir/ian)
74 fi
75 if ifclass demohost; then
76 lukspw=x
77 fi
78
79
80 crypt=/dev/mapper/crypt_dev_${d##/dev/}a$rootn
81
82
83 if ifclass frodo; then
84 # next upgrade is prolly 16 gigs of memory, across ~8 devices
85 swap_end=$((3500 + boot_end))
86 else
87 # 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
88 swap_end=$(( $(grep ^MemTotal: /proc/meminfo| awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1000 + boot_end ))
89 fi
90
91 mkdir -p /tmp/fai
92 shopt -s nullglob
93 if $partition; then
94 for dev in ${devs[@]}; do
95 for x in $dev[0-9]; do wipefs -a $x; done
96 done
97 for dev in ${devs[@]}; do
98 parted -s $dev mklabel gpt
99 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB. ehh, i'll do 4.
100 # also, using MB instead of MiB causes complains about alignment.
101 parted -s $dev mkpart primary "ext3" 4MB ${boot_end}MiB
102 parted -s $dev mkpart primary "linux-swap" ${boot_end}MiB ${swap_end}MiB
103 parted -s -- $dev mkpart primary "" ${swap_end}MiB -0
104 parted -s $dev mkpart primary "" 1MiB 4MiB
105 parted -s $dev set $bios_grubn bios_grub on
106 parted -s $dev set $bootn boot on # generally not needed on modern systems
107 # the mkfs failed randomly on a vm, so I threw a sleep in here.
108 sleep .1
109 mkfs.ext4 -F ${dev}1
110 # 3 is device which simply holds a key for the 4's,
111 # so we can unlock multi-device btrfs fs with 1 manually entered passphrase.
112 #
113 # Background: It's of course possible modify the initramfs to
114 # put the input from a passphrase prompt into a variable and use
115 # it to unlock multiple devices, but that would require figuring
116 # more things out.
117 #
118 for luks_dev in ${dev}3; do
119 yes YES | cryptsetup luksFormat $luks_dev $luks_dir/host-$HOSTNAME \
120 -c aes-cbc-essiv:sha256 -s 256 || [[ $? == 141 ]]
121 yes "$lukspw" | \
122 cryptsetup luksAddKey --key-file $luks_dir/host-$HOSTNAME \
123 $luks_dev || [[ $? == 141 ]]
124 # background: Keyfile and password are treated just
125 # like 2 ways to input a passphrase, so we don't actually need to have
126 # different contents of keyfile and passphrase, but it makes some
127 # security sense to a really big randomly generated passphrase
128 # as much as possible, so we have both.
129 #
130 # This would remove the keyfile.
131 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
132 # /key/file || [[ $? == 141 ]]
133
134 cryptsetup luksOpen $luks_dev crypt_dev_${luks_dev##/dev/} \
135 --key-file $luks_dir/host-$HOSTNAME
136 done
137 done
138 #bpart ${devs[@]/%/$rootn}
139 bpart ${crypt_devs[@]/%/3}
140 parted ${devs[0]} set 1 boot on
141 mount $crypt /mnt
142 else
143 for dev in ${devs[@]}; do
144 mkfs.ext4 -F ${dev}1
145 cryptsetup luksOpen ${dev}3 crypt_dev_${dev##/dev/}3 \
146 --key-file $luks_dir/host-$HOSTNAME || [[ $? == 141 ]]
147 done
148 sleep 1
149 mount -o subvolid=0 $crypt /mnt
150 # systemd creates subvolumes we want to delete.
151 s=($(btrfs subvolume list --sort=-path /mnt |
152 sed -rn 's#^.*path\s*(root/\S+)\s*$#\1#p'))
153 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
154 btrfs subvolume set-default 0 /mnt
155 btrfs subvolume delete /mnt/root
156 fi
157
158 ## create subvols ##
159 cd /mnt
160 for x in q home root; do
161 btrfs subvolume list . | grep "$x$" >/dev/null || btrfs subvolume create $x
162 done
163 for x in root/a q/a; do
164 mkdir -p $x
165 chown 1000:1000 $x
166 chmod 755 $x
167 done
168 btrfs subvolume set-default \
169 $(btrfs subvolume list . | grep 'root$' | awk '{print $2}') .
170 chattr -Rf +C root
171 cd /
172 umount /mnt
173 ## end create subvols ##
174
175
176
177 cat > /tmp/fai/fstab <<EOF
178 $crypt / btrfs noatime,subvol=/root 0 0
179 $crypt /q btrfs noatime,subvol=/q 0 0
180 /q/a /a none bind 0 0
181 $crypt /home btrfs noatime,subvol=/home 0 0
182 ${devs[0]}1 /boot ext4 noatime 0 2
183 EOF
184
185
186 swaps=()
187 for dev in ${devs[@]}; do
188 s=crypt_swap_${dev##/dev/}$swapn
189 swaps+=(/dev/mapper/$s)
190 cat >>/tmp/fai/crypttab <<EOF
191 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
192 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
193 EOF
194 cat >> /tmp/fai/fstab <<EOF
195 /dev/mapper/$s none swap sw 0 0
196 EOF
197 done
198
199
200 # swaplist seems to do nothing.
201 cat >/tmp/fai/disk_var.sh <<EOF
202 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
203 BOOT_PARTITION=\${BOOT_PARTITION:-${devs[0]}1}
204 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
205 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}
206 EOF