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.

No comments:

Post a Comment