Shell Script to Unmount Multiple Hard Drives?

I know that within Kodi it is possible to unmount hard drives one at a time in the file Manager.

File Manager - Name of Drive - Context Menu - Unmount

I usually have about five or six external hard drives connected at one time so I would like to be able to run a shell script to unmount all the external drives that are connected. That way I can simply run this one script, either via an option on the main menu(using a skin that supports this) or I could keymap this script to a button press on a remote.

Is this possible?

I tried mapping Filemanger window to one button and Context menu to another like in the link and it works but it still needs to be repeated for each hard drive

If all your drives are automounting under /media then you can do it with a single shell command:

umount /media/*

From within Kodi you might need to put a sudo in front of that since Kodi does not run as root.

1 Like

Thanks.

I didn’t think to add the sudo because you can unmount drives from the file manager. Will test it out later

Tried it out and it didnt work.

Created two scripts, Unmount.All.py and Unmount.All.Sudo.py
Unmount.All.py = umount /media/*
Unmount.All.Sudo = sudo umount /media/*

Tried RunScript(“/home/osmc/scripts/Unmount.All.py”) and RunScript(“/home/osmc/scripts/Unmount.All.Sudo.py”) While Kodi found both of them, they both said “Error Script Failed”

To test that it was actually finding the scripts I changed the path and as expected it did nothing, changing it back to the correct path results in the script error.

Googled and saw something about some scripts being run with XBMC.RunScript(“script location”) but that didn’t work either.

Anyone know what else could be wrong?

for disk in $(ls /dev/sd*); do grep -q $disk /proc/mounts; if [ $? == 0]; then sudo umount $disk; fi; done

Sam

1 Like

No need for all that, umount /media/* works just fine, I use it all the time.

1 Like

Make sure your script is marked as executable and has a hash bang line to load Python with the correct path.

Also make sure you use the full path to umount in your script. I checked and sudo is not needed.

If you’re not doing anything else in your script you should just run the mount command directly from runscript and make sure you have the full path to umount.

1 Like

You’re right, because mount is done as osmc user.

S

1 Like

It’s possible the wild card expansion won’t work without an explicit shell, so it might need to be:

/bin/bash -c umount /media/*

If run directly from the runscript command.

1 Like

This is a bit beyond my current level of experience with Linux :worried:

I can try this out, just so I don’t forget to do anything it will be :

TheNameOfMyScript.py full contents of which are /bin/bash -c umount /media/*

Copy this script into the scripts folder I created in osmc folder(which folder doesnt really matter)

Point Kodi Skin and/or Remote Button in “keymap.xml” to
RunScript(“/home/osmc/scripts/TheNameOfMyScript.py”)

Why don’t you just try:

RunScript("/bin/bash -c umount /media/*")
1 Like

On a fresh RC3 installation, I tried this and it did nothing.

Also tried it on another skin and it still did nothing. When run as an actual script, it gives the error message.

Sorry, missed out some single quotes, and it’s probably a good idea to include the path to umount as well. Try:

RunScript("/bin/bash -c '/bin/umount /media/*'")

What error message ?

1 Like

I’ve looked into this a bit further and RunScript() is only used for launching python scripts. To run shell commands directly you should use System.Exec() but I have been unable to get this working for some reason. (And can’t really spend any more time on it)

So I would suggest you go back to a python script to run the unmount command (probably using os.system() in python) and test that your python script works from the command line, once you have a script that works from the command line call it with RunScript() and it should work. In theory. :wink:

1 Like

[quote=“DBMandrake, post:14, topic:4564, full:true”]
I’ve looked into this a bit further and RunScript() is only used for launching python scripts. To run shell commands directly you should use System.Exec() but I have been unable to get this working for some reason. (And can’t really spend any more time on it)[/quote]

That is totally understandable. I am sure there must be more pressing issues with the OSMC release at the moment but thanks for taking the time to try to help me out.

I actually thought it would be a quick thing since I had used the RunScript within a Windows environment and it was pretty easy to use but I am new to Linux.

It is more of a Kodi thing anyway so I’ll just post a topic on that forum and see if anyone can help me be lazier which was my only goal with this :grinning:

I was trying to do the same thing, I don’t know if OP finally got it, but I’ll post it if anyone arrives here looking for the answer (like I did).
I created a Python script that only contains

from subprocess import call
call(["sudo", "umount", "/mnt/your_disk"])

then you just have to call it from the Menu Builder with

RunScript("/home/osmc/scripts/unmount.py")

or the path from wherever you saved it.

1 Like

[quote=“e_viesca, post:16, topic:4564, full:true”]I created a Python script that only contains

from subprocess import call
call(["sudo", "umount", "/mnt/your_disk"])

[/quote]

I tried this and it does nothing? Doesn’t give me the script error like before but it still doesn’t remove the drives. Have you got it to work and does it matter how many drives you have connected?

Sorry, I only had one drive and I was mounting manually to /mnt so I put the path directly to it. My Pi is unusable at the moment so I can’t try anything right know, if your drives are mounting to /media you could try to put an asterisk as wildcard in the command, it would be something like this:

from subprocess import call
call(["sudo", "umount", "/media/*"])

Best of luck!

1 Like

Thanks alot, it was exactly that. Can’t get the wildcard to remove all the drives at the same time to work just yet but a python script with this:

from subprocess import call
call([“sudo”, “umount”, “/media/label_of_your_drive*”])

And run like this

RunScript(“Full_Path_And_Name_Of_This_Script.py”)

Will remove the drive, just like using the context menu it will also brings up a notification to say the drive has been successfully removed.

Pretty sure I can just get it to repeat the process for each drive if the wildcard expression doesn’t work but haven’t had a chance to test it yet. Will update this post once I have got it working.

For the wild card to work the command has to be run in a shell as the shell does the wildcard expansion. Check the docs for the Python subprocess.call function - there is an option to run in a shell I believe.