Pm.sh

From Manjaro Linux
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 simple 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!

Beyond that I've added some info on how to make /media the system default location where external media will be mounted & how to remove any empty directories that my remain in /media on boot. This works for Artix OpenRC, you may have to modify the path to the command in Manjaro systemd?


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

Here's the pm.sh script:

#!/bin/bash
## pm.sh a script by handy. ;)
## 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 - 
## I have a button setup in Worker to do just that. This script could
## be modified to quickly run pumount the same as it runs pmount & 
## you just have to add the two characters & hit Return to unmount device.
## I don't need to as Worker makes the pumount happen with a button. :)
## There is separate script called pum.sh that uses pumount to undo
## 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


Set /media as default location for mounting external media

In /etc/udev/rules.d create a file called 99-udisks2.rules with the following contents:

# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

Having done that your system on reboot will no longer use the /run/media/<user>/* to mount external devices, it will instead use /media like it did in the old days.


Delete all empty directories from /media on system shutdown

Then (at least on Artix - Arch without systemd) you can if you choose to, use this path & filename (filename of your choice, just put the .stop extension on it) /etc/local.d/clear-media.stop with the following command which has been well tested & is safe:

#!/bin/bash
## This file's purpose is to delete any empty directories in /media
## as pmount creates them by default (as do I from time to time):

cd /media
find . -type d -empty -delete

That's it, have fun.

P.S. if I'll modify this section if/when I find out that Manjaro needs a different path/command to achieve the same result (clear empty dir's from /media).

Support

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