Deprecated: Assigning the return value of new by reference is deprecated in /membri/redclay/wiki/inc/parserutils.php on line 159

Deprecated: Assigning the return value of new by reference is deprecated in /membri/redclay/wiki/inc/parserutils.php on line 162

Deprecated: Assigning the return value of new by reference is deprecated in /membri/redclay/wiki/inc/parserutils.php on line 293

Deprecated: Function split() is deprecated in /membri/redclay/wiki/inc/auth.php on line 103

Deprecated: Function split() is deprecated in /membri/redclay/wiki/inc/common.php on line 737
projects:hal-automount [Redclay's Home Page]
 

Deprecated: Assigning the return value of new by reference is deprecated in /membri/redclay/wiki/inc/parser/xhtml.php on line 943

Usb drive automount with HAL and D-Bus

Description

This simple script listens for HAL events on SystemBus and uses pmount in order to mount usb drive devices on /media/DeviceLabel.

Requirements

  • D-Bus daemon
  • HAL (Hardware Abstraction Layer) daemon
  • Python D-Bus bindings (python-dbus package)

Code

Save as hal-automount and run: $ python hal-automount

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import dbus
import dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
    import dbus.glib
import gobject
import sys
import os
 
class DeviceManager:
    def __init__(self):
        self.bus = dbus.SystemBus()
        self.bus.add_signal_receiver(self.device_added,
                        'DeviceAdded',
                        'org.freedesktop.Hal.Manager',
                        'org.freedesktop.Hal',
                        '/org/freedesktop/Hal/Manager')
 
        self.bus.add_signal_receiver(self.device_removed,
                        'DeviceRemoved',
                        'org.freedesktop.Hal.Manager',
                        'org.freedesktop.Hal',
                        '/org/freedesktop/Hal/Manager')
        
    def udi_to_device(self, udi):
        return self.bus.get_object("org.freedesktop.Hal", udi)
 
    def device_added(self, udi):
        print 'Added', udi
        properties = self.udi_to_device(udi).GetAllProperties()
        if properties.get('info.category') == u'volume':
            label, dev = properties.get('volume.label'), properties.get('block.device')
            print 'Mounting %s on /media/%s' %(dev, label)
            os.system('pmount %s /media/%s' %(dev, label))
 
    def device_removed(self, udi):
        print 'Removed', udi
 
if __name__ == '__main__':
    m = DeviceManager()
    
    mainloop = gobject.MainLoop()
    try:
        mainloop.run()
    except KeyboardInterrupt:
        mainloop.quit()
        print 'Exiting...'
        sys.exit(0)

References

 
projects/hal-automount.txt · Ultima modifica: 2007/01/19 13:08 da redclay
 
Recent changes RSS feed Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki