Fix unmount error when unmounting below a bind mount point (!89)

Bind mounts duplicate part of the file system hierarchy to an additional
mount point [1].  When mounting and unmounting a second file system
below a duplicating bind mount Linux automatically presents this lower
file system as being mounted multiple times.  GParted displays these
multiple mount points.  However using GParted to unmount this lower file
system reports an error that the second mount point is no longer
mounted, because all were unmounted by the first unmount command.

Setup:

1.  Mount an upper file system

    # mkdir /mnt/1
    # mount /dev/sdb1 /mnt/1
    # fgrep sdb /proc/mounts
    /dev/sdb1 /mnt/1 ext4 rw,seclabel,relatime,data=ordered 0 0

2.  Bind mount it to a second directory

    # mkdir /mnt/b1
    # mount --bind /mnt/1 /mnt/b1
    # fgrep sdb /proc/mounts
    /dev/sdb1 /mnt/1 ext4 rw,seclabel,relatime,data=ordered 0 0
    /dev/sdb1 /mnt/b1 ext4 rw,seclabel,relatime,data=ordered 0 0

3.  Mount a file system below the first

    # mkdir /mnt/1/lower
    # mount /dev/sdb2 /mnt/1/lower
    # fgrep sdb /proc/mounts
    /dev/sdb1 /mnt/1 ext4 rw,seclabel,relatime,data=ordered 0 0
    /dev/sdb1 /mnt/b1 ext4 rw,seclabel,relatime,data=ordered 0 0
    /dev/sdb2 /mnt/1/lower xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
    /dev/sdb2 /mnt/b1/lower xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0

    Two mount records for sdb2 were added to /proc/mounts from one mount
    command.

Then use GParted to unmount sdb2.  It reports this error dialog:
    +-------------------------------------+
    |                                     |
    | Could not unmount /dev/sdb2         |
    |                                     |
    | # umount -v '/mnt/b1/lower'         |
    | umount: /mnt/b1/lower: not mounted. |
    +-------------------------------------+
    |            [  OK  ]                 |
    +-------------------------------------+

Fix by checking that the file system is still mounted before each
unmount attempt.

[1] mount (8), Description, Bind mount operation
    https://man7.org/linux/man-pages/man8/mount.8.html#DESCRIPTION

Closes !89 - Fix unmount error when unmounting below a bind mount point
This commit is contained in:
Movie Ma 2021-08-20 12:33:12 +00:00 committed by Mike Fleetwood
parent c0532c30e4
commit 6f811cfaca
1 changed files with 8 additions and 2 deletions

View File

@ -2847,13 +2847,19 @@ bool Win_GParted::unmount_partition( const Partition & partition, Glib::ustring
}
else
{
Glib::ustring checkmount = "grep -w " + Glib::shell_quote(fs_mountpoints[i]) + " /proc/mounts";
Glib::ustring cmd = "umount -v " + Glib::shell_quote( fs_mountpoints[i] );
Glib::ustring dummy;
Glib::ustring umount_error;
// Check mount point is still mounted
if (! Utils::execute_command(checkmount, dummy, umount_error))
{
if (Utils::execute_command(cmd, dummy, umount_error))
umount_errors.push_back("# " + cmd + "\n" + umount_error);
}
}
}
if ( umount_errors.size() > 0 )
{