A Raspberry Pi that pretends to be a USB stick

spectroman
spectroman
12 views

My resin printer, an Anycubic Photon Mono M3, takes its jobs from a USB stick. You slice the model on the computer, copy the file to the stick, walk to the printer, plug it in, pick the file on the touchscreen. Every single time, I would need to go downstairs to the garage level where the printer is to pick it up and do the round trip.

I wanted the walk to go away: slice, save to a network share, print. So the stick became a computer. A Raspberry Pi now sits next to the printer, plugged into its USB port, and the printer believes it is a perfectly ordinary USB stick. On the other side, the same Pi shares that "stick" over the network via NFS.


The trick: a Pi can be a USB device

Most of the time a Raspberry Pi is a USB host: keyboards, disks and dongles plug into it. But the USB controller on some Pis can also work the other way round, as a USB device (the kernel calls it gadget mode). Plug it into a computer, or a printer, and the Pi shows up as whatever the gadget driver says it is.

The driver here is g_mass_storage: give it a file holding a disk image, and the Pi presents that image as a USB mass storage device. To the printer it is a stick like any other, FAT-formatted, with files on it.

computer --NFS--> /network_usb  (loop mount of /piusb.bin)
                  /piusb.bin    --USB gadget, read-only--> printer

One file, /piusb.bin, two ways in:

  • The printer sees it over USB, as a stick.
  • The Pi loop-mounts the same image at /network_usb and exports it over NFS, so any computer on the network can drop files into it.

The hardware

The Pi is a Pi 4B on PoE, running Raspberry Pi OS (trixie, 64-bit). On a Pi 4 only one port can do gadget mode: the USB-C one, the same port normally used for power. Since this Pi is powered over Ethernet, the USB-C port is free for the printer.

There is one catch. The printer's USB port also supplies 5 V, and a Pi already powered by PoE should not get a second supply pushing into it. So the cable between the two is a data-only USB cable: data lines connected, power line not.

Wifi is switched off (dtoverlay=disable-wifi). The Pi lives on the cable, and a second address on the same network is just one more thing to go wrong.

Turning on gadget mode

Two pieces: the dwc2 overlay, which puts the controller in device mode, and the gadget module itself.

# /boot/firmware/config.txt, under [all]
dtoverlay=dwc2,dr_mode=peripheral

dr_mode=peripheral matters on a Pi 4. In the overlay's default otg mode the controller would have to detect its role from the ID pin, which a USB-C port does not have, so it stays a host.

The image is created once: a sparse file formatted as FAT32, 8 GiB on this Pi's 16 GB card.

truncate -s 8192M /piusb.bin
mkfs.vfat -F 32 -I -n PRINTER /piusb.bin

The gadget is a tiny systemd oneshot unit:

modprobe g_mass_storage file=/piusb.bin stall=0 ro=1

Read-only, on purpose

Look at ro=1. The printer sees the stick read-only.

FAT is not a shared filesystem. If two writers change the same FAT at the same time, the Pi through the loop mount and the printer through USB, each with its own cached idea of the allocation table, you get a corrupted stick. So there is exactly one writer: the Pi. The printer only reads, which is all a printer needs to do anyway.

The NFS side

The loop mount is world-writable (umask=000) and exported to the home network:

/network_usb 192.168.0.0/16(rw,sync,insecure,root_squash,no_subtree_check,fsid=1)

fsid=1 is the non-obvious one. FAT has no UUID, so by default NFS builds its file handles from the device number of the loop device. That number can change after a reboot or a remount, and every client then sees Stale file handle. A fixed fsid keeps the handles stable.

And then... the printer did not see the new files

With all of that in place, there was still a surprise: copy a file over NFS, look at the printer, and the file is not there.

The printer caches the stick's file list, and only reads it again when the stick is unplugged and plugged back in. On a virtual stick, "unplug" is easy: unload the gadget and load it again. A small script, usb-reconnect, does exactly that:

sync
systemctl stop usb-storage-gadget
sleep 2
systemctl start usb-storage-gadget

The hard part is when to run it. Doing it in the middle of a print takes the stick away from under the printer, and the print dies. That problem got its own small daemon, and its own post: usb-autorefresh.


Keeping an eye on it

This being me, it is all in Zabbix. The printer has a limit worth knowing: it only lists 50 files on the stick. File 51 is on the stick, but the printer cannot see it. So the template counts the files and complains well before that:

Files on the stick Trigger
40 Warning
45 High
50 Disaster: the printer stops seeing new files

Next to that, it watches the gadget itself (the file-storage kernel thread, which exists only while the stick is plugged in), the refresh daemon, the NFS server, free space in the image, and whether the printer is actually connected (/sys/class/udc/*/state says configured).

The same Pi also carries a camera pointed at the printer, streaming into Frigate. So the file goes in from my desk, and I can watch the print from my desk too; only pressing start still needs the touchscreen.

The whole thing in Ansible

The Pi is set up by an Ansible role, usb-storage-gadget: overlay, image, gadget unit, NFS export, the refresh daemon and the Zabbix template. An existing image is never recreated or resized, so a re-run never touches the files on the stick.

Related Posts

Magic Mirror
linuxinternetDIYtinkeringRaspberry Pi

Magic Mirror

The Fun DIY Anyone Can Do!

3/27/2026Read More →
The Bath Automation: Part 1, Heat Pump NIBE Reader
linuxRaspberry Piheat pumpgolangmonitoringIoT

The Bath Automation: Part 1, Heat Pump NIBE Reader

A Golang reader for NIBE heat pumps using the myUplink developer API, the first part of a larger project

3/27/2026Read More →
Trixie Hostname Persistence on RaspiOS
linuxRaspberry Pi

Trixie Hostname Persistence on RaspiOS

Have you ever need to change the hostname of a RaspiOS (Trixie) and see all your obvious tries fail?

4/17/2026Read More →