LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2013, 09:26 AM   #1
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Rep: Reputation: Disabled
improving a .desktop script for mounting and unmounting ISO files


Hi.
I'm trying to make a script to mount/unmount ISO files using the context menu in KDE.
I searched the internet for a program or a script but some are too old, some don't know about UDF, some don't work on my configuration.

I started from a simple .desktop script and this is what I have now:

Code:
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/octet-stream;
Actions=mountISO;umountISO;

[Desktop Action mountISO]
Name=Mount ISO
Icon=utilities-terminal
Exec=kdesu -c \sh -c 'mkdir -p /media/%n ; if mount -o loop,ro -t auto "%f" /media/%n/; then kdialog --msgbox "%n was succesfuly mounted\nin /media/%n."; else kdialog --sorry "Unable to mount %n\nin /media/%n!"; fi'\

[Desktop Action umountISO]
Name=Unmount ISO
Icon=utilities-terminal
Exec=kdesu -c \sh -c 'if umount "%f"; then rmdir /media/%n; kdialog --msgbox "%n was succesfuly unmounted\nfrom /media/%n."; else kdialog --sorry "Unable to unmount %n\nfrom /media/%n!"; fi'\
What it does: when the user clicks on "Mount ISO", it creates in /media a folder with the name of the ISO file, tries to mount it there and shows a message with the result.
On "Unmount ISO", same thing in reverse.

Maybe the commands are to long, I thought using separate sh scripts, but I don't know how to send the name of the ISO to the code from the script.
That's one problem...

Another problem is with the integration of a code that removes the extension and the dot from the file name (+ the path from the full file name). I found such codes on internet but their problem is that it doesn't explain how to use %n sau %f + they have too many lines which I don't know how to combine them into one.
For example this one:

