Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active January 8, 2026 19:48
Show Gist options
  • Select an option

  • Save stekhn/d23a09341f2f06f15ab87a820e739cbd to your computer and use it in GitHub Desktop.

Select an option

Save stekhn/d23a09341f2f06f15ab87a820e739cbd to your computer and use it in GitHub Desktop.
Turns a Raspberry Pi Zero 2 W into a USB flash drive that's also accessible as a network share via Samba.
#!/bin/bash
# Turns a Raspberry Pi Zero into a USB flash drive accessible via Samba.
if [[ $EUID -ne 0 || -z "$SUDO_USER" ]]; then
echo "Please run with: sudo ./setup.sh"
exit 1
fi
# Determine boot config location (changed in Bookworm+)
if [[ -f /boot/firmware/config.txt ]]; then
BOOT_CONFIG="/boot/firmware/config.txt"
elif [[ -f /boot/config.txt ]]; then
BOOT_CONFIG="/boot/config.txt"
else
echo "Error: Cannot find boot config.txt"
exit 1
fi
echo "Installing dependencies..."
apt-get update
apt-get install python3 python3-watchdog samba winbind dosfstools -y
apt-get clean
wget -q https://raw.githubusercontent.com/omiq/piusb/main/usb_share_watchdog.py -O /usr/local/share/usb_share_watchdog.py
chmod +x /usr/local/share/usb_share_watchdog.py
# Enable USB gadget mode (only if not already set)
grep -q "^dtoverlay=dwc2" "$BOOT_CONFIG" || echo "dtoverlay=dwc2" >> "$BOOT_CONFIG"
grep -q "^dwc2" /etc/modules || echo "dwc2" >> /etc/modules
USB_SIZE_MB=2560
MIN_FREE_MB=1024
REQUIRED_MB=$((USB_SIZE_MB + MIN_FREE_MB))
AVAILABLE_MB=$(df -m / | awk 'NR==2 {print $4}')
if [[ $AVAILABLE_MB -lt $REQUIRED_MB ]]; then
echo "Error: Not enough space. Need ${REQUIRED_MB}MB, have ${AVAILABLE_MB}MB free."
exit 1
fi
echo "Creating ${USB_SIZE_MB}MB USB storage (${AVAILABLE_MB}MB available)..."
dd bs=1M if=/dev/zero of=/piusb.bin count=$USB_SIZE_MB status=progress
mkfs.fat -F 32 -n PIUSB /piusb.bin
echo "Mounting storage..."
mkdir -p /mnt/share
grep -q "/piusb.bin" /etc/fstab || echo "/piusb.bin /mnt/share vfat rw,users,user,exec,umask=000 0 0" >> /etc/fstab
systemctl daemon-reload
mount -a
modprobe g_mass_storage file=/piusb.bin stall=0 ro=0
echo "Configuring Samba share..."
if ! grep -q "^\[share\]" /etc/samba/smb.conf; then
cat >> /etc/samba/smb.conf << 'EOF'
[share]
browseable = yes
path = /mnt/share
guest ok = no
read only = no
create mask = 0777
comment = PiUSB
directory mask = 0755
force create mask = 0777
force directory mask = 0755
force user = root
force group = root
kernel oplocks = yes
oplocks = False
valid users = @users
EOF
fi
systemctl restart smbd.service
echo "Setting up watchdog service..."
cat > /etc/systemd/system/usb-share-watchdog.service << EOF
[Unit]
Description=USB Share Watchdog
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/local/share/usb_share_watchdog.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable usb-share-watchdog.service
systemctl start usb-share-watchdog.service
echo ""
echo "Set Samba password for '$SUDO_USER':"
smbpasswd -a "$SUDO_USER"
if [[ $? -ne 0 ]]; then
echo "Error: Failed to set Samba password."
exit 1
fi
echo "Done! Rebooting..."
reboot now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment