Tuesday 25 October 2011

USB automount

I looked for a solution for simply (preferably automatically) mounting any USB storages (pendrives, external hard drives, etc.) on my Arch Linux notebook. Gnome and other desktop environments do it for you automatically but if you keep things simple with a small window manager (like Awesome or wmfs) you need to set it up by yourself.

There are several solutions for doing so:

1. mounting USB storages manually or with an fstab entry:
You can do it with some basic Linux knowledge, the method is well documented here. It is simple to set up but not flexible enough (i.e. you have to mount the devices manually, it is hard to handle several storages at the same time, fstab can automount only at boot time).

2. using an external application to handle your usb devices: 
pmountskvm, udiskie etc. can more or less handle your USB volumes but I  found none of them satisfactory. One of them have dependency problems (i.e. using hal that I did not want to install), others need consolekit or to be run as root, and all of them needs an extra daemon to be run in the background.

3. using udev rules to recognise and automount your volumes:
The udev daemon natively runs in the background, you do not have to install and/or place it in your /etc/rc.conf. You only need to tell it to mount your USB storages when they are plugged in. You can find nice examples for such rules in the udev Arch Wiki.

I use the following udev rule that mounts automatically the hot-plugged device under /media/<label> and works seamlessly with vfat, ntfs and other formats of USB storages. Simply create this file and fill it up with the contents as follows:

/etc/udev/rules.d/10-my-media-automount.rules

# vim:enc=utf-8:nu:ai:si:et:ts=4:sw=4:ft=udevrules:
#
# /etc/udev/rules.d/10-my-media-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
LABEL="my_media_automount_end"

Wednesday 19 October 2011

Wicd cannot connect to ad-hoc networks

In my Nokia (5800 Xpress music) phone I have a network sharing application called Joikuspot. Its purpose is to use your phone as a wireless access point offering your mobile data plan via wi-fi to other devices (such as my laptop with Arch Linux). The provided wlan network is in ad-hoc mode though and my Wicd client cannot connect to it. dmesg says: "Unable to find TIM Element in beacon". After some research I found this bug, some says that it is solved in the newest version of wicd but for me upgrading to 1.7.0 did not solve the issue.

Fortunately the ad-hoc network works fine with manual configuration. To bring down the wlan0 interface and restart it in ad-hoc mode without passphrase (as my phone app does not support protected networking) and obtaining an IP address with dhclient (dhcpcd might work as well) simply use this command:

ifconfig wlan0 down && iwconfig wlan0 mode Ad-Hoc && iwconfig wlan0 essid <ESSID_of_your_wlan> key off && ifconfig wlan0 up && dhclient wlan0

As a workaround for wicd I created a preconnect script (more about wicd scripting: here) that runs this command and does not let wicd to destroy my ad-hoc connection (because it wants to do it) until another connection is set manually in the wicd client. I used a loop that does not let the script end while a lock file exists. Choosing another connection in wicd deletes the lock file, the ad-hoc script ends and gives back the control to wicd.

The script lies under /etc/wicd/scripts/preconnect/ad-hoc.sh

#!/bin/bash

script="$(basename "$0")"
script_name="${script/.sh/}"

echo $1 $2 $3 $4 $5 >> "/var/log/wicd/${script_name}.log"

echo "Running ${script}" >"/var/log/wicd/${script_name}.log"
exec 2>>"/var/log/wicd/${script_name}.log"
exec 1>&2

connection_type="$1"
echo "Connection type: ${connection_type}"

if [ "${connection_type}" == "wired" ]; then
        profile="$3"
        echo "Profile: ${profile}"
elif [ "${connection_type}" == "wireless" ]; then
        essid="$2"
        bssid="$3"
        echo "ESSID: ${essid}"
        echo "BSSID: ${bssid}"
else
        echo "Unknown connection type: ${connection_type}" >&2
        exit
fi

rm /var/lock/wicd_adhoc.lock

case $2 in
*JoikuSpot*)
  # When I connect to my phone my ESSID contains JoikuSpot
     touch /var/lock/wicd_adhoc.lock
     ifconfig wlan0 down && iwconfig wlan0 mode Ad-Hoc && iwconfig wlan0 essid $2 key off && ifconfig wlan0 up && dhclient wlan0

     while [ -f /var/lock/wicd_adhoc.lock ]
     do
     sleep 2;
     done

esac


Waiting for the real bugfix in the newer releases of wicd!

Blueman applet with missing status icon

After upgrading the blueman package to version 1.23.1 the bluetooth icon in my systray showed a "no icon" graphics:





The Statusicon section under the plugins settings can be configured in this new version of blueman. The default setting is "blueman-tray". I tried to simply modify it to "blueman", the form marks it as an acceptable attribute but it did not change anything, the icon is still not there.

I downgraded to the former (1.21) version from my pacman cache:

pacman -U /var/cache/pacman/pkg/blueman-1.21-7-x86_64.pkg.tar.xz

and my status again appears nicely:





I hope to see a bugfix soon!