Ubuntu: “Eject usb-drive” action for pcmanfm


I wonder if there's a way to create custom pcmanfm action for ejecting usb-drive in lubuntu? I've tried to do this with udisksctl but the --object-path option is giving me this error:

(udisksctl unmount:10926): GLib-GIO-CRITICAL **: g_dbus_object_manager_get_object: assertion 'g_variant_is_object_path (object_path)' failed  Error looking up object with path /media/user/Flash-Drive  

And there's no difference of MIME-types between simple folder and mounted drive so "Eject" action is shown on all folders.


Well, it's seems I've found some kind of solution. This is will unmount and power-off your usb-drive

[Desktop Entry]  Type=Action  Tooltip=Eject selected flash drive  Name=Eject flash drive  Profiles=profile-zero;  Icon=media-eject    [X-Action-Profile profile-zero]  MimeTypes=inode/directory;  Exec=gksudo eject %f  Name=Default profile  

Also you can use it without gksudo prompt after adding setuid to /usr/bin/eject:

[Desktop Entry]  Type=Action  Tooltip=Eject selected flash drive  Name=Eject flash drive  Profiles=profile-zero;  Icon=media-eject    [X-Action-Profile profile-zero]  MimeTypes=inode/directory;  Exec=eject %f  Name=Default profile  

But this action will be shown on all folders. I still have no idea how to make a difference between usb-drive directory and flash-drive directory. I've seen specification Basenames for .desktop entry which specifies selected file-name (used in empty trash action) so may be there some way to specify file-placement?

Update 2

Okay, I've found appropriate MIME-type according to MIME-info Database - inode/mount-point, but now action isn't shown on any directory at all. What may cause a problem?

[Desktop Entry]  Type=Action  Tooltip=Eject selected drive  Name=Eject drive  Profiles=profile-zero;  Icon=media-eject    [X-Action-Profile profile-zero]  MimeTypes=inode/mount-point  Exec=eject %f  Name=Default profile  


My suggestion is to use the umount command.

With the fdisk -l command and inserting different usb-drives determined as identified, say they are /dev/sdd1

Before creating the actions (file.desktop), create its directory:

mkdir -p ~/.local/share/file-manager/actions  

Create and edit the action file (umount.desktop) by using these terminal commands one at a time:

touch ~/.local/share/file-manager/actions/umount.desktop  nano ~/.local/share/file-manager/actions/umount.desktop  

Paste the below content in umount.desktop:

[Desktop Entry]  Type=Action  Name=Eject Usb-Drive  Icon=dialog-password  Profiles=profile-zero;    [X-Action-Profile profile-zero]  Exec=umount /dev/sdd1  Name=Default profile  


I've made more progress on this, but solution is still not ideal:
I am using Lubuntu 16.04 with PCManFM 1.24. For USB pen drives I get "Eject Removable Media", but for USB disk drives, I do not get "Safely Remove Drive" as you do in Nautilus (I only get Unmount).
First issue is to get right-click context on devices where, unfortunately, it seems actions are not supported for devices in the left pane in PCManFM, (or Nautilus, but actions work in left pane in Nemo, but actions are implemented differently in Nemo). I have a work-a-round to this by showing "Devices" icon in left pane by ticking "Devices" under "Show in Places" in Edit-Preferences-Layout, where you can then right click on the device in the right pane as shown in attached link: pcmanfm devices right click image

Second issue is script to safely remove drive: The argument passed to action script in the above example is: computer:///ST1000LM010-9YH146.drive (or just ST1000LM010-9YH146 with %w) which is not ideal - I don't believe it evenly uniquely identifies drive as it does not include the serial number. Below is output from "udisksctl status":

$ udisksctl status  MODEL                     REVISION  SERIAL               DEVICE  --------------------------------------------------------------------------  WDC WD5000BEVT-22ZAT0     01.01A01  WD-WX70A9903180      sda  Kingston DataTraveler G3  1.00      001CC0EC31D0F081F6071284 sdb  ST1000LM010-9YH146        CC9F      W2M076TD             sdc   

