/ linux

Automatically Mount Partitions in Linux

We all might have come across the situation of adding additional partitions to our Linux Distro, I use Debian 8 on my laptop and created a separate partition for all my work related file.Also many a times I create an EBS for AWS EC2 machines we use at work.

On every reboot(which happen very rarely) the additional partition that we created wont be mounted automatically, lets see how to automount them.

mount - This is the command that we use in linux to mount a partition to a mount point. A mount point can be any directory on the file system preferably empty.If the mount point is not empty you wont be able to access the actual file in that directory untill you unmount the partition.

fstab - This is the configuration file in Linux which contain a list of all mounted partitions. It is located in /etc/fstab .

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda7 during installation
UUID=fb5d622b-b321-12c1-s240-d3f34dd32fd3 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=f242b0e0-fd23-4e12-b222-eca82920100a none swap sw 0 0
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0

That is how it look in my Laptop running Debian 8, it contain the root,swap and cd-rom mounted. Each line (expect the comments starting with #) is an entry for a partition.

The fields in each entry are as follows:

  • file system - The information on the actual device that we are mounting.This is a way to identify the partition/device eg: /dev/sda1 or it could be the Label of the partition or the UUID. It is a good choice to use UUID as mentioned in the sample config above. Use blkid command to get the UUID of partitions in your system.
  • mount point - The mount point or directory where we want to mount the partition to.
  • type  - The type of the partition that you are mounting, valid values can be any of the file system supported by your system eg: ext4,swap etc.
  • options  - These are filesystem specific options for the partition like access rights, if its a read-only partition etc We would leave this to 'default'  as it choose a list of sensible defaults based on the type of the filesystem.
  • dump  - If you have dump utility installed and want to backup the content of this partition set this to 1, if not set to 0. Mostly this would be set to 0.
  • pass - This options configure if this partition must be checked by fschk for file system errors at boot time and the order in which it must be done. The possible values are 0 - no , 1 - which must be given for root partition , 2 which would be the optimal choice for us since its an additional partition.

So the entry would look like :

UUID=1e2ee726-d3e1-6578-96a0-91cdc1cee02e /m4ver1k  ext4    defaults        0       2

IMPORTANT: There is a good chance that your boot will fail if there are errors in your fstab entries so it is a wise choice to run  mount -a and make sure your fstab entries are correct.