improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
## Prompts for the password of every user that uses a »passwordFile«, to later use that password for home encryption and/or save it in the »passwordFile«.
function prompt-for-user-passwords { # (void)
declare -g -A userPasswords = ( ) # (this ends up in the caller's scope)
2023-05-02 02:13:24 +02:00
local user ; for user in "@{!config.users.users!catAttrSets.password[@]}" ; do # Also grab any plaintext passwords for testing setups.
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
userPasswords[ $user ] = @{ config.users.users!catAttrSets.password[ $user ] }
done
2023-05-02 02:13:24 +02:00
local user ; for user in "@{!config.users.users!catAttrSets.passwordFile[@]}" ; do
2023-07-20 17:47:47 +02:00
for attempt in 2 3 x ; do
if userPasswords[ $user ] = $( prompt-new-password " for the user account » $user « " ) ; then break ; fi
if [ [ $attempt = = x ] ] ; then \r eturn 1 ; fi ; echo " Retrying ( $attempt /3): "
done
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
}
2023-06-16 02:14:51 +02:00
## Mounts a ramfs as the host's keystore and populates it with keys as requested by »config.setup.keystore.keys«.
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
# Depending on the specified key types/sources, this may prompt for user input.
2023-05-02 02:13:24 +02:00
function populate-keystore { # (void)
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
local keystore = /run/keystore-@{ config.networking.hostName!hashString.sha256:0:8}
2023-05-02 02:13:24 +02:00
mkdir -p $keystore && chmod 750 $keystore && prepend_trap " rmdir $keystore " EXIT || return
@{ native.util-linux} /bin/mount ramfs -t ramfs $keystore && prepend_trap " @{native.util-linux}/bin/umount $keystore " EXIT || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2023-05-02 02:13:24 +02:00
local -A methods = ( ) ; local -A options = ( )
2023-06-16 02:14:51 +02:00
local usage ; for usage in "@{!config.setup.keystore.keys[@]}" ; do
methods[ $usage ] = @{ config.setup.keystore.keys[ $usage ] %%= *}
options[ $usage ] = @{ config.setup.keystore.keys[ $usage ] :$(( ${# methods [ $usage ] } + 1 )) }
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
2023-05-02 02:13:24 +02:00
local usage ; for usage in " ${ !methods[@] } " ; do
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
if [ [ " ${ methods [ $usage ] } " != inherit ] ] ; then continue ; fi
2023-05-02 02:13:24 +02:00
local from = ${ options [ $usage ] }
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
methods[ $usage ] = ${ methods [ $from ] } ; options[ $usage ] = ${ options [ $from ] }
done
2023-05-02 02:13:24 +02:00
local usage ; for usage in " ${ !methods[@] } " ; do
2022-07-29 12:49:55 +02:00
if [ [ " ${ methods [ $usage ] } " = = home-composite || " ${ methods [ $usage ] } " = = copy ] ] ; then continue ; fi
2023-05-02 02:13:24 +02:00
local attempt ; for attempt in 2 3 x ; do
2022-08-31 08:38:33 +02:00
if gen-key-" ${ methods [ $usage ] } " " $usage " " ${ options [ $usage ] } " | write-secret " $keystore " /" $usage " .key ; then break ; fi
2023-05-02 02:13:24 +02:00
if [ [ $attempt = = x ] ] ; then \r eturn 1 ; fi ; echo " Retrying ( $attempt /3): "
2022-08-31 08:38:33 +02:00
done
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
2023-05-02 02:13:24 +02:00
local usage ; for usage in " ${ !methods[@] } " ; do
2022-07-29 12:49:55 +02:00
if [ [ " ${ methods [ $usage ] } " != home-composite ] ] ; then continue ; fi
2023-05-02 02:13:24 +02:00
gen-key-" ${ methods [ $usage ] } " " $usage " " ${ options [ $usage ] } " | write-secret " $keystore " /" $usage " .key || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
2023-05-02 02:13:24 +02:00
local usage ; for usage in " ${ !methods[@] } " ; do
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
if [ [ " ${ methods [ $usage ] } " != copy ] ] ; then continue ; fi
2023-05-02 02:13:24 +02:00
gen-key-" ${ methods [ $usage ] } " " $usage " " ${ options [ $usage ] } " | write-secret " $keystore " /" $usage " .key || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
2023-05-02 02:13:24 +02:00
}
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
## Creates the LUKS devices specified by the host using the keys created by »populate-keystore«.
2023-01-29 15:55:56 +01:00
function create-luks-layers { # (void)
2023-05-02 02:13:24 +02:00
local keystore = /run/keystore-@{ config.networking.hostName!hashString.sha256:0:8}
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
for luksName in "@{!config.boot.initrd.luks.devices!catAttrSets.device[@]}" ; do
2023-05-02 02:13:24 +02:00
local rawDev = @{ config.boot.initrd.luks.devices!catAttrSets.device[ $luksName ] }
2023-01-29 15:55:56 +01:00
if ! is-partition-on-disks " $rawDev " " ${ blockDevs [@] } " ; then echo " Partition alias $rawDev used by LUKS device $luksName does not point at one of the target disks ${ blockDevs [@] } " 1>& 2 ; \r eturn 1 ; fi
2023-05-02 02:13:24 +02:00
local primaryKey = " $keystore " /luks/" $luksName " /0.key
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2023-05-02 02:13:24 +02:00
local keyOptions = ( --pbkdf= pbkdf2 --pbkdf-force-iterations= 1000 )
2023-01-29 15:55:56 +01:00
( PATH = @{ native.cryptsetup} /bin ; ${ _set_x :- : } ; cryptsetup --batch-mode luksFormat --key-file= " $primaryKey " " ${ keyOptions [@] } " -c aes-xts-plain64 -s 512 -h sha256 " $rawDev " ) || return
2023-05-02 02:13:24 +02:00
local index ; for index in 1 2 3 4 5 6 7 ; do
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
if [ [ -e " $keystore " /luks/" $luksName " /" $index " .key ] ] ; then
2023-01-29 15:55:56 +01:00
( PATH = @{ native.cryptsetup} /bin ; ${ _set_x :- : } ; cryptsetup luksAddKey --key-file= " $primaryKey " " ${ keyOptions [@] } " " $rawDev " " $keystore " /luks/" $luksName " /" $index " .key ) || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
fi
done
done
2023-01-29 15:55:56 +01:00
}
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2023-05-02 02:13:24 +02:00
## Opens the LUKS devices specified by the host, using the host's (open) keystore.
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
function open-luks-layers { # (void)
2023-05-02 02:13:24 +02:00
local keystore = /run/keystore-@{ config.networking.hostName!hashString.sha256:0:8}
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
for luksName in "@{!config.boot.initrd.luks.devices!catAttrSets.device[@]}" ; do
2022-06-04 20:54:09 +02:00
if [ [ -e /dev/mapper/$luksName ] ] ; then continue ; fi
2023-05-02 02:13:24 +02:00
local rawDev = @{ config.boot.initrd.luks.devices!catAttrSets.device[ $luksName ] }
local primaryKey = " $keystore " /luks/" $luksName " /0.key
2023-01-29 15:55:56 +01:00
@{ native.cryptsetup} /bin/cryptsetup --batch-mode luksOpen --key-file= " $primaryKey " " $rawDev " " $luksName " || return
prepend_trap " @{native.cryptsetup}/bin/cryptsetup close $luksName " EXIT || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
}