4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
6 # # fai's setup-storage won't do btrfs on luks,
7 # # so we do it ourself :)
10 #### begin configuration
12 if ifclass TWO_DISK || ifclass demohost
; then
14 elif ifclass ONE_DISK
; then
19 ##### end configuration
29 skiptask partition ||
: # for running out of fai
30 devs
=(${letters[@]/#//dev/${d}})
31 crypt_devs
=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
33 # we can set this manually to force partitioning
36 # somewhat crude detection of whether to partition
37 for dev
in ${devs[@]}; do
39 [[ ${#x[@]} == 4 ]] || partition
=true
40 for part
in ${dev}{1,2,3,4}; do
41 [[ -e $part ]] || partition
=true
43 # type tells us it's not totally blank
44 for part
in ${dev}{1,3}; do
45 blkid |
grep "^$part:.*TYPE=" &>/dev
/null || partition
=true
49 #partition=true # for temporarily override
51 # keyfiles generated like:
52 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
53 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
55 lukspw
=$
(cat $luks_dir/traci
)
57 lukspw
=$
(cat $luks_dir/ian
)
59 if ifclass demohost
; then
65 crypt=/dev
/mapper
/crypt_dev_
${d##/dev/}a3
69 # 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
70 swap_end
=$
(( $
(grep ^MemTotal
: /proc
/meminfo|
awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1000 + boot_end
))
74 for x
in q home root
; do
75 btrfs subvolume list . |
grep "$x$" >/dev
/null || btrfs subvolume create
$x
77 for x
in root
/a q
/a
; do
82 btrfs subvolume set-default \
83 $
(btrfs subvolume list . |
grep 'root$' |
awk '{print $2}') .
92 for dev
in ${devs[@]}; do
93 for x
in $dev[0-9]; do wipefs
-a $x; done
95 for dev
in ${devs[@]}; do
96 parted
-s $dev mklabel gpt
97 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB. ehh, i'll do 4.
98 # also, using MB instead of MiB causes complains about alignment.
99 parted
-s $dev mkpart primary
"ext3" 4MB
${boot_end}MiB
100 parted
-s $dev set 1 boot on
101 parted
-s $dev mkpart primary
"linux-swap" ${boot_end}MiB
${swap_end}MiB
102 parted
-s -- $dev mkpart primary
"" ${swap_end}MiB
-0
103 parted
-s $dev mkpart primary
"" 1MiB
4MiB
104 parted
-s $dev set 4 bios_grub on
105 # the mkfs failed randomly on a vm, so I threw a sleep in here.
108 # 3 is device which simply holds a key for the 4's,
109 # so we can unlock multi-device btrfs fs with 1 manually entered passphrase.
111 # Background: It's of course possible modify the initramfs to
112 # put the input from a passphrase prompt into a variable and use
113 # it to unlock multiple devices, but that would require figuring
116 for luks_dev
in ${dev}3; do
117 yes YES | cryptsetup luksFormat
$luks_dev $luks_dir/host-
$HOSTNAME \
118 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
120 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
121 $luks_dev ||
[[ $?
== 141 ]]
122 # background: Keyfile and password are treated just
123 # like 2 ways to input a passphrase, so we don't actually need to have
124 # different contents of keyfile and passphrase, but it makes some
125 # security sense to a really big randomly generated passphrase
126 # as much as possible, so we have both.
128 # This would remove the keyfile.
129 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
130 # /key/file || [[ $? == 141 ]]
132 cryptsetup luksOpen
$luks_dev crypt_dev_
${luks_dev##/dev/} \
133 --key-file $luks_dir/host-
$HOSTNAME
136 mkfs.btrfs
-f ${crypt_devs[@]/%/3}
137 parted
${devs[0]} set 1 boot on
141 for dev
in ${devs[@]}; do
143 cryptsetup luksOpen
${dev}3 crypt_dev_
${dev##/dev/}3 \
144 --key-file $luks_dir/host-
$HOSTNAME ||
[[ $?
== 141 ]]
147 mount
-o subvolid
=0 $crypt /mnt
148 # systemd creates subvolumes we want to delete.
149 s
=($
(btrfs subvolume list
--sort=-path /mnt |
150 sed -rn 's#^.*path\s*(root/\S+)\s*$#\1#p'))
151 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
152 btrfs subvolume set-default
0 /mnt
153 btrfs subvolume delete
/mnt
/root
159 cat > /tmp
/fai
/fstab
<<EOF
160 $crypt / btrfs noatime,subvol=/root 0 0
161 $crypt /q btrfs noatime,subvol=/q 0 0
162 /q/a /a none bind 0 0
163 $crypt /home btrfs noatime,subvol=/home 0 0
164 ${devs[0]}1 /boot ext4 noatime 0 2
169 for dev
in ${devs[@]}; do
170 s
=crypt_swap_
${dev##/dev/}2
171 swaps
+=(/dev
/mapper
/$s)
172 cat >>/tmp
/fai
/crypttab
<<EOF
173 crypt_dev_${dev##/dev/}3 ${dev}3 none keyscript=/root/keyscript,discard,luks
174 $s ${dev}2 /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
176 cat >> /tmp
/fai
/fstab
<<EOF
177 /dev/mapper/$s none swap sw 0 0
182 # swaplist seems to do nothing.
183 cat >/tmp
/fai
/disk_var.sh
<<EOF
184 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
185 BOOT_PARTITION=\${BOOT_PARTITION:-${devs[0]}1}
186 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
187 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}