Automatically Mount Partitions on Ubuntu

Automatically Mount Partitions on Ubuntu

There are broadly two aproaches -

Per-user mounting (usually under /media)

Systemwide mounting (anywhere, often under /mnt)

Per-user mounting does not require root access, it's just automating the desktop interface. Systemwide mounts (/etc/fstab) can allow access from before login, and are therefore much more suitable for access through a network, or by system services.

Viewing the system's physical information

To read the layout of the physical disks in the system, the 'fdisk' command is used. Before panicking, realize that fdisk will be used with only non-destructive options; specifically, it will be used with 'l' (lower-case 'L', not '1'), which lists the partition table of the specified disk.
sudo fdisk -l
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x1baf0215

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     7813119     3905536   82  Linux swap / Solaris
/dev/sda2         7815166   976771071   484477953    5  Extended
Partition 2 does not start on physical sector boundary.
/dev/sda5         7815168    56641535    24413184   83  Linux
/dev/sda6        56643584   447266815   195311616   83  Linux
/dev/sda7       447268864   976771071   264751104   83  Linux

If the system has multiple hard disk drives, multiple lists will be displayed.

Per-User Mounts

You can do the same thing on the command line with the udisks tool easily.
/usr/bin/udisks --mount /dev/sdb1
The bit after --mount is the device name of the partition you want to mount. (/dev/something)

Finding the UUID of your partition

A device name like /dev/sdb1 is based on where your physical drive is plugged in and the order the drives were made available to the computer, so if your computer changes the same command could mount a different partition. It's possible for this to happen just from a software upgrade.

The solution is to use a UUID. A UUID is a globally unique name for the partition. A UUID will remain the same if you put an internal disk into an external USB caddy, or change the name of the partition.

 ls -al /dev/disk/by-uuid/ 
you will see an entry that matches the name :

total 0
drwxr-xr-x 2 root root 120 Apr 18 07:55 .
drwxr-xr-x 5 root root 100 Apr 18 07:55 ..
lrwxrwxrwx 1 root root  10 Apr 18 07:55 78a8d4f4-8739-407b-93fe-7fe23b560e74 -> ../../sda5
lrwxrwxrwx 1 root root  10 Apr 18 07:55 93b20629-db77-4a2f-998c-bc2df0227bbf -> ../../sda1
lrwxrwxrwx 1 root root  10 Apr 18 07:55 bb9a474e-a5a5-4b1c-9b98-e6becda49640 -> ../../sda6
lrwxrwxrwx 1 root root  10 Apr 18 07:55 f24b64fc-93df-4070-8181-4d3c1487763a -> ../../sda7


You can now determine the command you need for mounting the device by UUID. For our example it would be
/usr/bin/udisks --mount /dev/disk/by-uuid/78a8d4f4-8739-407b-93fe-7fe23b560e74 
Of course you need to replace 78a8d4f4-8739-407b-93fe-7fe23b560e74  with the UUID of the device you want to mount.

Editing Ubuntu's filesystem table
Sometimes we need to mount our disk automatically.It needs to edit Ubuntu's FS table and
It is possible to break Ubuntu if some of the earlier lines in the file opened during this step are modified, so be sure to read this section carefully.
Ubuntu's filesystem table is located at '/etc/fstab'. Open this file for editing by running the following command for Ubuntu.
gksu gedit /etc/fstab
The file opened contains lines of the form
<device> <location> <Linux type> <options> <dump> <pass>.
Every element in this line is separated by whitespace (spaces and tabs).

# /media/data1 was on /dev/sda6 during installation
UUID=bb9a474e-a5a5-4b1c-9b98-e6becda49640 /media/data1    ext4    defaults        0       2

Save file and exit then restart your computer. It ll mount your disk automatically when your computer start.