Merge pull request #11226 from keszybz/enable-remount-fs-dynamically

Enable systemd-remount-fs.service dynamically
This commit is contained in:
Lennart Poettering 2019-02-18 12:46:31 +01:00 committed by GitHub
commit 4d422d1f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 142 additions and 109 deletions

1
TODO
View File

@ -825,6 +825,7 @@ Features:
- man: document the very specific env the shutdown drop-in tools live in
- man: add more examples to man pages
- man: maybe sort directives in man pages, and take sections from --help and apply them to man too
- document root=gpt-auto properly
* systemctl:
- add systemctl switch to dump transaction without executing it

View File

@ -329,7 +329,7 @@
<listitem>
<para>Configures the root file system and its file system
type and mount options, as well as whether it shall be
mounted read-only or read-writable initially. For details,
mounted read-only or read-write initially. For details,
see
<citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
</listitem>

View File

@ -196,6 +196,45 @@
<citerefentry><refentrytitle>systemd.generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>
</refsect1>
<refsect1>
<title>Kernel Command Line</title>
<para><filename>systemd-gpt-auto-generator</filename> understands the following kernel command line
parameters:</para>
<variablelist class='kernel-commandline-options'>
<varlistentry>
<term><varname>systemd.gpt_auto</varname></term>
<term><varname>rd.systemd.gpt_auto</varname></term>
<listitem><para>Those options take an optional boolean argument, and default to yes.
The generator is enabled by default, and a negative value may be used to disable it.
</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>root=</varname></term>
<listitem><para>When used with the special value <literal>gpt-auto</literal>, automatic discovery of
the root parition based on the GPT partition type is enabled. Any other value disables this
generator.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>rw</varname></term>
<term><varname>ro</varname></term>
<listitem><para>Mount the root partition read-write or read-only <emphasis>initially</emphasis>.</para>
<para>Note that unlike most kernel command line options these settings do not override configuration
in the file system, and the file system may be remounted later. See
<citerefentry><refentrytitle>systemd-remount-fs.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>See Also</title>
<para>

View File

@ -30,26 +30,31 @@
<refsect1>
<title>Description</title>
<para><filename>systemd-remount-fs.service</filename> is an
early boot service that applies mount options listed in
<citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>
to the root file system, the <filename>/usr</filename> file system,
and the kernel API file systems. This is required so that the
mount options of these file systems — which are pre-mounted by
the kernel, the initial RAM disk, container environments or system
manager code — are updated to those listed in
<filename>/etc/fstab</filename>. This service ignores normal file
systems and only changes the root file system (i.e.
<filename>/</filename>), <filename>/usr</filename> and the virtual
kernel API file systems such as <filename>/proc</filename>,
<filename>/sys</filename> or <filename>/dev</filename>. This
service executes no operation if <filename>/etc/fstab</filename>
does not exist or lists no entries for the mentioned file
systems.</para>
<para><filename>systemd-remount-fs.service</filename> is an early boot service that applies mount options
listed in <citerefentry
project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>, or
gathered from the partition table (when
<citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
is active) to the root file system, the <filename>/usr</filename> file system, and the kernel API file
systems. This is required so that the mount options of these file systems — which are pre-mounted by the
kernel, the initial RAM disk, container environments or system manager code — are updated to those
configured in <filename>/etc/fstab</filename> and the other sources. This service ignores normal file
systems and only changes the root file system (i.e. <filename>/</filename>), <filename>/usr</filename>,
and the virtual kernel API file systems such as <filename>/proc</filename>, <filename>/sys</filename> or
<filename>/dev</filename>. This service executes no operation if no configuration is found
(<filename>/etc/fstab</filename> does not exist or lists no entries for the mentioned file systems, or
the partition table does not contain relevant entries).</para>
<para>For a longer discussion of kernel API file systems see
<ulink url="https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems">API
File Systems</ulink>.</para>
<para>Note: <filename>systemd-remount-fs.service</filename> is usually pulled in by
<citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
hence it is also affected by the kernel command line option <varname>fstab=</varname>, which may be used
to disable the generator. It may also pulled in by
<citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
which is affected by <varname>systemd.gpt_auto</varname> and other options.</para>
</refsect1>
<refsect1>
@ -57,7 +62,9 @@
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>mount</refentrytitle><manvolnum>8</manvolnum></citerefentry>
<citerefentry project='man-pages'><refentrytitle>mount</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
</para>
</refsect1>

