0

When I plug a USB stick/thumbdrive that has been formatted as FAT32 into a Raspberry Pi (running Raspbian) the file permissions default to 644 and I cannot change them.

I need to leave the stick as FAT32 so it can be used back and forward to a Windows system.

I have written a rule using udev but I can't get it to work.

My udev rule looks like this:

# Set up any USB stick for full write access KERNEL=="sd?1", MODE="0777"

I determined that the rule is getting triggered because I had it renaming the device. It is setting the permissions of the device itself, but not the individual files on the device.

What I am ultimately trying to achieve is the ability to write to the USB stick from PHP.

I feel I am very close to the answer but can't see what I am missing.

David G
  • 337
  • 6
  • 17
  • The device permissions don't have anything to do with file permissions and it's better to leave them alone. Since FAT and friends don't support UNIX file permissions the implementation just returns a fixed value for them. You can change them using mount options, but I'm not sure how you'd specify them with any sort of automount thing. You'd imagine automounting made the files readable to users by default... – Matti Virkkunen Apr 16 '14 at 01:20
  • Thanks. automount does make them readable by default but only writeable to the owner of the file which defaults to pi. PHP/Apache runs as user www-data. – David G Apr 16 '14 at 01:55
  • If you want this accessible to a server process maybe letting the automounter (which is usually a part of the desktop environment which you might not even need) mount the thing isn't the best option in the first place. Is it possible to mount it via a plain mount command? – Matti Virkkunen Apr 16 '14 at 01:59
  • I don't think so. I want it automatic so if someone inserts a USB stick it gets auto mounted and ready for use. – David G Apr 16 '14 at 02:13

1 Answers1

0

Problem resolved - after several hours and much advice from my Linux guru friend Jox.

It is somewhat involved and there may be an easier way - happy to hear about it if there is.

1) Disble GUI which in turn disables Automount. (The Pi is running headless anyway).

2) Create a udev rule as follows (saved in /etc/udev/rules.d/81-usbmount.rule ):

<code>
# Set up any USB stick for full write access

KERNEL=="sd?1", ACTION=="remove", RUN+="/home/pi/mount_usb.sh"
KERNEL=="sd?1", SYMLINK="stick1"
KERNEL=="sd?1", ACTION=="add", RUN+="/home/pi/mount_usb.sh"
</code>

This gives the USB drive a consistent name and runs a script to mount and unmount the drive.

3) Add an entry to FSTAB (stored in /etc/fstab):

 /dev/stick1     /media/usbstick  vfat    uid=www-data        0 0 

This sets the default user id to www-data because that is the user Apache/PHP runs as.

4) Create script to do the mount and unmount. (Stored in /home/pi/mount_usb.sh)

<code>
 #! /bin/bash
 # script to mount and unmount USB stick
 echo "Starting $ACTION" > /home/pi/mylog.txt
 case $ACTION in
   add)
      echo "Mounting" >> /home/pi/mylog.txt
      umount /dev/david  2>> /home/pi/mylog.txt
      mount -a  2>> /home/pi/mylog.txt ;;
   remove) 
      echo "Unmounting" >> /home/pi/mylog.txt
      umount /dev/sda1  2>> /home/pi/mylog.txt ;;
   *)  
      echo "NOTHING x$ACTIONx" >> /home/pi/mylog.txt ;;
 esac 
</code>

The echoes are just for debug purposes.

[I don't think I've mastered the code formatting thingy either]

David G
  • 337
  • 6
  • 17