Code:
$ s=/the/path/foo.txt
$ echo ${s##*/}
foo.txt
$ s=${s##*/}
$ echo ${s%.txt}
foo
$ echo ${s%.*}
foo
or this one:

Code:
filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
A third problem: these mount/unmount commands interfere with the Device notifier from the KDE. For example when I mount a ISO it shows a notification that it couldn't mount but it's adding a new "Loop device" in Delphin each time. When I unmount them it's not erasing the names from Dolphin's interface and I have to do that manually each time.
I can't deactivate the Device notifier because it would create problems with external drives.

Could you please help me, please?
Thank you.

Regards, David

Last edited by DavidBrenner; 05-02-2013 at 09:37 AM.
 
Old 05-02-2013, 06:34 PM   #2
rigor
Member
 
Registered: Sep 2011
Posts: 219

Rep: Reputation: Disabled
DavidBrenner,

Right click on the Device Notifier icon will bring up a menu from which you can navigate to the settings menu for Device Notifier. Under the Automounting selection, you can set "Device Overrides" for specific devices.

AFAIK, the substitutions that allow you to pass in values to the code you already have, work in that context regardless of what command you use. So you should be able to pass values to an external shell script; you shouldn't have to worry about combining everything into a single line.

WRT getting parts of the ISO file name, if you use

Code:
basename  "isoname"  .iso
where "isoname" is to be replaced with the full path and name of the iso file, will give you the basename of the ISO file with the extension removed. For example:

Code:
basename  /home/me/my.iso  .iso
would return just:

Code:
my
So you can get the filename with or without the extension. The dirname command will grab the path to the iso. For example:

Code:
dirname  /home/me/my.iso
returns:

Code:
/home/me

Hope this helps!

Last edited by rigor; 05-02-2013 at 06:48 PM.
 
1 members found this post helpful.
Old 05-02-2013, 11:52 PM   #3
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you.

I knew about the Device notifier settings but, unfortunately, changing them doesn't help me much.
The only way to make the Device notifier not to try to mount the Loop Device is to uncheck "Automatically mount removable media when attached" and setting them manually. The problem is that (on not so few occasions) I will have to mount them manually and this gives me more trouble that I have with the Loop Device.
Anyway, even if I will uncheck it, I will still have to manually erase the "Loop device" link from the left side panel in Dolphin.
A more elegant solution is that, in the script, to deactivate the Device notifier (just the AutoMount will suffice), mount/unmount the ISO and then reactivate it. But I don't know how to do that...

Ok, I knew that I can pass values to an external shell script but my problem is "how to do this"... I searched on Google and I didn't find something helpful...
The idea is that I want to learn how to do this not just copy what others are doing. So if somebody knows a link...

That command using ".iso" will still work if the extension is, for example, ".bin" or ".ISO"? I have seen such files and they are not so rare...

Anyway, I improved the script a little:

Code:
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/octet-stream;
Actions=mountISO;umountISO;

[Desktop Action mountISO]
Name=Mount ISO
Icon=kr_cdrom_mounted
Exec=kdesu -n -c \sh -c 'filename=$(basename "$%f");filename="${filename%.*}";mkdir -p /media/$filename ; if mount -o loop,ro -t auto "%f" /media/$filename/; then kdialog --msgbox "%n was succesfuly mounted\nin /media/$filename."; else kdialog --sorry "Unable to mount %n\nin /media/$filename!"; fi'\

[Desktop Action umountISO]
Name=Unmount ISO
Icon=kr_cdrom_unmounted
Exec=kdesu -n -c \sh -c 'filename=$(basename "$%f");filename="${filename%.*}";if umount "%f"; then rmdir /media/$filename; kdialog --msgbox "%n was succesfuly unmounted\nfrom /media/$filename."; else kdialog --sorry "Unable to unmount %n\nfrom /media/$filename!"; fi'\

Last edited by DavidBrenner; 05-03-2013 at 12:06 AM.
 
Old 05-03-2013, 05:51 AM   #4
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
Sorry for the double post but I'm happy that I found the solution

mount-iso.desktop, copied in /usr/share/kde4/services/ServiceMenus:
Code:
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/octet-stream;
Actions=mountISO;umountISO;

[Desktop Action mountISO]
Name=Mount ISO
Icon=kr_cdrom_mounted
Exec=kdesu -n -c 'sh mountunmountscript.sh "%f" "mount"'

[Desktop Action umountISO]
Name=Unmount ISO
Icon=kr_cdrom_unmounted
Exec=kdesu -n -c 'sh mountunmountscript.sh "%f" "unmount"'
mountunmountscript.sh copied in /bin:
Code:
#!/bin/bash

filename=$(basename "$1")
filenamenoext="${filename%.*}"
if [ "$2" == "mount" ]; then
   if grep -qs "/media/$filenamenoext" /proc/mounts; then
      kdialog --sorry "/media/$filenamenoext is already a mount point!"
   else
      mkdir -p /media/$filenamenoext
      mount -o loop,ro -t auto "$1" "/media/$filenamenoext/"
      case $? in
         0)
            kdialog --yesno "$filename was succesfuly mounted\nin /media/$filenamenoext.\n\nShould I open the file manager...?"
            if [ "$?" = 0 ]; then
               dolphin "/media/$filenamenoext/"
            fi
            ;;
         1)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: incorrect invocation or permissions"
           ;;
         2)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: system error (out of memory, cannot fork, no more loop devices)"
            ;;
         4)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: internal mount bug"
            ;;
         8)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: user interrupt"
            ;;
         16)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: problems writing or locking /etc/mtab"
            ;;
         32)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: mount failure"
            ;;
         64)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: some mount succeeded"
            ;;
         *)
      esac
   fi
