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