Mount and Friends
Introduction
For accessing files/data from a file system, it must be mounted using mount
command. When the system boots up mount
command automatically mounts file systems specified in /etc/fstab
.
Mounting Block Device
mount
command is flexible, but the most common usage is like below:
mount [-t type] [device-name] [mount-point]
If -t is omitted mount
tried to automatically determines the file system type. If either device-name or
mount-point is omitted mount
will determines other information from /etc/fstab
or prints error if not found.
All modern Linux distributor mounts USB flash devices automatically. But suppose an USB flash device is inserted and
detected as /dev/sdb but wasn’t able to mount automatically. Now we can list all block devices using lsblk
command.
$ lsblk --fs
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 7eb875e6-d73f-44e9-b9ec-074972cbb23b /
├─sda2 swap bd8597fb-8d2b-477f-a78f-0b50a8add1c8 [SWAP]
sdb
└─sdb1 vfat John CAFC-CDC5
lsblk
lists all the block devices detected in this machine. And from above information we can see this computer’s
primary HDD is sda with two partitions (sda1 and sda2). sda1 partition is mounted as root (/) of this
machine and sda2 partition is used as swap memory.
sdb is our USB flash drive with label ‘John’. It only has one partition sdb1 with file system type of vfat. Now for mounting sdb1 on ‘/media/John/’ folder we have to write the following command:
$ sudo mkdir /media/John
$ sudo mount -t vfat /dev/sdb1 /media/John/
Now we can access data from the flash drive using /media/John directory.
We can also omit -t <type>, in that case mount
will automatically determine the file system.
$ sudo mount /dev/sdb1 /media/John/
Unmounting
umount
command is used for unmounting a mounted device. Device can be unmounted either using device name or mount
point. Now for unmounting the previously mounted USB flash device we can write any of following commands:
$ sudo umount /dev/sdb1
# Or using mount point
$ sudo umount /media/John/
Mounting File
We can also access the data of an iso or img file using mount
command. Let consider we want to install Debian
in our system and we have downloaded Debian live CD debian-9.6.0-amd64-xfce-CD-1.iso from internet. Now we want to
check what is inside this iso. We can easily mount the iso using following command:
$ mkdir ~/debian
$ sudo mount debian-9.6.0-amd64-xfce-CD-1.iso ~/debian
In this case we mounted on debian folder in home instead of predefined mount folder like /mnt or /media.
Now if we do a lsblk
:
$ lsblk --fs
NAME FSTYPE LABEL UUID MOUNTPOINT
loop0 iso9660 Debian 9.6.0 amd64 1 2018-11-10-11-37-56-00 /home/john/debian
sda
├─sda1 ext4 7eb875e6-d73f-44e9-b9ec-074972cbb23b /
├─sda2 swap bd8597fb-8d2b-477f-a78f-0b50a8add1c8 [SWAP]
Now we can see there is an entry with label Debian 9.6.0 amd64, which is mounted in /home/john/debian. File system type is iso9660.
This iso is named as loop0. Loop device is used when we want to access a file as block device. mount
automatically assign first available loop device while mounting file. Actually previous command can be written like
following:
$ sudo mount -o loop debian-9.6.0-amd64-xfce-CD-1.iso ~/debian
We have specified loop option using -o. This will use the first available loop device. We can also specify specific loop device like below:
$ sudo mount -o loop=/dev/loop3 debian-9.6.0-amd64-xfce-CD-1.iso ~/debian
We can also assign and mount loop device separately. Loop device is assigned using losetup
command:
$ sudo losetup /dev/loop0 debian-9.6.0-amd64-xfce-CD-1.iso
$ sudo mount /dev/loop0 ~/debian
List Mounted Devices
We can see the already mounted device using various ways. mount
command without any arguments prints currently mounted
devices:
$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=8092868k,nr_inodes=2023217,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=1624848k,mode=755)
# ... (Showing first 5 lines)
This actually prints the content of /proc/mounts
. We can also filter the list by type using -t flag.
Previously mount used to keep mounted device list in /etc/mtab
file but now it just a symlink for /proc/mounts
.
We can also use findmnt
command for checking currently mounted file systems in computer. Following command shows a
tree view of mounted devices:
$ findmnt
TARGET SOURCE FSTYPE OPTIONS
/ /dev/sda1 ext4 rw,relatime,errors=remount-ro,data=ordered
├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/cgroup tmpfs tmpfs ro,nosuid,nodev,noexec,mode=755
# ... (Showing first 5 lines)
We can query details of a specific mount with findmnt
command using either device name or mount point:
$ findmnt /dev/loop0
TARGET SOURCE FSTYPE OPTIONS
/home/john/debian /dev/loop0 iso9660 ro,relatime,nojoliet,check=s,map=n,blocksize=2048
List Available File System Types
For mounting a file system, our kernel must know how to work with that file system. To list all known file systems we can run following command:
$ cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev ramfs
nodev bdev
nodev proc
# ... (Showing first 5 lines)
But this list doesn’t shows all the available file systems. We can directly list the file system kernel modules.
$ ls -l /lib/modules/$(uname -r)/kernel/fs/*/*ko
-rw-r--r-- 1 root root 93742 Apr 24 2018 /lib/modules/4.15.0-20-generic/kernel/fs/9p/9p.ko
-rw-r--r-- 1 root root 36534 Apr 24 2018 /lib/modules/4.15.0-20-generic/kernel/fs/adfs/adfs.ko
-rw-r--r-- 1 root root 122094 Apr 24 2018 /lib/modules/4.15.0-20-generic/kernel/fs/affs/affs.ko
-rw-r--r-- 1 root root 275398 Apr 24 2018 /lib/modules/4.15.0-20-generic/kernel/fs/afs/kafs.ko
-rw-r--r-- 1 root root 419038 Apr 24 2018 /lib/modules/4.15.0-20-generic/kernel/fs/aufs/aufs.ko
# ... (Shows first 5 lines)
blkid
is a tool for querying details information about block device. It has -k flags which also shows all
available file system types.
$ blkid -k
linux_raid_member
ddf_raid_member
isw_raid_member
lsi_mega_raid_member
via_raid_member
# ... (Shows first 5 lines)
Mount Options
We can specify comma separated options using -o flag. Following command will remount /dev/sdb1 device with read only mode.
$ sudo mount /dev/sdb1 /media/John
$ sudo mount -o remount,ro /dev/sbd1
Option | Description |
---|---|
ro | Mounts the file system read only |
rw | Mounts the file system read write |
auto | Added in /etc/fstab, automatically mounts the file system |
noauto | Reverse of auto |
remount | Mount an already mounted file system again |
sync | I/O operations are done synchronously |
async | I/O operations are done asynchronously |
dev | Interpret as character or block device |
nodev | Don’t interpret as character or block device |
exec | Can execute binary in mounted file system |
noexec | Reverse of exec |
user | Allow user to mount/unmount the file system |
nouser | Allow any user to mount/unmount the file system |
suid | Allows set-user-ID and set-group-ID bits to take effect |
nosuid | Ignores set-user-ID and set-group-ID bits |
defaults | Same as -o rw,suid,dev,exec,auto,nouser,async |
See man mount
for complete list of options:
Mount with /etc/fstab
While booting the system mounts read /etc/fstab
file automatically and mounts the partitions which has option
auto.
$ cat /etc/fstab
UUID=7eb875e6-d73f-44e9-b9ec-074972cbb23b / ext4 errors=remount-ro 0 1
UUID=bd8597fb-8d2b-477f-a78f-0b50a8add1c8 none swap sw 0 0
For manually mounting missing partitions we can execute following command:
$ sudo mount -a
Tools Version
- mount from util-linux 2.31.1 (libmount 2.31.1: selinux, btrfs, assert, debug)
- umount from util-linux 2.31.1 (libmount 2.31.1: selinux, btrfs, assert, debug)
- findmnt from util-linux 2.31.1
- losetup from util-linux 2.31.1