Mount ISO files easilly with KDE 4 ServiceMenus

I have never had fun typing the mount command manually. It is annoying, sometimes runs into problems, and sometimes fails altogether. I also rather like clicking in this case, just like I would have done in DAEMON Tools on Windows or similar.

I also use KDE 4 now, and the Service Menus system has changed only slightly. The .desktop files are now located in a number of places:

  • /opt/kde4/share/services/ServiceMenus/
  • /usr/share/services/ServiceMenus/
  • /home/aaron/.kde4/share/kde4/services/ServiceMenus/

I am choosing the 3rd because you do not have to be root to places files there. On Gentoo (what I use), KDE 4 is likely to not scan /opt. You can check and set paths to scan with this command: kde4-config --path services.

Before judging this script, note I am a Python newbie. Literally a few minutes of skimming through the documentation and I got started. Regardless, I feel much better about this script than the previous one I had written for Bash.

You will need to place this script, named mountiso somewhere in your $PATH such as /usr/local/bin, etc.

#!/usr/bin/python
# vim ts=2

import subprocess
import sys

if sys.argv[1]:
	try:
		subprocess.check_call(['sudo', 'mount', '-t', 'iso9660', '-o', 'loop', sys.argv[1], '/mnt/cdrom'])
	except subprocess.CalledProcessError:
		try:
			print 'Error occurred mounting as ISO9660. Trying UDF...'
			subprocess.check_call(['sudo', 'mount', '-t', 'udf', '-o', 'loop', sys.argv[1], '/mnt/cdrom'])
		except subprocess.CalledProcessError:
			try:
				print 'Error occurred mounting as UDF. Trying HFS+...'
				subprocess.check_call(['sudo', 'mount', '-t', 'hfsplus', '-o', 'loop', sys.argv[1], '/mnt/cdrom'])
			except subprocess.CalledProcessError:
				try:
					print 'Error occured mounting as HFS+. Trying HFS...'
					subprocess.check_call(['sudo', 'mount', '-t', 'hfs', '-o', 'loop', sys.argv[1], '/mnt/cdrom'])
				except subprocess.CalledProcessError:
					print 'Unable to mount ISO. Check conditions.'
					sys.exit(1)
	else:
		raw_input('Press RETN to umount the ISO.')

	try:
		subprocess.check_call(['sudo', 'umount', '/mnt/cdrom'])
	except CalledProcessError:
		print 'Error unmounting normally. Forcing umount...'
		try:
			subprocess.check_call(['sudo', 'umount', '-l', '/mnt/cdrom'])
		except subprocess.CalledProcessError:
			print 'Unable to unmount ISO. Check conditions.'
			sys.exit(1)
else:
	print 'Usage: '+sys.argv[0]+' ISOFILE'
	print 'This will try to mount as ISO9660, UDF, HFS+, and HFS in that order.'

You may want to change /mnt/cdrom to wherever you'd like to mount. I chose a static directory because I use /mnt/cdrom as Wine's CD-ROM directory, so you may want to choose that as well. Also, you will need sudo for this script to work or you will only see the message 'Unable to mount ISO. Check conditions.'. Most distros have this available for installing, but not all distros have it installed by default. I highly recommend it regardless of distro, but you must be careful who you grant sudo access to.

Remember to make this script executable (chmod a+x path/to/script).

Next, you will need this .desktop entry file in a place where KDE 4 will scan.

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/x-cd-image;application/x-apple-diskimage
Actions=mount

[Desktop Action mount]
Name=Mount
Icon=background
Exec=xterm -e /home/tatsh/usr/bin/mountiso %f

You will need xterm of course, something standard on all distros when you install X. You might notice this menu entry will load for a DMG file as well, however it will NOT work with a DMG file unless it is uncompressed. Most of what you can download for DMG files are compressed with a variant of the Deflate algorithm.

Right-click an ISO in Dolphin or Konqueror, go to Actions, and cilck 'Mount':

Mount an ISO with Konqueror

You will see this when you click 'Mount':

Xterm waits for input

Do not close the xterm window while the ISO is mounted or you will have to unmount yourself, probably in a command line. Use this command: umount /mnt/cdrom (as root or with sudo. If you cannot unmount that way, try using the -l option, which will force an unmount.

Upon pressing RETN (Enter or Return), you may need to type your password again. sudo by default has a 5 minute limit, so if it has been over 5 minutes this is normal behaviour.

You can make it possible to not require a password but I would not recommend this for any users except yourself. For more information, read sudo's documentation.

Next thing to do is make this launch the contents of Autorun.inf (the open= line) with Wine. Heh.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Syntax highlight code surrounded by the {syntaxhighlighter OPTIONS}...{/syntaxhighlighter} tags.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.