expand to multiple hosts starting with tp
[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 partition=false
10
11 # keyfiles generated like:
12 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
13 luks_dir=/var/lib/fai/config/distro-install-common/luks
14
15 letters=(a)
16
17 if ifclass VM; then
18 d=/dev/vd
19 else
20 d=/dev/sd
21 fi
22
23 if ifclass TWO_DISK; then
24 skiptask partition
25 devs=(${d}{a,b})
26 [[ -e /dev/md127 ]] || partition=true
27 elif ifclass ONE_DISK; then
28 skiptask partition
29 devs=(${d}a)
30 else
31 exit
32 fi
33
34 # somewhat crude detection of wehter to partition
35 for dev in ${devs[@]}; do
36 for part in ${dev}{1,2,3,4}; do
37 [[ -e $part ]] || partition=true
38 done
39 done
40
41
42 boot_end=504
43
44 ! ifclass tp || letters=(a b)
45
46 md() { ((${#letters[@]} > 1)); }
47
48 if md; then
49 # if partition with md0, then reboot into the installer,
50 # it becomes md127. So might as well start with 127 for simplicity.
51 crypt=md127
52 else
53 crypt=${d##/dev/}a3
54 fi
55
56
57
58 # 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
59 swap_end=$(( $(grep ^MemTotal: /proc/meminfo| awk '{print $2}') * 3/(${#letters[@]} * 2 ) / 1000 + boot_end ))MiB
60
61 shopt -s nullglob
62 if $partition; then
63 mkdir -p /tmp/fai
64 for dev in ${devs[@]}; do
65 for x in /dev/md*; do [[ -d $x ]] || mdadm --stop $x; done
66 for x in $dev[0-9]; do wipefs -a $x; done
67 parted -s $dev mklabel gpt
68 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB. ehh, i'll do 4.
69 # also, using MB instead of MiB causes complains about alignment.
70 parted -s $dev mkpart primary "ext3" 4MB ${boot_end}MiB
71 parted -s $dev set 1 boot on
72 parted -s $dev mkpart primary "linux-swap" ${boot_end}MiB $swap_end
73 parted -s -- $dev mkpart primary "" $swap_end -0
74 parted -s $dev set 3 raid on
75 parted -s $dev mkpart primary "" 1MiB 4MiB
76 parted -s $dev set 4 bios_grub on
77 # the mkfs failed randomly on a vm, so I threw a sleep in here.
78 sleep .1
79 mkfs.ext4 -F ${dev}1
80 done
81 if md; then
82 yes | mdadm --create /dev/$crypt --level=raid0 --force --run \
83 --raid-devices=${#devs[@]} ${devs[@]/%/3} || [[ $? == 141 ]]
84 fi
85
86 yes YES | cryptsetup luksFormat /dev/$crypt $luks_dir/host-$HOSTNAME \
87 -c aes-cbc-essiv:sha256 -s 256 || [[ $? == 141 ]]
88 yes $(cat $luks_dir/traci) | \
89 cryptsetup luksAddKey --key-file \
90 $luks_dir/host-$HOSTNAME /dev/$crypt || [[ $? == 141 ]]
91 # this would remove the keyfile. we will do that manually later.
92 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
93 # /key/file || [[ $? == 141 ]]
94 cryptsetup luksOpen /dev/$crypt crypt_dev_$crypt --key-file \
95 $luks_dir/host-$HOSTNAME
96 parted ${devs[0]} set 1 boot on
97 mkfs.btrfs -f /dev/mapper/crypt_dev_$crypt
98 mount /dev/mapper/crypt_dev_$crypt /mnt
99 cd /mnt
100 btrfs subvolume create a
101 btrfs subvolume create root
102 btrfs subvolume set-default $(btrfs subvolume list . | grep 'root$' | awk '{print $2}') .
103 cd /
104 umount /mnt
105 else
106 for dev in ${devs[@]}; do
107 mkfs.ext4 -F ${dev}1
108 done
109 yes $(cat $luks_dir/traci) | \
110 cryptsetup luksOpen /dev/$crypt crypt_dev_$crypt || [[ $? == 141 ]]
111 sleep 1
112 mount -o subvolid=0 /dev/mapper/crypt_dev_$crypt /mnt
113 # systemd creates subvolumes we want to delete.
114 s=($(btrfs subvolume list --sort=-path /mnt |
115 sed -rn 's#^.*path\s*(root/\S+)\s*$#\1#p'))
116 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
117 btrfs subvolume set-default 0 /mnt
118 btrfs subvolume delete /mnt/root
119 btrfs subvolume create /mnt/root
120 btrfs subvolume set-default $(btrfs subvolume list /mnt | grep 'root$' | awk '{print $2}') /mnt
121 umount /mnt
122 fi
123
124 cat > /tmp/fai/crypttab <<EOF
125 crypt_dev_$crypt /dev/$crypt none keyscript=/root/keyscript,discard,luks
126 EOF
127
128 for dev in ${devs[@]}; do
129 cat >> /tmp/fai/crypttab <<EOF
130 swap ${dev}2 /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
131 EOF
132 done
133
134 # this is duplicated in arch-init
135 cat > /tmp/fai/fstab <<EOF
136 /dev/mapper/crypt_dev_$crypt / btrfs noatime,subvol=/root 0 0
137 /dev/mapper/crypt_dev_$crypt /a btrfs noatime,subvol=/a 0 0
138 ${devs[0]}1 /boot ext4 noatime 0 2
139 EOF
140
141
142 cat >/tmp/fai/disk_var.sh <<EOF
143 ROOT_PARTITION=\${ROOT_PARTITION:-/dev/mapper/crypt_dev_$crypt}
144 BOOT_PARTITION=\${BOOT_PARTITION:-${devs[0]}1}
145 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
146 SWAPLIST=\${SWAPLIST:-"${devs[@]/%/2}"}
147 EOF