Ubuntu – Trouble automounting an SD card on Trusty

14.04automountfstab

I need an SD card to be mounted unattended, as soon as it is inserted in its slot. Usual enough, but the prbm is it does not get mounted and I am missing why.

In /etc/fstab:

#Entry for /dev/mmcblk0p1 :
UUID=_____   /mnt/SD-root ext3 defaults,nofail,umask=0027,utf8,comment=x-gvfs-show,x-gvfs-name=SD-root 0 2

EDIT-1: I also tried replacing /mnt/ with /media/myname/ above, and including the options user,uid=1000 (that's me) or users, to no avail.

Mount point is either /mnt/SD-root or /media/myname/SD-root. Mountpoint ownership is set to "root : adm".

$ ls -Al /mnt/ | grep -e 'SD-root'
1 drwxr-x--- 3 root adm 1024 Sep 25 16:51 SD-root/

Right now the only way for a non-root user to mount the SD card is to:

$  sudo mount /dev/mmcblk0p1 /mnt/SD-root/

What am I missing ?

Best Answer

To mount the card automatically at /mnt in a running system you could use an additional udev rule. Without the rule, the card is mounted in /media/$USER/… and /dev/disk/….


The following script and udev rule create a folder in /mnt and mount a the partition with a defined UUID. In the current version, the mountpoint is the label of the mounted partition. I personally think, that's a bad idea. I would prefer the UUID and not the label, but the OP wanted this solution.

  1. The script

    • Create a script in /usr/local/bin

      sudo nano /usr/local/bin/mount_by
      
    • Add the code below

      #!/bin/sh
      export mount_point="/mnt/$1"
      existing_device=$(awk '$2 == ENVIRON["mount_point"] {print $1; exit}' < /proc/mounts)
      if [ -n "$existing_device" ]; then
        exit 1
      fi
      mkdir -p "$mount_point"
      sleep 1 # Perhaps not necessary, but in the test with the OP it was necessary
      mount "/dev/disk/by-uuid/$2" "$mount_point"
      exit 0
      
    • Make the script executable

      sudo chmod +x /usr/local/bin/mount_by
      
  2. The udev rule

    • Create a new rule

      sudo nano /etc/udev/rules.d/99-myrules.rules
      
    • Add the code below

      ENV{ID_FS_UUID}=="c8bf306d-3d5d-4878-8045-e4087494eff0", RUN+="/usr/local/bin/mount_by '%E{ID_PART_ENTRY_NAME}' '%E{ID_FS_UUID}'"

      or if your drive hasn't the ID_PART_ENTRY_NAME, try ID_FS_LABEL

    • Suppose that the partition is /dev/sdc1 Replace the value for ENV{ID_FS_UUID}== above with the output of

      udevadm info /dev/sdc1 | awk -F= '/ID_FS_UUID=/ {print $2}'
  3. Restart udev

    sudo service udev restart
    
  4. Plugin your SD card and you should see something like this

    % ls -la /mnt
    total 12
    drwxr-xr-x  3 root root 4096 Okt  6 19:29 .
    drwxr-xr-x 25 root root 4096 Sep 29 17:04 ..
    drwxr-xr-x  4 root root 4096 Okt  6 19:31 Label1
    
Related Question