Find the answer to your Linux question:
Results 1 to 7 of 7
This is an odd one. I have a C program that calls umount to unmount a volume. A simplified case looks like this: Code: int main() { int rc = ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    37

    Calling umount2() unmounts volume but df entry still visible


    This is an odd one. I have a C program that calls umount to unmount a volume. A simplified case looks like this:

    Code:
    int main()
    {
        int rc = umount2("/v0", MNT_FORCE);
        if (rc != 0)
        {
            printf("Unable to unmount volume /v0, err='%s'", strerror(errno));
        }
    }
    When I run this code, the "/v0" volume is unmount, but it still shows up in df:

    Code:
    # mount /dev/sda7 /v0
    # touch /v0/xxx
    # df
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/sda1              8254240   3554504   4280444  46% /
    /dev/sda7             20954552      4260  20950292   1% /v0
    /dev/sdb7             20954552      4256  20950296   1% /v1
    /dev/sda2              4127108    180232   3737228   5% /var
    #./a.out
    #df
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/sda1              8254240   3554504   4280444  46% /
    /dev/sda7             20954552      4260  20950292   1% /v0
    /dev/sdb7             20954552      4256  20950296   1% /v1
    /dev/sda2              4127108    180232   3737228   5% /var
    As you can see, df still shows that /v0 is mount. However, it's not *really* mounted. If I try to access it, the commands fail:

    Code:
    # ls /v0/xxx
    ls: cannot access /v0/xxx: No such file or directory
    If I run a umount, it fails as well, but this removes the /v0 entry as well:

    Code:
    # umount -f /v0
    umount2: Invalid argument
    umount: /v0: not mounted
    umount2: Invalid argument
    
    # df
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/sda1              8254240   3554504   4280444  46% /
    /dev/sdb7             20954552      4256  20950296   1% /v1
    /dev/sda2              4127108    180232   3737228   5% /var
    So, the question is, why does calling umount2() in my code not completely unmount the volume?

    I also discovered the reverse effect with calling mount() in code. When I make a call like

    Code:
    mount(drive, volume, "xfs", MS_NOATIME, NULL);
    the indicated drive is mounted since I can access the files as expected, but df doesn't show the drive has been mounted.

    Why is my C code behaving this way? What do the mount/umount commands do when run from the command line that I'm not doing in my C code?
    Last edited by PeterSteele; 11-14-2010 at 09:07 PM.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Just because you can see the device entry doesn't mean that it is mounted. Mounted means that the volume is associated with a directory entry in the file system. What is the output of the "mount" command in the shell? The device should not show up in the mount list. Until the device is physically removed or turned off, it will show up in /dev.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    37
    My point is that when I run the command

    umount -f /v0

    from the command line, and then run "df" or "mount", the previous mount for this volume has been removed. However, when I do the same thing from code using the call

    umount2(volume, MNT_FORCE);

    and then run "df" or "mount", the entry for /v0 is still there. But it seems to be inaccurate, because if I follow this up with a

    umount -f /v0

    from the command line, it complains the the volume isn't mounted. Curiously though, after running this umount command, even though it complains the volume isn't mounted, the entry no longer appears when a "df" is run. So the umount command *does* clean up something.

    My question is, what does the command line app "umount" do in addition to making a call to umount2()?

  4. #4
    Just Joined!
    Join Date
    Nov 2010
    Posts
    37
    I've discovered that if I run the command

    umount -n /v0

    this gives me the same result as calling umount2() in code. The -n option tells umount not to update /etc/mtab, so this is apparently something that I have to do explicitly when I umount a volume in code. I just need to find the appropriate call to tell the system to refresh /etc/mtab.

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Posts
    37
    In looking at the source code to umount.c, it makes a call to update_mtab() to update /etc/mtab and calling umount2(). This function is not exposed though in libc, it's an internal function in fstab.c. So it appears the easiest way to unmount a volume in code is by using system() to call the umount command directly.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Well, you could use umount() instead of umount2(). It occurs to me, after reading the manpage that the MNT_FORCE may not be the flag you want, since it will only force unmounting of a busy file system if it is NTFS. You probably want to use the MNT_DETACH flag instead. Here is the manpage section on the MNT_DETACH flag:
    Code:
           MNT_DETACH (since Linux 2.4.11)
                  Perform a lazy unmount: make the mount point unavailable for new accesses, and  actually  per-
                  form the unmount when the mount point ceases to be busy.
    It sounds like it may remove the mtab entry, but you'd have to try that to be sure.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Nov 2010
    Posts
    37
    I tried MNT_DETACH as well and that produced the same behavior as far as whether /etc/mtab was updated. The bottom line is that apparently if you want to unmount a volume in code, you have to be prepared to update /etc/mstab manually if you want it to reflect reality.

    I am porting this code from FreeBSD that I had previously written. Manually updated /etc/mtab wasn't necessary under BSD--I could call umount() and nothing more was needed to be done. I've changed my Linux code to call the umount command line util via system(). Not ideal but I don't want to get into updating /etc/mtab myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •