From 38fe77083bf833cdeae390d8f24c4c1e0b06a9ba Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 3 Oct 2018 14:57:19 +0000 Subject: [PATCH] Support mounting /sysroot (and /boot) read-only We want to support extending the read-only state to cover `/sysroot` and `/boot`, since conceptually all of the data there should only be written via libostree. This change needs to be opt-in though to avoid breaking anyone. Add a `sysroot/readonly` key to the repository config which instructs `ostree-remount.service` to ensure `/sysroot` is read-only. This requires a bit of a dance because `/sysroot` is actually the same filesystem as `/`; so we make `/etc` a writable bind mount in this case. We also need to handle `/var` in the "OSTree default" case of a bind mount; the systemd generator now looks at the writability state of `/sysroot` and uses that to determine whether it should have the `var.mount` unit happen before or after `ostree-remount.service.` Also add an API to instruct the libostree shared library that the caller has created a new mount namespace. This way we can freely remount read-write. This approach extends upon in a much better way previous work we did to support remounting `/boot` read-write. Closes: https://github.com/ostreedev/ostree/issues/1265 --- Makefile-switchroot.am | 3 +- apidoc/ostree-sections.txt | 2 + src/boot/ostree-remount.service | 2 + src/libostree/libostree-devel.sym | 2 + src/libostree/ostree-impl-system-generator.c | 33 +++- src/libostree/ostree-sysroot-cleanup.c | 8 +- src/libostree/ostree-sysroot-deploy.c | 29 ++- src/libostree/ostree-sysroot-private.h | 13 +- src/libostree/ostree-sysroot.c | 187 ++++++++++++++----- src/libostree/ostree-sysroot.h | 11 ++ src/ostree/ot-main.c | 33 ++++ src/switchroot/ostree-remount.c | 75 ++++++-- 12 files changed, 329 insertions(+), 69 deletions(-) diff --git a/Makefile-switchroot.am b/Makefile-switchroot.am index ff44d4bc39..dc088990c2 100644 --- a/Makefile-switchroot.am +++ b/Makefile-switchroot.am @@ -55,7 +55,8 @@ ostree_remount_SOURCES = \ src/switchroot/ostree-mount-util.h \ src/switchroot/ostree-remount.c \ $(NULL) -ostree_remount_CPPFLAGS = $(AM_CPPFLAGS) -Isrc/switchroot +ostree_remount_CPPFLAGS = $(AM_CPPFLAGS) $(OT_INTERNAL_GIO_UNIX_CFLAGS) -Isrc/switchroot -I$(srcdir)/libglnx +ostree_remount_LDADD = $(AM_LDFLAGS) $(OT_INTERNAL_GIO_UNIX_LIBS) libglnx.la if BUILDOPT_SYSTEMD ostree_prepare_root_CPPFLAGS += -DHAVE_SYSTEMD=1 diff --git a/apidoc/ostree-sections.txt b/apidoc/ostree-sections.txt index e8faeb10df..9cd13c914c 100644 --- a/apidoc/ostree-sections.txt +++ b/apidoc/ostree-sections.txt @@ -497,6 +497,7 @@ ostree_sepolicy_get_type OstreeSysroot ostree_sysroot_new ostree_sysroot_new_default +ostree_sysroot_initialize ostree_sysroot_get_path ostree_sysroot_load ostree_sysroot_load_if_changed @@ -506,6 +507,7 @@ ostree_sysroot_lock_async ostree_sysroot_lock_finish ostree_sysroot_unlock ostree_sysroot_unload +ostree_sysroot_set_mount_namespace_in_use ostree_sysroot_get_fd ostree_sysroot_ensure_initialized ostree_sysroot_get_bootversion diff --git a/src/boot/ostree-remount.service b/src/boot/ostree-remount.service index b98110c2d6..ed8531ae63 100644 --- a/src/boot/ostree-remount.service +++ b/src/boot/ostree-remount.service @@ -24,6 +24,8 @@ OnFailure=emergency.target Conflicts=umount.target After=-.mount After=systemd-remount-fs.service +# Note code in ostree-impl-system-generator will generate an ordering +# relationship for var.mount Before=local-fs.target umount.target # Other early boot units that need to write to /var Before=systemd-random-seed.service plymouth-read-write.service systemd-journal-flush.service diff --git a/src/libostree/libostree-devel.sym b/src/libostree/libostree-devel.sym index f552bcea0b..470b8b2058 100644 --- a/src/libostree/libostree-devel.sym +++ b/src/libostree/libostree-devel.sym @@ -19,6 +19,8 @@ /* Add new symbols here. Release commits should copy this section into -released.sym. */ LIBOSTREE_2019.4 { + ostree_sysroot_initialize; + ostree_sysroot_set_mount_namespace_in_use; } LIBOSTREE_2019.3; /* Stub section for the stable release *after* this development one; don't diff --git a/src/libostree/ostree-impl-system-generator.c b/src/libostree/ostree-impl-system-generator.c index ce40a698c4..0c95541a1c 100644 --- a/src/libostree/ostree-impl-system-generator.c +++ b/src/libostree/ostree-impl-system-generator.c @@ -28,6 +28,7 @@ #ifdef HAVE_LIBMOUNT #include #endif +#include #include #include "otutil.h" @@ -163,6 +164,32 @@ _ostree_impl_system_generator (const char *ostree_cmdline, if (found_var_mnt) return TRUE; + struct statvfs stvfsbuf; + if (statvfs ("/sysroot", &stvfsbuf) == -1) + return glnx_throw_errno_prefix (error, "statvfs(/sysroot)"); + const gboolean sysroot_currently_writable = ((stvfsbuf.f_flag & ST_RDONLY) == 0); + const char *ordering_value; + /* For ostree as originally envisioned, e.g. Fedora Atomic Host, the system starts + * with the rootfs mounted ro (kernel default), and then gets remounted by + * systemd. + * + * However, Fedora CoreOS is an Ignition based system and starts out writable + * via rw on the kernel command line. + * + * Now, we want to support a read-only /sysroot: https://github.com/ostreedev/ostree/issues/1265 + * And the way that's currently implemented is in ostree-remount.service. + * Ideally systemd would support "Options=bind,rw" to force on a writable bind mount, + * but it currently doesn't. So we handle the ordering here. + */ + if (sysroot_currently_writable) + { + ordering_value = "Before=ostree-remount.service"; + } + else + { + ordering_value = "After=ostree-remount.service"; + } + /* Prepare to write to the output unit dir; we use the "normal" dir * that overrides /usr, but not /etc. */ @@ -179,6 +206,7 @@ _ostree_impl_system_generator (const char *ostree_cmdline, return FALSE; g_autoptr(GOutputStream) outstream = g_unix_output_stream_new (tmpf.fd, FALSE); gsize bytes_written; + /* This code is inspired by systemd's fstab-generator.c. * * Note that our unit doesn't run if systemd.volatile is enabled; @@ -189,14 +217,15 @@ _ostree_impl_system_generator (const char *ostree_cmdline, "[Unit]\n" "Documentation=man:ostree(1)\n" "ConditionKernelCommandLine=!systemd.volatile\n" - /* We need /sysroot mounted writable first */ - "After=ostree-remount.service\n" + /* See above for ordering */ + "%s\n" "Before=local-fs.target\n" "\n" "[Mount]\n" "Where=%s\n" "What=%s\n" "Options=bind\n", + ordering_value, var_path, stateroot_var_path)) return FALSE; diff --git a/src/libostree/ostree-sysroot-cleanup.c b/src/libostree/ostree-sysroot-cleanup.c index 7a352e6b4b..048a5ca93b 100644 --- a/src/libostree/ostree-sysroot-cleanup.c +++ b/src/libostree/ostree-sysroot-cleanup.c @@ -454,6 +454,9 @@ ostree_sysroot_cleanup_prune_repo (OstreeSysroot *sysroot, OstreeRepo *repo = ostree_sysroot_repo (sysroot); const guint depth = 0; /* Historical default */ + if (!_ostree_sysroot_ensure_writable (sysroot, error)) + return FALSE; + /* Hold an exclusive lock by default across gathering refs and doing * the prune. */ @@ -534,7 +537,10 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot *self, GError **error) { g_return_val_if_fail (OSTREE_IS_SYSROOT (self), FALSE); - g_return_val_if_fail (self->loaded, FALSE); + g_return_val_if_fail (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED, FALSE); + + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; if (!cleanup_other_bootversions (self, cancellable, error)) return glnx_prefix_error (error, "Cleaning bootversions"); diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c index c342d7e004..d180886f83 100644 --- a/src/libostree/ostree-sysroot-deploy.c +++ b/src/libostree/ostree-sysroot-deploy.c @@ -55,6 +55,9 @@ #define OSTREE_DEPLOYMENT_FINALIZING_ID SD_ID128_MAKE(e8,64,6c,d6,3d,ff,46,25,b7,79,09,a8,e7,a4,09,94) #endif +static gboolean +is_ro_mount (const char *path); + /* * Like symlinkat() but overwrites (atomically) an existing * symlink. @@ -806,6 +809,9 @@ write_origin_file_internal (OstreeSysroot *sysroot, GCancellable *cancellable, GError **error) { + if (!_ostree_sysroot_ensure_writable (sysroot, error)) + return FALSE; + GLNX_AUTO_PREFIX_ERROR ("Writing out origin file", error); GKeyFile *origin = new_origin ? new_origin : ostree_deployment_get_origin (deployment); @@ -2179,7 +2185,10 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self, GCancellable *cancellable, GError **error) { - g_assert (self->loaded); + g_assert (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED); + + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; /* Dealing with the staged deployment is quite tricky here. This function is * primarily concerned with writing out "finalized" deployments which have @@ -2336,7 +2345,6 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self, if (boot_was_ro_mount) { - /* TODO: Use new mount namespace. https://github.com/ostreedev/ostree/issues/1265 */ if (mount ("/boot", "/boot", NULL, MS_REMOUNT | MS_SILENT, NULL) < 0) return glnx_throw_errno_prefix (error, "Remounting /boot read-write"); } @@ -2362,8 +2370,10 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self, /* Note equivalent of try/finally here */ gboolean success = write_deployments_bootswap (self, new_deployments, opts, bootloader, &syncstats, cancellable, error); - /* Below here don't set GError until the if (!success) check */ - if (boot_was_ro_mount) + /* Below here don't set GError until the if (!success) check. + * Note we only bother remounting if a mount namespace isn't in use. + * */ + if (boot_was_ro_mount && !self->mount_namespace_in_use) { if (mount ("/boot", "/boot", NULL, MS_REMOUNT | MS_RDONLY | MS_SILENT, NULL) < 0) { @@ -2670,6 +2680,9 @@ ostree_sysroot_deploy_tree (OstreeSysroot *self, GCancellable *cancellable, GError **error) { + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; + g_autoptr(OstreeDeployment) deployment = NULL; if (!sysroot_initialize_deployment (self, osname, revision, origin, override_kernel_argv, &deployment, cancellable, error)) @@ -2771,6 +2784,9 @@ ostree_sysroot_stage_tree (OstreeSysroot *self, GCancellable *cancellable, GError **error) { + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; + OstreeDeployment *booted_deployment = ostree_sysroot_get_booted_deployment (self); if (booted_deployment == NULL) return glnx_throw (error, "Cannot stage a deployment when not currently booted into an OSTree system"); @@ -2997,6 +3013,9 @@ ostree_sysroot_deployment_set_kargs (OstreeSysroot *self, GCancellable *cancellable, GError **error) { + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; + /* For now; instead of this do a redeployment */ g_assert (!ostree_deployment_is_staged (deployment)); @@ -3044,6 +3063,8 @@ ostree_sysroot_deployment_set_mutable (OstreeSysroot *self, GCancellable *cancellable, GError **error) { + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; diff --git a/src/libostree/ostree-sysroot-private.h b/src/libostree/ostree-sysroot-private.h index 858673c5d3..919a1e9ddc 100644 --- a/src/libostree/ostree-sysroot-private.h +++ b/src/libostree/ostree-sysroot-private.h @@ -40,6 +40,12 @@ typedef enum { OSTREE_SYSROOT_DEBUG_TEST_STAGED_PATH = 1 << 3, } OstreeSysrootDebugFlags; +typedef enum { + OSTREE_SYSROOT_LOAD_STATE_NONE, + OSTREE_SYSROOT_LOAD_STATE_INIT, + OSTREE_SYSROOT_LOAD_STATE_LOADED, +} OstreeSysrootLoadState; + /** * OstreeSysroot: * Internal struct @@ -51,7 +57,8 @@ struct OstreeSysroot { int sysroot_fd; GLnxLockFile lock; - gboolean loaded; + OstreeSysrootLoadState loadstate; + gboolean mount_namespace_in_use; /* TRUE if caller has told us they used CLONE_NEWNS */ gboolean root_is_ostree_booted; /* TRUE if sysroot is / and we are booted via ostree */ /* The device/inode for /, used to detect booted deployment */ dev_t root_device; @@ -79,6 +86,10 @@ struct OstreeSysroot { #define _OSTREE_SYSROOT_DEPLOYMENT_RUNSTATE_DIR "/run/ostree/deployment-state/" #define _OSTREE_SYSROOT_DEPLOYMENT_RUNSTATE_FLAG_DEVELOPMENT "unlocked-development" +gboolean +_ostree_sysroot_ensure_writable (OstreeSysroot *self, + GError **error); + void _ostree_sysroot_emit_journal_msg (OstreeSysroot *self, const char *msg); diff --git a/src/libostree/ostree-sysroot.c b/src/libostree/ostree-sysroot.c index 2c0c0546d2..279a5caeb0 100644 --- a/src/libostree/ostree-sysroot.c +++ b/src/libostree/ostree-sysroot.c @@ -226,6 +226,33 @@ ostree_sysroot_new_default (void) return ostree_sysroot_new (NULL); } +/** + * ostree_sysroot_set_mount_namespace_in_use: + * + * If this function is invoked, then libostree will assume that + * a private Linux mount namespace has been created by the process. + * The primary use case for this is to have e.g. /sysroot mounted + * read-only by default. + * + * If this function has been called, then when a function which requires + * writable access is invoked, libostree will automatically remount as writable + * any mount points on which it operates. This currently is just `/sysroot` and + * `/boot`. + * + * If you invoke this function, it must be before ostree_sysroot_load(); it may + * be invoked before or after ostree_sysroot_initialize(). + * + * Since: 2018.10 + */ +void +ostree_sysroot_set_mount_namespace_in_use (OstreeSysroot *self) +{ + /* Must be before we're loaded, as otherwise we'd have to close/reopen all our + fds, e.g. the repo */ + g_return_if_fail (self->loadstate < OSTREE_SYSROOT_LOAD_STATE_LOADED); + self->mount_namespace_in_use = TRUE; +} + /** * ostree_sysroot_get_path: * @self: @@ -238,6 +265,7 @@ ostree_sysroot_get_path (OstreeSysroot *self) return self->path; } +/* Open a directory file descriptor for the sysroot if we haven't yet */ static gboolean ensure_sysroot_fd (OstreeSysroot *self, GError **error) @@ -251,13 +279,53 @@ ensure_sysroot_fd (OstreeSysroot *self, return TRUE; } +/* Remount /sysroot read-write if necessary */ +gboolean +_ostree_sysroot_ensure_writable (OstreeSysroot *self, + GError **error) +{ + /* Do nothing if no mount namespace is in use */ + if (!self->mount_namespace_in_use) + return TRUE; + + /* If a mount namespace is in use, ensure we're initialized */ + if (!ostree_sysroot_initialize (self, error)) + return FALSE; + + /* If we aren't operating on a booted system, then we don't + * do anything with mounts. Also, if the caller hasn't + * explicitly told us they made a mount namespace, we don't + * do any remounts. + */ + if (!self->root_is_ostree_booted) + return TRUE; + + /* Check if /sysroot is a read-only mountpoint */ + struct statvfs stvfsbuf; + if (statvfs ("/sysroot", &stvfsbuf) < 0) + return glnx_throw_errno_prefix (error, "fstatvfs(/sysroot)"); + if ((stvfsbuf.f_flag & ST_RDONLY) == 0) + return TRUE; + + /* OK, let's remount writable. */ + if (mount ("/sysroot", "/sysroot", NULL, MS_REMOUNT | MS_RELATIME, "") < 0) + return glnx_throw_errno_prefix (error, "Remounting /sysroot read-write"); + + /* Reopen our fd */ + glnx_close_fd (&self->sysroot_fd); + if (!ensure_sysroot_fd (self, error)) + return FALSE; + + return TRUE; +} + /** * ostree_sysroot_get_fd: * @self: Sysroot * - * Access a file descriptor that refers to the root directory of this - * sysroot. ostree_sysroot_load() must have been invoked prior to - * calling this function. + * Access a file descriptor that refers to the root directory of this sysroot. + * ostree_sysroot_initialize() (or ostree_sysroot_load()) must have been invoked + * prior to calling this function. * * Returns: A file descriptor valid for the lifetime of @self */ @@ -798,6 +866,58 @@ ensure_repo (OstreeSysroot *self, return TRUE; } +/** + * ostree_sysroot_initialize: + * @self: sysroot + * + * Subset of ostree_sysroot_load(); performs basic initialization. Notably, one + * can invoke `ostree_sysroot_get_fd()` after calling this function. + * + * It is not necessary to call this function if ostree_sysroot_load() is + * invoked. + * + * Since: 2018.10 + */ +gboolean +ostree_sysroot_initialize (OstreeSysroot *self, + GError **error) +{ + + if (self->loadstate < OSTREE_SYSROOT_LOAD_STATE_INIT) + { + /* Gather some global state; first if we have the global ostree-booted flag; + * we'll use it to sanity check that we found a booted deployment for example. + * Second, we also find out whether sysroot == /. + */ + if (!glnx_fstatat_allow_noent (AT_FDCWD, "/run/ostree-booted", NULL, 0, error)) + return FALSE; + const gboolean ostree_booted = (errno == 0); + + { struct stat root_stbuf; + if (!glnx_fstatat (AT_FDCWD, "/", &root_stbuf, 0, error)) + return FALSE; + self->root_device = root_stbuf.st_dev; + self->root_inode = root_stbuf.st_ino; + } + + struct stat self_stbuf; + if (!glnx_fstatat (AT_FDCWD, gs_file_get_path_cached (self->path), &self_stbuf, 0, error)) + return FALSE; + + const gboolean root_is_sysroot = + (self->root_device == self_stbuf.st_dev && + self->root_inode == self_stbuf.st_ino); + + self->root_is_ostree_booted = (ostree_booted && root_is_sysroot); + self->loadstate = OSTREE_SYSROOT_LOAD_STATE_INIT; + } + + if (!ensure_sysroot_fd (self, error)) + return FALSE; + + return TRUE; +} + /* Reload the staged deployment from the file in /run */ gboolean _ostree_sysroot_reload_staged (OstreeSysroot *self, @@ -870,7 +990,7 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self, GCancellable *cancellable, GError **error) { - if (!ensure_sysroot_fd (self, error)) + if (!ostree_sysroot_initialize (self, error)) return FALSE; /* Here we also lazily initialize the repository. We didn't do this @@ -880,34 +1000,6 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self, if (!ensure_repo (self, error)) return FALSE; - /* Gather some global state; first if we have the global ostree-booted flag; - * we'll use it to sanity check that we found a booted deployment for example. - * Second, we also find out whether sysroot == /. - */ - if (!self->loaded) - { - if (!glnx_fstatat_allow_noent (AT_FDCWD, "/run/ostree-booted", NULL, 0, error)) - return FALSE; - const gboolean ostree_booted = (errno == 0); - - { struct stat root_stbuf; - if (!glnx_fstatat (AT_FDCWD, "/", &root_stbuf, 0, error)) - return FALSE; - self->root_device = root_stbuf.st_dev; - self->root_inode = root_stbuf.st_ino; - } - - struct stat self_stbuf; - if (!glnx_fstat (self->sysroot_fd, &self_stbuf, error)) - return FALSE; - - const gboolean root_is_sysroot = - (self->root_device == self_stbuf.st_dev && - self->root_inode == self_stbuf.st_ino); - - self->root_is_ostree_booted = (ostree_booted && root_is_sysroot); - } - int bootversion = 0; if (!read_current_bootversion (self, &bootversion, cancellable, error)) return FALSE; @@ -989,8 +1081,8 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self, ostree_deployment_set_index (deployment, i); } - /* Determine whether we're "physical" or not, the first time we initialize */ - if (!self->loaded) + /* Determine whether we're "physical" or not, the first time we load deployments */ + if (self->loadstate < OSTREE_SYSROOT_LOAD_STATE_LOADED) { /* If we have a booted deployment, the sysroot is / and we're definitely * not physical. @@ -1008,13 +1100,14 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self, self->is_physical = TRUE; } /* Otherwise, the default is FALSE */ + + self->loadstate = OSTREE_SYSROOT_LOAD_STATE_LOADED; } self->bootversion = bootversion; self->subbootversion = subbootversion; self->deployments = deployments; deployments = NULL; /* Transfer ownership */ - self->loaded = TRUE; self->loaded_ts = stbuf.st_mtim; if (out_changed) @@ -1043,7 +1136,7 @@ ostree_sysroot_get_subbootversion (OstreeSysroot *self) OstreeDeployment * ostree_sysroot_get_booted_deployment (OstreeSysroot *self) { - g_return_val_if_fail (self->loaded, NULL); + g_return_val_if_fail (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED, NULL); return self->booted_deployment; } @@ -1059,7 +1152,7 @@ ostree_sysroot_get_booted_deployment (OstreeSysroot *self) OstreeDeployment * ostree_sysroot_get_staged_deployment (OstreeSysroot *self) { - g_return_val_if_fail (self->loaded, NULL); + g_return_val_if_fail (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED, NULL); return self->staged_deployment; } @@ -1073,7 +1166,7 @@ ostree_sysroot_get_staged_deployment (OstreeSysroot *self) GPtrArray * ostree_sysroot_get_deployments (OstreeSysroot *self) { - g_return_val_if_fail (self->loaded, NULL); + g_return_val_if_fail (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED, NULL); GPtrArray *copy = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref); for (guint i = 0; i < self->deployments->len; i++) @@ -1162,8 +1255,8 @@ ostree_sysroot_get_repo (OstreeSysroot *self, * @self: Sysroot * * This function is a variant of ostree_sysroot_get_repo() that cannot fail, and - * returns a cached repository. Can only be called after ostree_sysroot_load() - * has been invoked successfully. + * returns a cached repository. Can only be called after ostree_sysroot_initialize() + * or ostree_sysroot_load() has been invoked successfully. * * Returns: (transfer none): The OSTree repository in sysroot @self. * @@ -1172,7 +1265,7 @@ ostree_sysroot_get_repo (OstreeSysroot *self, OstreeRepo * ostree_sysroot_repo (OstreeSysroot *self) { - g_return_val_if_fail (self->loaded, NULL); + g_return_val_if_fail (self->loadstate >= OSTREE_SYSROOT_LOAD_STATE_INIT, NULL); g_assert (self->repo); return self->repo; } @@ -1367,6 +1460,10 @@ ostree_sysroot_lock (OstreeSysroot *self, { if (!ensure_sysroot_fd (self, error)) return FALSE; + + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; + return glnx_make_lock_file (self->sysroot_fd, OSTREE_SYSROOT_LOCKFILE, LOCK_EX, &self->lock, error); } @@ -1390,12 +1487,14 @@ ostree_sysroot_try_lock (OstreeSysroot *self, gboolean *out_acquired, GError **error) { - g_autoptr(GError) local_error = NULL; - if (!ensure_sysroot_fd (self, error)) return FALSE; + if (!_ostree_sysroot_ensure_writable (self, error)) + return FALSE; + /* Note use of LOCK_NB */ + g_autoptr(GError) local_error = NULL; if (!glnx_make_lock_file (self->sysroot_fd, OSTREE_SYSROOT_LOCKFILE, LOCK_EX | LOCK_NB, &self->lock, &local_error)) { @@ -1508,7 +1607,7 @@ ostree_sysroot_init_osname (OstreeSysroot *self, GCancellable *cancellable, GError **error) { - if (!ensure_sysroot_fd (self, error)) + if (!_ostree_sysroot_ensure_writable (self, error)) return FALSE; const char *deploydir = glnx_strjoina ("ostree/deploy/", osname); diff --git a/src/libostree/ostree-sysroot.h b/src/libostree/ostree-sysroot.h index 502cd75020..4dd33a6ebc 100644 --- a/src/libostree/ostree-sysroot.h +++ b/src/libostree/ostree-sysroot.h @@ -41,12 +41,19 @@ OstreeSysroot* ostree_sysroot_new (GFile *path); _OSTREE_PUBLIC OstreeSysroot* ostree_sysroot_new_default (void); +_OSTREE_PUBLIC +void ostree_sysroot_set_mount_namespace_in_use (OstreeSysroot *self); + _OSTREE_PUBLIC GFile *ostree_sysroot_get_path (OstreeSysroot *self); _OSTREE_PUBLIC int ostree_sysroot_get_fd (OstreeSysroot *self); +_OSTREE_PUBLIC +gboolean ostree_sysroot_initialize (OstreeSysroot *self, + GError **error); + _OSTREE_PUBLIC gboolean ostree_sysroot_load (OstreeSysroot *self, GCancellable *cancellable, @@ -90,6 +97,10 @@ GFile * ostree_sysroot_get_deployment_origin_path (GFile *deployment_path); _OSTREE_PUBLIC gboolean ostree_sysroot_lock (OstreeSysroot *self, GError **error); + +_OSTREE_PUBLIC +gboolean ostree_sysroot_lock_with_mount_namespace (OstreeSysroot *self, GError **error); + _OSTREE_PUBLIC gboolean ostree_sysroot_try_lock (OstreeSysroot *self, gboolean *out_acquired, diff --git a/src/ostree/ot-main.c b/src/ostree/ot-main.c index 4b72f3995b..920ad5168c 100644 --- a/src/ostree/ot-main.c +++ b/src/ostree/ot-main.c @@ -27,6 +27,7 @@ #include #include +#include #include "ot-main.h" #include "ostree.h" @@ -434,10 +435,42 @@ ostree_admin_option_context_parse (GOptionContext *context, sysroot_path = g_file_new_for_path (opt_sysroot); g_autoptr(OstreeSysroot) sysroot = ostree_sysroot_new (sysroot_path); + if (!ostree_sysroot_initialize (sysroot, error)) + return FALSE; g_signal_connect (sysroot, "journal-msg", G_CALLBACK (on_sysroot_journal_msg), NULL); if ((flags & OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED) == 0) { + /* If we're requested to lock the sysroot, first find out if the /sysroot + * subdir is a read-only mount point, and if so, create a new mount + * namespace and tell the sysroot that we've done so. See the docs for + * ostree_sysroot_set_mount_namespace_in_use(). + * + * This is a conservative approach; we could just always + * unshare() too. + */ + int sysroot_fd = ostree_sysroot_get_fd (sysroot); + g_assert_cmpint (sysroot_fd, !=, -1); + + glnx_autofd int sysroot_subdir_fd = glnx_opendirat_with_errno (sysroot_fd, "sysroot", TRUE); + if (sysroot_subdir_fd < 0) + { + if (errno != ENOENT) + return glnx_throw_errno_prefix (error, "opendirat"); + } + else if (getuid () == 0) + { + struct statvfs stvfs; + if (fstatvfs (sysroot_subdir_fd, &stvfs) < 0) + return glnx_throw_errno_prefix (error, "fstatvfs"); + if (stvfs.f_flag & ST_RDONLY) + { + if (unshare (CLONE_NEWNS) < 0) + return glnx_throw_errno_prefix (error, "preparing writable sysroot: unshare (CLONE_NEWNS)"); + ostree_sysroot_set_mount_namespace_in_use (sysroot); + } + } + /* Released when sysroot is finalized, or on process exit */ if (!ot_admin_sysroot_lock (sysroot, error)) return FALSE; diff --git a/src/switchroot/ostree-remount.c b/src/switchroot/ostree-remount.c index 5e6d23d3ae..f36ecc48bc 100644 --- a/src/switchroot/ostree-remount.c +++ b/src/switchroot/ostree-remount.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -37,10 +38,14 @@ #include #include +#include + #include "ostree-mount-util.h" +#include "glnx-backport-autocleanups.h" static void -do_remount (const char *target) +do_remount (const char *target, + bool writable) { struct stat stbuf; if (lstat (target, &stbuf) < 0) @@ -54,20 +59,33 @@ do_remount (const char *target) struct statvfs stvfsbuf; if (statvfs (target, &stvfsbuf) == -1) return; - /* If no read-only flag, skip it */ - if ((stvfsbuf.f_flag & ST_RDONLY) == 0) + + const bool currently_writable = ((stvfsbuf.f_flag & ST_RDONLY) == 0); + if (writable == currently_writable) return; - /* It's a mounted, read-only fs; remount it */ - if (mount (target, target, NULL, MS_REMOUNT | MS_SILENT, NULL) < 0) - { - /* Also ignore EINVAL - if the target isn't a mountpoint - * already, then assume things are OK. - */ - if (errno != EINVAL) - err (EXIT_FAILURE, "failed to remount %s", target); - } - else - printf ("Remounted: %s\n", target); + + int mnt_flags = MS_REMOUNT | MS_SILENT; + if (!writable) + mnt_flags |= MS_RDONLY; + if (mount (target, target, NULL, mnt_flags, NULL) < 0) + err (EXIT_FAILURE, "failed to remount %s", target); + + printf ("Remounted %s: %s\n", writable ? "rw" : "ro", target); +} + +static bool +sysroot_is_configured_ro (void) +{ + struct stat stbuf; + static const char config_path[] = "/ostree/repo/config"; + if (stat (config_path, &stbuf) != 0) + return false; + + g_autoptr(GKeyFile) keyfile = g_key_file_new (); + if (!g_key_file_load_from_file (keyfile, config_path, 0, NULL)) + return false; + + return g_key_file_get_boolean (keyfile, "sysroot", "readonly", NULL); } int @@ -95,8 +113,33 @@ main(int argc, char *argv[]) exit (EXIT_SUCCESS); } - do_remount ("/sysroot"); - do_remount ("/var"); + do_remount ("/var", true); + + /* We could also parse the ostree repo config, but...this service + * so far doesn't link to libostree or even glib. + */ + const bool sysroot_readonly = sysroot_is_configured_ro (); + if (!sysroot_readonly) + do_remount ("/sysroot", TRUE); + else + { + do_remount ("/sysroot", FALSE); + + /* Now, /etc is not normally a bind mount, but remounting the + * sysroot above made it read-only since it's on the same filesystem. + * Make it a self-bind mount, so we can then mount it read-write. + */ + if (mount ("/etc", "/etc", NULL, MS_BIND, NULL) < 0) + err (EXIT_FAILURE, "failed to make /etc a bind mount"); + do_remount ("/etc", TRUE); + /* If /var was created as as an OSTree default bind mount (instead of being a separate filesystem) + * then remounting the root mount read-only also remounted it. + * So just like /etc, we need to make it read-write by default. + * If it was a separate filesystem, we expect it to be writable anyways, + * so it doesn't hurt to remount it if so. + */ + do_remount ("/var", TRUE); + } exit (EXIT_SUCCESS); }