In this tutorial, I am going to show you how to configure the Raspberry Pi Zero Wireless as a smart USB flash drive. Typically, people use a USB flash drive to transfer files between different devices, but it takes time to move the drive to and from the source device. How about a remotely accessible USB flash drive, permanently located in the equipment, where you could transfer files using the wireless connection?
Before the procedure, here is a video with the SSH configuration if you want to see it in a graphic format.
Parts Required
Procedure
Following are the steps required to perform this configuration.
Step 1 – Using the SD Card Formatter software, format the Raspberry Pi SD card to be used.
Step 2 – Download the Raspbian Jessie Lite iso image.
Step 3 – Using the Win32DiskImager software to install the Raspbian Jesie.
Step 4 – In order to automatically access a wireless network, it is necessary to create a file named wpa_supplicant.conf with the following configuration.
Step 5 – Create an empty file named ssh (without extension). For this, it is necessary to activate the following functionality in Windows:
Step 6 – Paste these 2 files on the Raspberry Pi SD card.
Step 7 – Insert the SD card into the RPi and power on via USB.
Step 8 – Via the PuTTY software, connect to the RPi via SSH.
Step 9 – Login with default credentials. Name: pi Password: raspberry
Step 10 – Adding the ssh file to the boot partition is only temporary, the file is removed at startup and ssh does not start on subsequent boots. Immediately after logging into SSH, it is necessary to activate ssh via raspi-config.
Step 11 – Clean up some disk space (performance increase).
sudo apt-get remove –purge libreoffice* -y
sudo apt-get purge wolfram-engine -y
sudo apt-get clean
sudo apt-get autoremove -y
Step 12 – Enable the USB Driver.
The USB port on the Pi Zero is directly connected to the processor, unlike other RPi’s that use an integrated USB hub. This cheaper solution means that the Pi Zero can be configured to act as a USB host, like a computer, a “USB device”, among others. To activate the “USB device” mode, some specific modules of the “dcw2” kernel must be activated. In this specific case, the required module is “g_mass_storage”.
Enable the DCW2 USB Driver
Open config.txt file: sudo nano /boot/config.txt
Add this text to the end of the file: dtoverlay=dwc2
Open modules: sudo nano /etc/modules
Add this text to the end of the file: dwc2
After that turn off the raspberry: sudo halt
Step 13 – Create storage file.
It is necessary to create a large file in order to act as a storage interface on the SD card. This file will emulate the USB flash drive that different devices see.
The command below will create an empty 2 GB binary file (change the count = 2048 parameter if you want a different size). This value is limited by the space available on the SD card and it may take a few minutes to complete the setup:
sudo dd if=/dev/zero of=/usb-drive.img bs=1M count=2048
It is now necessary to format the file as a FAT32 file system:
sudo mkdosfs -F 32 /usb-drive.img
Step 14 – Mount the storage file.
First, create a folder in which we can mount the file system:
sudo mkdir /mnt/usb_share
Now let’s add this to fstab, the configuration file that records the available disk partitions:
sudo nano /etc/fstab
Add the following line at the end of the file:
/usb-drive.img /mnt/usb_share vfat users,umask=000 0 2
The line we added to fstab allows the USB file system to be checked and mounted automatically at boot time. Instead of restarting, we can manually reload fstab with the command below:
sudo mount -a
Step 15 – Mass Storage Device Mode
The command below will activate the mass storage device mode:
sudo modprobe g_mass_storage file=/usb-drive.img stall=0 removable=1
At this moment, if the Raspberry Pi is connected via USB to a device such as a computer, the connection pop-up for a new storage device is given.
To unmount manually:
sudo modprobe -r g_mass_storage
Step 16 – Automatic Start
To run a program on your RPi at startup it is necessary to modify the bashrc file. With the .bashrc method, it is possible to execute commands at startup. Place the command at the bottom of ‘/home/pi/.bashrc’. The program can be aborted with ‘ctrl-c’ while it is running!
sudo nano /home/pi/.bashrc
Go to the last line of the script and add:
sudo modprobe g_mass_storage file=/usb-drive.img stall=0 removable=1
Restart the RPi: sudo reboot
Step 17 – Enable Auto Login
sudo raspi-config
Boot Options -> Desktop / CLI -> Console Autologin
Step 18 – Install and Configure Samba Server
Update Raspbian:
sudo apt-get update
sudo apt-get upgrade
Install Samba Server:
sudo apt-get install samba samba-common-bin
We will now create a shared folder that we will access later using Windows:
mkdir /home/pi/datafolder
Assign all permissions to the folder we just created:
chmod 777 datafolder
Now let’s edit the samba configuration file, to include the folder we just created:
sudo nano /etc/samba/smb.conf
Insert the lines below at the end of the file:
[RPi]
comment = RPi Share
path = /home/pi/datafolder
create mask = 0777
directory mask = 0777
writable = true
security = share
browseable = true
public = yes
Restart samba service: sudo service smbd restart
Restart the RPi: sudo reboot
Step 19 – Access RPi from Windows
Find RPi IP command: hostname -I
References
[1] http://massimomusante.blogspot.com/2018/03/sharing-files-on-raspberry-pi-zero-w.html
[2] https://magpi.raspberrypi.org/articles/pi-zero-w-smart-usb-flash-drive
[3] https://magpi.raspberrypi.org/articles/samba-file-server
[4] https://gist.github.com/gbaman/50b6cca61dd1c3f88f41
Thank you a lot for giving everyone remarkably spectacular opportunity to discover important secrets from this web site. It really is very awesome and packed with a lot of fun for me personally and my office co-workers to search your website minimum three times a week to read through the new tips you have. And lastly, I am also always fulfilled concerning the special advice served by you. Some 4 points in this article are clearly the most effective I have had.
For some reason my Mac is not ‘seeing’ the USB Flash drive after issuing the “sudo modprobe g_mass_storage file=/usb-drive.img stall=0 removable=1”
Everything up to that point was done properly, according to your article. After the “mount -a” command typing in “mount” showed that /usb-drive was properly mounted.
However, as stated above, after issuing the “sudo modprobe…….” command the USB Flash drive did not mount or appear on my Mac.
HELP??????
Thanks much
Hello Jeff,
Unfortunately I’ve never done this on a mac but try taking a look at the link below:
https://raspberrypi.stackexchange.com/questions/63914/unable-to-configure-a-raspberry-pi-zero-as-a-usb-mass-storage-device-using-g-mas
Regards,
Bruno
Oh too bad I thought the samba share goes to the same directory as the USB stick 🙁
I have made it this way.
Thanks for this – finally managed to get working!
Your next challenge is to get this working on 64bit RPiOS – for some reason this fails for me 🙁
Thanks for info.
No 2021 pi zero’s in sight. Might find a 2017 model. Any thoughts as to if the 2021 zero w would work?