Skip to content

Commit 861a884

Browse files
committed
zfs: load keys for encrypted datasets during pool import
If a user has set up their own zpools and given them to us to manage, it's possible they've configured ZFS-native encryption. For the most part, this works completely transparently to us. However, because we manually do zpool-import and zpool-export during startup and shutdown of Incus, ZFS datasets with keys will have their keys unloaded during shutdown and then the keys are not automatically loaded on startup. This results in containers being unable to start on startup because all IOs are blocked indefinitely until the dataset keys are loaded manually by the admin -- even if the admin has configured automatic key loading on their system! The simplest solution would be to pass -l to zfs-import (which causes ZFS to auto-import all keys for all datasets in the pool). However, it is slightly nicer to do a separate zfs-load-key so that we can unmount the pool if the key import fails (zfs-import will leave the pool imported but without keys loaded). If the user has configured keylocation=prompt (or otherwise misconfigured the encryption settings for their pool), the command will fail and the pool import will fail loudly (rather than silently failing in the form of an imported pool that is not usable as a filesystem). Signed-off-by: Aleksa Sarai <[email protected]>
1 parent c120d62 commit 861a884

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

internal/server/storage/drivers/driver_zfs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,21 @@ func (d *zfs) importPool() (bool, error) {
567567
return false, err
568568
}
569569

570+
// We need to explicitly import the keys here so containers can start. This
571+
// is always needed because even if the admin has set up auto-import of
572+
// keys on the system, because incus manually imports and exports the pools
573+
// the keys can get unloaded.
574+
//
575+
// We could do "zpool import -l" to request the keys during import, but by
576+
// doing it separately we know that the key loading specifically failed and
577+
// not some other operation. If a user has keylocation=prompt configured,
578+
// this command will fail and the pool will fail to load.
579+
_, err = subprocess.RunCommand("zfs", "load-key", "-r", d.config["zfs.pool_name"])
580+
if err != nil {
581+
_, _ = d.Unmount()
582+
return false, fmt.Errorf("Failed to load keys for ZFS dataset %q: %w", d.config["zfs.pool_name"], err)
583+
}
584+
570585
if exists {
571586
return true, nil
572587
}

0 commit comments

Comments
 (0)