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
20 ##### end configuration
22 if ifclass REPARTITION
;then
23 partition
=true
# force a full wipe
25 partition
=false
# change to true to force a full wipe
34 for disk
in [sv
]d
[a-z
]; do
35 case $
(cat $disk/queue
/rotational
) in
36 0) ssds
+=(/dev
/$disk) ;;
37 1) hds
+=(/dev
/$disk) ;;
38 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
39 $(cat $disk/queue/rotational)"; exit 1 ;;
43 # install all ssds, or if there are none, all hdds
44 if (( ${#ssds[@]} > 0 )); then
51 if [[ ! $DISTRO ]]; then
52 if ifclass STABLE
; then
61 bpart
() { # btrfs a partition
64 [1-3]) mkfs.btrfs
-f $@
;;
65 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
69 first_boot_dev
=${devs[0]}$bootn
72 # somewhat crude detection of whether to partition
73 for dev
in ${devs[@]}; do
74 crypt_devs
+=( /dev
/mapper
/crypt_dev_
${dev#/dev/} )
76 [[ ${#x[@]} == ${lastn} ]] || partition
=true
77 for (( i
=1; i
<= $lastn; i
++ )); do
78 [[ -e ${dev}$i ]] || partition
=true
80 for part
in $dev$rootn $dev$bootn; do
81 # type tells us it's not totally blank
82 blkid |
grep "^${part}:.*TYPE=" &>/dev
/null || partition
=true
86 # keyfiles generated like:
87 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
88 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
90 lukspw
=$
(cat $luks_dir/traci
)
92 lukspw
=$
(cat $luks_dir/ian
)
94 if ifclass demohost
; then
99 crypt=${crypt_devs[0]}$rootn
102 # 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
103 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
104 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
105 # parted will round up the disk size. Do -1 so we can have
106 # fully 1MiB unit partitions for easy resizing of the last partition.
107 # Otherwise we would pass in -0 for the end argument for the last partition.
108 disk_mib
=$
(( $
(parted
-m ${devs[0]} unit MiB print | \
109 sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
110 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#devs[@]} ))
111 swap_end
=$
(( root_end
+ swap_mib
))
116 for dev
in ${devs[@]}; do
117 for x
in $dev[0-9]; do
119 # wipefs has failed, manual run works, google suggests timing issue
120 while ! wipefs
-a $x; do
122 count_down
=$
((count_down
- 1))
123 (( count_down
> 0 )) ||
exit 1
127 for dev
in ${devs[@]}; do
128 parted
-s $dev mklabel gpt
129 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
130 # I read something in the parted manual saying cheap flash media
131 # likes to start at 4.
132 # MiB because parted complains about alignment otherwise.
133 pcmd
="parted -a optimal -s -- $dev"
134 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
135 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
136 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
137 $pcmd mkpart primary
"" 1MiB
4MiB
138 $pcmd set $bios_grubn bios_grub on
139 $pcmd set $bootn boot on
# generally not needed on modern systems
140 # the mkfs failed randomly on a vm, so I threw a sleep in here.
144 yes YES | cryptsetup luksFormat
$luks_dev $luks_dir/host-
$HOSTNAME \
145 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
147 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
148 $luks_dev ||
[[ $?
== 141 ]]
149 # background: Keyfile and password are treated just
150 # like 2 ways to input a passphrase, so we don't actually need to have
151 # different contents of keyfile and passphrase, but it makes some
152 # security sense to a really big randomly generated passphrase
153 # as much as possible, so we have both.
155 # This would remove the keyfile.
156 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
157 # /key/file || [[ $? == 141 ]]
159 cryptsetup luksOpen
$luks_dev crypt_dev_
${luks_dev##/dev/} \
160 --key-file $luks_dir/host-
$HOSTNAME
162 bpart
${crypt_devs[@]/%/$rootn}
163 bpart
${devs[@]/%/$bootn}
165 for dev
in ${devs[@]}; do
166 cryptsetup luksOpen
$dev$rootn crypt_dev_
${dev##/dev/}$rootn \
167 --key-file $luks_dir/host-
$HOSTNAME ||
[[ $?
== 141 ]]
172 mount
-o subvolid
=0 $crypt /mnt
173 # systemd creates subvolumes we want to delete.
174 s
=($
(btrfs subvolume list
--sort=-path /mnt |
175 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
176 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
177 btrfs subvolume set-default
0 /mnt
178 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
183 for x
in q home_
$DISTRO root_
$DISTRO; do
184 btrfs subvolume list . |
grep "$x$" >/dev
/null || btrfs subvolume create
$x
187 mkdir
-p /mnt
/root_
$DISTRO/boot
188 for x
in root
/a q
/a
; do
193 # could set default like this, but no reason to.
194 # btrfs subvolume set-default \
195 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
196 chattr -Rf +C root_
$DISTRO
199 mount
-o subvolid
=0 $first_boot_dev /mnt
201 btrfs subvolume set-default
0 /mnt
202 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
203 btrfs subvolume create boot_
$DISTRO
206 ## end create subvols ##
210 cat > /tmp
/fai
/fstab
<<EOF
211 $crypt / btrfs noatime,subvol=root_$DISTRO 0 0
212 $crypt /q btrfs noatime,subvol=q 0 0
213 /q/a /a none bind 0 0
214 $crypt /home btrfs noatime,subvol=home_$DISTRO 0 0
215 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
219 for dev
in ${devs[@]}; do
220 s
=crypt_swap_
${dev##/dev/}$swapn
221 swaps
+=(/dev
/mapper
/$s)
222 cat >>/tmp
/fai
/crypttab
<<EOF
223 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
224 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
226 cat >> /tmp
/fai
/fstab
<<EOF
227 /dev/mapper/$s none swap sw 0 0
232 # swaplist seems to do nothing.
233 cat >/tmp
/fai
/disk_var.sh
<<EOF
234 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
235 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
236 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
237 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}