udisks

From Gentoo Wiki
Jump to: navigation, search

Resources

udisks is a D-Bus daemon offering some storage related services:

Installation

Prerequisites

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

Kernel

You need to activate the following kernel options:

KERNEL udisks1
Device Drivers  --->
    < > ATA/ATAPI/MFM/RLL support (DEPRECATED)  --->
    [*] USB support  --->
        <*>   Support for Host-side USB
        [*]     USB runtime power management (autosuspend) and wakeup
File Systems -->
    Native Language Support -->
        <*> NLS UTF8
KERNEL udisks2
General setup  --->
    [*] Support for paging of anonymous memory (swap)
Device Drivers  --->
    < > ATA/ATAPI/MFM/RLL support (DEPRECATED)  --->
File Systems -->
    Native Language Support -->
        <*> NLS UTF8
    Pseudo filesystems  --->
        -*- Tmpfs virtual memory file system support (former shm fs)
        [*]   Tmpfs POSIX Access Control Lists

USE flags

There are several sections on USE flags for udisks. Each section corresponds to version of the software to be installed. Slot 0 contains udisks version 1.x. Slot 2 contains version 2.x. Choose the appropriate version accordingly.

Global

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

FILE /etc/portage/make.confSetting the global USE flag
USE="udisks"

udisks 1 (slot 0)

Optional USE flags for sys-fs/udisks in slot 0:
USE flag (what is that?) Default Recommended Description
debug No Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see http://www.gentoo.org/proj/en/qa/backtraces.xml
nls Yes Add Native Language Support (using gettext - GNU locale utilities)
remote-access No Control whether connections from other clients over LAN are allowed

udisks 2 (slot 2)

Optional USE flags for sys-fs/udisks in slot 2:
USE flag (what is that?) Default Recommended Description
cryptsetup No Enable sys-fs/cryptsetup support
debug No Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see http://www.gentoo.org/proj/en/qa/backtraces.xml
gptfdisk Yes Pull in sys-apps/gptfdisk for sgdisk command as used by partitioning functionality
introspection Yes Use dev-libs/gobject-introspection for introspection
selinux No  !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
systemd No Support sys-apps/systemd's logind

Emerge

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

root #emerge --ask --changed-use --deep @world

Additional software

Configuration

Udisks uses polkit to handle permissions.

Groups

Make sure each user is the plugdev group. Here the example user larry is used.

foo $groups
wheel audio users plugdev larry

If plugdev is not among the available user groups (common users, who use a window manager instead of a desktop environment), add you user to the plugdev group.

root #usermod -a -G plugdev foo

Usage

Some useful commands include:

  • 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.

In some cases the udisks command is not found. If this is the case, try udisksctl. This command have different command options than udisks; use the -h option if assistant is required.

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 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

FILE {{{filename}}}udisk_listener.py
import os
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
 
uid = None
 
 
def device_added(device):
    print("---added---")
    device_dump(device)
 
 
def device_removed(device):
    print("---removed---")
    print(str(device))
    device_dump(device)
    pass
 
 
def device_changed(device):
    print("---changed---")
    device_dump(device)
    pass
 
 
def device_dump(device):
    device_obj = system_bus.get_object("org.freedesktop.UDisks", device)
    device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
    #
    #  beware.... anything after this may or may not be defined depending on the event and state of the drive. 
    #  Attempts to get a prop that is no longer set will generate a dbus.connection:Exception
    #
    try:
        print("DeviceFile:" + device_props.Get('org.freedesktop.UDisks.Device',"DeviceFile"))
    except:
        print("DeviceFile is unset")
 
    try:
        print("NativePath: " + device_props.Get('org.freedesktop.UDisks.Device',"NativePath"))
    except:
        print("NativePath: is unset")
 
    try:
        is_mounted = device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted")
        if is_mounted:
            mounted_uid = device_props.Get('org.freedesktop.UDisks.Device', "DeviceMountedByUid")
            if mounted_uid == uid:
                print("mounted by me")
            else:
                print("mounted by " + str(mounted_uid))
            mountpaths =  device_props.Get('org.freedesktop.UDisks.Device', "DeviceMountPaths")
            for test in mountpaths:
                print("paths: " + test)
        else:
            print("unmounted")
    except:
        print("DeviceIsMounted is unset")
 
    try:    
        is_media_available = device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMediaAvailable")
        if is_media_available:
            print("media available")
        else:
            print("media not available")
    except:
        print("DeviceIsMediaAvailable is not set")
 
    try:        
        is_partition_table = device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartitionTable")
        if is_partition_table:
            print("device is partition table")
    except:
        print("DeviceIsPartitionTable is not set")
 
    try:        
        is_partition = device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartition")
        if is_partition:
            print("device is partition")
    except:
        print("DeviceIsPartition is not set")
 
    try:
        is_removeable = device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsRemovable")
        if is_removeable:
            print("device is removable")
        else:
            print("device is not removable")
    except:
        print("DeviceIsRemovable is not set")
 
 
if __name__ == '__main__':
    uid = os.getuid()
    DBusGMainLoop(set_as_default=True)
    system_bus = dbus.SystemBus()
    udisk_proxy = system_bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
    udisk_iface = dbus.Interface(udisk_proxy, "org.freedesktop.UDisks")
 
    udisk_iface.connect_to_signal('DeviceAdded', device_added)
    udisk_iface.connect_to_signal('DeviceRemoved', device_removed)
    udisk_iface.connect_to_signal('DeviceChanged', device_changed)
 
    loop = gobject.MainLoop()
    loop.run()

USB drive example

The following example is from a Gentoo system when an 8GB USB 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.

CODE
---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.

CODE
---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

CODE
---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

CODE
---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.


CODE
---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
-
CODE
--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
Note
DeviceIsMountedByUid appears to be useful for the UID put on the mounted disk in RHEL/Centos6 at least for the default gnome session (Gnome 2.28). However it reports as mounted by root on Gentoo when the KDE desktop device notifier pops up and is used to mount, presumably because it's devicekit/policykit getting involved. We need to look at the /proc/mounts for the DeviceFile string instead to find the UID in the mount options.

Troubleshooting

See also

  • D-Bus - An inter-process communication (IPC) system for software applications.