🌐 🇵🇱 Polski · 🇬🇧 EN
Once we have created partitions and assigned them appropriate file systems, we can start using them. However, before we begin using a new file system, it must be attached to the existing directory hierarchy, which is done using the "mount" command. We can mount a file system to any directory, which then becomes its mount point. If we mount a file system to a directory that is not empty, everything contained within it will become inaccessible. It is best to create a new directory for the purpose of mounting new resources. Regarding file system mounting, we have two commands:
- mount - Mount a file system
- umount - Unmount a file system
In previous tasks related to this course, we have already created several partitions, including a swap partition. Let's mount one of the file systems to a directory.
Step 1 - The starting point is the /opt directory, where we will create mount points.
# cd /opt
# mkdir company_data
# mkdir backup
After creating the directories, they will become mount points for the partitions /dev/hdb1 and /dev/hdc1.
Syntax of the mount command: # mount [options] device mount_point
Mount command options:
- -r - Mount in read-only mode
- -w - Mount in read/write mode (default setting)
- -L LABEL - Mount using a label
- -v - Mount with verbose output
Step 2 - Mounting file systems to directories:
# mount /dev/hdb1 /opt/company_data
# mount /dev/hdc1 /opt/backup
During mounting, there are no switches required to specify the file system type, as it is detected automatically. By default, the file system is mounted in read/write mode, which is recommended for most file system types. There are also other options such as:
- rw - mount in read/write mode
- suid - Enable access for specific programs marked with uid and gid
- dev - Interpret character or block special devices on the file system
- exec - Allow execution of binaries (scripts)
- auto - Mount the file system when the -a option is defined
- nouser - Prevent users from mounting and unmounting file systems
- async - Perform all I/O to the file system asynchronously
After mounting a file system, it is used just like a regular directory. Removing and unmounting a file system is slightly more complex because users might be actively using it. You can use the "fuser" and "lsof" commands to check for files opened by specific users, and if no files are in use at the location to be unmounted, we can attempt to unmount it.
Syntax of the unmount command: # umount
Options:
- - f - force unmount
- - v - Verbose mode
Step 3 - Unmounting a file system.
# umount /opt/backup
umount: /opt/backup: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
As you can see, unmounting is not possible because the device is busy. To check who is causing the file system to be busy, we will use the "fuser" command.
Syntax of the fuser command: # fuser [options] mount_point | file_system
Options:
- -c - Check for mount points
- -k - Kill processes accessing the file system
- -m - Show all processes using the given file system
- -u - Show user IDs
- -v - Verbose mode
Step 4 - Checking who is using the given file system
# fuser -cu /dev/hdc1
/opt/backup: 2337c(root)
To get more information about file system usage, we can use the "lsof" command.
Step 5 - See which files are open on the given file system:
# lsof /dev/hdc1
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 2337 root cwd DIR 22,1 4096 2 /opt/backup
lsof 4815 root cwd DIR 22,1 4096 2 /opt/backup
lsof 4816 root cwd DIR 22,1 4096 2 /opt/backup
This will allow you to identify the users working on specific files and contact them so they can save their work and finish their tasks before you proceed with file system operations.
Step 6 - Killing open connections
# fuser -ck /opt/backup
Step 7 - Safely unmounting the file system:
# umount /opt/backup
WARNING
If we execute a command that kills all open files being used by users, any data entered before the last save will be lost. It is good practice to notify users about maintenance on the file system so they can safely save their data.
We have covered mounting and unmounting file systems and now know how to perform these operations. However, there is something else to learn. If the environment is restarted or shut down and turned back on, all the file systems we mounted will no longer be visible in the system. This is because the effects of these commands are not permanent and do not persist after an environment restart. I will explain how to handle this shortly. There are two configuration files in the system responsible for what is and is not mounted at system startup.
/etc/mtab - Contains a list of all currently used and mounted file systems
/etc/fstab - A list of file systems to be mounted at system startup
To start, we will look at the /etc/mtab file. Every time we mount or unmount a file system, this file is overwritten with current information about mounted file systems.
# cat /etc/mtab
/dev/mapper/vg_rhel01-lv_root / ext4 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs rw,rootcontext=”system_u:object_r:tmpfs_t:s0”
0 0
/dev/hda1 /boot ext4 rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0
/dev/hdb1 /opt/company_data ext4 rw 0 0
/dev/hdc1 /opt/backup ext3 rw 0 0
We can filter its content to search for the mounted "backup" resource.
# cat /etc/mtab | grep backup
/dev/hdc1 /opt/backup ext3 rw 0 0
Since it is not very convenient to search the file from scratch every time to find mounted file systems, it is better to run the "mount" command without any switches, which will show us the currently existing file systems.
# mount
/dev/mapper/vg_rhel01-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs
(rw,rootcontext=”system_u:object_r:tmpfs_t:s0”)
/dev/hda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdb1 on /opt/company_data type ext4 (rw)
/dev/hdc1 on /opt/backup type ext3 (rw)
Now we will deal with the second file, /etc/fstab, to understand its structure.
Structure of the /etc/fstab file
<device> <mount point> <file system type> <mount options>
<write data during shutdown> <check sequence>
# cat /etc/fstab
/dev/mapper/vg_rhel01-lv_root / ext4 defaults 1 1
UUID=5485df3d-7b48-42bf-b3dd-3dd84c6560fb /boot ext4 defaults 1 2
/dev/mapper/vg_rhel01-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
The first three fields do not require comment as we have been working with them for some time. However, in the last three, additional options are visible. Below is the list we already know:
- rw - mount in read/write mode
- suid - Enable access for specific programs marked with uid and gid
- dev - Interpret character or block special devices on the file system
- exec - Allow execution of binaries (scripts)
- auto - Mount the file system when the -a option is defined
- nouser - Prevent users from mounting and unmounting file systems
- async - Perform all I/O to the file system asynchronously
Modifying the /etc/fstab file
Open the file for editing: # vi /etc/fstab and add an additional line:
/dev/hdc1 /opt/backup ext3 defaults 0 0
Since we are editing the file responsible for mounting file systems at startup, we should also not forget to add the SWAP partition.
Add another entry: /dev/hdc2 swap swap defaults 0 0
After making the modifications, save the file. There is also no need to perform a restart to test the changes. It is enough to use the "mount -a" command to remount all file systems configured in the /etc/fstab file:
# mount -a
Now all necessary file systems are mounted and ready for use.
The mount command is strongly associated with hard drives existing in the system, but it also applies to other file systems, e.g., removable media like CD-ROMs:
# mount -o loop /dev/cd-rom /media
Comments