Ubuntu - How to add or create hard disk partition and make it automatically mount

In Ubuntu, adding or managing new partition or hard disk should use UUID (Universally Unique Identifier) of the device rather then using the device node name. e.g. /dev/sda2, /dev/hda3, /dev/sda5 and etc. IMHO its advantage compare to the conventional way of mounting device, using /dev/sda or /dev/hda are :
  • UUID is unique when it comes to device node naming. e.g. multiple USB device plug in and out can generate different /dev/sda nodes.
  • UUID have been used by ubuntu by default to assign partition or hard disk in /etc/fstab.
  • UUID have 25616 of combination and it takes a very long time to exhaust it even though if we were to generate it fast enough. e.g. generating 1 trillion a nano second would take 10 years to exhaust it.
  • using UUID will list the partition or hard disk to appear in the 'File Browser' (aka Nautilus) under 'Places'.

This post will list 2 ways to get the UUIDs in order to make the new partition or hard disk automatically mount under Linux (or particularly Ubuntu), the command line and the GUI (Graphical User Interface) programs provided by Ubuntu.

Prepare the partition or hard disk
Go to 'Ubuntu Menu' --> Administration --> Partition Editor to partition and format the new partition or hard disk. If the Partition Editor does not exists, install it by executing :
apt-get install gparted
in the command line.

First method (command line) :
How to get UUID in command line
ls -lh /dev/disk/by-uuid/

Make it automount
1. make a directory in order to mount the new partition or hard disk.
mkdir /media/new-mount-point

2. edit the file /etc/fstab by vim it.

3. insert the below line into the file /etc/fstab :
UUID=some-hex-numbers /media/new-mount-point  ext4  users,user,owner 0  0

4. mount the disk by executing :
mount /mnt/new-mount-p0int
without the hassle of specifying device node name.
mount -t auto /dev/sda /mnt/mount-point

Second method (GUI programs provided by Ubuntu) :
Make the partition or hard disk auto mount and appear on File Manager (aka Nautilus)
1. Go to 'Ubuntu Menu' --> Administration --> Storage Device Manager. Install the Storage Device Manager by executing :
apt-get install pysdm
if it does not exists.

2. Set it to choose the new partition in 'partition list' (on the first column of the left panel) and set the below information under 'General Configuration' :
Name : Give it a name (it will also appear as the mount point)
Options : (click on the 'Assistant' button)
  • Tick --> 'Allow any user to mount the file system'
  • Tick --> 'The file system is mounted at boot time'
  • Tick --> 'Allow a user to mount and unmount the file system'
  • Tick --> 'The owner of the device can mount it'

3. Optional : edit the file /etc/fstab and replace the device node name (e.g. /dev/sda1, /dev/sda2, /dev/hda1, /etc/hda2 or others) to "UUID=some-hex-numbers".

Trouble shooting
  • Should there be any permission or right issue to cause the directory cannot be change, run command before and after it is mounted

    chown -R owner-username:owner-username /media/mount-point
  • If mounting the new partition or hard disk in command line give an error saying "special device /dev/disk/by-uuid/ the uuid of the new partition does not exist", update the kernel with the late devices of UUIDs by executing the command

    sudo partprobe

Voilla !!!

No comments: