#!/usr/bin/python # -*- coding: iso-8859-1 -*- # iso_mount - Roger Heathcote - iso_mount@technicalbloke.com 2009 # Released under GPL3 - http://www.gnu.org/licenses/gpl.txt # Primarily for use with nautilus actions on Ubuntu 9.04, takes absolute path to ISO file and mounts as loopback device # Default mountpoint folder specified here... default_mount_folder = "/media" # How to manually configure Nautilus actions... # Make sure file is executable # Create link to file in /usr/local/bin # System/Preferences/Nautilus Actions/Add # Label: Mount # Tooltip: Mount this ISO # Add # Action, Path: gksudo iso_mount # Action, Parameter: %M # Condition: Appear if file matches: *.iso [ ] Match Case i.e. case insensitive # Condition: Only files import os, os.path, sys if False: # Log stdout to file, false by default so = se = open("/var/log/iso_mount.log", 'w', 0) # re-open stdout without buffering sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # redirect stdout and stderr to the log file opened above os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) if os.geteuid() != 0: # Ensure user is root sys.exit( "Only root can do that..." ) source_file_folder, source_file_name = os.path.split( sys.argv[1] ) assert source_file_name, source_file_folder mount_folder_name = default_mount_folder + os.sep + source_file_name if os.path.exists( mount_folder_name ): # If mountpoint already exists... assert( os.path.isdir( mount_folder_name ) ) # Ensure it is a folder if os.listdir (mount_folder_name): # Ensure it is empty sys.exit( "Fail: Mountpoint folder not empty!" ) else: if not os.mkdir( mount_folder_name ): # Create mountpoint... sys.exit( "Failed to create mountpoint:", mount_folder_name ) print "Issuing command:", 'mount -o loop "%s" "%s"' % (sys.argv[1], mount_folder_name) if os.system( 'mount -o loop "%s" "%s"' % (sys.argv[1], mount_folder_name) ): # Attempt mount, fail if err code non zero sys.exit( "Fail: mount command failed" )