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