This is a re-posting. It was originally posted on Feb. 2, 2020, on my personal web page at rawtext.club.
This is yet another wrapper around the mount
command. It
works in Debian Linux on a PC, and in Raspbian on the Raspberry Pi. It
is not better than others around. It is the one that best suits my
needs, that’s all. It is easily customizable. It is in BASH. Thanks to
Rawtext for letting me host this stuff here. Much obliged, Rawtext
folks.
This BASH script mounts a USB removable storage device, like a
pendrive, an external hard drive or a SD card reader. It works for a
user in the sudoers list. The user does not have to run the
mount
command, they don’t have to know the device name
(/dev/sdc1
or similar), they don’t have to specify the
mounting point. Just invoke the script, and it will do it all for you.
It will mount the device by UUID in
/media/user/uuidofdevice
.
The flag -o rw,umask=0000
is the default in my version
of the script, but hey, change it to whatever you like. These settings
work for me.
Some utilities allow mounting removable USB drives in the userland.
Usually those don’t give many privileges, like rw
. Being in
the userland, they don’t require a password. I prefer for the system to
prompt me for my password. It is a trade-off. I’ve worked with those
kinds of utilities, they are good for what they are and what they do.
Mine serves sudoers. I am a sudoer in my personal PC and my personal
Raspberry Pi. And my personal laptop, which also runs Debian.
This is the script:
#!/bin/bash
TEMPFILE=$(mktemp)
CURRENTUSER=$(whoami)
createdirectory () {
if [ ! -d /media/$CURRENTUSER/$1 ]
then
sudo mkdir /media/$CURRENTUSER/$1
sudo chown $CURRENTUSER:$CURRENTUSER /media/$CURRENTUSER/$1
fi
}
# check if drive is already mounted and returns boolean:
mounted () {
MOUNT=$(sudo lsblk -o UUID,MOUNTPOINT | awk -v u="$1" '$1 == u {print $2}')
[[ $MOUNT ]] && return 0 || return 1
}
actualmounting () {
if mounted $1
then
echo $1 "is mounted, moving on"
else echo $1 "is not mounted: mounting now"
createdirectory $1
sudo mount -U $1 /media/$CURRENTUSER/$1 -o rw,umask=0000
fi
}
sudo blkid | sed -n 's/.* UUID=\"\([^\"]*\)\".*/\1/p' > $TEMPFILE
while IFS= read -r line
do
MYTEST=$(sudo blkid -U $line | grep /dev/sd)
[[ $MYTEST ]] && actualmounting $line || continue
done < $TEMPFILE
rm $TEMPFILE
# this line displays the UUID of all devices plugged in:
# sudo blkid | sed -n 's/.* UUID=\"\([^\"]*\)\".*/\1/p'
# this other line does exactly the same but
# does not work on all Linux installations:
# blkid -s UUID -o value
I had to reach a compromise to have the exact same script to work in
a PC and in a Raspberry Pi without a single modification. In a PC, the
local filesystem has to be checked for mounting and skipped, and in a
Raspberry Pi it will only mount devices with the name
/dev/sd*
. Which is the only names a removable USB drive can
have, as far as I know, so you’re pretty much safe at that then.
My script does not detect when the USB drive is plugged in. I don’t like that much automation. I like to have control and give the final green light to a delicate process like mounting shit on my computer.
It would have helped having a function that accepts a UUID as input
and returns a binary yes or no if it is a USB removable device or not.
The command udisks --dump | less
returns that info, but
parsing and greping it proved to be an ordeal. I gave up. I used
yellow-belly workarounds.
As a final reflection, heck, I think it should be a standard in Linux in general, for all system utilities, to return the info in one single way and format. This way, the data returned would be easy to query. XML could be implemented, or CSV, or even SQL. Making it mandatory outputing in SQLite for all Linux core apps would be awesome-sause.
Click like if you liked this post and you have a mouse. Nothing will happen if you do it, just do it if you like to click on likes as is fashionable in this age and era. Email comments to gasconheart@sdf.org.