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.
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21 # for calling outside of FAI, first
22 # - source /a/bin/fai-wrapper
23 # - set any appropriate classes with: fai-setclass OPT1... which sets CLASS_OPT1=true...
24 # or run eval-fai-classfile FILE
28 # environment variables:
30 # HOSTNAME: if frodo, we exclude 2 devices from the /boot array, which
31 # the bios does not see. if demohost, we set the luks password to just
34 # SPECIAL_DISK: For use outside of fai. A base disk name like
35 # /dev/sdk. If set, we just cryptsetup and partition this one disk then
36 # exit. This is useful for partitioning a disk in preparation to replace
37 # a failed or failing disk from a raid10 array.
41 # REPARTITION: forces repartitioning even if we detect the proper amount
42 # of partitions already exist.
44 # ROTATIONAL: forces to install onto hdds instead of sdds. normally sdds
45 # are chosen if they exist.
47 # PARTITION_PROMPT: command line prompt before partitioning
49 # RAID0: forces raid0 filesystem. Normally with 4+ devices, we use
51 # RAID1: forces raid1 filesystem.
53 if [[ $SPECIAL_DISK ]]; then
54 export CLASS_REPARTITION
=true
57 # # fai's setup-storage won't do btrfs on luks,
58 # # so we do it ourself :)
59 # inspiration taken from files in fai-setup-storage package
61 # if we are not running in fai, skiptask won't be defined, so carry on.
62 skiptask partition ||
! type skiptask
64 if ! type -p devbyid
; then
65 for d
in $FAI/distro-install-common \
66 /a
/bin
/fai
/fai
/config
/distro-install-common
$FAI $PWD; do
67 [[ -d $d ]] ||
continue
68 if [[ -e $d/devbyid
]]; then
70 devbyid
() { $devbyid "$@"; }
74 if [[ ! $devbyid ]]; then
75 echo "$0: error: failed to find devbyid script" >&2
82 #### begin configuration
87 # ext partition so grub can write persistent variables,
88 # so it can do a one time boot. grub can't write to
89 # btrfs or any cow fs because it's more
90 # more complicated to do and they don't want to.
92 # bios boot partition,
93 # https://wiki.archlinux.org/index.php/GRUB
96 # this is larger than needed for several /boot subvols,
97 # becuase I keep a minimal debian install on it, for
98 # recovery needs, and for doing pxe-kexec.
102 ##### end configuration
105 add-part
() { # add partition suffix to $dev
107 if [[ $# == 1 ]]; then
114 if [[ $d == /dev
/disk
/by-id
/* ]]; then
122 bootdev
() { add-part $@
$bootn; }
123 rootdev
() { add-part $@
$rootn; }
124 swapdev
() { add-part $@
$swapn; }
125 grub_extdev
() { add-part $@
$grub_extn; }
126 bios_grubdev
() { add-part $@
$bios_grubn; }
128 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
129 crypt-name
() { echo crypt_dev_
${1##*/}; }
130 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
131 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
132 root-cryptname
() { crypt-name $
(rootdev $@
); }
133 swap-cryptname
() { crypt-name $
(swapdev $@
); }
135 ##### end function defs
137 if ifclass REPARTITION
;then
138 partition
=true
# force a full wipe
140 partition
=false
# change to true to force a full wipe
143 ## ignore disks that are mounted, eg when running from fai-cd
144 declare -A disk_excludes
147 if [[ ! $PKNAME ]]; then
150 if [[ $MOUNTPOINT ]]; then
151 disk_excludes
[$PKNAME]=true
153 done < <(lsblk
-nP -o KNAME
,MOUNTPOINT
,PKNAME
)
159 for disk
in [sv
]d
[a-z
]; do
160 if [[ ${disk_excludes[$disk]} ]]; then
163 case $
(cat $disk/queue
/rotational
) in
164 0) ssds
+=(/dev
/$disk) ;;
165 1) hdds
+=(/dev
/$disk) ;;
166 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
167 $(cat $disk/queue/rotational)"; exit 1 ;;
171 # install all ssds, or if there are none, all hdds.
172 # Note, usb flash disks are seen as rotational, which is
173 # very odd, but convenient for ignoring them here.
174 # TODO: find a reliable way to ignore them.
175 if ! ifclass ROTATIONAL
&& (( ${#ssds[@]} > 0 )); then
176 short_devs
=( ${ssds[@]} )
178 short_devs
=( ${hdds[@]} )
181 # check if the partitions exist have the right filesystems
182 #blkid="$(blkid -s TYPE)"
183 for dev
in ${short_devs[@]}; do
184 if $partition; then break; fi
185 y
=$
(readlink
-f $dev)
187 [[ ${#arr[@]} == "$lastn" ]] || partition
=true
188 for (( i
=1; i
<= lastn
; i
++ )); do
189 [[ -e ${dev}$i ]] || partition
=true
191 # On one system, blkid is missing some partitions.
192 # maybe we need a flag, like FUZZY_BLKID or something, so we
193 # can check that at least some exist.
194 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
195 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
199 if $partition && ifclass PARTITION_PROMPT
; then
200 echo "Press any key except ctrl-c to continue and partition these drives:"
201 echo " ${short_devs[*]}"
207 for short_dev
in ${short_devs[@]}; do
208 devs
+=($
(devbyid
$short_dev))
210 if [[ ! ${devs[0]} ]]; then
211 echo "$0: error: failed to detect devs" >&2
218 for dev
in ${devs[@]}; do
219 if ifclass frodo
; then
220 # I ran into a machine where the bios doesn't know about some disks,
221 # so 1st stage of grub also doesn't know about them.
222 # Also, grub does not support mounting degraded btrfs as far as
223 # I can tell with some googling.
224 # From within an arch install env, I could detect them by noting
225 # their partitions were mixed with the next disk in /dev/disk/by-path,
226 # and I have mixed model disks, and I could see the 8 models which showed
227 # up in the bios, and thus see which 2 models were missing.
228 # hdparm -I /dev/sdh will give model info in linux.
229 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
230 # and I don't see another way, so I'm hardcoding them.
231 # We still put grub on them and partition them the same, for uniformity
232 # and in case they get moved to a system that can recognize them,
233 # we just exclude them from the boot filesystem.
236 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
237 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
238 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
243 $bad_disk || boot_devs
+=($
(bootdev
))
245 boot_devs
+=($
(bootdev
))
247 if [[ $boot_devs && $first ]]; then
248 first_grub_extdev
=$
(grub_extdev
)
253 if ifclass RAID0 ||
(( ${#boot_devs[@]} == 1 )); then
255 elif ifclass RAID1 ||
(( ${#boot_devs[@]} <= 3 )); then
259 # need double the space if we are raid 10, and then
260 # might as well give some extra.
261 boot_mib
=$
((boot_mib
* 3))
266 if [[ ! $DISTRO ]]; then
267 if ifclass VOL_STRETCH_BOOTSTRAP
; then
268 DISTRO
=debianstretch_bootstrap
269 elif ifclass VOL_STRETCH
; then
271 elif ifclass VOL_BUSTER
; then
273 elif ifclass VOL_TESTING
; then
275 elif ifclass VOL_XENIAL
; then
277 elif ifclass VOL_BELENOS
; then
278 DISTRO
=trisquelbelenos
279 elif ifclass VOL_FLIDAS
; then
280 DISTRO
=trisquelflidas
281 elif ifclass VOL_ETIONA
; then
282 DISTRO
=trisqueletiona
284 echo "PARTITIONER ERROR: no distro class/var set" >&2
288 first_boot_dev
=${boot_devs[0]}
291 bpart
() { # btrfs a partition
293 0) mkfs.btrfs
-f $@
;;
294 1) mkfs.btrfs
-f -m raid1
-d raid1 $@
;;
295 10) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
300 # see README for docs about how to create these
301 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
304 luks_file
=$luks_dir/host-
$HOSTNAME
305 if [[ ! -e $luks_file ]]; then
306 hostkeys
=($luks_dir/host-
*)
307 # if there is only one key, we might be deploying somewhere
308 # where dhcp doesnt give us a proper hostname, so use that.
309 if [[ ${#hostkeys[@]} == 1 && -e ${hostkeys[0]} ]]; then
310 luks_file
=${hostkeys[0]}
312 echo "$0: error: no key for hostname at $luks_file" >&2
317 # # note, corresponding changes in /b/ds/keyscript-{on,off}
318 if ifclass tpnew
; then
319 lukspw
=$
(cat $luks_dir/traci
)
320 elif ifclass ziva
; then
321 lukspw
=$
(cat $luks_dir/ziva
)
322 elif ifclass demohost
; then
324 elif [[ -e $luks_dir/$HOSTNAME ]]; then
325 lukspw
=$
(cat $luks_dir/$HOSTNAME)
327 lukspw
=$
(cat $luks_dir/iank
)
331 first_root_crypt
=$
(root-cryptdev
${devs[0]})
333 # 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
334 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
335 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
339 for dev
in ${devs[@]}; do
340 root_devs
+=($
(rootdev
))
346 if [[ ! $SPECIAL_DISK ]]; then
347 for dev
in ${devs[@]}; do
348 # if we repartition to the same as an old partition,
349 # we don't want any old fses hanging around.
350 for (( i
=1; i
<= lastn
; i
++ )); do
351 x
=$
(add-part
$dev $i)
352 [[ -e $x ]] ||
continue
354 # wipefs has failed, manual run works, google suggests timing issue
355 while ! wipefs
-a $x; do
357 count_down
=$
((count_down
- 1))
358 (( count_down
> 0 )) ||
exit 1
365 for dev
in ${devs[@]}; do
366 if [[ $SPECIAL_DISK ]]; then
367 dev
=$
(devbyid
$SPECIAL_DISK)
370 # parted will round up the disk size. Do -1 so we can have
371 # fully 1MiB unit partitions for easy resizing of the last partition.
372 # Otherwise we would pass in -0 for the end argument for the last partition.
374 # Note: parted print error output is expected. example:
375 # Error: /dev/vda: unrecognised disk label
376 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
377 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
378 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
379 swap_end
=$
(( root_end
+ swap_mib
))
381 parted
-s $dev mklabel gpt
382 # MiB because parted complains about alignment otherwise.
383 pcmd
="parted -a optimal -s -- $dev"
384 $pcmd mkpart primary ext3
12MiB
${root_end}MiB
385 # without naming, systemd gives us misc errors like:
386 # dev-disk-by\x2dpartlabel-primary.device: Dev dev-disk-by\x2dpartlabel-primary.device appeared twice
387 $pcmd name
$rootn root
388 # normally a swap is type "linux-swap", but this is encrypted swap. using that
389 # label will confuse systemd.
390 $pcmd mkpart primary
"" ${root_end}MiB
${swap_end}MiB
391 $pcmd name
$swapn swap
392 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
393 $pcmd name
$bootn boot
394 # i only need a few k, but googling min size,
395 # I found someone saying that gparted required
396 # required at least 8 because of their hard drive cylinder size.
397 # And 8 is still very tiny.
398 $pcmd mkpart primary
"ext2" 4MiB
12MiB
399 $pcmd name
$grub_extn grubext
400 # gpt ubuntu cloud image uses ~4 mb for this partition. fai uses 1 MiB.
401 # so, I use 3, whatever.
402 # note: parted manual saying cheap flash media
403 # should to start at 4.
404 $pcmd mkpart primary
"" 1MiB
4MiB
405 $pcmd name
$bios_grubn biosgrub
406 $pcmd set $bios_grubn bios_grub on
407 $pcmd set $bootn boot on
# generally not needed on modern systems
408 # the mkfs failed before on a vm, which prompted me to add
410 # then it failed again on a physical machine
412 # Device /dev/disk/by-id/foo doesn't exist or access denied,
413 # so I added a wait until it existed.
414 # Then I added the mkfs.ext2, which claimed to succeed,
415 # but then couldn't be found upon reboot. In that case we didn't
416 # wait at all. So I've added a 3 second minimum wait.
419 while [[ ! -e $
(rootdev
) ]] && (( secs
< 10 )); do
423 # Holds just a single file, rarely written, so
424 # use ext2, like was often used for the /boot partition.
425 # This exists because grub can only persist data to a non-cow fs.
426 # And we use persisting a var in grub to do a one time boot.
427 # We could pass the data on the kernel command line and persist it
428 # to grubenv after booting, but that relies on the boot always succeeding.
429 # This is just a bit more robust, and it could work for booting
430 # into ipxe which can't persist data, if we ever got that working.
431 mkfs.ext2 $
(grub_extdev
)
432 # when we move to newer than trisquel 8, we can remove
433 # --type luks1. We can also check on cryptsetup --help | less /compil
434 # to see about the other settings. Default in debian 9 is luks2.
435 # You can convert from luks2 to luks 1 by adding a temporary key:
436 # cryptsetup luksAddKey --pbkdf pbkdf2
437 # then remove the new format keys with cryptsetup luksRemoveKey
438 # then cryptsetup convert DEV --type luks1, then readd old keys and remove temp.
439 yes YES | cryptsetup luksFormat $
(rootdev
) $luks_file \
440 --type luks1
-c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
442 cryptsetup luksAddKey
--key-file $luks_file \
443 $
(rootdev
) ||
[[ $?
== 141 ]]
444 # background: Keyfile and password are treated just
445 # like 2 ways to input a passphrase, so we don't actually need to have
446 # different contents of keyfile and passphrase, but it makes some
447 # security sense to a really big randomly generated passphrase
448 # as much as possible, so we have both.
450 # This would remove the keyfile.
451 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
452 # /key/file || [[ $? == 141 ]]
454 cryptsetup luksOpen $
(rootdev
) $
(root-cryptname
) \
455 --key-file $luks_file
457 if [[ $SPECIAL_DISK ]]; then
461 ls -la /dev
/btrfs-control
# this was probably for debugging...
463 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
464 bpart
${boot_devs[@]}
466 for dev
in ${devs[@]}; do
467 mkfs.ext2 $
(grub_extdev
)
468 cryptsetup luksOpen $
(rootdev
) $
(root-cryptname
) \
469 --key-file $luks_file
475 if [[ $DISTRO != debianstretch_bootstrap
]]; then
476 # bootstrap distro doesn't use separate encrypted root.
477 mount
-o subvolid
=0 $first_root_crypt /mnt
478 # systemd creates subvolumes we want to delete.
479 s
=($
(btrfs subvolume list
--sort=-path /mnt |
480 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
481 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
482 btrfs subvolume set-default
0 /mnt
483 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
488 btrfs subvolume create root_
$DISTRO
490 mkdir
-p /mnt
/root_
$DISTRO/boot
491 # could set default subvol like this, but no reason to.
492 # btrfs subvolume set-default \
493 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
495 # no cow on the root filesystem. it's setup is fully scripted,
496 # if it's messed up, we will just recreated it,
497 # and we can get better perf with this.
498 # I can't remember exactly why, but this is preferable to mounting with
499 # -o nodatacow, I think because subvolumes inherit that.
500 chattr -Rf +C root_
$DISTRO
505 mount
-o subvolid
=0 $first_boot_dev /mnt
507 btrfs subvolume set-default
0 /mnt
# already default, just ensuring it.
509 # for libreboot systems. grub2 only reads from subvolid=0
511 cp $FAI/distro-install-common
/libreboot_grub.cfg
/mnt
/grub2
513 if [[ $DISTRO == debianstretch_bootstrap
]]; then
514 # this is just convenience for the libreboot_grub config
515 # so we can glob the other ones easier.
518 boot_vol
=boot_
$DISTRO
520 [[ ! -e /mnt
/$boot_vol ]] || btrfs subvolume delete
/mnt
/$boot_vol
521 btrfs subvolume create
$boot_vol
524 ## end create subvols ##
527 mount
$first_grub_extdev /mnt
528 grub-editenv
/mnt
/grubenv
set did_fai_check
=true
529 grub-editenv
/mnt
/grubenv
set last_boot
=/$boot_vol
532 if [[ $DISTRO == debianstretch_bootstrap
]]; then
533 cat > /tmp
/fai
/fstab
<<EOF
534 $first_boot_dev / btrfs noatime,subvol=$boot_vol 0 0
536 cat >/tmp
/fai
/disk_var.sh
<<EOF
537 BOOT_DEVICE="${short_devs[@]}"
538 ROOT_PARTITION=$first_boot_dev
541 # note, fai creates the mountpoints listed here
542 cat > /tmp
/fai
/fstab
<<EOF
543 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
544 $first_root_crypt /mnt/root btrfs noatime,subvolid=0 0 0
545 $first_boot_dev /boot btrfs noatime,subvol=$boot_vol 0 0
548 for dev
in ${devs[@]}; do
549 swaps
+=($
(swap-cryptname
))
550 cat >>/tmp
/fai
/crypttab
<<EOF
551 $(root-cryptname) $(rootdev) none keyscript=decrypt_keyctl,discard,luks,initramfs
552 $(swap-cryptname) $(swapdev) /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
554 cat >> /tmp
/fai
/fstab
<<EOF
555 $(swap-cryptdev) none swap sw 0 0
560 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
562 # note: swaplist seems to do nothing.
563 cat >/tmp
/fai
/disk_var.sh
<<EOF
564 BOOT_DEVICE="${short_devs[@]}"
565 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
566 # ROOT_PARTITIONS is added by me, used in arch setup.
567 ROOT_PARTITIONS="${root_devs[@]}"
568 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
569 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}