Pm.sh

From Manjaro Linux
Revision as of 06:19, 5 March 2018 by Handy (Talk | contribs)

Jump to: navigation, search

Why & what is happening here

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

All without the need for su/sudo. That sub-directory is the name that is input for the partition - so if you input sdc1 then pmount creates sdc1 directory in /media like /media/sdc1 . (I'm sure there was a simpler way to say that!)

So using pmount, I've cobbled together a bash script that makes good use of a line of python that my buddy Joshua, from days gone by in Manjaro, made for me to use in another script (mhwd-kern.sh [1], 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), you can remove the .sh & beyond that you can name it whatever you want!


What's it do

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 in this script.

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.


Summation

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

With a name like pm.sh (as mentioned, you could drop the .sh) you don't even have to make an alias for it! ;) After making the script executable, just drop the file in your /usr/bin or in your ~/ .

I run pm.sh from a button in the Worker [2] 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'll make another little script that could be run as an alias that puts the command pumount/media/sd with a live cursor next to the "d" . When I do I'll add it to the bottom of this post.


The pm.sh script

To use this script you need to install pmount & you need to create (if it doesn't already exist) the /media directory. Copy the script to your text editor, save it in ~/home (or /usr/bin if you like). Make it executable or it won't run.

Then just call it by name like so at the terminal prompt:

./pm.sh


#!/bin/bash
  1. !/bin/bash
    1. pm.sh a script by handy. ;)
    2. This file is an attempt to make it a bit quicker to manually mount
    3. external storage devices (usually USB these days).
    4. You use the "pumount /media/<device.name> command to unmount them -
    5. I have a button setup in Worker to do just that. This script could
    6. be modified to quickly run pumount the same as it runs pmount &
    7. you just have to add the two characters & hit Return to unmount device.
    8. I don't need to as Worker makes the pumount happen with a button. :)
    9. There is separate script called pum.sh that uses pumount to undo
    10. what this script does = unmounts the pmounted /media/sd??
#-----------------------------------------------------------
# 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]))' "$@" 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


The pum.sh (pumount/unmounting) script

The following script partners the one above in that it does the opposite in that pumount, unmounts what pmount mounted. After which it deletes the /media/sd?? directory that pmount created.

All very tidy.

Here's the script:


#!/bin/bash
## pum.sh a script by handy. ;)
## This script is a partner to pm.sh in that pumount/unmounts 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]))' "$@" 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

Support

Following is a link to this page's forum counterpart where you can post any related feedback: [3]