Improve systemd-ask-password integration#413
Conversation
src/key.rs
Outdated
|
|
||
| fn is_dev_null(fd: BorrowedFd) -> io::Result<bool> { | ||
| let mut buf: MaybeUninit<libc::stat> = MaybeUninit::uninit(); | ||
| let ret = unsafe { libc::fstat(fd.as_raw_fd(), buf.as_mut_ptr()) }; |
There was a problem hiding this comment.
are you sure we don't have a better fstat wrapper available?
There was a problem hiding this comment.
rustix has one and is already in the dependencies. I've changed the code to use rustix's wrapper.
When mount.bcachefs is started with stdin set to /dev/null (such as when it is started by systemd during boot), try to use systemd-ask-password to ask for the passphrase.
Before asking for a passphrase, mount.bcachefs searches for an existing key in the keyring containing a passphrase for the filesystem. This means that the user only has to enter the passphrase once when mounting the filesystem multiple times. However, if the key appears in between the check and the time when systemd-ask-password queries the user for a password, the existing key will not be reused. Also, when multiple instances of mount.bcachefs are started in parallel for the same filesystem (such as during boot), each of them will see that the key is not in the keyring and start an instance of systemd-ask-password, meaning that the user will be queried multiple times. Fix the race condition by passing the --keyname and --accept-cached options to systemd-ask-password which also makes it try itself to retreive a cached password from the keyring before querying the user for a password.
f95c331 to
69cb642
Compare
|
I think it'd be better to follow the convention that |
|
I see Note that the kernel looks for the I wonder that the maintainers of bcachefs-tools think. I can try to make a PR this week. |
Right, if we're going to be doing the
Right well I'm not suggesting that. In this PR, you've added a new key to the keyring, which is just |
|
As long as we're talking about systemd integration, awhile back we were talking about abusing systemd-ask-password for prompting the user for whether to mount degraded (on workstations, not servers). Anyone have thoughts on that or want to take that on too? |
For users that mount the same encrypted bcachefs FS multiple times at boot, this patch makes it so the passphrase is only asked once.
The first commit makes it so
systemd-ask-passwordis invoked when stdin is/dev/null, instead of using our own passphrase prompting code. This is the case when a bcachefs FS is mounted by systemd: by default, systemd executes the mount helper with stdin set to/dev/null.The second commit fixes a race condition where when multiple
bcachefs mountcommands are executed in parallel, all of them will see that there is no key in the keyring and start an instance ofsystemd-ask-password. We can asksystemd-ask-passwordto try to reuse an existing passphrase in the keyring.systemd-ask-passwordis smart and knows to check again every time anothersystemd-ask-passwordinstance completes, so as soon as the firstsystemd-ask-passwordcommand is complete, the others ones will return as well.