So my script matches the model, so assumes you do not have 2 identical devices plugged in to pick up the device. To implement script create file ~/.local/share/filemanager/actions/eject.desktop

[Desktop Entry]  Type=Action  Name=Safely Remove Drive  Icon=drive-harddisk-usb  Profiles=profile-zero;    [X-Action-Profile profile-zero]  MimeTypes=inode/mount-point  Exec=/home/mike/.local/share/file-manager/actions/eject.sh %w   Name=Default profile  

I have put the script, eject.sh, specified by "Exec" in ~/.local/share/filemanager/actions, but note you cannot use "~" or "$HOME" in a desktop file so you have to specify complete path as I have done above

Script eject.sh should contain:

device=`udisksctl status | awk -v d=$1 '  BEGIN {      gsub("%20"," ",d)      sub("$"," ",d)  # Add a space on the end to give a more exact match      sub("^","^",d) # Add ^ to start to give a more exact match      }  $0 ~ d {      print "/dev/"$NF      }' `  if [ "$device" == "/dev" ]  then      echo "Removable device not found"  else      for dev in `df | awk -v device=$device '$1 ~ device {print $1}' `      do          echo "Unmounting "$dev          udisksctl unmount -b $dev      done        echo "Powering off: "$device      udisksctl power-off -b $device  fi  


Your command

udisksctl unmount --object-path /media/user/Flash-Drive  

is wrong.

/media/user/Flash-Drive isn't an object path.

  1. Find the object path with this command:

    $ gdbus introspect --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices --recurse --only-properties | grep -E "(readonly .+ (Device(IsMounted|IsDrive|File|MountPaths) ).*|\}|.*\{)" | awk -v RS="};" '/Flash-Drive/'      node /org/freedesktop/UDisks/devices/sdc1 {      interface org.freedesktop.UDisks.Device {          readonly as DeviceMountPaths = ['/media/user/Flash-Drive'];          readonly b DeviceIsMounted = true;          readonly b DeviceIsDrive = false;          readonly s DeviceFile = '/dev/sdc1';  
  2. Take the node /org/freedesktop/UDisks/devices/sdc1 via:

    (replace '/\/media\/user\/Flash-Drive/' with your path).

    $ gdbus introspect --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices --recurse --only-properties | grep -E "(readonly .+ (Device(IsMounted|IsDrive|File|MountPaths) ).*|\}|.*\{)" | awk -v RS="};" '/\/media\/user\/Flash-Drive/' | awk '/node/ {print $2}'    /org/freedesktop/UDisks/devices/sdc1  
  3. and unmount with

    gdbus call --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices/sdc1 --method org.freedesktop.UDisks.Device.FilesystemUnmount []  

    Or as AllInOne (replace '/\/media\/user\/Flash-Drive/' with your path):

    gdbus call --system --dest org.freedesktop.UDisks --object-path $(gdbus introspect --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices --recurse --only-properties | grep -E "(readonly .+ (Device(IsMounted|IsDrive|File|MountPaths) ).*|\}|.*\{)" | awk -v RS="};" '/\/media\/user\/Flash-Drive/' | awk '/node/ {print $2}') --method org.freedesktop.UDisks.Device.FilesystemUnmount []  


This answer does use pcmanfm, but I have found it is the most convenient one. TL;DR; use lxpanel to add an icon to gnome-disks.

In details:

In lxpanel right-click and select 'Add / Remove Panel Items'. Add a new 'Application Launch Bar' and position it before (or somewhere around) 'Digital Clock' that should be already there. In the newly added 'Application Launch Bar' add as application the 'Preferences / Disks'. This is the gnome-disks application that comes with Lubuntu.

If you need to do something with your disks, then just click the Disks icon in the panel. You select the disk and Power Off from the top menu.

This is same as handy as 'Safely remove hardware' in Windows OS, and the above positions it more or less in same place as the Windows has it in desktop.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »