Automount USB in Archlinux
Sunday 3 March 2024
  • Create a script that watches new devices and mount them. I’ll call it automount (ref)
 1#!/usr/bin/env bash
 2
 3pathtoname() {
 4    udevadm info -p /sys/"$1" | awk -v FS== '/DEVNAME/ {print $2}'
 5}
 6
 7stdbuf -oL -- udevadm monitor --udev -s block | while read -r -- _ _ event devpath _; do
 8        if [ "$event" = add ]; then
 9            devname=$(pathtoname "$devpath")
10            udisksctl mount --block-device "$devname" --no-user-interaction || true
11        fi
12done
  • Give it execute permission chmod +x automount
  • The path on my machine to this script is /home/emad/dotfiles/bin/automount
  • Create a systemd service in ~/.config/systemd/user/automount.service to run the script for the user
1[Unit]
2Description=Automount usb devices when plugged
3
4[Service]
5Type=simple
6ExecStart=%h/dotfiles/bin/automount
7
8[Install]
9WantedBy=default.target
  • Then enable the service for current user systemctl --user enable --now automount
  • Everytime a device is plugged in. its partitions will be mounted to /run/media/$USER/{patition-label}