Monday, August 4, 2014

Mounting disks as non root from the shell

The most popular method for mounting disks and the most traditional method is the "mount" command.

In most cases (with the exception of specifying before hand that unprivileged users can mount specific disk in /etc/fstab) the "mount" command requires root privileges.

Now often the question becomes "How come I can use a file manager to mount regular disks E.g. USB's, external HDD's without root and the mount command requires root ?"

The answer to this is to use the more traditional "Udisks1" or the newer "Udisks2" there has been a debate on why Udisks is better than Udisks2 but keeping the debate and arguments aside both tools serve the purpose.

For Udisk1 (the original) the command line  utility is "udisks". For Udisks2 (the rewrite) the command line utility is "udiskctl" both can coexist and normally do coexist on most linux distributions but are different. Both however are used for managing storage devices.

The original Udisks mounted all partitions in "/media/<partition-name> " E.g. /media/linux_data (Where linux_data is the partition name)

The new Udisks2 mounts storage devices  in /run/media/<username>/<partition-name> E.g. /run/media/lin-user/linux_data (WHere username is lin-user and partition is linux_data).

Note: /var/run is a symbolic link to /run so Udisks normally states that it has mountded the disks to
 /var/run/media/<username>/<partition-name> E.g. var/run/media/lin-user/linux_data (you can reference the same storage device as a directory using /run/media/ or /var/run/media/ it does not matter)

Now armed with basic info on the tools we will be using lets go ahead and try to use the tools mentioned above to mount and unmount storage devices.

Typical steps involved:

1) Plug in the storage device (if its an internal HDD and just an unmounted drive you can skip this step.)
2) type "lsblk" and look at the output to determine which drive/block-device you are interested in mounting E.g. /dev/sdb1 or /dev/sdc3 or /dev/sda2 etc.
3) Use Udisks or Udisks2 syntax to mount the specific drive automatically.

Udisks and Udisk2 syntax

Udisks syntax:

a) mounting a drive
udisks --mount /dev/sdX
E.g. udisks --mount /dev/sdb2

b) unmounting a drive
udisks --unmount /dev/sdX
E.g. udisks --unmount /dev/sdb2

The syntax is very clean and easy to use for udisks


Udisks2 syntax:
The syntax is for udisk2 is different.

a) mounting a drive
udisksctl mount -b /dev/sdX
E.g. udisksctl mount -b /dev/sdb2

b) unmounting a drive
udisksctl unmount -b /dev/sdX
E.g. udisksctl unmount -b /dev/sdb2

Note: there are no double dashes before the word "mount" and "unmount" also -b is a must so that udisks2 knows that you are giving it a block device to mount.

The advantage of udisks /udisks2 over the mount command is that you do not need to specify a mount location or create a special folder. Udisks/Udisks2 automatically exposes the device as a folder for you in the paths mentioned earlier on in the article.

Sunday, August 3, 2014

Fixing the problematic duplicate UUID issue

A UUID is a universally unique identifier. For disks UUIDS are used to uniquely identify a particular disk irrespective of in which order and when it is loaded. E.g. A UUID can be used to tell the system to always boot from the specific partition with a specific UUID irrespective of which order it is loaded in e.g. if there are 2 hard disk it may be loaded as /dev/sdb but another type as /dev/sda it would not matter.

Duplicate UUIDs occur only in rare cases. The most common cause is if you have cloned a disk using some utility. (E.g. dd). Duplicate UUIDs in general are not good and can cause progblems because the identifiers are no longer unique on the system.


The steps to solve this issue are:


1) type “blkid” on the main server and the other server as root. And identify the partitions and their corresponding numbers in /dev E.g. /dev/sda1, dev/sda2 .. /dev/sdX etc.. Now we
need to change only those partitions whose UUIDs match the UUIDs of partitions on the server.

2) generate as many random UUIDs as needed for the partitions. Using the “uuidgen” command. Running “uuidgen” in terminal will generate a random UUID e.g.
“4ae70274-5b0f-4384-a7fd-15fcbdbd0b58″ do this twice or thrice or as many times as needed.

4) Assign these randomly generate UUID’s to the partitions. This needs root privileges; the command is “tune2fs /dev/sdX -U New-UUID-Value”
E.g.: sudo tune2fs /dev/sda2 -U 73c6756e-aad2-41f6-8dff-597fe076cd07