Mounting Android Devices at sane mount points

May 21, 2011 at 1:37 am Leave a comment

My android phone (or my camera for that matter) doesn’t have a very nice name for it’s volume. However, I am too cowardly to try and change the volume name for an android device with gparted. (At least not until I can find some notes about it on the internet). As far as I can tell there is no way to tell gnome where to mount devices, and though you can just do everything you want at the udev level this removes gnome features from you (short cuts etc – not that I really use these…)

Thus I’ve been forced to use workarounds.

The following script will sit quietly wait for a given device to be mounted and create a sanely named symlink to it.

#!/usr/bin/python

# Silly script to create nicely named symlinks 
# for mounts in addition to the less nicely named volume links.

# In general you probably just want to change your volume label.
# However my volumne label comes from a phone and I am a coward!

# This script is written to crash out on failure. 
# A supervisor should ensure that it is kept alive (like upstart)

import os
import pyinotify
import select
import sys
import syslog
import traceback

def syslog_exceptions():
    hook = sys.excepthook
    def new_hook(type, value, tb):
        hook(type, value, tb)
        output = traceback.format_exception(type, value, tb)
        syslog.syslog(repr(output))
    new_hook.previous_hook = hook
    sys.excepthook = new_hook
syslog.openlog('autolinkmount')
syslog_exceptions()


def log(msg):
    syslog.syslog(msg)
    print msg

mount, link = sys.argv[1:]

class EventProcessor(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        path = os.path.join(event.path, event.name)
        if path == mount:
            os.symlink(mount, link)
            log("%s created. Linked to it from %s." % (path, link))

    def process_IN_DELETE(self, event):
        path = os.path.join(event.path, event.name)
        if path == mount:
            os.unlink(link)
            log("%s removed. Unlinking to it")
            
def wait_for_exit(thread, signal):
    """Wait for thread to exit and then signal"""
    # Hack to leave main thread free to handle signals
    thread.join()
    signal.set()

wm = pyinotify.WatchManager()
notifier = pyinotify.AsyncNotifier(wm, EventProcessor())
notifier.daemon = True
flags = pyinotify.EventsCodes.ALL_FLAGS
mount_dir = os.path.dirname(mount)
wm.add_watch(mount_dir, flags['IN_CREATE'] | flags['IN_DELETE'])

log("Waiting for %s to be mounted..." % mount)
notifier.loop()

Together with the following upstart file this will magically create symlinks for you.

description     "Create sane mounts locations for my phone"

start on local-filesystems
stop on runlevel [016]

respawn

exec /root/autolinkmount /media/ /media/phone

My goodness this was a waste of time.

PS: For almost all devices other than a phone you probably want to achieve this effect by changing the volume label.

Advertisements

Entry filed under: Uncategorized.

Code exceptions to syslog in addition to standard out Improved python dir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


May 2011
M T W T F S S
« Apr   Jun »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

%d bloggers like this: