Skip to content

Add systemd credentials#2545

Merged
stgraber merged 9 commits intolxc:mainfrom
bensmrs:systemd-credentials
Oct 9, 2025
Merged

Add systemd credentials#2545
stgraber merged 9 commits intolxc:mainfrom
bensmrs:systemd-credentials

Conversation

@bensmrs
Copy link
Copy Markdown
Contributor

@bensmrs bensmrs commented Oct 6, 2025

Closes: #2182

This PR is not ready yet, but I need some help to wrap it up. First, does my LXC attempt look reasonable? Second, as soon as I edit the LXC instance config, the bind mount is cleared:

root@trixie:~# ls -al /dev/.incus-systemd-credentials/
total 12
d--x------ 1 root root  18 Oct  6 23:23 .
drwxr-xr-x 9 root root 540 Oct  6 23:23 ..
-r-------- 1 root root  14 Oct  6 23:23 bar
-r-------- 1 root root   3 Oct  6 23:23 baz
-r-------- 1 root root   4 Oct  6 23:23 foo
root@trixie:~#
logout
root@incus-dev:~/incus# incus config set trixie user.xxx yyy
root@incus-dev:~/incus# incus shell trixie
root@trixie:~# ls -al /dev/.incus-systemd-credentials/
total 0

Any idea why, @stgraber? (the files are still there on the host)

Once I have a clearer view on that, I can start looking at the live update.

@bensmrs bensmrs requested a review from stgraber as a code owner October 6, 2025 23:28
@bensmrs bensmrs marked this pull request as draft October 6, 2025 23:28
@bensmrs bensmrs force-pushed the systemd-credentials branch from 4e5cb5f to 3d48ab1 Compare October 6, 2025 23:29
@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 7, 2025

That's pretty odd. The only thing I can really think of is that we may have some logic on Update() which attempts to activate/deactivate the instance volume and is somehow causing the unmount, but that's pretty weird.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

So, the problem is indeed in Update:

_, err = d.initLXC(true)

I think because I’m deleting the directory and recreating it, the bindmount still points to the deleted entry.

So, not sure if I should continue doing my initialization in initLXC, and if so, if I can really rely on cConfig to tell me that the initialization has already been done (because the reverter in Update resets it)…

@bensmrs bensmrs force-pushed the systemd-credentials branch from 3d48ab1 to f0b02c1 Compare October 7, 2025 09:36
@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

At least now it works. I don’t know how that would play with live-migration, but I also don’t think I should really bother, as live-migrating a systemd container is a very bad idea…

@bensmrs bensmrs changed the title WIP: Add systemd credentials Add systemd credentials Oct 7, 2025
@bensmrs bensmrs marked this pull request as ready for review October 7, 2025 09:39
@bensmrs bensmrs force-pushed the systemd-credentials branch from f0b02c1 to 8ed4a22 Compare October 7, 2025 10:26
@github-actions github-actions bot added Documentation Documentation needs updating API Changes to the REST API labels Oct 7, 2025
@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

I’m not sure all those failing tests are related to the PR

@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 7, 2025

I think they are, they're all failing in the same way and the only stuff that's passing is cluster tests where no mount is involved (dir/btrfs).

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

Hmmm ok, I’ll run the test suite locally then.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

Ok there’s something:

root@incus-dev:~/incus/test# grep -r .incus-systemd-credentials tmp.oJD/SuiwKDXIw
tmp.oJD/SuiwKDXIw/run/c1/lxc.conf:lxc.environment = CREDENTIALS_DIRECTORY=/dev/.incus-systemd-credentials
tmp.oJD/SuiwKDXIw/run/c1/lxc.conf:lxc.mount.entry = /root/incus/test/tmp.oJD/SuiwKDXIw/containers/c1/credentials dev/.incus-systemd-credentials none bind,ro,create=dir 0 0
tmp.oJD/SuiwKDXIw/logs/c1/lxc.log:lxc c1 20251007192936.694 ERROR    utils - ../src/lxc/utils.c:safe_mount:1330 - No such file or directory - Failed to mount "/root/incus/test/tmp.oJD/SuiwKDXIw/containers/c1/credentials" onto "/usr/lib/x86_64-linux-gnu/lxc/rootfs/dev/.incus-systemd-credentials"
tmp.oJD/SuiwKDXIw/logs/c1/lxc.log:lxc c1 20251007192936.694 ERROR    conf - ../src/lxc/conf.c:mount_entry:2217 - No such file or directory - Failed to mount "/root/incus/test/tmp.oJD/SuiwKDXIw/containers/c1/credentials" on "/usr/lib/x86_64-linux-gnu/lxc/rootfs/dev/.incus-systemd-credentials"

But

