A Little About Linux: Mounting and Un-mounting Devices

So, now that you've prepared a disk by choosing and creating a file system on a partition, you will probably want to use it to store data right?  Well, there is one last step that needs to be performed before the Linux OS can actually 'see' and use this device and that is to mount it.  Mounting a device can be performed temporarily via the command line or permanently so that it can survive a reboot and we will discuss both methods in this little guide.

In Windows, you would effectively assign drive letter to a disk to enable the OS to store files on it, but in Linux you create a mount point to a directory, rather than using the rather limiting drive letter scheme.

Before creating a mount point, the first step is to view what mount points are already there and this can be done using the findmnt command.  Once run, you will see something like this;

TARGET        SOURCE         FSTYPE      OPTIONS
/proc         proc           proc        rw,relatime
/sys          sysfs          sysfs       rw,relatime,seclabel
/dev          devtmpfs       devtmpfs    rw,relatime,seclabel,size=1947316k,nr_inodes=486829,mode=755
/dev/pts      devpts         devpts      rw,relatime,seclabel,gid=5,mode=620,ptmxmode=000
/dev/shm      tmpfs          tmpfs       rw,relatime,seclabel
/             /dev/mapper/vg_box-lv_root ext4        rw,relatime,seclabel,barrier=1,data=ordered
/selinux                     selinuxfs   rw,relatime
/dev          devtmpfs       devtmpfs    rw,relatime,seclabel,size=1947316k,nr_inodes=486829,mode=755
/proc/bus/usb /proc/bus/usb  usbfs       rw,relatime
/boot         /dev/sda1      ext4        rw,relatime,seclabel,barrier=1,data=ordered
/home         /dev/mapper/vg_box-lv_home ext4        rw,relatime,seclabel,barrier=1,data=ordered
/data1        /dev/sdb1      ext4        rw,relatime,seclabel,barrier=1,data=ordered

Running findmnt -l shows the display as above and running the command on its own, will give the display as a tree structure.

Temporarily Mounting Devices
Once you've prepared a disk, you need to choose at what point in the file system hierarchy this disk should appear.  Nominally, and strictly sticking to FHS design, the mount point should be a directory created under /mnt, but you can mount it anywhere really.  The first step is to make a temporary mount which will disappear upon a reboot.


mount - Use to mount a directory to a device

Using the mount command on it's own works very similarly to the findmnt command, in that it shows all of the currently used mount points;

/dev/mapper/vg_box-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/sda1 on /boot type ext4 (rw)
/dev/mapper/vg_box-lv_home on /home type ext4 (rw)
/dev/sdb1 on /data1 type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

However, the mount command is more usually used to actually create a mount point between a directory and a device;

Mount a USB drive
mount -t vfat /dev/sdc1 /media/usb

Mount and ISO image as a Read Only block level device
mount -o ro,loop Linux.iso /media/cdrom

There are plenty of other options that can be set at this stage, the most important of these are around permissions and setting R/W access.

Un-mounting Devices
Not so amazingly, if you've mounted a device you can also unmount it fairly easily;

umount - Use to unmount a directory/device pairing (the opposite of mount)

Force the unmounting of devices (Works especially well with stale NFS connections)
mount -f /mnt/nfs

The Lazy unmount succeeds more often than others by waiting until the file system is not busy and then cleaning up all references to it
mount -l /mnt/mountpoint

If the device can't be unmounted fully, make it read only if possible
mount -r /mnt/mountpoint

Allowing Mounted Devices Survive A Reboot
All Linux distributions have a file called fstab located at /etc/fstab in which the permanent mounting of devices are made.  There are also a number of extra options when adding devices to fstab rather than mounting via command line.  An example of /etc/fstab is shown below;

/dev/mapper/vg_cutebox-lv_root            /          ext4    defaults        1 1
UUID=0f759f11-de58-44bc-917c-7befeb27f274 /boot      ext4    defaults        1 2
/dev/mapper/vg_box-lv_home                /home      ext4    defaults        1 2
/dev/mapper/vg_box-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

UUID=4bc5430c-fb5c-4fdb-b7d0-7785254a9e1d /data1     ext4    defaults        0 0

The columns for an fstab file are;
DEVICE           MOUNT POINT           FILE SYSTEM           OPTIONS        DUMP      FSCK 

The description of each column is;

DEVICE - The device that will be mounted.  This can be specified by the device path, label or UUID.  This can also be a network based drive such as NFS or SMB.

MOUNT POINT - This will contain the name of the mount point or 'swap' for the swap file.

FILE SYSTEM - The type of file system for the device to be mounted is located here or this can be 'auto' if the kernel is to make the decision (useful for removable media but not always operational with all file system types.)

OPTIONS - Allows the inclusion of most options that can be used with the command line mount command.

DUMP - This dictates whether the 'Dump' backup tool should backup this mount.  However as nobody uses Dump any more, just leave as 0.

FSCK - The fsck command checks the health of the file system, the number here dictates whether this mount point should be checked on boot and if so, what order this should happen. 0 = Do Not Check (If the mount is for SMB or ReiserFS etc. this should be 0), 1 = Root Partition, 2 = All Other Mount Points.

No comments :