summaryrefslogtreecommitdiff
path: root/src/fstab-generator/fstab-generator.c
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-07-11 12:48:57 -0400
committerLennart Poettering <lennart@poettering.net>2017-07-11 18:48:57 +0200
commit634735b56b82bdde3c67193ba7b470bab80fdcbd (patch)
tree557e01b8d5e2ced28a9ff8df13f02e13a482661b /src/fstab-generator/fstab-generator.c
parentcd2d4c7f689cc02b5a592e9212b30e46c516b676 (diff)
downloadsystemd-634735b56b82bdde3c67193ba7b470bab80fdcbd.tar.gz
fstab-generator: Chase symlinks where possible (#6293)
This has a long history; see see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which originally introduced the behavior. Unfortunately that commit doesn't include any rationale, but IIRC the basic issue is that systemd wants to model the real mount state as units, and symlinks make canonicalization much more difficult. At the same time, on a RHEL6 system (upstart), one can make e.g. `/home` a symlink, and things work as well as they always did; but one doesn't have access to the sophistication of mount units (dependencies, introspection, etc.) Supporting symlinks here will hence make it easier for people to do upgrades to RHEL7 and beyond. The `/home` as symlink case also appears prominently for OSTree; see https://ostree.readthedocs.io/en/latest/manual/adapting-existing/ Further work has landed in the nspawn case for this; see e.g. d944dc9553009822deaddec76814f5642a6a8176 A basic limitation with doing this in the fstab generator (and that I hit while doing some testing) is that we obviously can't chase symlinks into mounts, since the generator runs early before mounts. Or at least - doing so would require multiple passes over the fstab data (as well as looking at existing mount units), and potentially doing multi-phase generation. I'm not sure it's worth doing that without a real world use case. For now, this will fix at least the OSTree + `/home` <https://bugzilla.redhat.com/show_bug.cgi?id=1382873> case mentioned above, and in general anyone who for whatever reason has symlinks in their `/etc/fstab`.
Diffstat (limited to 'src/fstab-generator/fstab-generator.c')
-rw-r--r--src/fstab-generator/fstab-generator.c41
1 files changed, 33 insertions, 8 deletions
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index ce9dccfe68..7f23b9fd74 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -25,6 +25,7 @@
#include "alloc-util.h"
#include "fd-util.h"
+#include "fs-util.h"
#include "fileio.h"
#include "fstab-util.h"
#include "generator.h"
@@ -290,6 +291,7 @@ static int add_mount(
const char *dest,
const char *what,
const char *where,
+ const char *original_where,
const char *fstype,
const char *opts,
int passno,
@@ -396,11 +398,10 @@ static int add_mount(
return r;
}
- fprintf(f,
- "\n"
- "[Mount]\n"
- "Where=%s\n",
- where);
+ fprintf(f, "\n[Mount]\n");
+ if (original_where)
+ fprintf(f, "# Canonicalized from %s\n", original_where);
+ fprintf(f, "Where=%s\n", where);
r = write_what(f, what);
if (r < 0)
@@ -520,7 +521,7 @@ static int parse_fstab(bool initrd) {
}
while ((me = getmntent(f))) {
- _cleanup_free_ char *where = NULL, *what = NULL;
+ _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
bool noauto, nofail;
int k;
@@ -540,8 +541,28 @@ static int parse_fstab(bool initrd) {
if (!where)
return log_oom();
- if (is_path(where))
+ if (is_path(where)) {
path_kill_slashes(where);
+ /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
+ * mount units, but causes problems since it historically worked to have symlinks in e.g.
+ * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
+ * where a symlink refers to another mount target; this works assuming the sub-mountpoint
+ * target is the final directory.
+ */
+ r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
+ CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
+ &canonical_where);
+ if (r < 0)
+ /* In this case for now we continue on as if it wasn't a symlink */
+ log_warning_errno(r, "Failed to read symlink target for %s: %m", where);
+ else {
+ if (streq(canonical_where, where))
+ canonical_where = mfree(canonical_where);
+ else
+ log_debug("Canonicalized what=%s where=%s to %s",
+ what, where, canonical_where);
+ }
+ }
noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
@@ -567,7 +588,8 @@ static int parse_fstab(bool initrd) {
k = add_mount(arg_dest,
what,
- where,
+ canonical_where ?: where,
+ canonical_where ? where: NULL,
me->mnt_type,
me->mnt_opts,
me->mnt_passno,
@@ -630,6 +652,7 @@ static int add_sysroot_mount(void) {
return add_mount(arg_dest,
what,
"/sysroot",
+ NULL,
arg_root_fstype,
opts,
is_device_path(what) ? 1 : 0, /* passno */
@@ -684,6 +707,7 @@ static int add_sysroot_usr_mount(void) {
return add_mount(arg_dest,
what,
"/sysroot/usr",
+ NULL,
arg_usr_fstype,
opts,
is_device_path(what) ? 1 : 0, /* passno */
@@ -724,6 +748,7 @@ static int add_volatile_var(void) {
return add_mount(arg_dest_late,
"tmpfs",
"/var",
+ NULL,
"tmpfs",
"mode=0755",
0,