4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' 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
25 if [[ $# == 1 ]]; then
32 if [[ $d == /dev
/disk
/by-id
/* ]]; then
40 bootdev
() { add-part $@
$bootn; }
41 rootdev
() { add-part $@
$rootn; }
42 swapdev
() { add-part $@
$swapn; }
43 bios_grubdev
() { add-part $@
$bios_grubn; }
45 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
46 crypt-name
() { echo crypt_dev_
${1##*/}; }
47 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
48 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
49 root-cryptname
() { crypt-name $
(rootdev $@
); }
50 swap-cryptname
() { crypt-name $
(swapdev $@
); }
53 ##### end function defs
55 if ifclass REPARTITION
;then
56 partition
=true
# force a full wipe
58 partition
=false
# change to true to force a full wipe
67 for disk
in [sv
]d
[a-z
]; do
68 case $
(cat $disk/queue
/rotational
) in
69 0) ssds
+=(/dev
/$disk) ;;
70 1) hdds
+=(/dev
/$disk) ;;
71 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
72 $(cat $disk/queue/rotational)"; exit 1 ;;
76 # install all ssds, or if there are none, all hdds
77 if (( ${#ssds[@]} > 0 )); then
78 short_devs
=( ${ssds[@]} )
80 short_devs
=( ${hdds[@]} )
83 # check if the partitions exist have the right filesystems
84 #blkid="$(blkid -s TYPE)"
85 for dev
in ${short_devs[@]}; do
89 [[ ${#x[@]} == "${lastn}" ]] || partition
=true
90 for (( i
=1; i
<= lastn
; i
++ )); do
91 [[ -e ${dev}$i ]] || partition
=true
93 # On one system, blkid is missing some partitions.
94 # maybe we need a flag, like FUZZY_BLKID or something, so we
95 # can check that at least some exist.
96 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
97 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
101 if $partition && ifclass PARTITION_PROMPT
; then
102 echo "Press any key except ctrl-c to continue and partition these drives:"
103 echo " ${short_devs[@]}"
109 for short_dev
in ${short_devs[@]}; do
110 devs
+=($
(devbyid
$short_dev))
116 for dev
in ${devs[@]}; do
117 if ifclass frodo
; then
118 # I ran into a machine where the bios doesn't know about some disks,
119 # so 1st stage of grub also doesn't know about them.
120 # Also, grub does not support mounting degraded btrfs as far as
121 # I can tell with some googling.
122 # From within an arch install env, I could detect them by noting
123 # their partitions were mixed with the next disk in /dev/disk/by-path,
124 # and I have mixed model disks, and I could see the 8 models which showed
125 # up in the bios, and thus see which 2 models were missing.
126 # hdparm -I /dev/sdh will give model info in linux.
127 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
128 # and I don't see another way, so I'm hardcoding them.
129 # We still put grub on them and partition them the same, for uniformity
130 # and in case they get moved to a system that can recognize them,
131 # we just exclude them from the boot filesystem.
134 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
135 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
136 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
141 $bad_disk || boot_devs
+=(`bootdev`)
143 boot_devs
+=(`bootdev`)
147 if [[ ! $DISTRO ]]; then
148 if ifclass STABLE
; then
157 case ${#boot_devs[@]} in
158 # need double the space if we are raid 10, and then
159 # might as well give some extra overhead.
160 [4-9]*|
[1-3]?
*) boot_mib
=$
((boot_mib
* 3)) ;;
164 bpart
() { # btrfs a partition
166 [1-3]) mkfs.btrfs
-f $@
;;
167 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
171 first_boot_dev
=${boot_devs[0]}
173 # keyfiles generated like:
174 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
175 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
177 if [[ ! -e $luks_dir/host-
$HOSTNAME ]]; then
178 echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
183 lukspw
=$
(cat $luks_dir/traci
)
185 lukspw
=$
(cat $luks_dir/ian
)
187 if ifclass demohost
; then
192 first_root_crypt
=$
(root-cryptdev
${devs[0]})
195 # 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
196 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
197 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
201 for dev
in ${devs[@]}; do
202 root_devs
+=(`rootdev`)
206 for dev
in ${devs[@]}; do
207 for x
in $dev[0-9]; do
209 # wipefs has failed, manual run works, google suggests timing issue
210 while ! wipefs
-a $x; do
212 count_down
=$
((count_down
- 1))
213 (( count_down
> 0 )) ||
exit 1
217 for dev
in ${devs[@]}; do
218 # parted will round up the disk size. Do -1 so we can have
219 # fully 1MiB unit partitions for easy resizing of the last partition.
220 # Otherwise we would pass in -0 for the end argument for the last partition.
222 # parted print error output is expected. example:
223 # Error: /dev/vda: unrecognised disk label
224 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
225 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
226 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
227 swap_end
=$
(( root_end
+ swap_mib
))
229 parted
-s $dev mklabel gpt
230 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
231 # I read something in the parted manual saying cheap flash media
232 # likes to start at 4.
233 # MiB because parted complains about alignment otherwise.
234 pcmd
="parted -a optimal -s -- $dev"
235 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
236 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
237 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
238 $pcmd mkpart primary
"" 1MiB
4MiB
239 $pcmd set $bios_grubn bios_grub on
240 $pcmd set $bootn boot on
# generally not needed on modern systems
241 # the mkfs failed before on a vm, which prompted me to add
243 # then failed on a physical machine
245 # Device /dev/disk/by-id/foo doesn't exist or access denied,
248 while [[ ! -e `rootdev` ]] && (( secs
< 10 )); do
252 yes YES | cryptsetup luksFormat
`rootdev` $luks_dir/host-
$HOSTNAME \
253 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
255 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
256 `rootdev` ||
[[ $?
== 141 ]]
257 # background: Keyfile and password are treated just
258 # like 2 ways to input a passphrase, so we don't actually need to have
259 # different contents of keyfile and passphrase, but it makes some
260 # security sense to a really big randomly generated passphrase
261 # as much as possible, so we have both.
263 # This would remove the keyfile.
264 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
265 # /key/file || [[ $? == 141 ]]
267 cryptsetup luksOpen
`rootdev` `root-cryptname` \
268 --key-file $luks_dir/host-
$HOSTNAME
270 ls -la /dev
/btrfs-control
272 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
273 bpart
${boot_devs[@]}
275 for dev
in ${devs[@]}; do
276 cryptsetup luksOpen
`rootdev` `root-cryptname` \
277 --key-file $luks_dir/host-
$HOSTNAME
282 mount
-o subvolid
=0 $first_root_crypt /mnt
283 # systemd creates subvolumes we want to delete.
284 s
=($
(btrfs subvolume list
--sort=-path /mnt |
285 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
286 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
287 btrfs subvolume set-default
0 /mnt
288 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
292 btrfs subvolume create root_
$DISTRO
293 [[ -e q
]] || btrfs subvolume create q
296 mkdir
-p /mnt
/root_
$DISTRO/boot
302 # could set default like this, but no reason to.
303 # btrfs subvolume set-default \
304 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
305 chattr -Rf +C root_
$DISTRO
308 mount
-o subvolid
=0 $first_boot_dev /mnt
310 btrfs subvolume set-default
0 /mnt
311 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
312 btrfs subvolume create boot_
$DISTRO
315 ## end create subvols ##
318 # note, the mount point /a seems to get automatically created somewhere
319 cat > /tmp
/fai
/fstab
<<EOF
320 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
321 $first_root_crypt /q btrfs noatime,subvol=q 0 0
322 /q/a /a none bind 0 0
323 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
327 if ifclass treetowl
; then
328 cat >> /tmp
/fai
/fstab
<<'EOF'
329 UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs noatime 0 2
333 if ifclass frodo
; then
334 cat >> /tmp
/fai
/fstab
<<'EOF'
335 /q/i /i none bind 0 0
340 for dev
in ${devs[@]}; do
341 swaps
+=(`swap-cryptname`)
342 cat >>/tmp
/fai
/crypttab
<<EOF
343 `root-cryptname` `rootdev` none keyscript=/root/keyscript,discard,luks
344 `swap-cryptname` `swapdev` /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
346 cat >> /tmp
/fai
/fstab
<<EOF
347 `swap-cryptdev` none swap sw 0 0
352 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
354 # swaplist seems to do nothing.
356 cat >/tmp
/fai
/disk_var.sh
<<EOF
357 # ROOT_PARTITIONS is added by me, used in arch setup.
358 ROOT_PARTITIONS="${root_devs[@]}"
359 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
360 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
361 BOOT_DEVICE="${short_devs[@]}"
362 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}