Tuesday, May 17, 2011

No Login Manager, No Hal, No Daemons, Just Udev

So, as a linux power user, I like to keep my system lightweight. I use a very slimmed down version of XFCE on my laptop and desktop.

However, sometimes you can go overboard and make life difficult for yourself by removing necessary services. A login manager isn't necessary if you are the only user of the computer. Unfortunately, often times the login manager sets up the permissions at login time. This can mean things such as flash drives will not mount when they are inserted.

So here is what I like to do.

First of all, with the new xfce 4.8, add the following to your ~/.xinitrc to start xfce4 when you login.

exec dbus-launch --exit-with-session ck-launch-session startxfce4

Now, what about mounting your flash drives and external hard drives? Hal is deprecated. It is not necessary on a modern linux system. Unfortunately, documentation on what to do, especially from Debian, is scarce. Clearly, you can install thunar-volman or gnome-volume-manager, or some other tool, but these are daemons that run all the time, and they may pull unnecessary dependencies if they do not fit in with the desktop installed on your system, or the lack there of.

Not only that, they are an extra daemon that isn't necessary. Udev is already running, and it handles hotplug events from hardware. However, it isn't setup to mount usb drives by default. Fortunately, it isn't too difficult to set it up. In my example, I will be adding support for ntfs-3g drives as well.

I only slightly modified this from some Arch Linux Documentation. The best way to do this is to open up a text editor, such as leafpad, and past the rules below into the file, and save the file as automount.rules and then copy the file to /etc/udev/rules.d/automount.rules

Then you need to restart the udev daemon, on debian as root,

/etc/init.d/udev restart

After this, devices should automatically mount, without permission problems, and without running an extra daemon, no hal, just udev.

# /etc/udev/rules.d/automount.rules

# start at sdb to ignore the system hard drive
KERNEL!="sd[b-z]*", GOTO="my_media_automount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="my_media_automount_end"

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

# get the label if present, otherwise assign one based on device/partition
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# create the dir in /media and symlink it to /mnt
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

# global mount options
ACTION=="add", ENV{mount_options}="relatime"
# filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat) 
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},gid=100,dmask=000,fmask=111,utf8"

# automount ntfs filesystems using ntfs-3g driver
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"
# automount all other filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mount -t auto -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"

# clean up after device removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# exit

KERNEL!="mmcblk[0-9]p[0-9]", GOTO="sd_cards_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem specific options
ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p %N"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

ACTION=="add", RUN+="/bin/mkdir -p /media/sd-%k", RUN+="/bin/ln -s /media/sd-%k /mnt/sd-%k", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/sd-%k"
ACTION=="remove", RUN+="/bin/umount -l /media/sd-%k", RUN+="/bin/rmdir /media/sd-%k"

I plan on adding this to the debian wiki, because this is valuable and necessary documentation.

No comments:

Post a Comment