View File

@ -76,9 +76,11 @@
/* Magic early boot services */
#define SPECIAL_FSCK_SERVICE "systemd-fsck@.service"
#define SPECIAL_FSCK_ROOT_SERVICE "systemd-fsck-root.service"
#define SPECIAL_QUOTACHECK_SERVICE "systemd-quotacheck.service"
#define SPECIAL_QUOTAON_SERVICE "quotaon.service"
#define SPECIAL_REMOUNT_FS_SERVICE "systemd-remount-fs.service"
#define SPECIAL_VOLATILE_ROOT_SERVICE "systemd-volatile-root.service"
/* Services systemd relies on */
#define SPECIAL_DBUS_SERVICE "dbus.service"

View File

@ -518,6 +518,8 @@ static int parse_fstab(bool initrd) {
int r = 0;
fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
log_debug("Parsing %s...", fstab_path);
f = setmntent(fstab_path, "re");
if (!f) {
if (errno == ENOENT)
@ -720,23 +722,14 @@ static int add_sysroot_usr_mount(void) {
}
static int add_volatile_root(void) {
const char *from, *to;
/* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
* requested, leaving only /usr from the root mount inside. */
if (arg_volatile_mode != VOLATILE_YES)
return 0;
/* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
* requested, leaving only /usr from the root mount inside. */
from = strjoina(SYSTEM_DATA_UNIT_PATH "/systemd-volatile-root.service");
to = strjoina(arg_dest, "/" SPECIAL_INITRD_ROOT_FS_TARGET, ".requires/systemd-volatile-root.service");
(void) mkdir_parents(to, 0755);
if (symlink(from, to) < 0)
return log_error_errno(errno, "Failed to hook in volatile remount service: %m");
return 0;
return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE);
}
static int add_volatile_var(void) {
@ -868,7 +861,7 @@ static int determine_root(void) {
}
static int run(const char *dest, const char *dest_early, const char *dest_late) {
int r;
int r, r2 = 0, r3 = 0;
assert_se(arg_dest = dest);
assert_se(arg_dest_late = dest_late);
@ -881,42 +874,27 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
/* Always honour root= and usr= in the kernel command line if we are in an initrd */
if (in_initrd()) {
int k;
r = add_sysroot_mount();
k = add_sysroot_usr_mount();
if (k < 0)
r = k;
r2 = add_sysroot_usr_mount();
k = add_volatile_root();
if (k < 0)
r = k;
r3 = add_volatile_root();
} else
r = add_volatile_var();
/* Honour /etc/fstab only when that's enabled */
if (arg_fstab_enabled) {
int k;
log_debug("Parsing /etc/fstab");
/* Parse the local /etc/fstab, possibly from the initrd */
k = parse_fstab(false);
if (k < 0)
r = k;
r2 = parse_fstab(false);
/* If running in the initrd also parse the /etc/fstab from the host */
if (in_initrd()) {
log_debug("Parsing /sysroot/etc/fstab");
k = parse_fstab(true);
if (k < 0)
r = k;
}
if (in_initrd())
r3 = parse_fstab(true);
else
r3 = generator_enable_remount_fs_service(arg_dest);
}
return r;
return r < 0 ? r : r2 < 0 ? r2 : r3;
}
DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -467,13 +467,13 @@ static int add_root_rw(DissectedPartition *p) {
return 0;
}
(void) generator_enable_remount_fs_service(arg_dest);
path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf");
(void) mkdir_parents(path, 0755);
r = write_string_file(path,
"# Automatically generated by systemd-gpt-generator\n\n"
"[Unit]\n"
"ConditionPathExists=\n\n" /* We need to turn off the ConditionPathExist= in the main unit file */
"[Service]\n"
"Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW);
@ -678,6 +678,9 @@ static int add_root_mount(void) {
return r;
}
/* Note that we do not need to enable systemd-remount-fs.service here. If
* /etc/fstab exists, systemd-fstab-generator will pull it in for us. */
return add_mount(
"root",
"/dev/gpt-auto-root",

View File

@ -47,6 +47,31 @@ static int track_pid(Hashmap **h, const char *path, pid_t pid) {
return 0;
}
static int do_remount(const char *path, bool force_rw, Hashmap **pids) {
pid_t pid;
int r;
log_debug("Remounting %s...", path);
r = safe_fork(force_rw ? "(remount-rw)" : "(remount)",
FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
if (r < 0)
return r;
if (r == 0) {
/* Child */
execv(MOUNT_PATH,
STRV_MAKE(MOUNT_PATH,
path,
"-o",
force_rw ? "remount,rw" : "remount"));
log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
_exit(EXIT_FAILURE);
}
/* Parent */
return track_pid(pids, path, pid);
}
static int run(int argc, char *argv[]) {
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
_cleanup_endmntent_ FILE *f = NULL;
@ -66,36 +91,20 @@ static int run(int argc, char *argv[]) {
if (!f) {
if (errno != ENOENT)
return log_error_errno(errno, "Failed to open /etc/fstab: %m");
} else {
} else
while ((me = getmntent(f))) {
pid_t pid;
/* Remount the root fs, /usr and all API VFS */
/* Remount the root fs, /usr, and all API VFSs */
if (!mount_point_is_api(me->mnt_dir) &&
!PATH_IN_SET(me->mnt_dir, "/", "/usr"))
continue;
log_debug("Remounting %s...", me->mnt_dir);
if (path_equal(me->mnt_dir, "/"))
has_root = true;
r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
if (r < 0)
return r;
if (r == 0) {
/* Child */
execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
_exit(EXIT_FAILURE);
}
/* Parent */
r = track_pid(&pids, me->mnt_dir, pid);
r = do_remount(me->mnt_dir, false, &pids);
if (r < 0)
return r;
}
}
if (!has_root) {
/* The $SYSTEMD_REMOUNT_ROOT_RW environment variable is set by systemd-gpt-auto-generator to tell us
@ -103,27 +112,14 @@ static int run(int argc, char *argv[]) {
* which takes precedence. */
r = getenv_bool("SYSTEMD_REMOUNT_ROOT_RW");
if (r > 0) {
pid_t pid;
log_debug("Remounting / writable...");
r = safe_fork("(remount-rw)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
if (r < 0)
return r;
if (r == 0) {
/* Child */
execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, "/", "-o", "remount,rw"));
log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
_exit(EXIT_FAILURE);
}
r = track_pid(&pids, "/", pid);
if (r < 0)
return r;
} else if (r < 0 && r != -ENXIO)
if (r < 0 && r != -ENXIO)
log_warning_errno(r, "Failed to parse $SYSTEMD_REMOUNT_ROOT_RW, ignoring: %m");
if (r > 0) {
r = do_remount("/", true, &pids);
if (r < 0)
return r;
}
}
r = 0;

View File

@ -55,13 +55,14 @@ int generator_open_unit_file(
return 0;
}
int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src) {
/* Adds a symlink from <dst>.<dep_type>.d/ to ../<src> */
int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src) {
/* Adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute)
* or ../<src> (otherwise). */
const char *from, *to;
from = strjoina("../", src);
to = strjoina(root, "/", dst, ".", dep_type, "/", src);
from = path_is_absolute(src) ? src : strjoina("../", src);
to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src));
mkdir_parents_label(to, 0755);
if (symlink(from, to) < 0)
@ -85,7 +86,7 @@ static int write_fsck_sysroot_service(const char *dir, const char *what) {
if (!escaped2)
return log_oom();
unit = strjoina(dir, "/systemd-fsck-root.service");
unit = strjoina(dir, "/"SPECIAL_FSCK_ROOT_SERVICE);
log_debug("Creating %s", unit);
r = unit_name_from_path(what, ".device", &device);
@ -157,10 +158,10 @@ int generator_write_fsck_deps(
if (path_equal(where, "/")) {
const char *lnk;
lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/"SPECIAL_FSCK_ROOT_SERVICE);
mkdir_parents(lnk, 0755);
if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
if (symlink(SYSTEM_DATA_UNIT_PATH "/"SPECIAL_FSCK_ROOT_SERVICE, lnk) < 0)
return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
} else {
@ -172,7 +173,7 @@ int generator_write_fsck_deps(
if (r < 0)
return r;
fsck = "systemd-fsck-root.service";
fsck = SPECIAL_FSCK_ROOT_SERVICE;
} else {
r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck);
if (r < 0)
@ -498,6 +499,12 @@ int generator_hook_up_growfs(
return generator_add_symlink(dir, where_unit, "wants", unit);
}
int generator_enable_remount_fs_service(const char *dir) {
/* Pull in systemd-remount-fs.service */
return generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants",
SYSTEM_DATA_UNIT_PATH "/" SPECIAL_REMOUNT_FS_SERVICE);
}
void log_setup_generator(void) {
log_set_prohibit_ipc(true);
log_setup_service();

View File

@ -11,7 +11,7 @@ int generator_open_unit_file(
const char *name,
FILE **file);
int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src);
int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src);
int generator_write_fsck_deps(
FILE *f,
@ -50,6 +50,8 @@ int generator_hook_up_growfs(
const char *where,
const char *target);
int generator_enable_remount_fs_service(const char *dir);
void log_setup_generator(void);
/* Similar to DEFINE_MAIN_FUNCTION, but initializes logging and assigns positional arguments. */

View File

@ -759,7 +759,7 @@ static void test_unit_name_from_dbus_path(void) {
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dcoredump_2esocket", 0, "systemd-coredump.socket");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dcoredump_400_2eservice", 0, "systemd-coredump@0.service");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dfirstboot_2eservice", 0, "systemd-firstboot.service");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dfsck_2droot_2eservice", 0, "systemd-fsck-root.service");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dfsck_2droot_2eservice", 0, SPECIAL_FSCK_ROOT_SERVICE);
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dhwdb_2dupdate_2eservice", 0, "systemd-hwdb-update.service");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dinitctl_2eservice", 0, "systemd-initctl.service");
test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/systemd_2dinitctl_2esocket", 0, "systemd-initctl.socket");

View File

@ -186,8 +186,7 @@ in_units = [
['systemd-quotacheck.service', 'ENABLE_QUOTACHECK'],
['systemd-random-seed.service', 'ENABLE_RANDOMSEED',
'sysinit.target.wants/'],
['systemd-remount-fs.service', '',
'local-fs.target.wants/'],
['systemd-remount-fs.service', ''],
['systemd-resolved.service', 'ENABLE_RESOLVE',
join_paths(pkgsysconfdir, 'system/dbus-org.freedesktop.resolve1.service') + ' ' +
join_paths(pkgsysconfdir, 'system/multi-user.target.wants/')],

View File

@ -16,7 +16,6 @@ Conflicts=shutdown.target
After=systemd-fsck-root.service
Before=local-fs-pre.target local-fs.target shutdown.target
Wants=local-fs-pre.target
ConditionPathExists=/etc/fstab
[Service]
Type=oneshot