File Systems: Part 3 -- Mounting
Contents
Mounting
The below commands are done on a stand alone Debian Linux system and not on Hills. We do not have permission to run these types of commands on hills; nor do we have access to the actual physical system to place a flash drive on it. We know that the mounting associates a folder with a file system that is a device in the "/dev" folder. The associated folder is usually empty and if it's not then it's contents are overlaid with the device contents. We use the 2 commands "mount" and "umount" to connect and disconnect the device. What does it mean that the non-empty folder's contents are overlaid. It means they are not lost. It's just that after mounting the user will see the contents of the device rather than the folder. Let's try this with an example:ajay@ajkumar08-PC:~$ df Filesystem 1K-blocks Used Available Use% Mounted on udev 946364 0 946364 0% /dev tmpfs 192844 1416 191428 1% /run /dev/sda1 151741000 5554408 138405780 4% / tmpfs 964212 0 964212 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192840 940 191900 1% /run/user/1000 /dev/sdc1 15519752 24 14721932 1% /home/ajay/flash Unount the flash drive. root@ajkumar08-PC:/home/ajay# umount /dev/sdc1 root@ajkumar08-PC:/home/ajay# root@ajkumar08-PC:/home/ajay# cd flash root@ajkumar08-PC:/home/ajay/flash# ls root@ajkumar08-PC:/home/ajay/flash# mkdir mine root@ajkumar08-PC:/home/ajay/flash# cd mine root@ajkumar08-PC:/home/ajay/flash/mine# date > test.txt root@ajkumar08-PC:/home/ajay/flash/mine# ls test.txt root@ajkumar08-PC:/home/ajay/flash/mine# cd .. root@ajkumar08-PC:/home/ajay/flash# ls mine The "home/ajay/flash" is not empty . It has a folder called "mine" with a file "test.txt" on it. Mount the flash drive. root@ajkumar08-PC:/home/ajay/flash# mount /dev/sdc1 /home/ajay/flash root@ajkumar08-PC:/home/ajay# cd flash root@ajkumar08-PC:/home/ajay/flash# ls lost+found Now the contents of "/home/ajay/flash" show the contents of the flash drive. Create a folder on it along with a file. root@ajkumar08-PC:/home/ajay/flash# mkdir folder_on_flash root@ajkumar08-PC:/home/ajay/flash# cd folder_on_flash/ root@ajkumar08-PC:/home/ajay/flash/folder_on_flash# date > test1.txt root@ajkumar08-PC:/home/ajay/flash/folder_on_flash# cd .. root@ajkumar08-PC:/home/ajay/flash# ls folder_on_flash lost+found Now we unmount it and see that the original folder contents are still there. root@ajkumar08-PC:/home/ajay# umount /dev/sdc1 root@ajkumar08-PC:/home/ajay# cd flash root@ajkumar08-PC:/home/ajay/flash# ls mine root@ajkumar08-PC:/home/ajay/flash#The "mount" command by itself will show the mounted devices. This information is also there in the file "/etc/mtab" . But how can this be ? What if we mount something new device ? We can't have a static file with just the information after a boot. If do a ls -l on this file we get the output:
[amittal@hills etc]$ ls -l mtab lrwxrwxrwx. 1 root root 19 May 4 2021 mtab -> ../proc/self/mountsWe see from above that it is a shortcut to the folder "proc" which we know is the virtual file system. The format of the mount command is:
mount [ -t type ] [ -o mountoptions ] { device | -L label | -U uuid | dir } [ dir ]Let's mount the flash drive using it's label instead of the device name.
root@ajkumar08-PC:/etc# mount -L FLASH_1 /home/ajay/flash root@ajkumar08-PC:/etc# df Filesystem 1K-blocks Used Available Use% Mounted on udev 946364 0 946364 0% /dev tmpfs 192844 1424 191420 1% /run /dev/sda1 151741000 5554460 138405728 4% / tmpfs 964212 0 964212 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192840 940 191900 1% /run/user/1000 /dev/sdc1 15519752 32 14721924 1% /home/ajay/flash
fstab
To help us with the mounting process there is a file called "/etc/fstab" . This has entries that contains information about the mounting points so we don't have to give all the information in the mount command. Each row consists of 6 columns:- Device: usually the given name or UUID of the mounted device (sda1/sda2/etc). For label use "LABEL=" and for uuid us "UUID=" .
- Mount Point: designates the directory where the device is/will be mounted.
- File System Type: Shows the type of filesystem in use.
- Options: lists any active mount options. If using multiple options they must be separated by commas. This can be "default" to indicate that default options are to be used.
- Backup Operation: (the first digit) this is a binary system where 1 = dump utility backup of a partition. 0 = no backup. This is an outdated backup method and should NOT be used.
- File System Check Order: (second digit) Here we can see three possible outcomes. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. The root filesystem should be set to 1 and other partitions set to 2.
LABEL=FLASH_1 /home/ajay/flash ext4 defaults 0 0Now we mount the flash drive.
root@ajkumar08-PC:/etc# mount -L "FLASH_1" Check whether the flash drive got mounted. root@ajkumar08-PC:/etc# df Filesystem 1K-blocks Used Available Use% Mounted on udev 946176 0 946176 0% /dev tmpfs 192808 1420 191388 1% /run /dev/sda1 151741000 4249012 139711176 3% / tmpfs 964028 0 964028 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192804 136 192668 1% /run/user/1000 /dev/sdb1 30013380 24 28463432 1% /home/ajay/FLASHNotice we only specified the label. The mount point was obtained by "mount" from the fstab file. Examples of mount commands if the missing information exists in the fstab file:
mount /dev/sdb1 --By device name mount -L /FLASG_1 --By label mount -U 4b2f66e2-6034-4011-b2b7-4c87653014b3 --By UID mount /usr --By the mount pointRecall the mount command has the below options.
mount options
mount [ -t type ] [ -o mountoptions ] { device | -L label | -U uuid | dir } [ dir ]
In addition to the above we can have: -a automount all filesystems indicated as automount (auto) in the fstab. This can be combined with -t type to automount only filesystems of type type We have seen how to use the labels to mount. The UUID is used in a similar manner. -L label mount the filesystem with this label. (The label can be used instead of the device file.) -U UUID mount the filesystem with this uuid.We can give the command "mount -a" to automatically mount all the entries in the fstab file. An entry can have the word "noauto" in the options column if it is desired that the entry is not automatically mounted. The "-o" lists options that are comma separated and sometimes specific to the file system. The "fstab" also has a column for options.
defaults this is the defaults set. It contains at least: rw suid dev exec auto nouser async rw --read write suid -- Honor set-user-ID and set-group-ID bits or file capabilities when executing programs from this filesystem. dev -- Interpret character or block special devices on the filesystem. exec -- Permit execution of binaries and other executable files. auto -- Filesystem should be mounted automatically. nouser -- Forbid an ordinary user to mount the filesystem. async -- All I/O to the filesystem should be done asynchronously. Some of the other options are: ro / rw mount the filesystem read-only or read-write (rw is the default) remount the filesystem is already mounted. Change its mount parameters as indicated here and leave it mounted. You cannot change the mount point with remount, only the options. remount is used most frequently to toggle between the ro and rw option of a filesystem. auto / noauto (when used in the fstab) indicates whether filesystem should be mounted automatically when the system is started (in response to mount -a). exec / noexec whether to allow the execution of programs on this filesystem. It has been suggested that this option be turned on for public temporary filesystems (/tmp). This is discussed in a later section. suid / nosuid whether to allow the set-user-id bit to have effect on programs executed from this filesystem. Again, this is a good idea to turn on for public temporary filesystems (/tmp). This is discussed in a later section. dev / nodev whether device files should be honored on this filesystem. The nodev option should be on for all filesystems except the root (and perhaps /boot) filesystem. acl / noacl (ext filesystems only) whether access-control-lists should be effective on this filesystem. user whether a non-privileged user can mount this filesystem. Once mounted, only that user can umount it. users whether a non-privileged user can mount and umount this filesystem. Any user can mount it, and any user can unmount it. loop mount using the loop device. sb=n (ext filesystems only) ignore the default superblock, instead using the superblock at block n. Warning: The block number here is in 1k blocks, so, depending on your filesystem's blocksize, you may have to translate from the block numbers of the duplicate superblocks output by mkfs . (To find out where the spare superblocks are on your filesystem, as well as the blocksize the filesystem was created with, use mke2fs -n.) barrier/nobarrier This disables / enables the use of write barriers in the journaling code. barrier=none disables, barrier=flush enables (default). bind/rbind can be used to remount all or part of a mounted filesystem in a second location.The "man mount" page contains documentation about all the options. These same options are used in the "fstab" file. The "mkfs" utility for formatting will also have it's own set of options.
Mounting an ISO file
An ISO file is an exact copy of an entire optical disk such as a CD, DVD, or Blu-ray archived into a single file. It can be mounted with the following command:
# mount -t iso9660 -o ro,loop myiso.iso /mnt/iso
In the above the file is named "myiso.iso" and we give the options "ro" and "loop".
A loop device is a pseudo-device that makes a file accessible as a block device.
Loop devices are often used for CD ISO images and floppy disc images.
Mounting a file containing a filesystem via such a loop mount makes the files within that filesystem accessible.
Unmounting
We may encounter a message of the form "target is busy" when trying to unmount a filesystem. This can happen if a file in that system is being used by a process. Example:root@ajkumar08-PC:/etc# umount /dev umount: /dev: target is busy. root@ajkumar08-PC:/etc# lsof /dev COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root 0u CHR 1,3 0t0 4 /dev/null systemd 1 root 1u CHR 1,3 0t0 4 /dev/null systemd 1 root 2u CHR 1,3 0t0 4 /dev/null systemd 1 root 3w CHR 1,11 0t0 10 /dev/kmsg systemd 1 root 27r CHR 10,235 0t0 334 /dev/autofsThe command "lsof" will list all the processes using the file system. It's possible to kill those processes and then unmount the system. However we can see from the output above that the root process is using this filesystem and we cannot kill the root process as that will bring down the system. The "lsof" command will look in the sub folders also for any file that is being used.
The FD column has a number denoting the file descriptor and the letters "r,w" or "u". The letters indicate how the file was opened. r for read access w for write access u for read and write access We can also have the following options for this field: cwd current working directory; Lnn library references (AIX); err FD information error (see NAME column); jld jail directory (FreeBSD); ltx shared library text (code and data); Mxx hex memory-mapped type number xx. m86 DOS Merge mapped file; mem memory-mapped file; mmap memory-mapped device; pd parent directory; rtd root directory; tr kernel trace file (OpenBSD); txt program text (code and data); v86 VP/ix mapped file;Another utility that we can use is "fuser".
root@ajkumar08-PC:/etc# fuser /dev /dev: 21rc root@ajkumar08-PC:/etc# fuser /dev/null /dev/null: 1 222 254 261 407 409 410 411 412 415 416 417 419 421 422 438 478 483 490 578 667 796 965 1157 1168 1169 1183 1184 1186 1191 1195 1200 1203 1217 1225 1230 12 root@ajkumar08-PC:/etc#The utility needs the full path ( it will not do a recursive search ). For the first command the "/dev" does not yield any results. However with "/dev/null" yields the process id's. We can also use the "-v" option for verbose and "-m" option tp show the users.
root@ajkumar08-PC:/etc# fuser -v -m /dev/ USER PID ACCESS COMMAND /dev/null: root kernel swap /dev/sda5 root kernel mount /dev root 1 F.... systemd ...The access letter "F" indicates that a file is open. The letter "c" indicates a directory is open. The values can be:
- c current directory.
- e executable being run.
- f open file.
- F open file for writing.
- r root directory.
- m mmap'ed file or shared library.
- F open file for writing.
File: 1.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int num; FILE *fptr; // use appropriate location if you are using MacOS or Linux fptr = fopen("1.txt","w"); if(fptr == NULL) { printf("Error!"); exit(1); } sleep( 300 ) ; fclose( fptr ) ; }
This program opens a file "1.txt" for writing and then waits 300 seconds and then closes the file. It basically has a lock on the file for 5 minutes. We place this in the "temp" folder on the hills server home folder. In my case that is "/users/amittal/temp" but it could be any folder in your home director.
[amittal@hills temp]$ gcc 1.c [amittal@hills temp]$ cd .. [amittal@hills ~]$ temp/a.out & [1] 3614561 [amittal@hills ~]$ fuser -m /users/amittal/temp /users/amittal/temp: 3614455c 3614561ce [amittal@hills ~]$ ps -aef | grep "3614455" amittal 3614455 3614454 0 08:24 pts/0 00:00:00 -bash amittal 3614561 3614455 0 08:26 pts/0 00:00:00 temp/a.out amittal 3614604 3614455 0 08:27 pts/0 00:00:00 ps -aef amittal 3614605 3614455 0 08:27 pts/0 00:00:00 grep --color=auto 3614455 [amittal@hills ~]$ fuser -mki /users/amittal/temp /users/amittal/temp: 3614455c 3614561ce Kill process 3614455 ? (y/N) N Kill process 3614561 ? (y/N) y [1]+ Killed temp/a.outWe compile this program with the command "gcc 1.c" and that produces the executable "a.out" . We run this program in the background and a process id of 3614561 is assigned to it. We then run "fuser" with the "-m" specifying the directory ( we could have also used the file system itself "/users/amittal" ) . This shows 2 processes that are using this file system. One is the "a.out" and the other is the bash shell that we opened in Putty. We then run "fuser" with the "-k" option and kill the "a.out" program that was sleeping.
Removable Devices
A daemon( background process) usually handles the insertion, removal of removable devices like the flash drive to mount the device. This can be something like "/mnt" or "/media" folder. If for some reason that doesn't happen then we have to use the mount command manually.Finding filesystems
findfs
This will look through all filesystems whether mounted or unmounted.
root@ajkumar08-PC:/mnt# /sbin/findfs LABEL=FLASH_1 /dev/sdb1
blkid
This will also search through all filesystems whether mounted or unmounted.
root@ajkumar08-PC:/mnt# /sbin/blkid --label FLASH_1 /dev/sdb1
findmnt
This will through the mounted file systems by default but can also
search .
root@ajkumar08-PC:/mnt# df Filesystem 1K-blocks Used Available Use% Mounted on udev 946176 0 946176 0% /dev tmpfs 192808 1440 191368 1% /run /dev/sda1 151741000 4501028 139459160 4% / tmpfs 964028 0 964028 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192804 132 192672 1% /run/user/1000 /dev/sdb1 30013380 48 28463408 1% /home/ajay/FLASH root@ajkumar08-PC:/mnt# umount /dev/sdb1 root@ajkumar08-PC:/mnt# man findmnt root@ajkumar08-PC:/mnt# findmnt /home/ajay/FLASH root@ajkumar08-PC:/mnt# An entry still exists in the "fstab" file and we can search for that. root@ajkumar08-PC:/mnt# findmnt --fstab /home/ajay/FLASH TARGET SOURCE FSTYPE OPTIONS /home/ajay/FLASH LABEL=FLASH_1 ext4 defaults The "-t" option can be used to look for filesystems of a particular type. root@ajkumar08-PC:/mnt# findmnt -t ext4 TARGET SOURCE FSTYPE OPTIONS / /dev/sda1 ext4 rw,relatime,errors=remount-ro +-/home/ajay/FLASH /dev/sdb1 ext4 rw,relatime TO DO Use the findmnt to find all the filesystem on the hills server with type = "xfs" .