2

When I attach new drive it pops up in system tray ( I use KDE currently). When I click it gets mounted in /run/media/....

Can I trigger this operation from command line , without using mouse?

(For clarity: I am not asking how to use mount or fstab, I would like to trigger same action as I do with mouse)

The same with unmounting drive.

  • 1
    Related: https://unix.stackexchange.com/q/62676/315749, https://unix.stackexchange.com/q/394320/315749, https://unix.stackexchange.com/q/598740/315749 – fra-san Oct 20 '20 at 09:22

1 Answers1

3

Mounting in user space is usually done with UDisks so you can just use udisksctl to tell UDisks to mount a device in same way KDE does. (I'm not sure which service does this in KDE, in GNOME mounting is done by GVFS.)

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

If you want to do everything manually, including detecting a new removable device was connected, you'll need to use either DBus to communicate with UDisks or libudisks. GVFS (and similar tools) watch for change event from UDisks and if the new block device contains a mountable filesystem, it will show some notification to user and then use UDisks mount function to mount the device.

If you are interested in doing something like that manually, I recommend these UDisks API examples from Canonical.

  • rust library looks promisting: https://github.com/pop-os/dbus-udisks2 – Grzegorz Wierzowiecki Oct 21 '20 at 06:19
  • Wow, it's written by hand? Sounds like a lot of unnecessary work to me. The C and Python libraries are generated from the DBus code, there must be a way to do this for rust without this much of manual work. – Vojtech Trefny Oct 21 '20 at 06:38
  • 1
    Re: "I'm not sure which service [mounts devices] in KDE"...

    The devicenotifier Plasma applet shows the "Disks & Devices" panel; kcm_device_automounter implements System Settings > Removable Devices, and ends up writing $HOME/.config/kded_device_automounterrc. Both drive the Solid hardware layer talking (via kded? DBUS?) to udisks2... and eventually creating a runtime path.to.mount.point.mount systemd unit file. Whew!

    – skierpage Jul 07 '22 at 03:10