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 :)
9 skiptask partition ||
: # for running out of fai
11 #### begin configuration
27 if ifclass TWO_DISK
; then
29 elif ifclass ONE_DISK
; then
31 elif ifclass MANY_DISK
; then
32 for dev
in /dev
/${d}?
; do letters
+=(${dev#/dev/${d}}); done
36 ##### end configuration
42 [1-3]) mkfs.btrfs
-f $@
;;
43 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
47 devs
=(${letters[@]/#//dev/${d}})
48 crypt_devs
=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
49 first_boot_dev
=${devs[0]}$bootn
53 # somewhat crude detection of whether to partition
54 for dev
in ${devs[@]}; do
56 [[ ${#x[@]} == ${lastn} ]] || partition
=true
57 for (( i
=1; i
<= $lastn; i
++ )); do
58 [[ -e ${dev}$i ]] || partition
=true
60 for part
in $dev$rootn $dev$bootn; do
61 # type tells us it's not totally blank
62 blkid |
grep "^${part}:.*TYPE=" &>/dev
/null || partition
=true
66 #partition=true # for temporarily override
68 # keyfiles generated like:
69 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
70 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
72 lukspw
=$
(cat $luks_dir/traci
)
74 lukspw
=$
(cat $luks_dir/ian
)
76 if ifclass demohost
; then
81 crypt=/dev
/mapper
/crypt_dev_
${d##/dev/}a
$rootn
84 # 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
85 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
86 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
87 # parted will round up the disk size. Do -1 so we can have
88 # fully 1MiB unit partitions for easy resizing of the last partition.
89 # Otherwise we would pass in -0 for the end argument for the last partition.
90 disk_mib
=$
(( $
(parted
-m ${devs[0]} unit MiB print | \
91 sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
92 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
))
93 swap_end
=$
(( root_end
+ swap_mib
))
98 for dev
in ${devs[@]}; do
99 for x
in $dev[0-9]; do wipefs
-a $x; done
101 for dev
in ${devs[@]}; do
102 parted
-s $dev mklabel gpt
103 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
104 # I read something in the parted manual saying cheap flash media
105 # likes to start at 4.
106 # MiB because parted complains about alignment otherwise.
107 pcmd
="parted -a optimal -s -- $dev"
108 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
109 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
110 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
111 $pcmd mkpart primary
"" 1MiB
4MiB
112 $pcmd set $bios_grubn bios_grub on
113 $pcmd set $bootn boot on
# generally not needed on modern systems
114 # the mkfs failed randomly on a vm, so I threw a sleep in here.
118 yes YES | cryptsetup luksFormat
$luks_dev $luks_dir/host-
$HOSTNAME \
119 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
121 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
122 $luks_dev ||
[[ $?
== 141 ]]
123 # background: Keyfile and password are treated just
124 # like 2 ways to input a passphrase, so we don't actually need to have
125 # different contents of keyfile and passphrase, but it makes some
126 # security sense to a really big randomly generated passphrase
127 # as much as possible, so we have both.
129 # This would remove the keyfile.
130 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
131 # /key/file || [[ $? == 141 ]]
133 cryptsetup luksOpen
$luks_dev crypt_dev_
${luks_dev##/dev/} \
134 --key-file $luks_dir/host-
$HOSTNAME
136 bpart
${crypt_devs[@]/%/$rootn}
139 for dev
in ${devs[@]}; do
140 cryptsetup luksOpen
$dev$rootn crypt_dev_
${dev##/dev/}$rootn \
141 --key-file $luks_dir/host-
$HOSTNAME ||
[[ $?
== 141 ]]
144 mount
-o subvolid
=0 $crypt /mnt
145 # systemd creates subvolumes we want to delete.
146 s
=($
(btrfs subvolume list
--sort=-path /mnt |
147 sed -rn 's#^.*path\s*(root/\S+)\s*$#\1#p'))
148 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
149 btrfs subvolume set-default
0 /mnt
150 btrfs subvolume delete
/mnt
/root
152 bpart
${devs[@]/%/$bootn}
157 for x
in q home root
; do
158 btrfs subvolume list . |
grep "$x$" >/dev
/null || btrfs subvolume create
$x
160 for x
in root
/a q
/a
; do
165 btrfs subvolume set-default \
166 $
(btrfs subvolume list . |
grep 'root$' |
awk '{print $2}') .
170 mount
$first_boot_dev /mnt
172 btrfs subvolume create boot
173 btrfs subvolume set-default \
174 $
(btrfs subvolume list . |
grep 'boot$' |
awk '{print $2}') .
177 ## end create subvols ##
181 cat > /tmp
/fai
/fstab
<<EOF
182 $crypt / btrfs noatime,subvol=/root 0 0
183 $crypt /q btrfs noatime,subvol=/q 0 0
184 /q/a /a none bind 0 0
185 $crypt /home btrfs noatime,subvol=/home 0 0
186 $first_boot_dev /boot btrfs noatime,subvol=/boot 0 0
190 for dev
in ${devs[@]}; do
191 s
=crypt_swap_
${dev##/dev/}$swapn
192 swaps
+=(/dev
/mapper
/$s)
193 cat >>/tmp
/fai
/crypttab
<<EOF
194 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
195 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
197 cat >> /tmp
/fai
/fstab
<<EOF
198 /dev/mapper/$s none swap sw 0 0
203 # swaplist seems to do nothing.
204 cat >/tmp
/fai
/disk_var.sh
<<EOF
205 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
206 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
207 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
208 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}