arch fixes plus name subvols by distro in prep for multi-distro
[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 || ! type skiptask # for running not in fai
10
11 #### begin configuration
12
13 bootn=3
14 rootn=1
15 swapn=2
16 bios_grubn=4
17 boot_mib=750
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
37 if [[ ! $DISTRO ]]; then
38 if ifclass STABLE; then
39 DISTRO=debianjessie
40 else
41 DISTRO=debiantesting
42 fi
43 fi
44
45 ##### end configuration
46
47
48 bpart() { # btrfs a partition
49 dev_n=$1
50 case ${#@} in
51 [1-3]) mkfs.btrfs -f $@ ;;
52 [4-9]*|[1-3]?*) mkfs.btrfs -f -m raid10 -d raid10 $@ ;;
53 esac
54 }
55
56 devs=(${letters[@]/#//dev/${d}})
57 crypt_devs=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
58 first_boot_dev=${devs[0]}$bootn
59
60 partition=true # hardcoded for now
61
62 # somewhat crude detection of whether to partition
63 for dev in ${devs[@]}; do
64 x=($dev[0-9])
65 [[ ${#x[@]} == ${lastn} ]] || partition=true
66 for (( i=1; i <= $lastn; i++ )); do
67 [[ -e ${dev}$i ]] || partition=true
68 done
69 for part in $dev$rootn $dev$bootn; do
70 # type tells us it's not totally blank
71 blkid | grep "^${part}:.*TYPE=" &>/dev/null || partition=true
72 done
73 done
74
75 #partition=true # for temporarily override
76
77 # keyfiles generated like:
78 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
79 luks_dir=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
80 if ifclass tp; then
81 lukspw=$(cat $luks_dir/traci)
82 else
83 lukspw=$(cat $luks_dir/ian)
84 fi
85 if ifclass demohost; then
86 lukspw=x
87 fi
88
89
90 crypt=/dev/mapper/crypt_dev_${d##/dev/}a$rootn
91
92 bios_grub_end=4
93 # 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
94 swap_mib=$(( $(grep ^MemTotal: /proc/meminfo | \
95 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
96 # parted will round up the disk size. Do -1 so we can have
97 # fully 1MiB unit partitions for easy resizing of the last partition.
98 # Otherwise we would pass in -0 for the end argument for the last partition.
99 disk_mib=$(( $(parted -m ${devs[0]} unit MiB print | \
100 sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
101 root_end=$(( disk_mib - swap_mib - boot_mib ))
102 swap_end=$(( root_end + swap_mib))
103
104 mkdir -p /tmp/fai
105 shopt -s nullglob
106 if $partition; then
107 for dev in ${devs[@]}; do
108 for x in $dev[0-9]; do
109 count_down=10
110 # wipefs has failed, manual run works, google suggests timing issue
111 while ! wipefs -a $x; do
112 sleep 2
113 count_down=$((count_down - 1))
114 (( count_down > 0 )) || exit 1
115 done
116 done
117 done
118 for dev in ${devs[@]}; do
119 parted -s $dev mklabel gpt
120 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
121 # I read something in the parted manual saying cheap flash media
122 # likes to start at 4.
123 # MiB because parted complains about alignment otherwise.
124 pcmd="parted -a optimal -s -- $dev"
125 $pcmd mkpart primary "ext3" 4MiB ${root_end}MiB
126 $pcmd mkpart primary "linux-swap" ${root_end}MiB ${swap_end}MiB
127 $pcmd mkpart primary "" ${swap_end}MiB ${disk_mib}MiB
128 $pcmd mkpart primary "" 1MiB 4MiB
129 $pcmd set $bios_grubn bios_grub on
130 $pcmd set $bootn boot on # generally not needed on modern systems
131 # the mkfs failed randomly on a vm, so I threw a sleep in here.
132 sleep .1
133
134 luks_dev=$dev$rootn
135 yes YES | cryptsetup luksFormat $luks_dev $luks_dir/host-$HOSTNAME \
136 -c aes-cbc-essiv:sha256 -s 256 || [[ $? == 141 ]]
137 yes "$lukspw" | \
138 cryptsetup luksAddKey --key-file $luks_dir/host-$HOSTNAME \
139 $luks_dev || [[ $? == 141 ]]
140 # background: Keyfile and password are treated just
141 # like 2 ways to input a passphrase, so we don't actually need to have
142 # different contents of keyfile and passphrase, but it makes some
143 # security sense to a really big randomly generated passphrase
144 # as much as possible, so we have both.
145 #
146 # This would remove the keyfile.
147 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
148 # /key/file || [[ $? == 141 ]]
149
150 cryptsetup luksOpen $luks_dev crypt_dev_${luks_dev##/dev/} \
151 --key-file $luks_dir/host-$HOSTNAME
152 done
153 bpart ${crypt_devs[@]/%/$rootn}
154 bpart ${devs[@]/%/$bootn}
155 else
156 for dev in ${devs[@]}; do
157 cryptsetup luksOpen $dev$rootn crypt_dev_${dev##/dev/}$rootn \
158 --key-file $luks_dir/host-$HOSTNAME || [[ $? == 141 ]]
159 done
160 sleep 1
161 fi
162
163 mount -o subvolid=0 $crypt /mnt
164 # systemd creates subvolumes we want to delete.
165 s=($(btrfs subvolume list --sort=-path /mnt |
166 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
167 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
168 btrfs subvolume set-default 0 /mnt
169 [[ ! -e /mnt/root_$DISTRO ]] || btrfs subvolume delete /mnt/root_$DISTRO
170
171
172 ## create subvols ##
173 cd /mnt
174 for x in q home_$DISTRO root_$DISTRO; do
175 btrfs subvolume list . | grep "$x$" >/dev/null || btrfs subvolume create $x
176 done
177 for x in root/a q/a; do
178 mkdir -p $x
179 chown 1000:1000 $x
180 chmod 755 $x
181 done
182 btrfs subvolume set-default \
183 $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
184 chattr -Rf +C root
185 cd /
186 umount /mnt
187 mount $first_boot_dev /mnt
188 cd /mnt
189 [[ ! -e /mnt/boot_$DISTRO ]] || btrfs subvolume delete /mnt/boot_$DISTRO
190 btrfs subvolume create boot_$DISTRO
191 btrfs subvolume set-default \
192 $(btrfs subvolume list . | grep "boot_$DISTRO$" | awk '{print $2}') .
193 cd /
194 umount /mnt
195 ## end create subvols ##
196
197
198
199 cat > /tmp/fai/fstab <<EOF
200 $crypt / btrfs noatime,subvol=root_$DISTRO 0 0
201 $crypt /q btrfs noatime,subvol=q 0 0
202 /q/a /a none bind 0 0
203 $crypt /home btrfs noatime,subvol=home_$DISTRO 0 0
204 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
205 EOF
206
207 swaps=()
208 for dev in ${devs[@]}; do
209 s=crypt_swap_${dev##/dev/}$swapn
210 swaps+=(/dev/mapper/$s)
211 cat >>/tmp/fai/crypttab <<EOF
212 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
213 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
214 EOF
215 cat >> /tmp/fai/fstab <<EOF
216 /dev/mapper/$s none swap sw 0 0
217 EOF
218 done
219
220
221 # swaplist seems to do nothing.
222 cat >/tmp/fai/disk_var.sh <<EOF
223 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
224 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
225 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
226 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}
227 EOF