Skip to content

Commit

Permalink
Merge pull request #28 from micxer/auto-unlock
Browse files Browse the repository at this point in the history
Auto-load encryption keys on boot
  • Loading branch information
mrlesmithjr authored Nov 1, 2023
2 parents 23da5fd + 22a18a5 commit 81687dc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ zfs_enable_performance_tuning: false
# Defines if Samba is installed and configured
zfs_enable_samba: false

# Defines if keys for encrypted filesystems are loaded on boot
zfs_autoload_encryption_keys: false

# Defines filesystems to manage
zfs_filesystems: []
# - name: nfs
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ zfs_enable_performance_tuning: false
# Defines if Samba is installed and configured
zfs_enable_samba: false

# Defines if keys for encrypted filesystems are loaded on boot
zfs_autoload_encryption_keys: false

# Defines filesystems to manage
zfs_filesystems: []
# - name: nfs
Expand Down
36 changes: 36 additions & 0 deletions tasks/encryption_keys.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Encryption keys | Get encrypted pools
ansible.builtin.set_fact:
zfs_encrypted_pools: >-
{{ zfs_pools | selectattr('options', 'search', 'keylocation') | map(attribute='name') | list }}
when: zfs_create_pools

- name: Encryption keys | Get encrypted datasets
ansible.builtin.set_fact:
zfs_encrypted_datasets: >-
{{ zfs_filesystems | selectattr('keylocation', 'defined') | map('combine', {'dataset_name': '{{ pool }}/{{name}}'})
| map(attribute='dataset_name') | list }}
when: zfs_create_filesystems

- name: Encryption keys | Create key-load service unit file
ansible.builtin.template:
src: etc/systemd/system/[email protected]
dest: /etc/systemd/system/[email protected]
owner: root
group: root
mode: "0644"
when: zfs_encrypted_pools or zfs_encrypted_datasets

- name: Encryption keys | Activate key-load service for encrypted pools
ansible.builtin.systemd_service:
name: zfs-load-key@{{ item }}
enabled: true
loop: "{{ zfs_encrypted_pools }}"
when: zfs_create_pools

- name: Encryption keys | Activate key-load service for encrypted datasets
ansible.builtin.systemd_service:
name: zfs-load-key@{{ item }}
enabled: true
loop: "{{ zfs_encrypted_datasets }}"
when: zfs_create_filesystems
7 changes: 7 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
- include_tasks: ubuntu.yml
when: ansible_distribution == "Ubuntu" and zfs_install_update == true

- name: Encryption keys
include_tasks: encryption_keys.yml
when: >
(zfs_create_pools or
zfs_create_filesystems) and
zfs_autoload_encryption_keys
- include_tasks: manage_zfs.yml

- include_tasks: samba.yml
Expand Down
16 changes: 16 additions & 0 deletions templates/etc/systemd/system/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Load encryption keys for zpool
Documentation=man:zfs(8)
DefaultDependencies=no
After=systemd-udev-settle.service
After=systemd-remount-fs.service
After=zfs-import.target
Before=zfs-mount.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/zfs load-key %i

[Install]
WantedBy=zfs-mount.service

0 comments on commit 81687dc

Please sign in to comment.