Skip to contentSkip to conversation

mount: "nofail" and already mounted devices?

DISCUSSION Stanislav Brabec 2 participants 4 messages Jan 8, 2014 – Jan 9, 2014 Util-Linux package

lore
Share & tools
lore
#0 lore raw

Attempting to mount of already mounted devices is a common situation in many scripts. There is no easy way to detect it in the current mount implementation.

One of our users complains that this situation is not covered by "nofail" mount option. ("nofail" does exactly what the documentation describes and nothing more.)

Would be acceptable a patch that adds "already mounted" to "nofail"?

If not, what would be the best way of enabling this feature for mounting a single device? (It already exists in mount, but it is usable only with "mount -a".)

- Add another mount option, e. g. mountednofail or mountedok
- Make possible to use "-a" with further arguments.
- Add new option, e. g. "-A".
and/or
- New result code, e. g. 33 instead of 32. (As it can not suppress the error message, it is well usable only as an addition to above.)

Signature · 10 lines
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: [email protected]
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                 fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76
maintainer
#1 lore raw
Quoted 2 lines · Stanislav Brabec

Attempting to mount of already mounted devices is a common situation in many scripts.

What about to fix the scripts? :-)

All you need is to call

    findmnt --source <dev> --target <mountpoint>

and another possibility is compose /tmp/fstab by the script and then call "mount --all --fstab /tmp/fstab"

Quoted 4 lines

There is no easy way to detect it in the current mount implementation.

Would be acceptable a patch that adds "already mounted" to "nofail"?

Maybe.

Quoted 3 lines

If not, what would be the best way of enabling this feature for mounting a single device? (It already exists in mount, but it is usable only with "mount -a".)

It's not the same situation. The mount --all checks for the problem in userspace (don't call mount(2) if the device is already mounted), but for a single device we always call mount(2) and "already mounted or busy" is based on EBUSY from kernel and you have to check mtab to verify that the device is really already mounted (sys-utils/mount.c:481).

Quoted 1 line

- Add another mount option, e. g. mountednofail or mountedok

I'd like to avoid new mount options.

Quoted 1 line

- Make possible to use "-a" with further arguments.

mount -a /foo /bar [...]

Right?

It seems also attractive feature when you want to manually mount more filesystems and it's without any side effects to the current mount(8) semantic. (And we already ave "umount /foo /bar [...]".)

If you want to implement it then you have to filter out fstab entries in the sys-utils/mount.c:mount_all() because "mount-all" logic is hidden in libmount and I'm not sure if I want to extend the API for this functionality :-)

So something like, sys-utils/mount.c:mount_all():

 if (nwanted) {
         struct libmnt_table *fstab;

         mnt_context_get_fstab(cxt, &fstab);

         while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
            int yes = 0;

            for (i = 0; yes == 0 && i < nwanted; i++)
                yes = mnt_fs_match_target(fs, wanted[i], cache);
            if (!yes)
                mnt_table_remove_fs(fstab, fs);
         }

}

while (mnt_context_next_mount ....

... where wanted[] is array composed from argv[].

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 9 lines · Karel Zak
Quoted 2 lines · Stanislav Brabec

Attempting to mount of already mounted devices is a common situation in many scripts.

What about to fix the scripts? :-)

All you need is to call

    findmnt --source <dev> --target <mountpoint>

Yes, it is an option.

The new behavior could simplify common situation and also prevent a race:
if the device is not yet mounted
mount it
do the work

Quoted 3 lines
Quoted 1 line

Would be acceptable a patch that adds "already mounted" to "nofail"?

Maybe.

Well, It could be a straightforward solution (~2 lines change). But it could break following script (theoretical, a bit dumb and fragile, but still depending only on documented behavior):

... do some actions that will initiate delayed $DEVICE creation ... while mount -o nofail $DEVICE /opt ; do
if test -d /opt/myvendor ; then
break
fi
sleep 1
done

Now imagine, that somebody previously called:
mount $DEVICE /opt

What will happen with the current mount?
The script will fail as expected.

What will happen after such patch?
The script will enter to a dead loop.

Quoted 5 lines
Quoted 1 line

- Make possible to use "-a" with further arguments.

mount -a /foo /bar [...]

Right?

Well, it sounds interesting as well, but I was thinking about (conflicting):
mount -a /dev/device /mountpoint

It can be done both:

mount -a /foo /bar [...]
mount -A /dev/device /mountpoint

-A Do not report errors if the mount point is already used or device is
already mounted.

Signature · 10 lines
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: [email protected]
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                 fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76
maintainer
#3 lore raw
Quoted 6 lines · Stanislav Brabec

Karel Zak wrote:
The new behavior could simplify common situation and also prevent a race:
if the device is not yet mounted
mount it
do the work

It would be enough to add extra return code for EBUSY (which is probably a good idea for all scenarios).

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

- Make possible to use "-a" with further arguments.

mount -a /foo /bar [...]

Right?

Well, it sounds interesting as well, but I was thinking about (conflicting):
mount -a /dev/device /mountpoint

It's new feature, so we can be pretty strict about it

mount -a [<mountpoint> ..]

and do not support "mount -a <source> <target>". If you need something more advanced then you have to compose a script.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com