To a Windows user is is not intuitive to unmount a removable drive using a file manager. To a Linux user it is more usual, but not always convenient if a file manager it is not permanently running.
A variety of removable (flash and hard disk) drives are used here. Some have multiple partitions. Frequently, multiple drives with multiple partitions are concurrently connected. It can rapidly become tedious to have to adopt root privileges and unmount each individually.
I decided to try and do something about it. The objectives were to produce something that is obvious, convenient and concise to use, and does not require root privileges.
The result is the script shown below. It requires no additional software above that supplied as standard in antiX-M11.
- * It works via the GUI
* It lists only removable drives
* It is independant of the file manager
* It works for an unprivileged user
* It is launched from a button on the taskbar or entry in the menu
* It caters for multiple concurrently connected removable drives
* It simultaneously unmounts each partition on drive(s) specified by the user
* It removes the released mountpoint(s)
Script - unplugdrive.sh
Code: Select all
#!/bin/bash
# unplugdrive.sh
#
# Enables unmounting prior to unplugging removable storage
# Allows simultaneous selection of multiple drives
# Unmounts all mounted partitions on nominated drive(s)
# Removes mountpoints
#
# Requires zenity and pmount to be installed.
# Requires /etc/udev/rules.d/99-usbstorage.unused to be renamed 99-usbstorage
# Store and set the locale value
langbackup=$LANG
LANG=C
# Collect details of each removable device that has at least one mounted partition
discovered=$(pmount|grep /dev/|sort|tr ' ' '_'|cut -d _ -f 1,2,3)
# Create a list of removable devices excluding CD/DVD
for item in $discovered;do
if [[ ! $item = /dev/sr* ]];then
detectedlist="$detectedlist$item"
fi
done
# Create a list of each removable drive, mounted partition and mountpoint
removablelist=""
removablenow=""
for item in $detectedlist;do
removablenow=$(echo $item|cut -c 6-|tr '_' ' ')
removablelist="$removablelist$removablenow\n"
done
# Create a list of each unplugable drive
drivelist=""
drivenow=""
driveprevious=""
position=0
for item in $detectedlist;do
drivenow=$(echo $item|cut -d _ -f 1|cut -c 6-8)
if ["$drivenow" !="$driveprevious" ];then
drivelist="$drivelist $position $drivenow"
driveprevious=$drivenow
fi
position=$(expr $position + 1)
done
# Display a message that no candidate for unmounting was discovered
if [ -z"$drivelist" ];then
zenity --warning --text"A removable drive with a mounted partition was not found.\n\nIt is safe to unplug the drive(s)"
LANG=$langbackup
exit 0
fi
# Display a list from which the drives to be unplugged may be selected
selected=$(zenity --list --width="300" --height="350" --text="The following are currently mounted:\n$removablelist\nChoose the drive(s) to be unplugged\n" --checklist --column="Select" --column="Drives" --separator="" $drivelist)
if [ -z"$selected" ];then
zenity --warning --text="Nothing selected.\nAborting without unmounting."
LANG=$langbackup
exit 1
fi
# Create a list of mountpoints used by the drives selected to be unplugged
mountpointlist=""
mountpointnow=""
for item in $selected;do
mountpointnow=$(pmount|grep $item|cut -d"" -f 3)
mountpointlist="$mountpointlist$mountpointnow"
done
# Create a list summarising what is about to be unmounted
summarylist=""
summarypoint=""
for item in $selected;do
summarypoint=$(pmount|grep $item|cut -d"" -f 1,2,3|cut -c 6-)
summarylist="$summarylist$summarypoint\n"
done
# Obtain confirmation to proceed with unmounting
zenity --question --text="About to unmount:\n$summarylist\nPlease confirm you wish to proceed."
if [ $? ="1" ];then
zenity --warning --text="Nothing has been unmounted.\nAborting as requested with no action taken."
LANG=$langbackup
exit 1
else
# Ensure everything is written to storage then unmount
zenity --progress --text="Data is being written to devices. Please wait..." & pid1="$!"
sync
kill $pid1
for item in $mountpointlist;do
$(pumount $item)
done
fi
# Collect details of each removable device that has at least one mounted partition
postunmount=$(pmount|grep /dev/|sort|cut -d _ -f 1,2,3)
# Collect details of each mountpoint that pumount failed to remove
mountpointerror=""
mountpointerrorlist=""
for item in $mountpointlist;do
mountpointerror="$(echo $postunmount|grep -o $item)"
if [ ! -z $mountpointerror ];then
mountpointerrorlist="$mountpointerrorlist$mountpointerror\n"
fi
done
# Display a message if unmount failed
if [ ! -z"$mountpointerrorlist" ];then
zenity --error --text="Mountpoint removal failed.\n\nA mountpoint remains present at:\n$mountpointerrorlist\nCheck each mountpoint listed before unpluging the drive(s)."
LANG=$langbackup
exit 1
else
# Display a message if unmount successful
zenity --info --text="Unmounted:\n$summarylist\n It is safe to unplug the drive(s)"
LANG=$langbackup
exit 0
fi
Configuration steps for IceWM on AntiX-M11
Required Applications
pmount and zenity are needed. These are both installed by default.
Verify the required applications are installed:
Code: Select all
apt-cache policy pmount zenity
Automatically Mount Upon Plugging-in
An antiX default file that is unused needs to be enabled.
Rename /etc/udev/rules.d/99-usbstorage.unused to /etc/udev/rules.d/99-usbstorage
Create the unplugdrive.sh Executable Script
Ensure line wrapping is switched off in your text editor as it may help to preserve long lines
Copy and paste the script code into a new text file
Save the file as /usr/local/unplugdrive.sh
Make the script executable
Code: Select all
chmod +x /usr/local/bin/unplugdrive.sh
Add an Entry at the Head of the IceWM Menu
Start Menu-->Applications-->Preferences-->antiXCC-->Edit IceWM Settings
Select the Menu tab
Add the following entry at line number 1
Code: Select all
prog"Unplug Drive" /usr/share/icons/gTangish-2.0a1/32x32/actions/remove.png unplugdrive.sh
Add a Button to the IceWM Toolbar
Start Menu-->Applications-->Preferences-->antiXCC-->Edit IceWM Settings
Select the Toolbar tab
Add the following entry in the section: System/settings applications
Code: Select all
prog"Unplug Drive" /usr/share/icons/gTangish-2.0a1/32x32/actions/remove.png unplugdrive.sh
Initiate the Changes in IceWM
Start Menu-->Logout-->Restart IceWM
If needed:
Start Menu-->Desktop-->IceWM-->Update Menu (Auto)
Edits by SamK
- * It no longer finds and reports a mounted CD/DVD as an upluggable drive
- * The icons referred to in the configuration steps now point to icons that are provided as part of the default installation of antiX-M11-Full