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