Udisks

udisks is a D-Bus daemon offering some storage related services:
 * enumeration of storage devices, much like udev but with more details.
 * mounting of filesystems.
 * partition of storage devices.
 * monitor of storage devices.
 * configuration of storage devices.

Prerequisites
Udisks uses D-Bus and polkit, so set them up first.

Kernel
You need to activate the following kernel options:

Software
Portage knows the global USE flag udisks for enabling support for udisks in other packages. Enabling this USE flag will pull in automatically (default for desktop profiles):

After setting this you want to update your system so the changes take effect:

Addon software

 * - GNOME program to partition, configure and monitor storage devices.
 * - Automatic mount of drives.
 * - Run actions on udisks events.

Configuration
Udisks uses polkit to handle permissions.

Usage
Some useful commands are:
 * udisks --enumerate - Lists all storage devices.
 * udisks --monitor - Monitor udisks.
 * udisks --monitor-details - Monitor udisks with more details.
 * udisks --inhibit-polling /dev/DEVICEFILE - Disable polling on the given device.

Python Scripting
The following code is an example of listening to udisk on the system dbus for device events. We stick with udisk1 and glib style event loops in order for the code to also be able to run on a RedHat or Centos system. Otherwise, we could use the udisk2 monitor api to just look at mount events.

The relevant udisk documentation url for 1.0.5 (Rhel6 is at 1.0.1) is here

USB Thumb Drive Example
The following example is from a gentoo system when an 8gb thumb drive containing kubuntu 14.04 installer is plugged into a usb port. It was created by dd'ing the iso file directly to the thumb drive. fdisk shows that the 1gb or so of good stuff ended up in /dev/sdf2 while /dev/sdf1 is the free space left over. Note that the first event is the "whole drive" or partition table getting looked at.

---added--- DeviceFile:/dev/sdg NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg unmounted media available device is partition table device is removable ---added--- DeviceFile:/dev/sdg2 NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg/sdg2 unmounted media available device is partition device is not removable ---added--- DeviceFile:/dev/sdg1 NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg/sdg1 unmounted media available device is partition device is not removable

This happens when the device notifier pops up and we have kde open the drive in the dolphin file manager.

---changed--- DeviceFile:/dev/sdg1 NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg/sdg1 mounted by 0 paths: /run/media/someuser/Kubuntu 14.04 LTS amd64 media available device is partition device is not removable

All this happens when we do a "safely remove" in dolphin. Notice that some of the properties are already unset and we are catching exceptions in device_dump during examination

---changed--- DeviceFile:/dev/sdg1 NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg/sdg1 unmounted media available DeviceIsPartition is not set DeviceIsRemovable is not set ---removed--- /org/freedesktop/UDisks/devices/sdg1 DeviceFile is unset NativePath: is unset DeviceIsMounted is unset DeviceIsMediaAvailable is not set DeviceIsPartitionTable is not set DeviceIsPartition is not set DeviceIsRemovable is not set ---removed--- /org/freedesktop/UDisks/devices/sdg2 DeviceFile is unset NativePath: is unset DeviceIsMounted is unset DeviceIsMediaAvailable is not set DeviceIsPartitionTable is not set DeviceIsPartition is not set DeviceIsRemovable is not set ---changed--- DeviceFile:/dev/sdg NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg unmounted media not available device is removable ---changed--- DeviceFile:/dev/sdg NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg unmounted media not available device is removable ---changed--- DeviceFile:/dev/sdg NativePath: /sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4:1.0/host46/target46:0:0/46:0:0:0/block/sdg unmounted media not available device is removable

Then we get one last removal signal when we yank the thumb drive

---removed--- /org/freedesktop/UDisks/devices/sdg DeviceFile is unset NativePath: is unset DeviceIsMounted is unset DeviceIsMediaAvailable is not set DeviceIsPartitionTable is not set DeviceIsPartition is not set DeviceIsRemovable is not set

CD/DVD media example
The use of a dvd or cdrom in an internal drive will cause only changes events to signal. Here's an example for a mount followed by an unmount of a gentoo autobuild iso in a Centos6 VM's virtual cdrom. Notice that there are two change signals per action. The insertion shows the first event with media available and then a second event on the mount where the DeviceMountedByUid value properly matched up with our uid. Presumably the two for the removal are the unmount followed by an eject, but media shows as not being available on both so the two are indistinguishable.

---changed--- DeviceFile:/dev/sr0 NativePath: /sys/devices/pci0000:00/0000:00:01.1/host1/target1:0:0/1:0:0:0/block/sr0 unmounted media available device is removable ---changed--- DeviceFile:/dev/sr0 NativePath: /sys/devices/pci0000:00/0000:00:01.1/host1/target1:0:0/1:0:0:0/block/sr0 mounted by me paths: /media/Gentoo Linux amd64 20140227 media available device is removable

---changed--- DeviceFile:/dev/sr0 NativePath: /sys/devices/pci0000:00/0000:00:01.1/host1/target1:0:0/1:0:0:0/block/sr0 unmounted media not available device is removable ---changed--- DeviceFile:/dev/sr0 NativePath: /sys/devices/pci0000:00/0000:00:01.1/host1/target1:0:0/1:0:0:0/block/sr0 unmounted media not available device is removable

Troubleshooting

 * udisks communicates over D-Bus, so also see the D-Bus "Troubleshooting" section.
 * udisks communicates over D-Bus, so also see the D-Bus "Troubleshooting" section.
 * udisks communicates over D-Bus, so also see the D-Bus "Troubleshooting" section.