Search code examples
usbraspberry-pifile-permissionsudevfat32

Default file permissions for FAT32 USB stick on Raspberry Pi


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.


Solution

  • 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]