forked from Ferk/udev-media-automount
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit for the udev-media-automount fork
- Loading branch information
Showing
8 changed files
with
141 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.