Using the mount Command

WARNING:

This documentation is a draft and is not meant for production use. Branch: OL10-FSADMIN

Use the mount command to attach the device containing the file system to the mount point as follows:

sudo mount [options] device mount_point

The device can be mounted by referencing its name, UUID, or label. For example, any of the following mount commands are appropriate:

sudo mount /dev/sdb1 /mnt
sudo mount UUID="ad8113d7-b279-4da8-b6e4-cfba045f66ff" /mnt
sudo mount LABEL="Projects" /mnt

The mount_point must exist as a directory within an existing file system. Typically the mount point is an empty directory, but if it isn't empty the contents of the directory aren't accessible while a device is mounted onto it.

Options

The following options are commonly used when mounting file systems. For more comprehensive coverage of available options, see the mount(8) manual page.

auto

Causes the file system to be mounted automatically by using the mount -a command.

exec

Allows the execution of any binary files in the file system.

loop

Uses a loop device (/dev/loop*) to mount a file that contains a file system image. See losetup(8) manual page. See alsoUsing the loop Device to Mount a File System Image

Note:

The default number of available loop devices is 8. You can use the kernel boot parameter max_loop=N to configure up to 255 devices. Or, add the following entry to /etc/modprobe.conf:

options loop max_loop=N 

In the previous example, N is the number of loop devices that you require (from 0 to 255), and then reboot the system.

noauto

Prevents the file system from being mounted automatically when mount -a is issued.

noexec

Prevents the execution of any binary files in the file system.

nouser

Prevents any user other than the root user from mounting or unmounting the file system.

remount

Remounts the file system if it's already mounted. You would typically combine this option with another option such as ro or rw to change the behavior of a mounted file system.

ro

Mounts a file system as read-only.

rw

Mounts a file system for reading and writing.

user

Allows any user to mount or unmount the file system.

Example 3-1 Mount a Device With Read-Only Access

Mount the /dev/sdd1 device onto the mount point /test with read-only access and grant only the root user to mount or unmount the file system:

sudo mount -o nouser,ro /dev/sdd1 /test

Example 3-2 Mount an ISO Image

Mount an ISO image file onto the mount point /media/cdrom with read-only access by using the loop device:

sudo mount -o ro,loop ./Oracle_Linux.iso /media/cdrom

Example 3-3 Remount a File System With Read-Write Access and Prevent Running of Binary Files

Remount the /test file system with both read and write access, and prohibit the running of any binary files that are in the file system:

sudo mount -o remount,rw,noexec /test

Using Bind Mounts

A bind mount can be used to remount a directory or file at another location in the file system hierarchy.

Bind mounts are commonly used to expose data from an underlying file system to chroot or container environments. Bind mounts are also useful when working with applications that might expect data to appear in alternative locations on a file system.

Each directory hierarchy acts as a mirror of the other. The same files are accessible in either location.

For a standard bind mount, any submounts aren't replicated. You can also use a recursive bind mount to replicate any submounts within the new mount location.

The mount command also includes options to prevent a bind on a mount point, to share mount actions for submounts between mirrors.

The mirrors that are created by using bind mounts don't provide data redundancy.

  • To perform a standard bind mount of a directory or file in the file system to another directory or file, use the mount -B or mount --bind command:
    sudo mount --bind source target                    

    Replace source with the path to the directory or file you want to mount, and target with the path where you want to mount it. For example:

    sudo mount --bind /mnt/data /home/user/data

    This command binds the /mnt/data directory to the /home/user/data directory.

    To mount a file over another file, you can use the same command, but use a file as the source and a file in a different location as the target. For example:

    sudo mount -B /etc/hosts /mnt/foo

    The /etc/hosts file is mounted over the /mnt/foo file. The existing file that acts as a mount point isn't accessible until you unmount the overlying file.

  • To perform a recursive bind mount, use the mount --rbind or mount -R command.
    sudo mount --rbind source target                    

    Replace source with the path to the directory or file you want to mount, and target with the path where you want to mount it. For example:

    sudo mount --R /mnt/data /home/user/data

    In this case if /mnt/data contains any submounts, the submounts are also replicated within /home/user/data.

  • To change the mount options on a bind mount, you must remount the bind mount an specify the required options.

    When you use the -B or -R option, the file system mount options remain the same as those for the original mount point. To change, the mount options, use a separate remount command, for example:

    sudo mount -o remount,ro /home/user/data
  • Control how mount and unmount actions are propagated to submounts for any mount point.

    You can mark the submounts in a mount point as being shared, private, or secondary. You can specify the following options:

    mount --make-shared mount_point

    Any mounts or unmounts under the specified mount point propagate to any mirrors that you create, and this mount hierarchy reflects mounts or unmount changes that you make to other mirrors.

    mount --make-private mount_point

    Any mounts or unmounts under the specified mount point don't propagate to other mirrors, nor does this mount hierarchy reflect mounts or unmount changes that you make to other mirrors.

    mount --make-slave mount_point

    Any mounts or unmounts under the specified mount point don't propagate to other mirrors, but this mount hierarchy does reflect mounts or unmount changes that you make to other mirrors.

    For example, to propagate mount actions within a bind mount, run:

    sudo mount --bind --make-shared /mnt/data /home/user/data

    If a new mount is performed within the /mnt/data subtree, the mount is also propagated to the same location within the /home/user/data subtree.

  • To prevent a mount from being mirrored by using the -B or -R options, mark its mount point as being unbindable:
    sudo mount --make-unbindable mount_point

Using the loop Device to Mount a File System Image

You can create a file system image within an existing file system and mount the file system image by using a loop device.

A loop device lets you access a file as a block device. For example, you can mount a file that contains a DVD ISO image on the directory mount point /ISO as follows:

sudo mount -t iso9660 -o loop /tmp/OracleLinux.iso /ISO

The mount command can detect the file system type and understands that when you're mounting a file image to a mount point, a loop device is intended. Therefore, you can also run the mount command without specifying the -t and -o options:

sudo mount /tmp/OracleLinux.iso /ISO

You can also create a file system image with an alternative file system within an existing file system. The following procedure describes how to create an empty file, format it with a file system and then mount it using a loop device.

  1. Create an empty file of the required size.
    sudo dd if=/dev/zero of=/fsfile bs=1M count=4096

    Or alternatively, run:

    sudo fallocate -l 4G /fsfile
  2. Create a file system on the image file.

    For example, to format the file with the Ext4 file system, you can run:

    sudo mkfs.ext4 -F /fsfile
  3. Mount the image file as a file system by using a loop device.
    sudo mount -o loop /fsfile /mnt

    If required, create a permanent entry for the file system in /etc/fstab:

    /fsfile          /mnt      ext4    rw,loop     0 0

Moving a File System to a New Mount Point

You can move a file system mounted at a mount point to a new mount point by using the mount --move command.

The mount --move command. can move a file system from one mount point to another without unmounting it.

Moving the file system can be useful if you need to switch mount points without interrupting processes that are running and accessing files. Moves are more commonly performed in early stage boot processes, or in containerized. environments. The move operation removes the original mount after the file system is moved to the new target. If a process has an open file based on the original mount point, the process is unaffected because the file descriptor points to the inode and not the path, and the file system continues to be available because it's not unmounted during the move operation.

  • To move a mounted file system, directory hierarchy, or file to a new mount point, use the mount --move or mount -M command.

    For example, run:

    sudo mount -M /mnt/users /home

    The source mount that you're moving must be a mount point and can't be a directory within a mount. Moving a mount under a shared mount is invalid and errors out.