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 ||
! type skiptask
# for running not in fai
11 #### begin configuration
13 partition
=false
# change to true to force a full wipe
28 if ifclass TWO_DISK
; then
30 elif ifclass ONE_DISK
; then
32 elif ifclass MANY_DISK
; then
33 for dev
in /dev
/${d}?
; do letters
+=(${dev#/dev/${d}}); done
38 if [[ ! $DISTRO ]]; then
39 if ifclass STABLE
; then
46 ##### end configuration
49 bpart
() { # btrfs a partition
52 [1-3]) mkfs.btrfs
-f $@
;;
53 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
57 devs
=(${letters[@]/#//dev/${d}})
58 crypt_devs
=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
59 first_boot_dev
=${devs[0]}$bootn
62 # somewhat crude detection of whether to partition
63 for dev
in ${devs[@]}; do
65 [[ ${#x[@]} == ${lastn} ]] || partition
=true
66 for (( i
=1; i
<= $lastn; i
++ )); do
67 [[ -e ${dev}$i ]] || partition
=true
69 for part
in $dev$rootn $dev$bootn; do
70 # type tells us it's not totally blank
71 blkid |
grep "^${part}:.*TYPE=" &>/dev
/null || partition
=true
75 # keyfiles generated like:
76 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
77 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
79 lukspw
=$
(cat $luks_dir/traci
)
81 lukspw
=$
(cat $luks_dir/ian
)
83 if ifclass demohost
; then
88 crypt=/dev
/mapper
/crypt_dev_
${d##/dev/}a
$rootn
91 # 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
92 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
93 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
94 # parted will round up the disk size. Do -1 so we can have
95 # fully 1MiB unit partitions for easy resizing of the last partition.
96 # Otherwise we would pass in -0 for the end argument for the last partition.
97 disk_mib
=$
(( $
(parted
-m ${devs[0]} unit MiB print | \
98 sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
99 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#devs[@]} ))
100 swap_end
=$
(( root_end
+ swap_mib
))
105 for dev
in ${devs[@]}; do
106 for x
in $dev[0-9]; do
108 # wipefs has failed, manual run works, google suggests timing issue
109 while ! wipefs
-a $x; do
111 count_down
=$
((count_down
- 1))
112 (( count_down
> 0 )) ||
exit 1
116 for dev
in ${devs[@]}; do
117 parted
-s $dev mklabel gpt
118 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
119 # I read something in the parted manual saying cheap flash media
120 # likes to start at 4.
121 # MiB because parted complains about alignment otherwise.
122 pcmd
="parted -a optimal -s -- $dev"
123 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
124 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
125 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
126 $pcmd mkpart primary
"" 1MiB
4MiB
127 $pcmd set $bios_grubn bios_grub on
128 $pcmd set $bootn boot on
# generally not needed on modern systems
129 # the mkfs failed randomly on a vm, so I threw a sleep in here.
133 yes YES | cryptsetup luksFormat
$luks_dev $luks_dir/host-
$HOSTNAME \
134 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
136 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
137 $luks_dev ||
[[ $?
== 141 ]]
138 # background: Keyfile and password are treated just
139 # like 2 ways to input a passphrase, so we don't actually need to have
140 # different contents of keyfile and passphrase, but it makes some
141 # security sense to a really big randomly generated passphrase
142 # as much as possible, so we have both.
144 # This would remove the keyfile.
145 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
146 # /key/file || [[ $? == 141 ]]
148 cryptsetup luksOpen
$luks_dev crypt_dev_
${luks_dev##/dev/} \
149 --key-file $luks_dir/host-
$HOSTNAME
151 bpart
${crypt_devs[@]/%/$rootn}
152 bpart
${devs[@]/%/$bootn}
154 for dev
in ${devs[@]}; do
155 cryptsetup luksOpen
$dev$rootn crypt_dev_
${dev##/dev/}$rootn \
156 --key-file $luks_dir/host-
$HOSTNAME ||
[[ $?
== 141 ]]
161 mount
-o subvolid
=0 $crypt /mnt
162 # systemd creates subvolumes we want to delete.
163 s
=($
(btrfs subvolume list
--sort=-path /mnt |
164 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
165 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
166 btrfs subvolume set-default
0 /mnt
167 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
172 for x
in q home_
$DISTRO root_
$DISTRO; do
173 btrfs subvolume list . |
grep "$x$" >/dev
/null || btrfs subvolume create
$x
175 mkdir
-p /mnt
/root_
$DISTRO/boot
176 for x
in root
/a q
/a
; do
181 # could set default like this, but no reason to.
182 # btrfs subvolume set-default \
183 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
184 chattr -Rf +C root_
$DISTRO
187 mount
-o subvolid
=0 $first_boot_dev /mnt
189 btrfs subvolume set-default
0 /mnt
190 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
191 btrfs subvolume create boot_
$DISTRO
194 ## end create subvols ##
198 cat > /tmp
/fai
/fstab
<<EOF
199 $crypt / btrfs noatime,subvol=root_$DISTRO 0 0
200 $crypt /q btrfs noatime,subvol=q 0 0
201 /q/a /a none bind 0 0
202 $crypt /home btrfs noatime,subvol=home_$DISTRO 0 0
203 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
207 for dev
in ${devs[@]}; do
208 s
=crypt_swap_
${dev##/dev/}$swapn
209 swaps
+=(/dev
/mapper
/$s)
210 cat >>/tmp
/fai
/crypttab
<<EOF
211 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
212 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
214 cat >> /tmp
/fai
/fstab
<<EOF
215 /dev/mapper/$s none swap sw 0 0
220 # swaplist seems to do nothing.
221 cat >/tmp
/fai
/disk_var.sh
<<EOF
222 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
223 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
224 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
225 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}