Skip to main content
Topic: pm.sh is a bash/python script to semi automate manually mounting external drives (Read 342 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

pm.sh is a bash/python script to semi automate manually mounting external drives

Due to udiskie not working for me anymore for some (at this stage) unknown reason. I created an alternate method for myself to mount/unmount external (USB in my case) drives. This method uses pmount/pumount, a wrapper for mount/umount, that doesn't require root privileges but does need the /media directory to exist. pmount creates its own subdirectory in /media for the mounted partition & pumount removes that subdirectory after it unmounts the partition. All without su/sudo.

So using pmount, I've cobbled together a bash script that makes good use of a line or python that my buddy Joshua, from days gone by in Manjaro, made for me to use in another script (mhwd-kern.sh, that I made in March 2014).

This script (found below) I call pm.sh (I like to use the deprecated .sh just because it makes it easy for me to see straight away that it is a script file that I'm looking at when I see it listed). When run, pm.sh displays all of the partitions attached (internally & externally) to a machine via the lsblk -f command. This makes it relatively simple to identify the device that you want to mount & see its /dev/sd?? (without the /dev/ ) which you need to know to use pmount.

Then pm.sh echo's to the terminal that you use the Ctrl c key combination to terminate the script.

After which there are a few lines of instructions with an example. Followed by the command pmount /dev/sd   with a live cursor sitting next to the "d" awaiting the next 2 characters.

So as you can see, this lot just tries to make the manual mounting of external drives as quick & easy, at least enough for me to be happy to use it to do the job. Which I am, I've deleted udiskie from my machine. :)

With a name like pm.sh (you could drop the .sh) you don't even have to make an alias for it! ;)

I run pm.sh from a button in the Worker file manager, & have another RMB button underneath it that has the function of running pumount on an already highlighted/selected /media/sdc1 (c1 for this example), after which (as mentioned earlier) the partition is unmounted & the sdc1 used in this example is deleted from /media.

I've made another little script called pum.sh that uses pumount to undo what pmount did. It can be found at the bottom of this post.


Code: [Select]
#!/bin/bash
## pm.sh a bash script by handy (with a little python thanks to Joshua). ;)
## This file is an attempt to make it a bit quicker to manually mount
## external storage devices (usually USB these days).
## You use the "pumount /media/<device.name> command to unmount them.
## You must have the /media directory existent for pmount/pumount to work. 
## I have a button setup in Worker to do just that.

#-----------------------------------------------------------
 # A little python FUNCTION - picked up from Joshua follows:
 #----------------------------------------------------------

 function input {
    python2 -c 'import sys,readline;readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]));sys.stderr.write(raw_input(sys.argv[1]))' "[email protected]" 3>&1 1>&2 2>&3
 }

clear # Clear the screen.

echo -e "\e[0;33m"

## The next line lists all of the available filesystems on the system:

lsblk -f
 
 ## Where the action is:

 echo
 echo
 echo -e "Use the \e[0;31mCtrl c\e[0;33m key combination to exit this script."
 echo
 echo -e "\e[0;33mTo mount an external device (usually USB), input the missing"
 echo -e "parts directly after the \e[0;32md\e[0;32m\e[0;33m in the following command."
 echo -e "(Like I have with the red \e[0;31me1\e[0;33m in this example -> \e[0;32mpmount /dev/sd\e[0;31me1 \e[0;33m)\e[0;32m"
 echo
 deviceName=$( input 'pmount /dev/sd')
 echo
 printf "pmount /dev/sd$deviceName\n"
 echo -e "\e[0;31m"
 pmount /dev/sd$deviceName
 echo -e "\e[0m"

 exit 0
 

OK, here is the simply changed script called pum.sh that unmount via pumount what pmount mounted (& then deletes the /media/sd?? directory that pmount created).

Code: [Select]
#!/bin/bash
## pum.sh a script by handy. ;)
## This script is a partner to pm.sh in that it pumounts what the other
## file pmounted. In as quick & easy manner as I can come up with.

#-----------------------------------------------------------
 # A little python FUNCTION - picked up from Joshua follows:
 #----------------------------------------------------------

 function input {
    python2 -c 'import sys,readline;readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]));sys.stderr.write(raw_input(sys.argv[1]))' "[email protected]" 3>&1 1>&2 2>&3
 }

clear # Clear the screen.

echo -e "\e[0;33m"

## The next line lists all of the available filesystems on the system:

lsblk -f
 
 ## Where the action is:

 echo
 echo
 echo -e "Use the \e[0;31mCtrl c\e[0;33m key combination to exit this script."
 echo
 echo -e "\e[0;33mTo \e[0;31mUN\e[0;33mmount an external device previously mounted via, \e[0;31mpmount"
 echo -e "\e[0;33minput the missing parts directly after the \e[0;32md\e[0;32m\e[0;33m in the following command."
 echo -e "(Like I have with the red \e[0;31me1\e[0;33m in this example -> \e[0;32mpumount /dev/sd\e[0;31me1 \e[0;33m)\e[0;32m"
 echo
 deviceName=$( input 'pumount /dev/sd')
 echo
 printf "pumount /dev/sd$deviceName\n"
 echo -e "\e[0;31m"
 pumount /dev/sd$deviceName
 echo -e "\e[0m"

 exit 0