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
=true
# change to true to force a full wipe
21 ##### end configuration
29 for disk
in [sv
]d
[a-z
]; do
30 case $
(cat $disk/queue
/rotational
) in
31 0) ssds
+=(/dev
/$disk) ;;
32 1) hds
+=(/dev
/$disk) ;;
33 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
34 $(cat $disk/queue/rotational)"; exit 1 ;;
38 # install all ssds, or if there are none, all hdds
39 if (( ${#ssds[@]} > 0 )); then
46 if [[ ! $DISTRO ]]; then
47 if ifclass STABLE
; then
56 bpart
() { # btrfs a partition
59 [1-3]) mkfs.btrfs
-f $@
;;
60 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
64 first_boot_dev
=${devs[0]}$bootn
67 # somewhat crude detection of whether to partition
68 for dev
in ${devs[@]}; do
69 crypt_devs
+=( /dev
/mapper
/crypt_dev_
${dev#/dev/} )
71 [[ ${#x[@]} == ${lastn} ]] || partition
=true
72 for (( i
=1; i
<= $lastn; i
++ )); do
73 [[ -e ${dev}$i ]] || partition
=true
75 for part
in $dev$rootn $dev$bootn; do
76 # type tells us it's not totally blank
77 blkid |
grep "^${part}:.*TYPE=" &>/dev
/null || partition
=true
81 # keyfiles generated like:
82 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
83 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
85 lukspw
=$
(cat $luks_dir/traci
)
87 lukspw
=$
(cat $luks_dir/ian
)
89 if ifclass demohost
; then
94 crypt=${crypt_devs[0]}$rootn
97 # 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
98 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
99 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
100 # parted will round up the disk size. Do -1 so we can have
101 # fully 1MiB unit partitions for easy resizing of the last partition.
102 # Otherwise we would pass in -0 for the end argument for the last partition.
103 disk_mib
=$
(( $
(parted
-m ${devs[0]} unit MiB print | \
104 sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
105 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#devs[@]} ))
106 swap_end
=$
(( root_end
+ swap_mib
))
111 for dev
in ${devs[@]}; do
112 for x
in $dev[0-9]; do
114 # wipefs has failed, manual run works, google suggests timing issue
115 while ! wipefs
-a $x; do
117 count_down
=$
((count_down
- 1))
118 (( count_down
> 0 )) ||
exit 1
122 for dev
in ${devs[@]}; do
123 parted
-s $dev mklabel gpt
124 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
125 # I read something in the parted manual saying cheap flash media
126 # likes to start at 4.
127 # MiB because parted complains about alignment otherwise.
128 pcmd
="parted -a optimal -s -- $dev"
129 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
130 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
131 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
132 $pcmd mkpart primary
"" 1MiB
4MiB
133 $pcmd set $bios_grubn bios_grub on
134 $pcmd set $bootn boot on
# generally not needed on modern systems
135 # the mkfs failed randomly on a vm, so I threw a sleep in here.
139 yes YES | cryptsetup luksFormat
$luks_dev $luks_dir/host-
$HOSTNAME \
140 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
142 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
143 $luks_dev ||
[[ $?
== 141 ]]
144 # background: Keyfile and password are treated just
145 # like 2 ways to input a passphrase, so we don't actually need to have
146 # different contents of keyfile and passphrase, but it makes some
147 # security sense to a really big randomly generated passphrase
148 # as much as possible, so we have both.
150 # This would remove the keyfile.
151 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
152 # /key/file || [[ $? == 141 ]]
154 cryptsetup luksOpen
$luks_dev crypt_dev_
${luks_dev##/dev/} \
155 --key-file $luks_dir/host-
$HOSTNAME
157 bpart
${crypt_devs[@]/%/$rootn}
158 bpart
${devs[@]/%/$bootn}
160 for dev
in ${devs[@]}; do
161 cryptsetup luksOpen
$dev$rootn crypt_dev_
${dev##/dev/}$rootn \
162 --key-file $luks_dir/host-
$HOSTNAME ||
[[ $?
== 141 ]]
167 mount
-o subvolid
=0 $crypt /mnt
168 # systemd creates subvolumes we want to delete.
169 s
=($
(btrfs subvolume list
--sort=-path /mnt |
170 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
171 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
172 btrfs subvolume set-default
0 /mnt
173 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
178 for x
in q home_
$DISTRO root_
$DISTRO; do
179 btrfs subvolume list . |
grep "$x$" >/dev
/null || btrfs subvolume create
$x
181 mkdir
-p /mnt
/root_
$DISTRO/boot
182 for x
in root
/a q
/a
; do
187 # could set default like this, but no reason to.
188 # btrfs subvolume set-default \
189 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
190 chattr -Rf +C root_
$DISTRO
193 mount
-o subvolid
=0 $first_boot_dev /mnt
195 btrfs subvolume set-default
0 /mnt
196 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
197 btrfs subvolume create boot_
$DISTRO
200 ## end create subvols ##
204 cat > /tmp
/fai
/fstab
<<EOF
205 $crypt / btrfs noatime,subvol=root_$DISTRO 0 0
206 $crypt /q btrfs noatime,subvol=q 0 0
207 /q/a /a none bind 0 0
208 $crypt /home btrfs noatime,subvol=home_$DISTRO 0 0
209 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
213 for dev
in ${devs[@]}; do
214 s
=crypt_swap_
${dev##/dev/}$swapn
215 swaps
+=(/dev
/mapper
/$s)
216 cat >>/tmp
/fai
/crypttab
<<EOF
217 crypt_dev_${dev##/dev/}$rootn $dev$rootn none keyscript=/root/keyscript,discard,luks
218 $s $dev$swapn /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
220 cat >> /tmp
/fai
/fstab
<<EOF
221 /dev/mapper/$s none swap sw 0 0
226 # swaplist seems to do nothing.
227 cat >/tmp
/fai
/disk_var.sh
<<EOF
228 ROOT_PARTITION=\${ROOT_PARTITION:-$crypt}
229 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
230 BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
231 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}