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