Skip to contentSkip to conversation

mount: overwrite options from /etc/fstab when given on the commandline

RFC In review Niels de Vos 2 participants 9 messages Aug 1, 2012 – Aug 2, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

RFC 1/1 mount: overwrite options from /etc/fstab when given on the commandline

Hi all,

it seems that some of the options given in /etc/fstab are not overloaded by options given on the mount-commandline. Some options are not allowed to be passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

Is there a reason for not overloading the options from /etc/fstab by the options given on the commandline?

The patch below changes the behaviour of mount so that options on the commandline replace options given in /etc/fstab. This example is based on the now deprecated code of mount in util-linux-2.17.2. I'd like to receive some responses if this change would be acceptible, or if this was done by design.

If this can be accepted, I'll happily look into the libmount code and provide a patch for the current util-linux version.

Many thanks,
Niels

---
 mount/mount.c |   44 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 1 deletions(-)
mount/mount.c +43 -1
index 45f9699..e594f5e 100644
@@ -294,6 +294,48 @@ print_all (char *types) {
      exit (0);
 }
 
+/* reallocates its first arg
+ * opt is the single option, val is the opt=val pair */
+static char *
+merge_one_opt(char *s, const char *opt, const char *val)
+{
+	char *old_opt, *next_opt;
+
+	if (!opt)
+		return s;
+	if (!s)
+		return xstrdup(val);		/* opt & opt=val */
+	if (!val)
+		return s;				/* s,opt */
+
+	old_opt = strstr(s, opt);
+	if (old_opt) {
+		next_opt = strchr(old_opt, ',');
+		if (!next_opt)
+			memset(old_opt, '\0', 1);
+		else
+			memmove(old_opt, next_opt + 1, strlen(next_opt) + 1);
+	}
+	return xstrconcat3(s, ",", val);		/* s,opt=val */
+}
+
+/* reallocates its first arg */
+static char *
+merge_opts(char *s, const char *extra_opts)
+{
+	char *new_opts = xstrdup(extra_opts);
+	char *opt, *opt_pos, *o;
+
+	opt = strtok_r(new_opts, ",", &opt_pos);
+	while (opt != NULL) {
+		o = xstrndup(opt, strchrnul(opt, '=') - opt);
+		s = merge_one_opt(s, o, opt);
+		opt = strtok_r(NULL, ",", &opt_pos);
+	}
+
+	return s;
+}
+
 /* reallocates its first arg */
 static char *
 append_opt(char *s, const char *opt, const char *val)
