Linux – How to unmount one of two devices mounted to the same mount point

blocklinuxoperating system

I accidentally left two different devices mounted on /opt:

/dev/xvdf on /opt type ext4 (rw,relatime,seclabel,data=ordered)
/dev/md0 on /opt type ext4 (rw,relatime,seclabel,stripe=256,data=ordered)

It would be a problem to umount /dev/md0, and things are using /opt. /dev/md0 should be mounted on top of /dev/xvdf. Any suggestions how I can just unmount /dev/xvdf?

Tried:

[root@redacted ~]# umount /dev/xvdf
umount: /dev/xvdf: umount failed: Invalid argument

[root@redacted ~]# mount --move /dev/xvdf /temp
mount: bad option. Note that moving a mount residing under a shared
       mount is unsupported.

Also I've had AWS support for volumes yell at me about force detaching in-use volumes, so that's not an option.

Best Answer

You cannot do it atomically. You can however do it with a sequence of mount --move commands. And you will need two other directories to use as mount points.

cp /etc/mtab /root/mtab-before
mkdir /mnt/shuffle-md0 /mnt/shuffle-xvdf
mount --move /opt /mnt/shuffle-md0
mount --move /opt /mnt/shuffle-xvdf
mount --move /mnt/shuffle-md0 /opt
umount /mnt/shuffle-xvdf
cp /etc/mtab /root/mtab-after

Notice that the /etc/mtab entry for /dev/xvdf may end up looking pretty weird in the end. So I recommend you create a copy of /etc/mtab before you start such that you can reconstruct that entry once you are done.

Anything opening paths through /opt while you are shuffling around the mount points may get unexpected results. But files and directories which were opened before you started will be unaffected by this maneuver.

Related Topic