Higher Logic notes on screwing with tech…


Linux USB drive automatic mount unmount management script

Hi All,

Just a little something I wrote to make my travelling life easier. I wrote this 3am London time while jetlagged... don't blame me :)

Preface, I run a barebones Linux GUI (blackbox). I got sick of constantly reading dmesg/blkid and manually mounting the USB drives. Here are two python scripts to make my life simple.

usbon.py - just mounts whatever USB you have attached under /mnt/usb_label_name.
usboff.py - just safely umounts the USB and cleans up the directory for you.

Use as you see fit, just give me some credit :)

I'll put the raw files on this page once I find out how to get wordpress to do it :(


#Author: Mark Liu
#Version: 0.1
#Date: 19 August 2010

# good old imports
import commands, re, os

# global variables
total_devices = []
mount_base = '/mnt/'

# class definition to make lives a bit easier
class MountableDevices:
"""Simple Object to represent system mountable disk"""
def __init__(self, dev_name, dev_UUID, dev_label, dev_type):
self.dev_name = dev_name
self.dev_UUID = dev_UUID
self.dev_label = dev_label
self.dev_type = dev_type
self.dev_mount = False

def already_mounted(self):
self.dev_mount = True

def details(self):
print 'Device Name: %s' % (self.dev_name)
print 'Device UUID: %s' % (self.dev_UUID)
print 'Device Label: %s' % (self.dev_label)
print 'Device Type: %s' % (self.dev_type)
print 'Device Mounted?: %s' % (self.dev_mount)
print ''

# Functions to help with process blkid outputs
def process_blkid_output(device_blk_output):
global total_devices
dev_LABEL, dev_UUID, dev_TYPE = '_not_set', 'N/A', 'N/A'
dev_NAME = device_blk_output[0][:-1]

for item in device_blk_output:
if re.match('UUID', item): dev_UUID = re.search('\".*\"', item).group(0).strip('\"'); continue
elif re.match('TYPE', item): dev_TYPE = re.search('\".*\"', item).group(0).strip('\"'); continue
elif re.match('LABEL', item): dev_LABEL = re.search('\".*\"', item).group(0).strip('\"'); continue

total_devices.append(MountableDevices(dev_NAME, dev_UUID, dev_LABEL, dev_TYPE))

# Functions to help with process mount outputs
def process_mount_output(device_blk_output):
global total_devices
dev_NAME = device_blk_output[0]
if dev_NAME == 'none' or dev_NAME == 'proc' or dev_NAME == 'binfmt_misc' or dev_NAME == 'gvfs-fuse-daemon':
for device in total_devices:
if dev_NAME == device.dev_name:

# Do some mounting Prep work
os_commands = ['blkid', 'mount']
process_system_output = {'blkid': process_blkid_output, 'mount': process_mount_output}

# Processing System information like blkid and mounts`
for operation in os_commands:
raw_outputs = commands.getstatusoutput('sudo ' + operation)
if raw_outputs[0] == 0:
#split output via new line
outputs = raw_outputs[1].split('\n')
for output in outputs:
clean_blk_output = output.strip().split()
print 'sudo %s... failed' % (operation)

# Main section compare and mount any desired devices
for device in total_devices:
if device.dev_mount == True:
print 'Ignoring device: %s (already mounted)' % (device.dev_name)
elif device.dev_type == 'swap':
print 'Ignoring device: %s (swap)' % (device.dev_name)
mount_dir = mount_base + device.dev_label
mkdir = 'sudo mkdir %s' % (mount_dir)
mount = 'sudo mount -U %s %s' % (device.dev_UUID, mount_dir)
print 'Mounting device: %s (%s)' % (device.dev_name, mount_dir)
if not os.path.exists(mount_dir):
result = commands.getstatusoutput(mkdir)
if result[0] != 0:
print 'dir creation failed, aborting USB mount...'
result = commands.getstatusoutput(mount)
print 'unclean mount directory, please clean up...'


# Author: Mark Liu
# Version: 1.0
# Date: 19 Aug 2010

# Imports
import dircache, commands

# Definitions and prepwork
mount_base = '/mnt/'
mounts = dircache.listdir(mount_base)

# Umount, then safely delete directories
for mount in mounts:
mount_dir = mount_base + mount
result = commands.getstatusoutput('sudo umount %s' % (mount_dir))
result = commands.getstatusoutput('sudo umount %s' % (mount_dir))
if result[1] == 'umount: %s: not mounted' % (mount_dir):
commands.getstatusoutput('sudo rm -r %s' % (mount_dir))

Filed under: Development 2 Comments