mount Command: Mounting Disks and Image Files, Unmounting with umount, and Fixing target is busy

Test environmentRHEL familyAlmaLinux 9.8root account
Disk · mount commandsmountLocation/usr/bin/mount
Mounting partitions and image files, checked on a server

The mount command is a Linux command that attaches the file system of a disk partition or an image file to a directory, and umount is used to detach it. On an AlmaLinux 9 test server, a partition of a practice image was mounted, switched to read-only and unmounted, and the target is busy error that appeared while unmounting was solved by finding the process in use with fuser.

The output below is exactly the result of running the commands under the root account of an AlmaLinux 9 test server with a 2 GiB image created by truncate and attached to the loop device /dev/loop0. mount is version util-linux 2.37.4. Partition 1 of the image was formatted as ext4 and partition 2 as xfs. The /mnt/data, /mnt/backup and /mnt/x directories used as mount points were created first with mkdir. The connection address shown in the simulator is an example value.

1️⃣ Basic Usage of the mount Command

mount is run with the device and the mount point written in that order. Before mounting, the partitions and file system types of the practice device were checked first with lsblk.

lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINTS /dev/loop0
NAME      FSTYPE LABEL  MOUNTPOINTS
loop0                   
|-loop0p1 ext4   data   
`-loop0p2 xfs    backup 

loop0p1 was ext4 and loop0p2 was xfs, and the MOUNTPOINTS column was empty, so neither partition was mounted. Partition 1 was mounted on /mnt/data.

mount /dev/loop0p1 /mnt/data

When mounting succeeds, nothing is printed. The result was checked by passing the mount point to findmnt.

findmnt /mnt/data
TARGET    SOURCE       FSTYPE OPTIONS
/mnt/data /dev/loop0p1 ext4   rw,relatime,seclabel

The SOURCE column showed the device, the FSTYPE column the file system type, and the OPTIONS column the mount options. Since no options were given, it was mounted as rw, which allows both reading and writing. The capacity was checked with [df -h].

df -h /mnt/data
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0p1    974M   24K  907M   1% /mnt/data

The 1 GiB partition was attached to /mnt/data with a size of 974M. Because it was a newly created ext4 file system, ls showed only the lost+found directory.

ls /mnt/data
lost+found

Running mount without arguments lists every file system that is currently mounted. On this server it printed 26 lines, so grep was used to print only the /mnt/data line.

mount | wc -l
26
mount | grep /mnt/data
/dev/loop0p1 on /mnt/data type ext4 (rw,relatime,seclabel)

Each line shows the device, mount point, type and options in that order. Giving only a type to -t without a device lists only the lines mounted with that type.

mount -t ext4
/dev/vda2 on / type ext4 (rw,relatime,seclabel)
/dev/loop0p1 on /mnt/data type ext4 (rw,relatime,seclabel)

Two lines appeared: /dev/vda2 for the root (/) and /dev/loop0p1, which had just been mounted.

Adding -t when mounting specifies the file system type. Partition 2, formatted as xfs, was mounted on /mnt/backup with [-t xfs].

mount -t xfs /dev/loop0p2 /mnt/backup
findmnt /mnt/backup
TARGET      SOURCE       FSTYPE OPTIONS
/mnt/backup /dev/loop0p2 xfs    rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota

The FSTYPE column showed xfs. When -t is omitted, mount detects the file system type automatically. After unmounting the same partition and running the command again without -t, it was also mounted without errors.

umount /mnt/backup
mount /dev/loop0p2 /mnt/backup

File systems to be mounted automatically at boot are written in /etc/fstab. To add a line to fstab, see Linux disk mount for the full process.

2️⃣ mount Options: Read-Only and remount

After -o, mount options are written one after another, separated by commas. ro is the read-only option, used when opening a file system whose contents must not be changed. /mnt/data was unmounted and then mounted again with [-o ro].

umount /mnt/data
mount -o ro /dev/loop0p1 /mnt/data
findmnt -o TARGET,OPTIONS /mnt/data
TARGET    OPTIONS
/mnt/data ro,relatime,seclabel

The first value in the OPTIONS column changed from rw to ro. In this state, creating a file with touch produced an error.

touch /mnt/data/test.txt
touch: cannot touch '/mnt/data/test.txt': Read-only file system

A [Read-only file system] error appeared and the exit code was 1. If writing is needed, only the options can be changed with [-o remount,rw] without unmounting. For remount, only the mount point was given, without the device.

mount -o remount,rw /mnt/data
touch /mnt/data/test.txt
findmnt -o TARGET,OPTIONS /mnt/data
TARGET    OPTIONS
/mnt/data rw,relatime,seclabel

The same touch finished without errors, and the OPTIONS column was back to rw.

3️⃣ The umount Command and target is busy

To unmount, give the mount point to umount. /mnt/backup was unmounted.

umount /mnt/backup

When unmounting succeeds, nothing is printed, just as with mount. If a process is using that file system, it cannot be unmounted. umount was run while a sleep process whose working directory was /mnt/data had been left running in the background.

umount /mnt/data
umount: /mnt/data: target is busy.

A [target is busy] error appeared and the exit code was 32. The process in use was checked with [fuser -vm].

fuser -vm /mnt/data
                     USER        PID ACCESS COMMAND
/mnt/data:           root     kernel mount /mnt/data
                     root       6444 ..c.. sleep

The kernel line is the mount itself, and the line below it is the process in use. A sleep process with PID 6444 appeared. The same information was checked with lsof.

lsof +f -- /mnt/data
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sleep   6444 root  cwd    DIR  259,2     4096    2 /mnt/data

The FD column showed cwd, confirming that the working directory of this process was /mnt/data. After the process was terminated, umount was run again.

kill 6444
umount /mnt/data

This time it finished without errors, and [findmnt /mnt/data] printed nothing, confirming that it had been unmounted.

The option that unmounts without terminating the process is [umount -l]. The manual describes -l as an option that detaches the file system from the directory tree right away and cleans up the remaining references once they are no longer in use. After mounting again and leaving a sleep process running the same way, it was unmounted with -l.

mount /dev/loop0p1 /mnt/data
umount -l /mnt/data
lsblk /dev/loop0
NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0       7:0    0    2G  0 loop 
|-loop0p1 259:2    0    1G  0 part 
`-loop0p2 259:3    0 1023M  0 part 

[umount -l] finished without errors, and /mnt/data disappeared from the MOUNTPOINTS column of lsblk. The sleep process was still running at that point, so it was terminated with kill.

4️⃣ Image Files and bind Mounts

A disk image file can be mounted directly with [-o loop]. A 200 MiB file was created with truncate and formatted with mkfs.ext4. mkfs.ext4 is the command that creates an ext4 file system. -F was added so that it creates one even on a regular file that is not a partition, and -q so that it does not print progress.

truncate -s 200M /root/lab/fs.img
mkfs.ext4 -q -F /root/lab/fs.img
mount -o loop /root/lab/fs.img /mnt/x
findmnt /mnt/x
TARGET SOURCE     FSTYPE OPTIONS
/mnt/x /dev/loop1 ext4   rw,relatime,seclabel

The SOURCE column showed /dev/loop1, not the image file. This is because mount attached the image file to a free loop device and then mounted that device. Unmounting with umount also detached the loop device, so only /dev/loop0, which had been there from the start, remained in the losetup list.

umount /mnt/x

--bind attaches a directory, not a device, to another path once more. The /root/lab directory was attached to /mnt/x.

mount --bind /root/lab /mnt/x
findmnt /mnt/x
TARGET SOURCE               FSTYPE OPTIONS
/mnt/x /dev/vda2[/root/lab] ext4   rw,relatime,seclabel

The SOURCE column showed the device of the original file system, /dev/vda2, followed by the attached directory /root/lab in square brackets. /mnt/x was checked with ls.

ls /mnt/x
disk.img
fs.img

disk.img and fs.img from /root/lab appeared as they were.

