-
Notifications
You must be signed in to change notification settings - Fork 175
add virtiofs systemd credential handling #4230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dustymabe
merged 1 commit into
coreos:testing-devel
from
dustymabe:dusty-virtifs-systemd-credential
Jul 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...dules.d/40import-virtiofs-systemd-credentials/import-virtiofs-systemd-credentials.service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| [Unit] | ||
| Description=Import systemd credentials from virtiofs | ||
| Documentation=https://systemd.io/CREDENTIALS/ | ||
| Documentation=https://github.com/systemd/systemd/issues/29175 | ||
|
|
||
| # Only run inside a VM — virtiofs is a VM-only transport. | ||
| ConditionVirtualization=vm | ||
|
|
||
| # Only run if the virtiofs sysfs directory exists, meaning the | ||
| # virtiofs kernel module is loaded and at least one device is present. | ||
| ConditionDirectoryNotEmpty=/sys/fs/virtiofs | ||
|
|
||
| # Only run if we're not in a confidential compute setting as this is | ||
| # another vector for configuring the machine and it's not currently | ||
| # measured. | ||
| ConditionSecurity=!cvm | ||
| ConditionSecurity=!measured-uki | ||
| ConditionSecurity=!measured-os | ||
|
|
||
| # Only run on Ignition first boot for now as we don't need it for | ||
| # anything other than that. | ||
| ConditionKernelCommandLine=ignition.firstboot | ||
|
|
||
| # Run in the initramfs so that systemd credentials are in | ||
| # /run/credentials/@initrd/ before the initramfs-to-real-root transition. | ||
| # Since /run persists across that transition, the systemd credentials | ||
| # will be available to generators and services in the real root (e.g. | ||
| # systemd-debug-generator for systemd.extra-unit.* and | ||
| # systemd.unit-dropin.*, systemd-tmpfiles-setup for tmpfiles.extra, | ||
| # systemd-sysusers for passwd.hashed-password.*, etc.) | ||
| # | ||
| # This must run after the virtio devices are discovered and the | ||
| # virtiofs kernel module is loaded. | ||
| DefaultDependencies=no | ||
| After=systemd-modules-load.service | ||
| After=systemd-udev-settle.service | ||
| Before=initrd-switch-root.target | ||
|
|
||
| [Service] | ||
| Type=oneshot | ||
| RemainAfterExit=no | ||
| ExecStart=/usr/libexec/import-virtiofs-systemd-credentials.sh | ||
|
|
||
| [Install] | ||
| WantedBy=initrd.target |
92 changes: 92 additions & 0 deletions
92
...ut/modules.d/40import-virtiofs-systemd-credentials/import-virtiofs-systemd-credentials.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #!/bin/bash | ||
| # import-virtiofs-systemd-credentials.sh - Import systemd credentials from a virtiofs share | ||
| # | ||
| # This script scans /sys/fs/virtiofs/*/tag for a virtiofs filesystem tagged | ||
| # "io.systemd.credentials". If found, it mounts the filesystem and copies | ||
| # credential files into /run/credentials/@system/ where systemd will pick | ||
| # them up as system credentials. | ||
| # | ||
| # virtiofs tag discovery requires kernel >= 6.9 (commit a8f62f50b4e4). | ||
| # See https://systemd.io/CREDENTIALS/ for the systemd credentials specification. | ||
| # See https://github.com/systemd/systemd/issues/29175 for upstream discussion. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| VIRTIOFS_TAG="io.systemd.credentials" | ||
| SYSFS_DIR="/sys/fs/virtiofs" | ||
| SYSTEMD_CREDS_DIR="" # defined below | ||
| MOUNTPOINT="/run/credentials/@virtiofs" | ||
|
|
||
| # For EL 9 we need to copy to a different directory because importing | ||
| # from /run/credentials/@initrd was only added in systemd v254. | ||
| # https://github.com/systemd/systemd/pull/28207 | ||
| source /etc/os-release | ||
| if [ "${PLATFORM_ID:-}" == "platform:el9" ]; then | ||
| SYSTEMD_CREDS_DIR="/run/credstore" # Works with LoadCredential | ||
| else | ||
| SYSTEMD_CREDS_DIR="/run/credentials/@initrd" # Works with ImportCredential | ||
| fi | ||
|
|
||
| log() { | ||
| echo "import-virtiofs-systemd-credentials: $*" >&2 | ||
| } | ||
|
|
||
| # Check if the virtiofs sysfs directory exists at all. If the virtiofs | ||
| # kernel module isn't loaded or no virtiofs devices are present, there's | ||
| # nothing to do. | ||
| if [ ! -d "${SYSFS_DIR}" ]; then | ||
| log "no virtiofs sysfs directory found, nothing to do" | ||
| exit 0 | ||
| fi | ||
|
|
||
| log "importing into ${SYSTEMD_CREDS_DIR}" | ||
|
|
||
| # Scan all virtiofs devices for our well-known tag. The kernel exposes | ||
| # virtiofs tags at /sys/fs/virtiofs/<N>/tag (since Linux 6.9). | ||
| found=0 | ||
| for tagfile in "${SYSFS_DIR}"/*/tag; do | ||
| [ -e "${tagfile}" ] || continue | ||
| tag=$(cat "${tagfile}" 2>/dev/null) || continue | ||
| if [ "${tag}" = "${VIRTIOFS_TAG}" ]; then | ||
| found=1 | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if [ "${found}" -eq 0 ]; then | ||
| log "no virtiofs share with tag '${VIRTIOFS_TAG}' found, nothing to do" | ||
| exit 0 | ||
| fi | ||
|
|
||
| log "found virtiofs share with tag '${VIRTIOFS_TAG}'" | ||
|
|
||
| # Create the mountpoint and mount the virtiofs share | ||
| mkdir -p "${MOUNTPOINT}" | ||
| if ! mount -t virtiofs "${VIRTIOFS_TAG}" "${MOUNTPOINT}"; then | ||
| log "failed to mount virtiofs tag '${VIRTIOFS_TAG}'" | ||
| rmdir "${MOUNTPOINT}" 2>/dev/null || true | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Create the system credentials directory if it doesn't exist. | ||
| # This is where systemd looks for system-level credentials. | ||
| mkdir -p "${SYSTEMD_CREDS_DIR}" | ||
|
|
||
| # Copy each systemd credential file from the virtiofs mount into the systemd | ||
| # credentials import initrd directory. Credential names are the filenames. | ||
| count=0 | ||
| for credfile in "${MOUNTPOINT}"/*; do | ||
| [ -f "${credfile}" ] || continue | ||
| credname=$(basename "${credfile}") | ||
| cp "${credfile}" "${SYSTEMD_CREDS_DIR}/${credname}" | ||
| # Credentials should be readable only by root | ||
| chmod 0600 "${SYSTEMD_CREDS_DIR}/${credname}" | ||
| log "imported systemd credential: ${credname}" | ||
| count=$((count + 1)) | ||
| done | ||
|
|
||
| # Clean up the mount | ||
| umount "${MOUNTPOINT}" | ||
| rmdir "${MOUNTPOINT}" 2>/dev/null || true | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, maybe not if we need to cleanup the mountpoint |
||
|
|
||
| log "imported ${count} systemd credential(s)" | ||
50 changes: 50 additions & 0 deletions
50
...redentials/usr/lib/dracut/modules.d/40import-virtiofs-systemd-credentials/module-setup.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #!/bin/bash | ||
| # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- | ||
| # ex: ts=8 sw=4 sts=4 et filetype=sh | ||
| # | ||
| # Dracut module: import-virtiofs-systemd-credentials | ||
| # | ||
| # Imports systemd credentials from a virtiofs share tagged | ||
| # "io.systemd.credentials" into /run/credentials/@system/. | ||
| # This runs in the initramfs so that systemd credentials are available | ||
| # to generators in the real root (e.g. systemd-debug-generator for | ||
| # systemd.extra-unit.* and systemd.unit-dropin.*) and early services | ||
| # (e.g. systemd-tmpfiles-setup for tmpfiles.extra). | ||
| # | ||
| # This provides a cross-architecture alternative to SMBIOS OEM strings | ||
| # and fw_cfg for passing systemd credentials into VMs. Unlike those | ||
| # mechanisms, virtiofs works on all architectures including s390x and | ||
| # ppc64le. | ||
| # | ||
| # See https://systemd.io/CREDENTIALS/ | ||
| # See https://github.com/systemd/systemd/issues/29175 | ||
|
|
||
| check() { | ||
| # Don't include in kdump initramfs | ||
| if [[ $IN_KDUMP == 1 ]]; then | ||
| return 1 | ||
| fi | ||
| return 0 | ||
| } | ||
|
|
||
| depends() { | ||
| echo systemd | ||
| } | ||
|
|
||
| installkernel() { | ||
| instmods -c virtiofs | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make our initrd 35K (xz compressed) bigger so should be fine. |
||
| } | ||
|
|
||
| install_and_enable_unit() { | ||
| local unit="$1"; shift | ||
| local target="$1"; shift | ||
| inst_simple "$moddir/$unit" "$systemdsystemunitdir/$unit" | ||
| systemctl -q --root="$initdir" add-requires "$target" "$unit" || exit 1 | ||
| } | ||
|
|
||
| install() { | ||
| inst_script "$moddir/import-virtiofs-systemd-credentials.sh" \ | ||
| "/usr/libexec/import-virtiofs-systemd-credentials.sh" | ||
|
|
||
| install_and_enable_unit import-virtiofs-systemd-credentials.service initrd.target | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to skip that by using a dedicated mount namespace like we do in other units that mount filesystems. https://github.com/coreos/bootupd/blob/main/systemd/bootloader-update.service#L16