else
   if grep -qs "/media/$filenamenoext" /proc/mounts; then
      errno=0
      umount "$1"
      if [ "$?"=="0" ]; then
         rmdir /media/$filenamenoext
         kdialog --msgbox "$filename was succesfuly unmounted\nfrom /media/$filenamenoext."
      else
         case $errno in
            EPERM)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The user is not the super-user!"
               ;;
            ENODEV)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Filesystemtype not configured in the kernel!"
               ;;
            ENOTBLK)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source is not a block device (and a device was required)"
               ;;
            EBUSY)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source is already mounted.\nOr, it cannot be remounted read-only, because it still holds files open for writing.\nOr, it cannot be mounted on target because target is still busy (it is the working directory of some task, the mount point of another device, has open files, etc.).\nOr, it could not be unmounted because it is busy."
               ;;
            EINVAL)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source had an invalid superblock.\nOr, a remount was attempted, while source was not already mounted on target.\nOr, a move was attempted, while source was not a mount point, or was '/'.\nOr, an umount was attempted, while target was not a mount point."
               ;;            
            ENOTDIR)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The second argument, or a prefix of the first argument, is not a directory"
               ;;
            EFAULT)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: One of the pointer arguments points outside the user address space"
               ;;
            ENOMEM)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The kernel could not allocate a free page to copy filenames or data into."
               ;;
            ENAMETOOLONG)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A pathname was longer than MAXPATHLEN."
               ;;
            ENOENT)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A pathname was empty or had a nonexistent component"
               ;;
            ELOOP)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Too many link encountered during pathname resolution.\nOr, a move was attempted, while target is a descendant of source."
               ;;
            EACCES)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A component of a path was not searchable.\nOr, mounting a read-only filesystem was attempted without giving the MS_RDONLY flag.\nOr, the block device Source is located on a filesystem mounted with the MS_NODEV option"
               ;;
            ENXIO)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The major number of the block device source is out of range"
               ;;
            EMFILE)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: (In case no block device is required:) Table of dummy devices is full"
               ;;            
         *)      
         esac
      fi
   else
      kdialog --sorry "/media/$filenamenoext is not a mount point!"
   fi
fi
What do you think, is it good or it needs some improvements...?
 
Old 05-03-2013, 06:04 AM   #5
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 1,973

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
This little program will let you mount and edit iso/udf files (on the fly) as an unprivileged user:

http://www.littlesvr.ca/isomaster/

And it's only 166kb.
 
Old 05-03-2013, 06:48 AM   #6
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you.

But I see at its page:
Quote:
What formats are supported?

ISO Master can read .ISO files (ISO9660, Joliet, RockRidge, and El Torito), most .NRG files, and some single-track .MDF files; it can save only as .ISO.
So it doesn't say about UDF.

Anyway I tried to compile it but it shows error (something about gtk). I looked in Apper, there are many with *gtk* so I'm not sure what to install...
 
Old 05-03-2013, 04:26 PM   #7
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
Well, after searching and installing numerous *gtk* I finally managed to compile and install iso master.
But, when I test it - (not exactly a) surprise: it can't load well UDF iso files...
 
Old 05-05-2013, 07:08 PM   #8
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 1,973

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Isomaster hasn't failed me yet.

You should be able to mount any UDF/ISO file as 'data' (i.e. ISO9660). UDF is predominantly used for movies. Data storage is normally in ISO9660 format... Or at least, this has been my experience.

But I guess it depends upon what you're trying to do... So the best place to start is with this question: What are you trying to do? Why do you need to mount UDF files?
 
Old 05-05-2013, 10:05 PM   #9
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
Well I have met UDF iso files which were correctly mounted ONLY with "/mount auto/udf" command.
I have one now but it's too big to sent so you can test it (over 2 GB). And it's not a movie, it's a boot DVD...

Why do I need to mount UDF iso files? Well, I don't need just UDF but ANY iso file. I use my USB HDD to boot the content of bootable isos. So I need a tool for mounting any kind of iso (so I can extract the content). So far only "/mount" worked fine (for any iso).

Last edited by DavidBrenner; 05-05-2013 at 10:06 PM.
 
Old 05-13-2013, 01:23 AM   #10
DavidBrenner
LQ Newbie
 
Registered: Apr 2013
Distribution: Fedora 18 686
Posts: 18

Original Poster
Rep: Reputation: Disabled
A few small improvements to mountunmountscript.sh:

Code:
#!/bin/bash