root@incus-dev:~/incus/test# ls -al /root/incus/test/tmp.oJD/SuiwKDXIw/containers/c1/credentials
total 0
d--x------ 2 1000000 1000000 40 Oct  7 19:29 .
d--x------ 3 root    root    60 Oct  7 19:29 ..

This part however, /usr/lib/x86_64-linux-gnu/lxc/rootfs/dev/.incus-systemd-credentials is very intriguing (well, to me at least).

@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 7, 2025

This part however, /usr/lib/x86_64-linux-gnu/lxc/rootfs/dev/.incus-systemd-credentials is very intriguing (well, to me at least).

That's normal, /usr/lib/x86_64-linux-gnu/lxc/rootfs/ is the temporary mount point of the instance's root disk before pivot_root and such.

@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 7, 2025

The error could happen on either side of the mount, so it could be because the source of the mount is somehow unavailable at the time the mount is attempted or because the mount target doesn't exist.

Now create=dir should in theory take care of the target part, making the source more likely to be an issue though I'm not too sure what may be going on there.

@bensmrs bensmrs force-pushed the systemd-credentials branch from c87d666 to 37821e8 Compare October 7, 2025 20:41
@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 7, 2025

,optional seems to help quite a bit, but to be honest, I don’t really understand what I’m doing on that mount line…
I’m looking at the new errors.

@bensmrs bensmrs force-pushed the systemd-credentials branch from 37821e8 to d5504ab Compare October 7, 2025 21:39
@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 7, 2025

Definitely shouldn't need optional as that would make it skip the missing source path which isn't really what we want here, we always want that mount.

Did you figure out a simple reproducer for the failure? Then I can take a look at it.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 8, 2025

Ok I’ll revert the recent changes and I’ll try to produce a MWE. This is probably a rather trivial oversight; I’m quite new to the LXC driver codebase, and I’m still figuring out its general structure.

Edit: looks like it fails with tests using ZFS

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 8, 2025

I can reproduce the problem on lvm and zfs drivers, but not dir or btrfs:

root@incus-dev:~/incus# incus launch testimage test-1 --profile dir
Launching test-1
root@incus-dev:~/incus# incus launch testimage test-2 --profile zfs
Launching test-2
Error: Failed to run: /root/go/bin/incusd forkstart test-2 /var/lib/incus/containers /run/incus/test-2/lxc.conf: exit status 1

So it looks like something is mounted too late when working with logical volume managers, but I’m having some trouble understanding the whole LXC driver dance for now.

@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 8, 2025

Okay, I should be able to easily test it here on ZFS then.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 8, 2025

Nice. Please use c87d666, as it doesn’t include my desperate attempts :)

@stgraber
Copy link
Copy Markdown
Member

stgraber commented Oct 8, 2025

I found the problem:

root@dakara:~# ls /var/lib/incus/containers/d13/
backup.yaml  metadata.yaml  rootfs  templates
root@dakara:~# umount /var/lib/incus/containers/d13/
root@dakara:~# ls /var/lib/incus/containers/d13/
credentials
root@dakara:~# 

So basically the credentials folder was created prior to the instance's volume being mounted.
So when the instance volume does get mounted, it hides the credentials folder, causing the error.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 9, 2025

Oh, makes perfect sense! Thanks a lot, I’ll review that tomorrow.

@bensmrs bensmrs force-pushed the systemd-credentials branch from d5504ab to cac26c7 Compare October 9, 2025 10:30
@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 9, 2025

Aaaaah it looks good now.
I’m not 100% sure about the idmap, and I don’t fully grasp the implications of live changes of the idmap on that mounted directory. Any pointers?
I’ll write a few tests in the meantime.

@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 9, 2025

The tests are showing some rather strange race conditions on update (or maybe setupCredentials not getting called at all) where the DB gets updated but not the FS files

Edit: the tests have shown something that I completely overlooked: if a user sets both systemd.credential.foo and systemd.credential-binary.foo.

@bensmrs bensmrs force-pushed the systemd-credentials branch 2 times, most recently from 2bad5b9 to 4916ba2 Compare October 9, 2025 14:24
@bensmrs
Copy link
Copy Markdown
Contributor Author

bensmrs commented Oct 9, 2025

Great, should be ready to review now

@stgraber stgraber force-pushed the systemd-credentials branch from 4916ba2 to 6b838dd Compare October 9, 2025 20:59
@stgraber stgraber force-pushed the systemd-credentials branch from 6b838dd to 5c20d6c Compare October 9, 2025 21:06
@stgraber stgraber merged commit bdf1484 into lxc:main Oct 9, 2025
34 of 36 checks passed
@bensmrs bensmrs deleted the systemd-credentials branch October 15, 2025 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API Changes to the REST API Documentation Documentation needs updating

Development

Successfully merging this pull request may close these issues.

systemd credentials for containers

2 participants