2

Let's consider these conditions:

  • There's multiple nested mounts with a lot of folders/files in /mnt:
    sdb      8:16   0   300G  0 disk
    ├─sdb1   8:17   0   256M  0 part /mnt/1/1
    ├─sdb2   8:18   0 199.7G  0 part /mnt/2
    └─sdb3   8:19   0   100G  0 part /mnt/3/1/2
    
  • Let's pretend I don't know which device(s) (such as /dev/sdb) are mounted under /mnt (as I'd like to use this in a unattended script).
  • The path to mounted directories aren't known either.
  • not mounted error shouldn't stop the process

Here's what I've tried with illustration of the result:

$ umount --all-targets --recursive /mnt; lsblk /dev/sdb
umount: /mnt: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part /mnt/1/1
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*/**; lsblk /dev/sdb
umount: /mnt/2/boot: not mounted
umount: /mnt/2/ostree: not mounted
umount: /mnt/3/1: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*?; lsblk /dev/sdb
umount: /mnt/1: not mounted
umount: /mnt/3: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part /mnt/1/1
├─sdb2   8:18   0 199.7G  0 part
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*/**?; lsblk /dev/sdb
umount: /mnt/2/boot: not mounted
umount: /mnt/2/ostree: not mounted
umount: /mnt/3/1: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

Is there any good way of doing this without installing any extra packages?

3 Answers 3

1

Please let me know if there's any better way of doing this!

I'm posting my findings to help others:

  1. Out of curiosity I found this quite useful as it doesn't throw not mounted error but this only works if device's name is known and if each partition/disk-part has none- or 1 mountpoint (for reference, adding --all-targets can result in not mounted error and suppressing it with || true is a bad practice):

    $ umount /dev/sdb?*
    

    Instead, to handle multiple mountpoints per partition/disk-part I came up with this to automatically unmount all mountpoints for a disk:

    $ lsblk --noheadings --output MOUNTPOINTS /dev/sdb | xargs -r umount
    
  2. However, to workaround issues in my original question, I came up with this:

    $ umount $(df -h --output=target | grep /mnt)
    

    Turns out it's only useful if something is mounted, here's a improved version which gives more accurate matches and only unmounts if mounted:

    $ df -h --output=target | grep -w /mnt | xargs -r umount
    

    This can be tweaked even further.. The previous version fails to list all directories if a disks is mounted in multiple locations. Fixed it with a different approach:

    $ lsblk --output MOUNTPOINTS | grep -w /mnt | xargs -r umount
    
3
  • 1
    +1+1; When running as a regular user for example in Ubuntu you need elevated permissions with sudo like so: lsblk --output MOUNTPOINTS | grep -w '/mnt' | xargs -r sudo umount. Commented Sep 28, 2023 at 17:28
  • Have you tried findmnt to find the relevant mounted filesystems and the info about them? Commented Sep 29, 2023 at 6:02
  • @SottoVoce Thanks for pointing it out! It might be a good alternative to lsblk for someone. I just tried it really quickly (and not thoroughly at all) and it seems like I only get hits if I specify exact partitions. I didn't want to spend too much time playing around with it. Here's my extremely quick attempt at trying to get results for wildcards, directories and disk devices without luck/any results. Maybe I've missed to specify a option? $ findmnt --noheadings --output TARGET /mnt $ findmnt --noheadings --output TARGET /dev/sdb $ findmnt --noheadings --output TARGET /dev/sdb* Commented Sep 29, 2023 at 9:03
0

I'd use:

cd /mnt
find -type d -exec mountpoint {} \;|grep -v not|cut -d" " -f1|tac|xargs umount

This will unmount all mounted directories under /mnt. I assume this is what you want to do. This does not need to know the devices or paths to the mountpoints (other than mountpoints below /mnt.) It only operates on mountpoints that are mounted.

This way is looking for mountpoints, rather than trying to guess devices and checking if they are mounted under /mnt. It will handle loop mounts and network mounts as well as mounts using disk devices.

find -type d searches for directories for efficiency (although maybe files could be bind mounted as well as directories, not sure). tac will reverse the order, so we unmount mountpoints further down the hierarchy first.

1
  • Your command can be simplified: find -depth -type d -exec mountpoint -q {} \; -exec umount {} \; Not that find-ing the whole tree can be very time-consuming, especially on network drives. Commented Oct 19, 2023 at 22:43
0

Got this from a storage vendor, it works in bash

MOUNTDIR=/mnt

# Sort MOUNTDIR to unmount nested filesystems first
if [[ "$OS" == "AIX" ]]
then
  KEYS=$(
  for KEY in ${!MOUNTDIR[@]}
  do
    echo "${MOUNTDIR[$KEY]}:::$KEY"
  done | sort | awk -F::: '{print $2}'
  )
else
  KEYS=$(
  for KEY in ${!MOUNTDIR[@]}
  do
    echo "${MOUNTDIR[$KEY]}:::$KEY"
  done | sort -r | awk -F::: '{print $2}'
  )
fi

for OLDLV in $KEYS
do
  if [[ "$OS" == "AIX" ]]
  then
    echo "/usr/sbin/fuser -k ${MOUNTDIR[$OLDLV]}"
    if [[ -v COMMIT ]]; then /usr/sbin/fuser -k ${MOUNTDIR[$OLDLV]}; fi
    echo "umount ${MOUNTDIR[$OLDLV]}"
    if [[ -v COMMIT ]]; then umount ${MOUNTDIR[$OLDLV]}; fi
  else
    echo "fuser -kmMs ${MOUNTDIR[$OLDLV]}"
    if [[ -v COMMIT ]]; then fuser -kmMs ${MOUNTDIR[$OLDLV]}; fi
    echo "umount ${MOUNTDIR[$OLDLV]}"
    if [[ -v COMMIT ]]; then umount ${MOUNTDIR[$OLDLV]}; fi
  fi
done
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Mar 30, 2024 at 8:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.