filename=$(basename "$1")
filenamenoext="${filename%.*}"
if [ "$2" == "mount" ]; then
   if grep -qs "/media/$filenamenoext" /proc/mounts; then
      kdialog --sorry "$filename is already mounted!"
   else
      mkdir -p /media/$filenamenoext
      mount -o loop,ro -t auto "$1" "/media/$filenamenoext/"
      case $? in
         0)
            kdialog --yesno "$filename was succesfuly mounted\nin /media/$filenamenoext.\n\nShould I open the file manager...?"
            if [ "$?" = 0 ]; then
               dolphin "/media/$filenamenoext/"
            fi
            ;;
         1)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: incorrect invocation or permissions"
            rmdir /media/$filenamenoext
           ;;
         2)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: system error (out of memory, cannot fork, no more loop devices)"
            rmdir /media/$filenamenoext
            ;;
         4)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: internal mount bug"
            rmdir /media/$filenamenoext
            ;;
         8)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: user interrupt"
            rmdir /media/$filenamenoext
            ;;
         16)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: problems writing or locking /etc/mtab"
            rmdir /media/$filenamenoext
            ;;
         32)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: mount failure"
            rmdir /media/$filenamenoext
            ;;
         64)
            kdialog --sorry "Unable to mount $filename\nin /media/$filenamenoext!\n\nError: some mount succeeded"
            rmdir /media/$filenamenoext
            ;;
         *)
      esac
   fi
else
   if grep -qs "/media/$filenamenoext" /proc/mounts; then
      errno=0
      umount -d $1
      if [ "$?"=="0" ]; then
         rmdir /media/$filenamenoext
         kdialog --msgbox "$filename was succesfuly unmounted\nfrom /media/$filenamenoext."
      else
         case $errno in
            EPERM)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The user is not the super-user!"
               ;;
            ENODEV)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Filesystemtype not configured in the kernel!"
               ;;
            ENOTBLK)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source is not a block device (and a device was required)"
               ;;
            EBUSY)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source is already mounted.\nOr, it cannot be remounted read-only, because it still holds files open for writing.\nOr, it cannot be mounted on target because target is still busy (it is the working directory of some task, the mount point of another device, has open files, etc.).\nOr, it could not be unmounted because it is busy."
               ;;
            EINVAL)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Source had an invalid superblock.\nOr, a remount was attempted, while source was not already mounted on target.\nOr, a move was attempted, while source was not a mount point, or was '/'.\nOr, an umount was attempted, while target was not a mount point."
               ;;
            ENOTDIR)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The second argument, or a prefix of the first argument, is not a directory"
               ;;
            EFAULT)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: One of the pointer arguments points outside the user address space"
               ;;
            ENOMEM)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The kernel could not allocate a free page to copy filenames or data into."
               ;;
            ENAMETOOLONG)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A pathname was longer than MAXPATHLEN."
               ;;
            ENOENT)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A pathname was empty or had a nonexistent component"
               ;;
            ELOOP)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: Too many link encountered during pathname resolution.\nOr, a move was attempted, while target is a descendant of source."
               ;;
            EACCES)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: A component of a path was not searchable.\nOr, mounting a read-only filesystem was attempted without giving the MS_RDONLY flag.\nOr, the block device Source is located on a filesystem mounted with the MS_NODEV option"
               ;;
            ENXIO)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: The major number of the block device source is out of range"
               ;;
            EMFILE)
               kdialog --sorry "Unable to unmount $filename\nfrom /media/$filenamenoext!\n\nError: (In case no block device is required:) Table of dummy devices is full"
               ;;
         *)
         esac
      fi
   else
      kdialog --sorry "$filename is not mounted!"
   fi
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mounting iso files dmellott45 Linux - Newbie 4 08-25-2012 01:26 PM
When mounting cd-image files the ISO unpacks and iso-inates it self Hevithan Linux - Newbie 6 05-04-2012 09:14 AM
LXer: Mounting ISO Images With Furius ISO Mount On Ubuntu 8.04 Desktop LXer Syndicated Linux News 0 09-26-2008 04:30 PM
mounting iso files on linux minm Linux - Newbie 26 01-16-2005 10:41 PM
problems mounting and unmounting iso images. Snerkel Linux - Software 5 08-21-2004 06:57 PM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration