Raspberry Pi USB stick/media automounting

Pi_usbstick.jpg


On the Pi it's useful to be able to plug USB devices in, especially if they have music you like to play on them.
Plug-in, count to 5, then hit 'rescan' in SeeDeClip4 and play the contents, making the Pi a little more 'plug and play'!
Or just plug it in and power it up.

Make sure "/media" is mentioned in SeeDeclip4's Settings->Import page. If should be by default.
Note that this doesn't cover removing the USB device...

1. Install udisks-glue

udisks-glue manages the detection and mounting of the USB sticks and drives that are plugged in.

Firstly ensure your 3B+ is up to date and the udisks-glue package is installed:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install udisks-glue

Then edit the config file for it to allow it to work pretty much as we'd like it to.
sudo vi /etc/udisks-glue.conf

/etc/udisks-glue.conf (644 bytes)
#
# Filters
#

filter disks {
    optical = false
    partition_table = false
    usage = filesystem
}

filter burnable {
    optical = true
    optical_disc_closed = false
}

filter optical {
    optical = true
}

#
# The default entry (only used if no filters match)
#

default {
}

#
# Additional entries
#

match disks {
    automount = true
    automount_options = { sync, noatime }
}

match burnable {
    post_insertion_command = "k3b %device_file"
    post_insertion_command = "udisks --mount %device_file --mount-options ro"
}

match optical {
    automount = true
    automount_options = ro
    post_insertion_command = "udisks --mount %device_file --mount-options ro"
}

Note the line:

automount_options = { sync, noatime }

If you want to mount devices that are plugged in as 'read-only' add the 'ro' option. It's left out because as user 'pi' you may want to also write your music to the USB card from that Raspberry Pi. E.g:

automount_options = { sync, noatime,ro }


2 Running udisks-glue as user 'pi'

Because the user running SeeDeclip needs access to the USB disc/stick (So SeeDeclip4 can read the music files) we now need to make sure the USB disk is owned by that user when it gets plugged in. This is done in two steps:

  1. The udisks-glue daemon has to be run as user 'pi'
  2. The user 'pi' has to be allowed to mount those discs.

2.1 Running udisks-glue as user 'pi'


Edit the /etc/rc.local file..
sudo vi /etc/rc.local

..and add the udisks-glue section to the bottom as detailed here:

/etc/rc.local (877 bytes)
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Run USB Automounting - get this in first!!
printf "Starting udisks-glue as user 'pi'"
sudo -u pi /usr/bin/udisks-glue
sleep 1

# Hotspot setup starts now
printf "Starting Wifi Hotspot"
/bin/bash /usr/local/bin/wifistart &

# Run seedeclip4 - use wildcard in case we reinstall it later with a newer version..
printf "Starting SeeDeClip4 as user 'pi' (after 10s to allow disks to mount)"
(sleep 10 && sudo -u pi /home/pi/ARM32_SeeDeClipV4.*/seedeclip4 ) &

exit 0

This then starts up the USB disk auto-mounting (for startup and plugging in while running) under the user 'pi', so the disks plugged in will now appear in /media owned by 'pi', and therefore be accessible to SeeDeclip4 to read.


2.2 Allowing a 'policy' of user pi mounting a USB storage device

This is controlled by the policy system which means adding this file to it:

Create/edit this file: /etc/polkit-1/localauthority/50-local.d/50-mount-as-pi.pkla
sudo vi /etc/polkit-1/localauthority/50-local.d/50-mount-as-pi.pkla

and fill it with this data:

/etc/polkit-1/localauthority/50-local.d/50-mount-as-pi.pkla (112 bytes)
[Media mounting by user pi]
Identity=unix-user:pi
Action=org.freedesktop.udisks.filesystem-mount
ResultAny=yes

And now the udisks-glue is allowed to do the work for you.
For example listing the contents of /media with a card in will give this result:
{pi@SeeDeclip4:~ 5} ls -lash /media
total 28K
4.0K drwxrwxrwx   5 root root  4.0K May  8 15:30 .
4.0K drwxr-xr-x  23 root root  4.0K May  4 14:32 ..
4.0K drwx------   2 pi   pi    4.0K May  8 15:30 USB Disk


Copyright © 2007-2023, CuteStudio
Page generated in 0.178s, Powered by Silk V1.3-0 from Cutestudio