umount /mnt/x

A path attached with --bind was also detached with umount, like any other mount.

🔲 mount Command Options

OptionCopyDescription
-t--typesMount with the given file system type. Without a device, list only the mounts of that type
-o roMount read-only
-o remount,rwSwitch a mounted file system to read-write without unmounting it
-o loopAttach an image file to a loop device and mount it
-o loop,roMount an image file read-only
--bind-BAttach a directory to another path once more
umountUnmount by giving the mount point or device name
umount -l--lazyDetach from the directory tree right away and clean up when no longer in use
OptionCopyDescription
-a--allMount all file systems listed in fstab
-r--read-onlyMount read-only (same as -o ro)
-w--rwMount read-write (default)
-L--labelSpecify the device by file system label
-U--uuidSpecify the device by file system UUID
-v--verbosePrint progress details
-f--fakeTest run without actually mounting
-T--fstabSpecify a file to use instead of /etc/fstab
-R--rbindAttach a directory, including the mounts below it, to another path
-M--moveMove a mounted directory to another path
umount -a--allUnmount all file systems
umount -f--forceForce unmount (for unreachable NFS)
umount -R--recursiveUnmount the mount point and the mounts below it in order
umount -d--detach-loopWhen a loop device was mounted, detach that loop device as well
umount -r--read-onlyIf unmounting fails, remount read-only
umount -q--quietDo not print not mounted error messages

🔲 mount Command Simulator

In the simulator below, the mount and umount commands from this article can be entered with the chips or the keyboard. Moving with [cd /mnt/data] and then running umount lets you see the [target is busy] error as well.

mount command simulatorAlmaLinux 9.8
[root@server work]#
It runs only inside your browser. Mounting and unmounting only imitate a change to the state of the virtual server on this screen.

5️⃣ Practical Examples of the mount Command

  • ↪️ A backup partition and an image file mounted read-only, and common mistakes

This section shows the results of creating a new practice image on the same test server, and the same steps can be entered in the mount command simulator.

🔲 mount Read-Only Mount Example

When copying files from a backup partition, it was mounted read-only so that its contents could not be changed by mistake. First, the xfs partition was mounted, a configuration file app.conf was created on it, and it was unmounted.

mount /dev/loop0p2 /mnt/backup
touch /mnt/backup/app.conf
umount /mnt/backup

The same partition was mounted with [-o ro], and the options were checked with findmnt.

mount -o ro /dev/loop0p2 /mnt/backup
findmnt -o TARGET,SOURCE,OPTIONS /mnt/backup
TARGET      SOURCE       OPTIONS
/mnt/backup /dev/loop0p2 ro,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota

The OPTIONS column starts with ro. In this state, the file list was checked and app.conf was copied to /root/lab with cp.

ls /mnt/backup
app.conf
cp /mnt/backup/app.conf /root/lab/
ls /root/lab
app.conf
disk.img

The copy finished without errors. Trying to delete the same file with rm produced an error.

rm /mnt/backup/app.conf
rm: cannot remove '/mnt/backup/app.conf': Read-only file system

A [Read-only file system] error appeared and the file was not deleted. After the work was done, it was unmounted with umount.

umount /mnt/backup

An image file becomes read-only when mounted with the options joined by a comma, as in [-o loop,ro]. A new 200 MiB image fs.img was created with truncate and mkfs.ext4, mounted on /mnt/x, and checked with findmnt and losetup.

mount -o loop,ro /root/lab/fs.img /mnt/x
findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS /mnt/x
TARGET SOURCE     FSTYPE OPTIONS
/mnt/x /dev/loop1 ext4   ro,relatime,seclabel
losetup -l -O NAME,RO,BACK-FILE
NAME       RO BACK-FILE
/dev/loop1  1 /root/lab/fs.img
/dev/loop0  0 /root/lab/disk.img

The OPTIONS column of findmnt was ro, and in the RO column of losetup, only /dev/loop1, to which fs.img was attached, showed 1. After unmounting with umount, losetup was run again.

