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