104.3. Control mounting and unmounting of filesystems
Weight: 3
Description: Candidates should be able to configure the mounting of a filesystem.
Key Knowledge Areas:
Manually mount and unmount filesystems
Configure filesystem mounting on bootup
Configure user mountable removable filesystems
Terms and Utilities:
/etc/fstab
/media/
mount
umount
In previous lessons we have learn how to partition hard drives and how to format file system on to those partitions. In this section we will learn how to mount and unmount files systems so we can access them, store and retrieves files from the actual hard drive.
Linux filesystems
The Linux filesystem is one big tree rooted at /, and yet we have filesystems on different devices and partitions. How do we resolve this apparent incompatibility?
The root (/) filesystem is mounted as part of the initialization process. Each of the other filesystems that you create is not usable by Linux system until it is mounted at a mount point.
mounting and unmounting
mount command is used to mount the filesystem found on a device to big tree structure(Linux filesystem) rooted at ‘/‘. Conversely, another command umount can be used to detach these devices from the Tree.
mount
Actually the place we specify as location is mount point. mount point should be exist other wise the device could not be mounted.
With -t option we can specify file system type, how ever it can be omitted as mount command is smart enough to determine file system type!(ubuntu 16.4):
Lets create a directory called "mydisk" as a mount point and mount /dev/sdb1 on that:
When you mount a filesystem over an existing directory, the files on the filesystem you are mounting become the files and subdirectories of the mount point. If the mount point directory already contained files or subdirectories, they are not lost, but are no longer visible until the mounted filesystem is unmounted, at which point they become visible again. It is a good idea to avoid this problem by using only empty directories as mount points.
mount command without any arguments displays all the mounts on the system:
mount options
The mount
command has several options that override the default behavior. For example, we can mount a filesystem read-only by specifying -o ro
. If the filesystem is already mounted, add remount:
notes:
Use commas to separate multiple options, such as
remount
andro
.When remounting an already mounted filesystem, it suffices to specify either the mount point or the device name. It is not necessary to specify both.
You cannot mount a read-only filesystem as read-write. Media that cannot be modified, such as CD-ROM discs, will automatically be mounted read-only.
To remount a writable device read-write, specify
-o remount,rw
mount command examples | description |
mount -l | Add the ext2, ext3 and XFS labels in the mount output. |
mount -r /dev/sdb1 /mydisk | Mount the file system read-only. Same as -o ro |
mount -r /dev/sdb1 /mydisk | Mount the file system read/write. This is the default. It is same as -o rw |
mount -L mydata /mydisk/ | Mount the partition that has the specified label |
mount -U uuid /mydisk | Mount the partition that has the specified uuid |
mount -t iso9660 -o ro /dev/cdrom /mnt | Mount a CD-ROM(just in case system didn't that) |
mount -o loop /mydisk.iso /mnt | Mount an iso image |
mount -B /mydisk /mysecondplace
:After mounting to a directory the mount point can be changed. We will provide the current mounted point and new mount point in a row with -B
parameter. This actually does not removes old mount only adds the new directory as the mount.
All mounts that we create are not permanent and get vanished as system restarted. In order to make a them permanent we need to edit /etc/fstab file.
umount
Once a file system is mounted, we can use the umount command (without an “n”) to unmount the file system. We can unmount the file system by using umount with the device or the mount point.
Note that a file system cannot be unmounted when it is busy - for example, when there are open files on it, or when some process has its working directory there, or when a swap file on it is in use.
lets unmount /dev/sdb1 as an example:
/etc/fstab
In previous sections about Boot Managers we learned in both GRUB and LILO root= parameter tell the boot loader what filesystem should be mounted as root. For GRUB2, this is thesetroot
statement. Once the root filesystem is mounted, the initialization process runs mount
with the -a
option to automatically mount a set of filesystems. The set is specified in the file /etc/fstab.
Fstab is operating system’s file system table:
file system :This may be a device name such as /dev/sda1, or a label (LABEL=) or UUID (UUID=).
We can either mount using device name, labels or uuid. Which one is better ?Every body can set any labels the he/she wants, Device name might be changed for example if you attach two usb devices, so the best way is using uuid for mounting(use
blkid
command).
mount point : mount point, but For swap space, this should be the value ‘none’ or ‘swap’.
type : Specifies the type of filesystem. it can be (ext2,3,4, reiserfs,swap, vfat and ntfs,ISO9660,auto)
option : Specifies the mount options. Specify defaults if you want default mount options.
sync/async - All I/O to the file system should be done (a)synchronously.
auto - The filesystem can be mounted automatically (at bootup, or when mount is passed the -a option). This is really unnecessary as this is the default action of mount -a anyway.
noauto - The filesystem will NOT be automatically mounted at startup, or when mount passed -a. You must explicitly mount the filesystem.
dev/nodev - Interpret/Do not interpret character or block special devices on the file system.
exec / noexec - Permit/Prevent the execution of binaries from the filesystem.
suid/nosuid - Permit/Block the operation of suid, and sgid bits.
ro - Mount read-only.
rw - Mount read-write.
user - Permit any user to mount the filesystem. This automatically implies noexec, nosuid,nodev unless overridden.
nouser - Only permit root to mount the filesystem. This is also a default setting.
defaults - Use default settings. Equivalent to rw, suid, dev, exec, auto, nouser, async.
_netdev - this is a network device, mount it after bringing up the network. Only valid with fstype nfs.
note1: User-mounted filesystems default to noexec unless exec is specified afteruser.
note2: noatime will disable recording of access times. Not using access times may improve performance.
dump: Specifies whether the dump command should consider this ext2 or ext3 filesystem for backups. A value of 0 tells dump to ignore this filesystem.
pass: Non-zero values of pass specify the order of checking filesystems at boot time,( based on check interval and mount count, try
tune2fs -l
).
Lets add /dev/sdb1 to the /etc/fstab file using UUID and have it even after reboot:
if any thing goes wrong in /etc/fstab , it can cause system failure and system can't boots up! So try mount -a
in order to try mounting all fstab items before rebooting the system and check for errors.
Ops! that is defaults not default, so try to edit this line again in fstab:
and we are done:
users can only mount and unmount things they are allowed to in /etc/fstab!
.
.
.
https://developer.ibm.com/tutorials/l-lpic1-104-3/
https://www.geeksforgeeks.org/mount-command-in-linux-with-examples/
https://www.sanfoundry.com/mount-command-usage-example/
https://www.poftut.com/linux-mount-command-tutorial-examples/
https://www.thegeekdiary.com/how-to-mount-and-umount-a-file-system-in-linux/
https://help.ubuntu.com/community/Fstab
https://linoxide.com/file-system/understanding-each-entry-of-linux-fstab-etcfstab-file/
.
Last updated