Automount USB Devices in Ubuntu 12.04 Server: Setup with udev Rules and VirtualBox Passthrough


4 views

Unlike desktop editions that automatically handle USB connections through graphical managers like udisks/udisks2, Ubuntu Server requires manual configuration for USB device recognition. This becomes critical when:

  • Deploying printer/scanner servers
  • Setting up backup solutions with external drives
  • Virtual machine passthrough scenarios

First ensure required packages are installed:

sudo apt-get update
sudo apt-get install usbmount udisks udev

For VirtualBox integration, add these if not present:

sudo apt-get install virtualbox-guest-additions-iso virtualbox-dkms

Create a custom udev rule to trigger automount actions:

sudo nano /etc/udev/rules.d/99-usb-automount.rules

Add these rules for basic storage devices:

# USB storage devices
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/usr/bin/mkdir -p /media/usb%n"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mount -o uid=1000,gid=1000,umask=000 /dev/%k /media/usb%n"

# Remove mount point when disconnected
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/umount -l /media/usb%n"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/usr/bin/rmdir /media/usb%n"

For VirtualBox VM access, modify your VM configuration:

1. Add your user to vboxusers group:

sudo usermod -aG vboxusers $USER

2. Create USB filter rules in VirtualBox:

VBoxManage usbfilter add 0 --target "VM_Name" --name "MyUSBDevice" --vendorid "1234" --productid "5678"

3. Verify devices are visible to VirtualBox:

VBoxManage list usbhost

For more control, create systemd mount units (example for /dev/sdb1):

# /etc/systemd/system/media-usb.mount
[Unit]
Description=Mount USB Drive

[Mount]
What=/dev/sdb1
Where=/media/usb
Type=auto
Options=defaults,noatime,nofail

[Install]
WantedBy=multi-user.target

Then enable with:

sudo systemctl enable media-usb.mount
sudo systemctl start media-usb.mount
  • Monitor udev events: udevadm monitor --property
  • Check kernel messages: dmesg | grep usb
  • Verify device nodes: ls -la /dev/sd*
  • Test mount manually before automating

When working with Ubuntu Server 12.04 (Precise Pangolin), you'll notice that USB devices don't automatically mount like they do in the desktop edition. This behavior stems from the server's minimal installation philosophy - it omits GUI components including the udisks/udisks2 service that handles automatic mounting in desktop environments.

First, let's install the required components:

sudo apt-get update
sudo apt-get install usbmount udisks

The usbmount package provides automatic mounting capabilities, while udisks offers the DBus interface that many applications (including VirtualBox) expect for device management.

Edit the configuration file to customize mounting behavior:

sudo nano /etc/usbmount/usbmount.conf

Key parameters to adjust:

ENABLED=1
MOUNTPOINTS="/media/usb0 /media/usb1 /media/usb2 /media/usb3"
FILESYSTEMS="vfat ntfs ext4 hfsplus"
FS_MOUNTOPTIONS="-fstype=vfat,gid=users,dmask=0002,fmask=0113"

For VirtualBox VM access, you'll need to:

  1. Add your user to the vboxusers group:
    sudo usermod -aG vboxusers $USER
  2. Install VirtualBox Extension Pack for USB 2.0/3.0 support
  3. Configure VM USB settings in VirtualBox Manager or via CLI:
    VBoxManage modifyvm "VM_Name" --usb on
    VBoxManage modifyvm "VM_Name" --usbehci on

If devices still aren't appearing:

# Check kernel messages
dmesg | tail -20

# Verify device detection
lsusb

# Check udev rules
sudo udevadm monitor --property

For VirtualBox-specific problems, ensure the vboxdrv module is loaded:

sudo /etc/init.d/vboxdrv status

For more control, create a udev rule to trigger custom actions:

# /etc/udev/rules.d/99-usb-automount.rules
ACTION=="add", SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", RUN+="/usr/local/bin/usb-automount.sh %k"

Sample script (/usr/local/bin/usb-automount.sh):

#!/bin/bash
DEVICE=$1
MOUNT_POINT="/media/usb-$DEVICE"
mkdir -p "$MOUNT_POINT"
mount "/dev/$DEVICE" "$MOUNT_POINT"