umount /mnt/x
losetup -l -O NAME,RO,BACK-FILE
NAME       RO BACK-FILE
/dev/loop0  0 /root/lab/disk.img

The /dev/loop1 line disappeared and only /dev/loop0 remained.

🔲 Common Mistakes When Using mount

A mistake that is easy to make at first is giving a directory that has not been created as the mount point. It was run with /mnt/nodir, a directory that does not exist.

mount /dev/loop0p1 /mnt/nodir
mount: /mnt/nodir: mount point does not exist.

A [mount point does not exist.] error appeared. mount does not create the mount point, so the directory must be created first with mkdir.

The mistake of giving the whole disk instead of a partition was also checked. The file system is on the partition /dev/loop0p1, but /dev/loop0 was given.

mount /dev/loop0 /mnt/data
mount: /mnt/data: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error.

A [wrong fs type, bad option, bad superblock] error appeared. Giving -t a type different from the actual one also produced the same error. This is the result of specifying [-t ext4] for an xfs partition.

mount -t ext4 /dev/loop0p2 /mnt/x
mount: /mnt/x: wrong fs type, bad option, bad superblock on /dev/loop0p2, missing codepage or helper program, or other error.

When this error appears, first check the device name (NAME column) and the file system type (FSTYPE column) in lsblk. For a new disk with no partitions, create partitions with the gdisk command, then create a file system with mkfs and mount it.

Mounting an already mounted device on the same mount point again produced an [already mounted] error.

mount /dev/loop0p1 /mnt/data
mount: /mnt/data: /dev/loop0p1 already mounted on /mnt/data.

Running umount once more on a mount point that had already been unmounted produced a [not mounted] error.

umount /mnt/data
umount: /mnt/data: not mounted.

Another mistake is running umount from inside the mount point. [umount /mnt/data] was run while the current directory of the shell was /mnt/data.

pwd
/mnt/data
umount /mnt/data
umount: /mnt/data: target is busy.

Because the shell was using the mount point, a [target is busy] error appeared. You need to move outside the mount point with cd and run it again. All the mount and umount errors in this section had exit code 32.

🔲 Checking the Change in mount Output

mount and umount print nothing even when they succeed, so the result was checked by comparing the output of findmnt and df before and after running them. Before mounting, [findmnt /mnt/data] printed nothing and the exit code was 1. Passing the same path to df showed the root file system.

df --output=source,fstype,target /mnt/data
Filesystem     Type Mounted on
/dev/vda2      ext4 /

This means /mnt/data was still a regular directory on the root (/) file system. Files saved in this state are written to the root disk, not to the practice partition. After mounting partition 1, the same commands were run.

df --output=source,fstype,target /mnt/data
Filesystem     Type Mounted on
/dev/loop0p1   ext4 /mnt/data

The Filesystem column changed from /dev/vda2 to /dev/loop0p1, and the Mounted on column from / to /mnt/data. In lsblk as well, /mnt/data appeared in the MOUNTPOINTS column of the loop0p1 line.

lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINTS /dev/loop0
NAME      FSTYPE LABEL  MOUNTPOINTS
loop0                   
|-loop0p1 ext4   data   /mnt/data
`-loop0p2 xfs    backup 

In a script, the exit code can be checked instead of the output. The exit code of findmnt was 0 when mounted and 1 when not mounted.

🔢 FAQ & Recommended Content

They do not appear while something is mounted there, and they appear again after unmounting. After before.txt was created in /mnt/x and an ext4 partition was mounted there, ls showed only lost+found, and after unmounting with umount, before.txt appeared again.

-r is the same option as -o ro. After mounting with [mount -r /dev/loop0p1 /mnt/data] and checking with findmnt, the OPTIONS column showed ro,relatime,seclabel.

Giving the device name instead of the mount point also unmounts it. With /dev/loop0p1 mounted on /mnt/data, running [umount /dev/loop0p1] finished without errors, and [findmnt /mnt/data] printed nothing.

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 0   +   6   =