Here's a python script that will gather all mounted USB partitions, and perform udisksctl unmount -b <DEV>
on each one of them. The standard rules for making this a working shortcut apply: ensure that the script is executable and give full path to script as command.
As requested only errors will be displayed in GUI dialog, other output won't be shown.
#!/usr/bin/env python
import os
import subprocess
import sys
def run_cmd(cmdlist):
""" utility: reusable function for running external commands """
try:
stdout = subprocess.check_output(cmdlist,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
# GtkDialog error should be ignored.
if not "GtkDialog mapped without a transient parent" in cpe.output:
return error_dialog(cpe.output," ".join(cmdlist))
def error_dialog(err_msg,called_cmd):
""" Displays graphical error dialog and exits the script"""
subprocess.call(['zenity','--error','--text',err_msg,'--title',called_cmd])
sys.exit(1)
def find_usb_partitions():
""" Constructs a list of USB partitions """
return tuple( os.path.realpath(os.path.join("/dev/disk/by-path",p))
for p in os.listdir("/dev/disk/by-path")
if 'usb' in p and 'part' in p
)
def find_mounted_devs():
devs=[]
with open('/proc/mounts') as mounts:
for line in mounts:
dev = line.split()[0]
if dev.startswith('/dev/'):
devs.append(dev)
return devs
def main():
parts=find_usb_partitions()
devs=find_mounted_devs()
mounted_parts=tuple(i for i in parts if i in devs)
for part in mounted_parts:
run_cmd(['udisksctl', 'unmount', '-b',part])
run_cmd(['udisksctl', 'power-off', '-b',part])
if __name__ == '__main__': main()
Additional thoughts
While the script works, I find the idea of unmounting from shortcut slightly redundant. Such task requires at least some involvement from a user. If the goal is to be able to unmount without terminal, there are already ways to do that. For instance, if you are a Unity user you already have device icon on the launcher from which you can right click and eject the device. I've written Udisks-Indicator, which you can use to unmount partitions from a graphical indicator (and which uses some of the same ideas I've used in this answer). In either case, there are alternatives already, but I personally would caution against using shortcuts to unmount things. But then again, this is only my personal opinion.