Skip to content

Commit

Permalink
First commit for the udev-media-automount fork
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferk committed Dec 15, 2013
1 parent f0ebc93 commit 2165c32
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 44 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
install:
install -D usb-automount.rules $(DESTDIR)/usr/lib/udev/rules.d/99-usb-automount.rules
install -D media-automount.rules $(DESTDIR)/usr/lib/udev/rules.d/99-media-automount.rules
install -D umount_dmenu $(DESTDIR)/usr/bin/umount_dmenu
install -D usb-automount $(DESTDIR)/usr/bin/usb-automount
install -D [email protected] $(DESTDIR)/usr/lib/systemd/system/[email protected]
install -D media-automount $(DESTDIR)/usr/bin/media-automount
install -D [email protected] $(DESTDIR)/usr/lib/systemd/system/[email protected]
udevadm control --reload-rules
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
usb-automount
=============
udev-media-automount
====================

auto mount usb with only udev rules
Auto mount removable media devices by means of udev rules.

This is intended for simple systems that don't want or can't run the udisks2 daemon (which is designed for GNOME/KDE desktop environments and at the time of this writting is frustrating to set up from a bare commandline).

This combines the previous udev rules I was using in my xdg_config repository with some structure and ideas taken from tylorchu's usb-automount.

Every device that is inserted and isn't already configured in /etc/fstab will be mounted by media-automount.

If there are devices you don't want to automount neither at boot nor with media-automount, you can add them in /etc/fstab with the option 'noauto'.

The mount options might differ to the filesystem type for the device plugged in. E.g. vFAT and NTFS devices will be mounted with the "flush" option to try to prevent damage on accidental removals of the device with unclean umounts. Also whenever ntfs-3g is available, it will be used to mount ntfs devices.

The mount directory will appear in /media/ under a name with pattern: "<LABEL_OF_THE_FS>.<FS_TYPE>"


Due to changes in udev (long running processes are killed), it's necessary to use systemd for spawning a mounting service.

The script will also use the 'logger' tool to write to the system log.
82 changes: 82 additions & 0 deletions media-automount
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
#$1 = <action>/<dev>/<type>/<label>

# Directory to use for mounting the devices
mdir=/media/
# Default options to use for mounting
mopts='errors=remount-ro,relatime,utf8,user'

[[ $EUID != 0 ]] && {
echo "This tool requires root permissions"
exit 1
}
shopt -s nullglob

log() {
logger -st "media-automount" "$*"
}

# clean
for dir in "$mdir"/*; do
mountpoint -q "$dir" || rmdir "$dir"
done

IFS=/ read -r action dev type label <<<"$1"
dir="${mdir}/${label:-$dev}.$type"

case $type in
vfat)
mopts="$mopts,flush,gid=100,dmask=000,fmask=111"
;;
ntfs)
mopts="$mopts,flush"
hash ntfs-3g && mtype="ntfs-3g"
;;
*)
mopts="$mopts,sync"
;;
esac


case $action in

mount)
# Don't automount devices that are already in fstab (unless they use 'noauto')
if grep /etc/fstab -e "^[ \t]*/dev/$dev[ \t]" | grep -qv 'noauto'
then
log "device $dev is already in fstab with 'auto' option, the media-automount service won't manage this entry"
exit 1
fi

log "mounting device $dev in $dir"
mkdir -p "$dir"
if mount -t "${mtype:-auto}" -o "$mopts" "/dev/$dev" "$dir"
then
# Notify
username="$(ps au | awk '$11 ~ /^xinit/ { print $1; exit }')"
[[ "$username" ]] && DISPLAY=:0 runuser -u "$username" xdg-open "$dir"
log "Device successfully mounted: $dir"
exit 0
else
log "Mount error: $?"
rmdir "$dir"
exit 1
fi
;;

umount)
log "Umounting device $dev from $dir"
if [ -d "$dir" ]
then
umount "$dir" && rmdir "$dir"
else
log "Missing directory: $dir"
exit 1
fi
;;

*)
log "Wrong parameters! action/dev/type/label = '$1'"
exit 1
;;
esac
28 changes: 28 additions & 0 deletions media-automount.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- conf-unix -*-
#
#

# start at sdb to ignore the system hard drive
KERNEL!="sd[c-z]*", GOTO="media_automount_end"

ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_automount_end"

# import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Ever since early systemd-udevd merge, we are forced to use some
# hack to be able to run long lived processes from udev (which is required for
# some filesystems that will spawn some daemon, like ntfs-3g).
#
# udev will kill the running process and all childs after 4-5 seconds, so the
# mounting has to be done as a separate "service"

# mount the device when added
ACTION=="add", RUN+="/usr/bin/systemctl restart media-automount@mount-%k-%E{ID_FS_TYPE}-%E{ID_FS_LABEL}.service"

# clean up after device removal
ACTION=="remove", RUN+="/usr/bin/systemctl restart media-automount@umount-%k-%E{ID_FS_TYPE}-%E{ID_FS_LABEL}.service"

# exit
LABEL="media_automount_end"

2 changes: 1 addition & 1 deletion [email protected][email protected]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Service]
Type=forking
GuessMainPID=no
ExecStart=/usr/bin/usb-automount %I
ExecStart=/usr/bin/media-automount %I
12 changes: 6 additions & 6 deletions umount_dmenu
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ if [[ $EUID != 0 ]]; then
exit 1
fi

usb="$(awk '$2 ~ /^\/usb\// {print $2}' /etc/mtab | cut -d / -f 3 | dmenu -p "Select USB to remove")"
if [[ ! "$usb" || ! -d "/usb/$usb" ]]; then
dir="$(awk '$2 ~ /^\/media\// {print $2}' /etc/mtab | cut -d / -f 3 | dmenu -p "Select media device to remove")"
if [[ ! "$dir" || ! -d "/media/$dir" ]]; then
exit 1
fi

if umount "/usb/$usb"; then
rmdir "/usb/$usb"
dmenu_notify "You can now safely remove $usb"
if umount "/media/$dir"; then
rmdir "/media/$dir"
dmenu_notify "You can now safely remove $dir"
else
dmenu_notify "Some apps are using $usb"
dmenu_notify "Some apps are using $dir"
fi
29 changes: 0 additions & 29 deletions usb-automount

This file was deleted.

2 changes: 0 additions & 2 deletions usb-automount.rules

This file was deleted.

0 comments on commit 2165c32

Please sign in to comment.