Mount and unmount USB drives with simple commands

Automount doesn't work reliably for me, partly because I don't use a big bloated desktop environment.  However, I have a bunch of USB disks and flash drives, each with a specific purpose, that I want to mount, use, and unmount with ease.  Since I can't make it happen automatically, I made it happen semi-automatically.

I wrote a shell script that I use for the target of my WindowMaker menu commands, one to mount a specific drive and one to unmount it, and show me any errors that might happen.  Usually, it's because I plugged in the wrong drive or because I already unmounted the drive and forgot it was safe to unplug.

#!/bin/sh # # xmount # Mount or unmount a filesystem defined in /etc/fstab and display any # error messages generated in a dialog box. Useful for one-click # buttons and customized desktop pop-up menus in not-so-popular desktop # environments. # # Author: Ariel Millennium Thornton # Copyright: 2016. License is BSD 3-clause at end of file. # # Requires: Xdialog # # Prerequisite: # # Configure a filesystem for mounting/unmounting by the user, such as a # USB disk or flash drive. For Linux, add a line to /etc/fstab like: # # UUID=1234-ABCD /disk/me/my-flashdrive vfat user,noauto 0 0 # # where "1234-ABCD" is the drive's UUID listed in /dev/disk/by-uuid and # where "/disk/me/my-flashdrive" is what you want the drive's mountpoint # to be. (And assuming a DOS/Windows FAT filesystem.) # # For FreeBSD, add a line to /etc/fstab like: # # /dev/diskid/[string]s1 /disk/me/my-flashdrive msdosfs rw,noauto,-l 0 0 # # where "/dev/diskid/[string]s1" is the long string of letters and # numbers in /dev/diskid that starts with "DISK-" and ends with "s1" (or # "s2" for the second partition/slice of a multi-partition USB disk) and # where "/disk/me/my-flashdrive" is what you want the drive's mountpoint # to be. (And likewise assuming a DOS/Windows FAT filesystem.) # # The mountpoint directory must exist already, and in FreeBSD, it must # be owned by the user who will mount the USB disk. # # Once configured, the following commands should mount and unmount the # filesystem (with "/disk/me/my-flashdrive" replaced with your actual # mountpoint): # # mount /disk/me/my-flashdrive # umount /disk/me/my-flashdrive # # Without needing to be root, without needing to specify the device, and # without needing to pass any other options at all. # # Usage: # # To mount the defined mountpoint /disk/me/my-flashdrive: # # xmount m /disk/me/my-flashdrive # # To unmount the filesystem mounted at /disk/me/my-flashdrive: # # xmount u /disk/me/my-flashdrive # # Silence GTK and related log messages. XDIALOG_NO_GMSGS=1 gotime=1 # Is it go time? 1 for go, 0 for abort. # Program usage message, in case we need to abort. u="Usage: xmount { m | u } mountpoint" # If we need to abort, $t is also the reason title and $m is also the # reason message for the dialog box. # Do we have a mountpoint? if [ -z "$2" ] then t="Not enough parameters" m="Where is the mountpoint?\n" gotime=0 fi # Do we even have a mount/unmount flag? if [ -z "$1" ] then t="Not enough parameters" m="Am I mounting or unmounting?\n" gotime=0 fi # Is the mount/unmount flag actually "m" or "u"? if [ $1 != "m" -a $1 != "u" ] then t="First parameter is wrong" m="The first parameter must be 'm' or 'u'\nfor mount or unmount.\n" gotime=0 fi # If we need to abort, show the dialog box explaining what went wrong, # then quit with an error. if [ $gotime -eq 0 ] then Xdialog --title "xmount: $t" \ --msgbox "$m\n$u" \ 8 40 exit 1 fi # Mount the filesystem and quit successfully. if [ "$1" = "m" ] then message=$( mount $2 2>&1 ) # # If the mount command was noisy (such as the USB drive not inserted # or it's already mounted), show its output. if [ ! -z "${message}" ] then echo "${message}" | \ Xdialog --title "xmount: mounting $2" \ --no-cancel \ --textbox - 8 40 fi exit 0 fi # Unmount the filesystem and quit successfully. if [ "$1" = "u" ] then message=$( umount $2 2>&1 ) # # If the umount command was noisy (such as being busy or not even # mounted to begin with), show its output. if [ ! -z "${message}" ] then echo "${message}" | \ Xdialog --title "xmount: unmounting $2" \ --no-cancel \ --textbox - 8 40 fi exit 0 fi # # Copyright (C) 2016 Ariel Millennium Thornton. All rights reserved. # # Developed by: Ariel Millennium Thornton # # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal with the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimers. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimers in # the documenation and/or other materials provided with the # distribution. # # 3. The name of Ariel Millennium Thornton may not be used to endorse # or promote products derived from this Software without specific # prior written permission. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #

Posted because I think it may be useful to more people than just me.  --ArielMT

Edit: Slightly mangled because "<" and ">" surrounding a word confuses the PlainText card editor.  --ArielMT

Edit: I forgot a very important comment in the script: If you set up a removable disk in FreeBSD and forget the "noauto" option, it'll halt during reboot because it can't mount the removed USB drive.  Yes, I was reminded the hard way; oops.  --ArielMT




2110 medium
Full list of journal entries