@@ -1701,7 +1743,7 @@ mount_one (const char *spec, const char *node, const char *types,
 	opts = usersubst(fstabopts);
 
 	/* Merge the fstab and command line options.  */
-	opts = append_opt(opts, cmdlineopts, NULL);
+	opts = merge_opts(opts, cmdlineopts);
 
 	if (types == NULL && !mounttype && !is_existing_file(spec)) {
 		if (strchr (spec, ':') != NULL) {
Signature · 1 line
1.7.7.6
maintainer
#1 lore raw
Quoted 2 lines · Niels de Vos

it seems that some of the options given in /etc/fstab are not overloaded by options given on the mount-commandline.

Well, all depends on kernel (and FS driver) how mount options are evaluated. We have no any strict rules for 'source' and 'mount options' in the mount(2) syscall.

The generic convention (!= rule) is that the option overwrites an option previously specified in the mount options string:

    aaa=x,bbb,aaa=y     -> aaa should be 'y'

This is reason why mount(8) only appends the options from command line to the options string from fstab.

Anyway, depends on FS driver how the options string will be interpreted -- for example btrfs supports duplicate device= mount option.

Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Quoted 5 lines

Is there a reason for not overloading the options from /etc/fstab by the options given on the commandline?

The patch below changes the behaviour of mount so that options on the commandline replace options given in /etc/fstab.

I'd like to avoid that mount(8) tries to understand filesystem specific options. The command mount(8) has no clue about the options (non-VFS options). For example:

fstab:
/dev/sda /mnt btrfs device=/dev/sdb 0 0

command line:

mount /mnt -o device=/dev/sdc,device=/dev/sdd

so the final mount options string:

device=/dev/sdb,device=/dev/sdc,device=/dev/sdd

.. and this all is pretty valid for btrfs.

I'd like to implement for the next release 2.23 a new command line options to specify a way how compose the final options string in the mount(8). It's already in our Documentation/TODO file, see --options-mode, probably something like:

    --options-mode={ignore,append,prepend,replace,dedup}

or so.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#2 lore raw
Quoted 5 lines · Karel Zak
Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Note that mount(8) and libmount removes all selinux context options on remount for kernels < 2.6.39 because SELinux context remount was unsupported, so this SELinux disadvantage should be hidden for usual use case.

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw
Quoted 7 lines · Karel Zak
Quoted 5 lines · Karel Zak
Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Quoted 8 lines

Note that mount(8) and libmount removes all selinux context options on remount for kernels< 2.6.39 because SELinux context remount was unsupported, so this SELinux disadvantage should be hidden for usual use case.

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

That would be great. What is your opinion on getting the SElinux options handled differently in the kernel instead or in addition? If the kernel would allow multiple SElinux options (and only the last gets applied) would you still be interested in adding this SElinux exception?

Thanks again,
Niels

maintainer
#4 lore raw
Quoted 13 lines · Niels de Vos
Quoted 12 lines · Karel Zak
Quoted 5 lines · Karel Zak
Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Note that mount(8) and libmount removes all selinux context options on remount for kernels< 2.6.39 because SELinux context remount was unsupported, so this SELinux disadvantage should be hidden for usual use case.

Quoted 6 lines
Quoted 3 lines

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

That would be great. What is your opinion on getting the SElinux options handled differently in the kernel instead or in addition?

SELinux is no filesystem and we already have (must have) SELinux specific code in mount(8) (and many others utils;-).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw
Quoted 18 lines · Karel Zak
Quoted 17 lines · Niels de Vos
Quoted 15 lines · Karel Zak
Quoted 5 lines · Karel Zak
Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Note that mount(8) and libmount removes all selinux context options on
remount for kernels< 2.6.39 because SELinux context remount was
unsupported, so this SELinux disadvantage should be hidden for usual use case.

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

Quoted 5 lines
Quoted 2 lines

That would be great. What is your opinion on getting the SElinux options handled differently in the kernel instead or in addition?

SELinux is no filesystem and we already have (must have) SELinux specific code in mount(8) (and many others utils;-).

Okay, so as it's not a filesystem, it warrants the de-dup handling. I'd appreciate it if this makes it in 2.22-rc2. Please let me know if you want me to provide patches and/or test the results.

Many thanks,
Niels

maintainer
#6 lore raw
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Karel Zak
Quoted 3 lines

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

Implemented (for libmount) in the master branch. You can try it:

./autogen.sh
./configure
make mount

./mount -o ...

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#7 lore raw
Quoted 5 lines · Karel Zak
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Karel Zak
Quoted 3 lines

Maybe we can also add another exception for SELinux to de-duplicate the SELinux context options in the options string. It's not perfect, but probably acceptable for SELinux -- I can do this for 2.22-rc2.

Quoted 7 lines

Implemented (for libmount) in the master branch. You can try it:

   ./autogen.sh
   ./configure
   make mount

   ./mount -o ...

Great! It works for me on Fedora Rawhide when I add --with-selinux to ./configure.

Thanks for your fast responses and implementation, Niels

#8 lore raw
In reply to Karel Zak
Quoted 11 lines · Karel Zak
Quoted 2 lines · Niels de Vos

it seems that some of the options given in /etc/fstab are not overloaded by options given on the mount-commandline.

Well, all depends on kernel (and FS driver) how mount options are evaluated. We have no any strict rules for 'source' and 'mount options' in the mount(2) syscall.

The generic convention (!= rule) is that the option overwrites an option previously specified in the mount options string:

Quoted 8 lines
     aaa=x,bbb,aaa=y     ->  aaa should be 'y'

This is reason why mount(8) only appends the options from command line to the options string from fstab.

Anyway, depends on FS driver how the options string will be interpreted -- for example btrfs supports duplicate device= mount option.

Ah, okay, if there are valid use-cases for options to be listed multiple times, it won't be possible to overload the options from /etc/fstab by the options from the commandline.

Quoted 5 lines
Quoted 3 lines

Some options are not allowed to be
passed multiple times (like the SElinux context options) and mounting will fail if options are present in both /etc/fstab and on the commandline.

This is stupid SELinux disadvantage and SELinux should be fixed.

Yeah, sounds like it. I may have a look on how the kernel can handle the SElinux options better.

Quoted 15 lines
Quoted 5 lines

Is there a reason for not overloading the options from /etc/fstab by the options given on the commandline?

The patch below changes the behaviour of mount so that options on the commandline replace options given in /etc/fstab.

I'd like to avoid that mount(8) tries to understand filesystem specific options. The command mount(8) has no clue about the options (non-VFS options). For example:

fstab:
/dev/sda /mnt btrfs device=/dev/sdb 0 0

command line:

Quoted 8 lines

mount /mnt -o device=/dev/sdc,device=/dev/sdd

so the final mount options string:

device=/dev/sdb,device=/dev/sdc,device=/dev/sdd

.. and this all is pretty valid for btrfs.

Nice example!

Quoted 8 lines

I'd like to implement for the next release 2.23 a new command line options to specify a way how compose the final options string in the mount(8). It's already in our Documentation/TODO file, see --options-mode, probably something like:

     --options-mode={ignore,append,prepend,replace,dedup}

or so.

Hmm, that sounds like it'll solve the problem, unless you'r combining SElinux contexts and btrsf...

Thanks for your detailed response,
Niels