2 # Copyright (C) 2016 Ian Kelling
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
22 if [[ $EUID != 0 ]]; then
23 echo "$0: error: need to run as root" >&2
27 # for calling outside of FAI:
28 # # need to redep and set that location so that we get luks keys
29 # export FAI=/srv/fai/config
31 # source /b/fai/fai-wrapper
32 # - set any appropriate classes with: fai-setclass OPT1... which sets CLASS_OPT1=true...
33 # or run eval-fai-classfile FILE
37 # environment variables:
39 # HOSTNAME: if frodo, we exclude 2 devices from the /boot array, which
40 # the bios does not see. if demohost, we set the luks password to just
43 # SPECIAL_DISK: For use outside of fai. A base disk name like
44 # /dev/sdk. If set, we just cryptsetup and partition this one disk then
45 # exit. This is useful for partitioning a disk in preparation to replace
46 # a failed or failing disk from a raid10 array.
50 # REPARTITION: forces repartitioning even if we detect the proper amount
51 # of partitions already exist.
53 # NOWIPE: use existing subvolumes if they exist
55 # ROTATIONAL: forces to install onto hdds instead of sdds. normally sdds
56 # are chosen if they exist.
58 # PARTITION_PROMPT: command line prompt before partitioning
60 # RAID0: forces raid0 filesystem. Normally with 4+ devices, we use
62 # RAID1: forces raid1 filesystem.
64 if [[ $SPECIAL_DISK ]]; then
65 export CLASS_REPARTITION
=true
68 # # fai's setup-storage won't do btrfs on luks,
69 # # so we do it ourself :)
70 # inspiration taken from files in fai-setup-storage package
72 # if we are not running in fai, skiptask won't be defined, so carry on.
73 skiptask partition ||
! type skiptask
75 if ! type -p devbyid
; then
76 for d
in $FAI/distro-install-common \
77 /a
/bin
/fai
/fai
/config
/distro-install-common
$FAI $PWD; do
78 [[ -d $d ]] ||
continue
79 if [[ -e $d/devbyid
]]; then
81 devbyid
() { $devbyid "$@"; }
85 if [[ ! $devbyid ]]; then
86 echo "$0: error: failed to find devbyid script" >&2
93 #### begin configuration
98 # ext partition so grub can write persistent variables,
99 # so it can do a one time boot. grub can't write to
100 # btrfs or any cow fs because it's more
101 # more complicated to do and they don't want to.
103 # bios boot partition,
104 # https://wiki.archlinux.org/index.php/GRUB
110 ##### end configuration
113 add-part
() { # add partition suffix to $dev
115 if [[ $# == 1 ]]; then
122 if [[ $d == /dev
/disk
/by-id
/* ]]; then
130 bootdev
() { add-part $@
$bootn; }
131 rootdev
() { add-part $@
$rootn; }
132 swapdev
() { add-part $@
$swapn; }
133 grub_extdev
() { add-part $@
$grub_extn; }
134 bios_grubdev
() { add-part $@
$bios_grubn; }
136 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
137 crypt-name
() { echo crypt_dev_
${1##*/}; }
138 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
139 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
140 root-cryptname
() { crypt-name $
(rootdev $@
); }
141 swap-cryptname
() { crypt-name $
(swapdev $@
); }
143 ##### end function defs
145 if ifclass REPARTITION
; then
146 partition
=true
# force a full wipe
148 partition
=false
# change to true to force a full wipe
150 if ifclass NOWIPE
; then
157 declare -A disk_excludes
158 if ! ifclass USE_MOUNTED
; then
159 ## ignore disks that are mounted, eg when running from fai-cd
162 if [[ ! $PKNAME ]]; then
165 if [[ $MOUNTPOINT ]]; then
166 disk_excludes
[$PKNAME]=true
168 done < <(lsblk
-nP -o KNAME
,MOUNTPOINT
,PKNAME
)
174 for disk
in [sv
]d
[a-z
]; do
175 if [[ ${disk_excludes[$disk]} ]]; then
178 case $
(cat $disk/queue
/rotational
) in
179 0) ssds
+=(/dev
/$disk) ;;
180 1) hdds
+=(/dev
/$disk) ;;
181 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
182 $(cat $disk/queue/rotational)"; exit 1 ;;
186 # install all ssds, or if there are none, all hdds.
187 # Note, usb flash disks are seen as rotational, which is
188 # very odd, but convenient for ignoring them here.
189 # TODO: find a reliable way to ignore them.
190 if ! ifclass ROTATIONAL
&& (( ${#ssds[@]} > 0 )); then
191 short_devs
=( ${ssds[@]} )
193 short_devs
=( ${hdds[@]} )
196 # check if the partitions exist have the right filesystems
197 #blkid="$(blkid -s TYPE)"
198 for dev
in ${short_devs[@]}; do
199 if $partition; then break; fi
200 y
=$
(readlink
-f $dev)
202 [[ ${#arr[@]} == "$lastn" ]] || partition
=true
203 for (( i
=1; i
<= lastn
; i
++ )); do
204 [[ -e ${dev}$i ]] || partition
=true
206 # On one system, blkid is missing some partitions.
207 # maybe we need a flag, like FUZZY_BLKID or something, so we
208 # can check that at least some exist.
209 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
210 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
214 if $partition && ifclass PARTITION_PROMPT
; then
215 echo "Press any key except ctrl-c to continue and partition these drives:"
216 echo " ${short_devs[*]}"
222 for short_dev
in ${short_devs[@]}; do
223 devs
+=($
(devbyid
$short_dev))
225 if [[ ! ${devs[0]} ]]; then
226 echo "$0: error: failed to detect devs" >&2
233 for dev
in ${devs[@]}; do
234 if ifclass frodo
; then
235 # I ran into a machine where the bios doesn't know about some disks,
236 # so 1st stage of grub also doesn't know about them.
237 # Also, grub does not support mounting degraded btrfs as far as
238 # I can tell with some googling.
239 # From within an arch install env, I could detect them by noting
240 # their partitions were mixed with the next disk in /dev/disk/by-path,
241 # and I have mixed model disks, and I could see the 8 models which showed
242 # up in the bios, and thus see which 2 models were missing.
243 # hdparm -I /dev/sdh will give model info in linux.
244 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
245 # and I don't see another way, so I'm hardcoding them.
246 # We still put grub on them and partition them the same, for uniformity
247 # and in case they get moved to a system that can recognize them,
248 # we just exclude them from the boot filesystem.
251 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
252 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
253 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
258 $bad_disk || boot_devs
+=($
(bootdev
))
260 boot_space
=$
(( boot_space
+ $
(parted
-m $dev unit MiB print | \
261 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
262 boot_devs
+=($
(bootdev
))
264 if [[ $boot_devs && $first ]]; then
265 first_grub_extdev
=$
(grub_extdev
)
270 if ifclass RAID0 ||
(( ${#boot_devs[@]} == 1 )); then
272 elif ifclass RAID1 ||
(( ${#boot_devs[@]} <= 3 )); then
278 ### Begin calculate boot partition space
279 # due to raid duplication
281 1*) boot_space
=$
(( boot_space
/ 2 )) ;;
283 if (( boot_space
> 60000 )); then
284 # this is larger than needed for several /boot subvols,
285 # becuase I keep a minimal debian install on it for
286 # recovery needs and for doing pxe-kexec.
288 elif (( boot_space
> 30000 )); then
289 boot_mib
=$
(( 5000 + (boot_space
- 30000) / 2 ))
291 # Small vms don't have room for /boot recovery. With 3 kernels
292 # installed, i'm using 132M on t8, so this seems like plenty of
293 # room. note: rhel 8 recomments 1g for /boot.
298 1*) boot_mib
=$
(( boot_mib
* 2 )) ;;
300 ### end calculate boot partition space
304 if [[ ! $DISTRO ]]; then
305 if ifclass VOL_STRETCH_BOOTSTRAP
; then
306 DISTRO
=debianstretch_bootstrap
307 elif ifclass VOL_STRETCH
; then
309 elif ifclass VOL_BUSTER
; then
311 elif ifclass VOL_TESTING
; then
313 elif ifclass VOL_XENIAL
; then
315 elif ifclass VOL_BIONIC
; then
317 elif ifclass VOL_FOCAL
; then
319 elif ifclass VOL_BELENOS
; then
320 DISTRO
=trisquelbelenos
321 elif ifclass VOL_FLIDAS
; then
322 DISTRO
=trisquelflidas
323 elif ifclass VOL_ETIONA
; then
324 DISTRO
=trisqueletiona
326 echo "PARTITIONER ERROR: no distro class/var set" >&2
330 first_boot_dev
=${boot_devs[0]}
333 bpart
() { # btrfs a partition
335 0) mkfs.btrfs
-f $@
;;
336 1) mkfs.btrfs
-f -m raid1
-d raid1 $@
;;
337 10) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
342 if [[ ! -e /a
/bin
/fai
/fai-wrapper
]]; then
343 # see README for docs about how to create these
344 luks_dir
=$FAI/distro-install-common
/luks
346 luks_file
=$luks_dir/host-
$HOSTNAME
347 if [[ ! -e $luks_file ]]; then
348 hostkeys
=($luks_dir/host-
*)
349 # if there is only one key, we might be deploying somewhere
350 # where dhcp doesnt give us a proper hostname, so use that.
351 if [[ ${#hostkeys[@]} == 1 && -e ${hostkeys[0]} ]]; then
352 luks_file
=${hostkeys[0]}
354 echo "$0: error: no key for hostname at $luks_file" >&2
359 # # note, corresponding changes in /b/ds/keyscript-{on,off}
360 if ifclass demohost
; then
362 elif [[ -e $luks_dir/$HOSTNAME ]]; then
363 lukspw
=$
(cat $luks_dir/$HOSTNAME)
365 lukspw
=$
(cat $luks_dir/iank
)
370 first_root_crypt
=$
(root-cryptdev
${devs[0]})
372 # 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
373 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
374 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
378 for dev
in ${devs[@]}; do
379 root_devs
+=($
(rootdev
))
385 if [[ ! $SPECIAL_DISK ]]; then
386 for dev
in ${devs[@]}; do
387 # if we repartition to the same as an old partition,
388 # we don't want any old fses hanging around.
389 for (( i
=1; i
<= lastn
; i
++ )); do
390 x
=$
(add-part
$dev $i)
391 [[ -e $x ]] ||
continue
393 # wipefs has failed, manual run works, google suggests timing issue
394 while ! wipefs
-a $x; do
396 count_down
=$
((count_down
- 1))
397 (( count_down
> 0 )) ||
exit 1
404 for dev
in ${devs[@]}; do
405 if [[ $SPECIAL_DISK ]]; then
406 dev
=$
(devbyid
$SPECIAL_DISK)
409 # parted will round up the disk size. Do -1 so we can have
410 # fully 1MiB unit partitions for easy resizing of the last partition.
411 # Otherwise we would pass in -0 for the end argument for the last partition.
413 # Note: parted print error output is expected. example:
414 # Error: /dev/vda: unrecognised disk label
415 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
416 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
417 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
418 swap_end
=$
(( root_end
+ swap_mib
))
420 parted
-s $dev mklabel gpt
421 # MiB because parted complains about alignment otherwise.
422 pcmd
="parted -a optimal -s -- $dev"
423 $pcmd mkpart primary ext3
12MiB
${root_end}MiB
424 # without naming, systemd gives us misc errors like:
425 # dev-disk-by\x2dpartlabel-primary.device: Dev dev-disk-by\x2dpartlabel-primary.device appeared twice
426 $pcmd name
$rootn root
427 # normally a swap is type "linux-swap", but this is encrypted swap. using that
428 # label will confuse systemd.
429 $pcmd mkpart primary
"" ${root_end}MiB
${swap_end}MiB
430 $pcmd name
$swapn swap
431 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
432 $pcmd name
$bootn boot
433 # i only need a few k, but googling min size,
434 # I found someone saying that gparted required
435 # required at least 8 because of their hard drive cylinder size.
436 # And 8 is still very tiny.
437 $pcmd mkpart primary
"ext2" 4MiB
12MiB
438 $pcmd name
$grub_extn grubext
439 # gpt ubuntu cloud image uses ~4 mb for this partition. fai uses 1 MiB.
440 # so, I use 3, whatever.
441 # note: parted manual saying cheap flash media
442 # should to start at 4.
443 $pcmd mkpart primary
"" 1MiB
4MiB
444 $pcmd name
$bios_grubn biosgrub
445 $pcmd set $bios_grubn bios_grub on
446 $pcmd set $bootn boot on
# generally not needed on modern systems
447 # the mkfs failed before on a vm, which prompted me to add
449 # then it failed again on a physical machine
451 # Device /dev/disk/by-id/foo doesn't exist or access denied,
452 # so I added a wait until it existed.
453 # Then I added the mkfs.ext2, which claimed to succeed,
454 # but then couldn't be found upon reboot. In that case we didn't
455 # wait at all. So I've added a 3 second minimum wait.
458 while [[ ! -e $
(rootdev
) ]] && (( secs
< 10 )); do
462 # Holds just a single file, rarely written, so
463 # use ext2, like was often used for the /boot partition.
464 # This exists because grub can only persist data to a non-cow fs.
465 # And we use persisting a var in grub to do a one time boot.
466 # We could pass the data on the kernel command line and persist it
467 # to grubenv after booting, but that relies on the boot always succeeding.
468 # This is just a bit more robust, and it could work for booting
469 # into ipxe which can't persist data, if we ever got that working.
470 mkfs.ext2 $
(grub_extdev
)
471 # when we move to newer than trisquel 8, we can remove
472 # --type luks1. We can also check on cryptsetup --help | less /compil
473 # to see about the other settings. Default in debian 9 is luks2.
474 # You can convert from luks2 to luks 1 by adding a temporary key:
475 # cryptsetup luksAddKey --pbkdf pbkdf2
476 # then remove the new format keys with cryptsetup luksRemoveKey
477 # then cryptsetup convert DEV --type luks1, then readd old keys and remove temp.
478 yes YES | cryptsetup luksFormat $
(rootdev
) $luks_file \
479 --type luks1
-c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
481 cryptsetup luksAddKey
--key-file $luks_file \
482 $
(rootdev
) ||
[[ $?
== 141 ]]
483 # background: Keyfile and password are treated just
484 # like 2 ways to input a passphrase, so we don't actually need to have
485 # different contents of keyfile and passphrase, but it makes some
486 # security sense to a really big randomly generated passphrase
487 # as much as possible, so we have both.
489 # This would remove the keyfile.
490 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
491 # /key/file || [[ $? == 141 ]]
493 cryptsetup luksOpen $
(rootdev
) $
(root-cryptname
) \
494 --key-file $luks_file
496 if [[ $SPECIAL_DISK ]]; then
500 ls -la /dev
/btrfs-control
# this was probably for debugging...
502 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
503 bpart
${boot_devs[@]}
505 for dev
in ${devs[@]}; do
506 if [[ -e /dev
/mapper
/$
(root-cryptname
) ]]; then
509 cryptsetup luksOpen $
(rootdev
) $
(root-cryptname
) \
510 --key-file $luks_file
516 if $wipe && [[ $DISTRO != debianstretch_bootstrap
]]; then
517 # bootstrap distro doesn't use separate encrypted root.
518 mount
-o subvolid
=0 $first_root_crypt /mnt
519 # systemd creates subvolumes we want to delete.
520 s
=($
(btrfs subvolume list
--sort=-path /mnt |
521 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
522 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
523 btrfs subvolume set-default
0 /mnt
524 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
529 btrfs subvolume create root_
$DISTRO
531 # could set default subvol like this, but no reason to.
532 # btrfs subvolume set-default \
533 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
535 # no cow on the root filesystem. it's setup is fully scripted,
536 # if it's messed up, we will just recreated it,
537 # and we can get better perf with this.
538 # I can't remember exactly why, but this is preferable to mounting with
539 # -o nodatacow, I think because subvolumes inherit that.
540 chattr -Rf +C root_
$DISTRO
545 mount
-o subvolid
=0 $first_boot_dev /mnt
547 btrfs subvolume set-default
0 /mnt
# already default, just ensuring it.
549 # for libreboot systems. grub2 only reads from subvolid=0
551 cp $FAI/distro-install-common
/libreboot_grub.cfg
/mnt
/grub2
553 if [[ $DISTRO == debianstretch_bootstrap
]]; then
554 # this is just convenience for the libreboot_grub config
555 # so we can glob the other ones easier.
558 boot_vol
=boot_
$DISTRO
560 if $wipe && [[ -e /mnt
/$boot_vol ]]; then
561 btrfs subvolume delete
/mnt
/$boot_vol
563 if [[ ! -e /mnt
/$boot_vol ]]; then
564 btrfs subvolume create
$boot_vol
568 ## end create subvols ##
571 mount
$first_grub_extdev /mnt
572 grub-editenv
/mnt
/grubenv
set did_fai_check
=true
573 grub-editenv
/mnt
/grubenv
set last_boot
=/$boot_vol
576 if [[ $DISTRO == debianstretch_bootstrap
]]; then
577 cat > /tmp
/fai
/fstab
<<EOF
578 $first_boot_dev / btrfs noatime,subvol=$boot_vol 0 0
580 cat >/tmp
/fai
/disk_var.sh
<<EOF
581 BOOT_DEVICE="${short_devs[@]}"
582 ROOT_PARTITION=$first_boot_dev
585 # note, fai creates the mountpoints listed here
586 cat > /tmp
/fai
/fstab
<<EOF
587 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
588 $first_root_crypt /mnt/root btrfs nofail,noatime,subvolid=0 0 0
589 $first_boot_dev /boot btrfs nofail,noatime,subvol=$boot_vol 0 0
590 $first_boot_dev /mnt/boot btrfs nofail,noatime,subvolid=0 0 0
593 rm -f /tmp
/fai
/crypttab
594 for dev
in ${devs[@]}; do
595 swaps
+=($
(swap-cryptname
))
596 cat >>/tmp
/fai
/crypttab
<<EOF
597 $(root-cryptname) $(rootdev) none keyscript=/root/keyscript,discard,luks,initramfs
598 $(swap-cryptname) $(swapdev) /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
600 cat >> /tmp
/fai
/fstab
<<EOF
601 $(swap-cryptdev) none swap nofail,sw 0 0
606 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
608 # note: swaplist seems to do nothing.
609 cat >/tmp
/fai
/disk_var.sh
<<EOF
610 BOOT_DEVICE="${short_devs[@]}"
611 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
612 # ROOT_PARTITIONS is added by me, used in arch setup.
613 ROOT_PARTITIONS="${root_devs[@]}"
614 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
615 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}