Skip to content

Commit 62109dc

Browse files
prepare-root: create /run/systemd/volatile-root for composefs
systemd resolves the block device backing the root filesystem via blockdev_get_root(), which checks "/" and falls back to "/usr". With composefs enabled both are overlayfs mounts with an anonymous st_dev (and prepare-root intentionally skips the read-only /usr bind mount in that case), so the lookup finds no device. As a result systemd-gpt-auto-generator silently skips all partition discovery: the ESP is never automounted on /boot, which in turn breaks tools that expect the bootfs to be reachable, e.g. bootctl, systemd-bless-boot (boot counting) and "ostree admin status" when the loader entries live on the ESP. systemd provides a hook for exactly this situation: if the root mount has been replaced by some form of volatile file system, the original root block device is expected to be symlinked at /run/systemd/volatile-root. Create that symlink when composefs is in use. Note the target must be in the /dev/block/MAJ:MIN form since systemd parses it without ever going to disk (device_path_parse_major_minor()). The root device is resolved as follows: prefer /dev/gpt-auto-root when it exists, since on systemd-based initrds udev has already identified the root partition for us (EFI boot with LoaderDevicePartUUID set and a discoverable-partitions-spec root partition type). Otherwise fall back to stat()ing the root mountpoint, which also covers initrds without systemd (e.g. meta-updater). Since btrfs reports an anonymous st_dev (major 0) for its mounts, in that case query the backing device via BTRFS_IOC_FS_INFO/BTRFS_IOC_DEV_INFO the same way systemd's btrfs_get_block_device_fd() does, refusing multi-device filesystems just as systemd does. This matters in practice as Fedora Atomic Desktops default to composefs on btrfs. bootc solves the same problem the same way for its composefs-native backend: bootc-root-setup.service (gated on the composefs= karg) runs a gpt_workaround() that creates this exact symlink. That service is ordered after ostree-prepare-root.service, so with this change the symlink already exists by the time it runs; bootc is being updated to tolerate that instead of failing. See also the systemd discussion of gpt-auto with overlay roots: systemd/systemd#35017 Failure to create the symlink is not fatal: the system boots fine without it, only auto-discovery of auxiliary partitions is degraded. Signed-off-by: Ricardo Salveti <[email protected]>
1 parent 4abc815 commit 62109dc

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

‎src/switchroot/ostree-prepare-root.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <errno.h>
5858
#include <fcntl.h>
5959
#include <libglnx.h>
60+
#include <linux/btrfs.h>
61+
#include <linux/magic.h>
6062
#include <stdarg.h>
6163
#include <stdbool.h>
6264
#include <stdio.h>
@@ -67,7 +69,9 @@
6769
#include <sys/param.h>
6870
#include <sys/stat.h>
6971
#include <sys/syscall.h>
72+
#include <sys/sysmacros.h>
7073
#include <sys/types.h>
74+
#include <sys/vfs.h>
7175
#include <unistd.h>
7276

7377
#include <ostree-core.h>
@@ -139,6 +143,89 @@ resolve_deploy_path (const char *kernel_cmdline, const char *root_mountpoint)
139143
return deploy_path;
140144
}
141145

146+
/* Resolve the block device backing a mounted btrfs filesystem, mirroring
147+
* systemd's btrfs_get_block_device_fd(): btrfs mounts expose an anonymous
148+
* st_dev, so the backing device has to be queried via ioctl. Multi-device
149+
* filesystems are rejected (as systemd does) since no single backing device
150+
* can be determined for them. Returns 0 if no device was found. */
151+
static dev_t
152+
get_btrfs_block_device (const char *path)
153+
{
154+
glnx_autofd int fd = open (path, O_RDONLY | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
155+
if (fd < 0)
156+
return 0;
157+
158+
struct statfs sfs;
159+
if (fstatfs (fd, &sfs) < 0 || sfs.f_type != BTRFS_SUPER_MAGIC)
160+
return 0;
161+
162+
struct btrfs_ioctl_fs_info_args fsi = { 0 };
163+
if (ioctl (fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
164+
return 0;
165+
166+
if (fsi.num_devices != 1)
167+
return 0;
168+
169+
for (uint64_t id = 1; id <= fsi.max_id; id++)
170+
{
171+
struct btrfs_ioctl_dev_info_args di = { .devid = id };
172+
struct stat st;
173+
174+
if (ioctl (fd, BTRFS_IOC_DEV_INFO, &di) < 0)
175+
{
176+
if (errno == ENODEV)
177+
continue;
178+
return 0;
179+
}
180+
181+
if (stat ((char *)di.path, &st) < 0)
182+
return 0;
183+
if (!S_ISBLK (st.st_mode) || major (st.st_rdev) == 0)
184+
return 0;
185+
186+
return st.st_rdev;
187+
}
188+
189+
return 0;
190+
}
191+
192+
/* systemd resolves the block device backing an overlay root via the
193+
* /run/systemd/volatile-root symlink (see blockdev_get_root()). With
194+
* composefs both / and /usr are overlays with an anonymous st_dev, so tools
195+
* like systemd-gpt-auto-generator would otherwise be unable to find the
196+
* backing disk (breaking e.g. ESP automounting). Point it at the physical
197+
* root device. The symlink target must use the /dev/block/MAJ:MIN form, as
198+
* systemd never stats the path.
199+
*
200+
* Prefer /dev/gpt-auto-root when it exists: on systemd-based initrds udev
201+
* has already resolved the root partition for us. Otherwise (e.g. initrds
202+
* without systemd) fall back to the device backing the root mountpoint,
203+
* where btrfs needs special handling due to its anonymous st_dev. */
204+
static void
205+
create_volatile_root_symlink (const char *root_mountpoint)
206+
{
207+
dev_t dev = 0;
208+
struct stat st;
209+
210+
if (stat ("/dev/gpt-auto-root", &st) == 0 && S_ISBLK (st.st_mode))
211+
dev = st.st_rdev;
212+
else if (stat (root_mountpoint, &st) == 0)
213+
{
214+
if (major (st.st_dev) > 0)
215+
dev = st.st_dev;
216+
else
217+
dev = get_btrfs_block_device (root_mountpoint);
218+
}
219+
220+
if (dev == 0)
221+
return;
222+
223+
g_autofree char *devpath = g_strdup_printf ("/dev/block/%u:%u", major (dev), minor (dev));
224+
(void)mkdir ("/run/systemd", 0755);
225+
if (symlink (devpath, "/run/systemd/volatile-root") < 0 && errno != EEXIST)
226+
g_printerr ("failed to create /run/systemd/volatile-root: %s\n", strerror (errno));
227+
}
228+
142229
int
143230
main (int argc, char *argv[])
144231
{
@@ -275,6 +362,8 @@ main (int argc, char *argv[])
275362
if (mount (deploy_path, TMP_SYSROOT, NULL, MS_BIND | MS_SILENT, NULL) < 0)
276363
err (EXIT_FAILURE, "failed to make initial bind mount %s", deploy_path);
277364
}
365+
else
366+
create_volatile_root_symlink (root_mountpoint);
278367

279368
/* Pass on the state for use by ostree-prepare-root */
280369
g_variant_builder_add (&metadata_builder, "{sv}", OTCORE_RUN_BOOTED_KEY_SYSROOT_RO,

0 commit comments

Comments
 (0)