Ranger mount removable USB?

Good Day folks,

I am trying to configure ranger file manager. My issue is that I’m trying to find a simple way to mount removable USB that are plugged in?
The reason being that I can access my USB with the gm command.

Please note that I have already tweaked the gm command that it points to /run/media/user/. All I need is a command that mounts the drive or show available drives to mount.

A possible solution could be where I use Thunar to mount the USB when inserted and then I can just open ranger and view the files. But it seems that the volume management of thunar isn’t cooperating.

Any help would be appreciated.

Probably this:
command-line tool udisksctl

use “lsblk” to find external drives

udisksctl mount -b /dev/sdxY
and
udisksctl unmount -b /dev/sdxY

Ranger rc.conf “map gm cd /run/media”

I use a script I wrote for this a while ago

#!/usr/bin/env bash

# Utility to mount/unmount drives

# get device UUIDs with `lsblk -lno UUID,NAME,SIZE`
DRIVES=(
'DEBC8E9CBC8E6EB9'
)

# check if drive is already mounted
# returns boolean
mounted() {
    MOUNT=$(lsblk -lno UUID,MOUNTPOINT | grep "$1" | awk '{print $2}')
    [[ $MOUNT ]] && return 0 || return 1
}

# get device /dev/ name using UUID
# returns boolean
dev_name() {
    NAME=$(lsblk -lno UUID,NAME | grep "$1" | awk '{print $2}')
    [[ $NAME ]] && return 0 || return 1
}

# check if the action is valid and can be done without error
# ie. trying to mount a drive that is already mounted or unmounting a drive that isnt mouned
# returns boolean
valid() {
    if ([[ $1 == 'mount' ]] && mounted $UUID) || ([[ $1 == 'unmount' ]] && ! mounted $UUID); then
        return 1
    fi
    return 0
}

# loop over device UUIDs, perform $1 action on each device
# requires udisks
drive_loop() {
    (! hash udisksctl) && return
    local action=$1
    for UUID in "${DRIVES[@]}"; do
        dev_name $UUID && valid $action && udisksctl $action -b /dev/$NAME || continue
    done
}

# USAGE: script [-r]
# without -r option drives will be mounted
case $1 in
    -r) drive_loop unmount ;;
    *)  drive_loop mount
esac

exit 0

I use this in my ~/.config/rc.confmntr is just what I called the script, and External is the drive name I chose when I originally created it.

# mount all drives and cd to a preffered drive
map ge chain shell mntr ; cd /media/External

# unmount all drives
map gr shell mntr -r

I made an edit to the udev rules to have udiskctl mount drives to /media rather than /run/media.
See Udisks Arch Wiki

Edit or create /etc/udev/rules.d/99-udisks2.rules

# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

Hope this helps,

Cheers

1 Like

I use a little proggie ‘udiskie’ and autostart it in my i3 config file. It’s very quick in detecting any newly attached usb device. To quickly go to external places in ranger I just make soft links to the right locations. It works like a charm.