udisks

From Gentoo Wiki
Jump to: navigation, search

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

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 configurationudisks2

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

Software

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

USE="... udisks ..."
udisks1 → Information about USE flags
USE flag 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 Adds Native Language Support (using gettext - GNU locale utilities)
remote-access No Control whether connections from other clients over LAN are allowed


udisks2 → Information about USE flags
USE flag 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

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

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

Addon software

Configuration

Udisks uses polkit to handle permissions.

Groups

Make sure your user in the group plugdev. Here the example user foo is used.

foo $ groups
wheel audio users plugdev foo

If plugdev is not among your user's 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 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.

In some cases the udisks command is not found. If this is the case, try udisksctl (Will have different command options from udisks)


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

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


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