You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Mageas e9bb2e9e28
Update: support for many drives
3 weeks ago
README.md Update: support for many drives 3 weeks ago

README.md

Pi-Samba

How to configure samba on a raspberry pi with guest only. It also auto mount a specific drive when it is plugged.

Samba config

In /etc/samba/smb.conf

[pishare]
   path = /home/pi/share
   guest ok = yes
   guest only = yes
   read only = no
   writable = yes
   create mask = 0777
   directory mask = 0777
   public = yes

Make sure the path is writtable with sudo chmod a+w /home/pi/share/

Udev config

In /etc/udev/rules.d/99-disk-update.rules

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_UUID}!="", RUN+="/home/pi/mount.sh add %E{ID_FS_UUID}"
ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_UUID}!="", RUN+="/home/pi/mount.sh remove %E{ID_FS_UUID}"

Reload the rules with

sudo udevadm control --reload-rules

Udev needs a little bit of a config:

In /etc/systemd/system/systemd-udevd.service.d/myoverride.conf

[Service]
MountFlags=shared
PrivateMounts=no

Restart udevd

sudo systemctl daemon-reload
sudo service systemd-udevd --full-restart

Auto mount

This script auto mount drives based on the UUID

#!/bin/bash

CONFIG=(
    "EBFB-6DA4 => /home/pi/pishare => exfat"
)

UID="pi"
GID="pi"

TEMPFILE="/var/tmp/pimount"

#
# DO NOT UPDATE AFTER THIS
#

for config in "${CONFIG[@]}"; do
    _uuid=$(echo $config | awk -F " => " '{print $1}')
    _mp=$(echo $config | awk -F " => " '{print $2}')
    _fs=$(echo $config | awk -F " => " '{print $3}')
    if [ "$_uuid" == "$2" ]; then
        UUID=$_uuid
        MOUNTPOINT=$_mp
        FS=$_fs
        break
    fi
done

[ -z "$UUID" ] && exit

TEMPFILE="${TEMPFILE}-${UUID}"

function create_directory() {
  mkdir "$MOUNTPOINT" &> /dev/null
  if [ $? -eq 0 ]; then
    chown -R pi:pi "$MOUNTPOINT"
    chmod a+w "$MOUNTPOINT"
  fi
}

function add() {
  rm -rf "$MOUNTPOINT"
  create_directory

  id=$(lsblk -o name,uuid | grep "$UUID" | awk '{print $1}' | sed 's/[^a-zA-Z0-9\/]//g')

  i=0
  while [ -z "$id" ]; do
    sleep 2s
    id=$(lsblk -o name,uuid | grep "$UUID" | awk '{print $1}' | sed 's/[^a-zA-Z0-9\/]//g')
    if [[ "$i" -gt '10' ]]; then
        exit 1
    fi
    ((i++))
  done

  grep -qs "$UUID" /proc/mounts && exit;

  mount -t "$FS" -o umask=000,uid="${UID}",gid="${GID}" /dev/disk/by-uuid/"$UUID" "$MOUNTPOINT" \
    && echo "/dev/$id" > $TEMPFILE
}

function remove() {
  umount -l $(cat $TEMPFILE)
  rm -rf "$MOUNTPOINT"
  rm -rf "$TEMPFILE"

  create_directory
  touch "${MOUNTPOINT}/THE_DRIVE_IS_NOT_MOUNTED"
}

sleep 5s

case $1 in
  "add" )
    add;;
  "remove" )
    remove;;
  * )
    exit 1;;
esac