- Raspberry Pi Zero Cookbook
- Edward Snajder
- 739字
- 2021-07-09 19:12:13
Making your RPZ a USB mass storage device
This last hack makes it possible to use your Raspberry Pi Zero as a mass storage device. While at first it doesn't seem like much more than turning it into a flash drive, depending on what you are doing, and what else you are doing with your Raspberry Pi Zero, you can make your Raspberry Pi Zero be recognized as a USB flash drive to other computers.
Getting ready
Since we will be using the USB connection for a mass storage device, we should connect to the Raspberry Pi Zero using the USB-to-serial adapter and the GPIO pins, as shown in the first recipe of this chapter. You can make your modifications here, connect your USB and Raspberry Pi together, reboot, and work with the Pi as a flash drive or as the little computer you're used to.
How to do it...
- We're going to add one piece after
rootwait
to make the Pi enabled for USB mass storage. You've seen this in the previous recipes:pi@rpz14101:~$ sudo cat /boot/cmdline.txt dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_mass_storage
This change should be pretty familiar, but to finish setting up your Zero as a storage device, you need to create a space that acts as storage. Most USB flash drives are formatted using FAT32, one of the more common formatting standards available.
- Let's make a mini "filesystem" for our Zero flash drive:
sudo dd if=/dev/zero of=/opt/cookbook.share bs=1Ml count=512
- Then, we will format this into a FAT32 system with the
mkfs.fat
command:sudo mkfs.fat /opt/cookbook.share
- Set permissions:
sudo chwon 755 /opt/cookbook.share
- Finally, reboot and connect your USB cable:
sudo reboot
- At first your computer will probably not recognize your Pi as a mass storage device. Reconnect over your serial connection and log in. After login, activate the device with this:
sudo modprobe g_mass_storage file=/opt/cookbook.share stall=0
- On a Windows machine, you will probably see drivers loading, and then you will see a new icon under USB devices:
- From, here, looking at disk management, we will see we have a new, initialized volume:
- From here, it is all yours: format a new simple volume, name it, give it a drive letter, and it will be ready to go! Back on the Raspberry Pi, you can see the initialization of the USB driver in the logs:
pi@rpz14101:~$ dmesg | tail -n 20 [ 14.017876] i2c /dev entries driver [ 15.469264] bcm2835-wdt 20100000.watchdog: Broadcom BCM2835 watchdog timer [ 15.541326] gpiomem-bcm2835 20200000.gpiomem: Initialised: Registers at 0x20200000 [ 15.761343] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null) [ 18.711165] systemd-journald[105]: Received request to flush runtime journal from PID 1 [ 20.907976] Installing knfsd (copyright (C) 1996 okir@monad.swb.de). [ 21.160769] Adding 102396k swap on /var/swap. Priority:-1 extents:1 across:102396k SSFS [ 80.621706] random: nonblocking pool is initialized [ 1605.922774] Mass Storage Function, version: 2009/09/11 [ 1605.922809] LUN: removable file: (no medium) [ 1605.922989] LUN: file: /opt/cookbook.share [ 1605.923005] Number of LUNs=1 [ 1605.923520] g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11 [ 1605.923548] g_mass_storage gadget: userspace failed to provide iSerialNumber [ 1605.923562] g_mass_storage gadget: g_mass_storage ready [ 1605.923607] dwc2 20980000.usb: dwc2_hsotg_enqueue_setup: failed queue (-11) [ 1605.926714] dwc2 20980000.usb: bound driver g_mass_storage [ 1606.099808] dwc2 20980000.usb: new device is high-speed [ 1606.115562] dwc2 20980000.usb: new address 12 [ 1606.147599] g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage
There's more...
Let's say you don't want to pick between virtual serial or virtual Ethernet, but want both! For this, there is an OTG option called g_multi
. There is quite a long list of options you can use to make your Raspberry Pi Zero act like a variety of USB devices: a webcam, a printer, or even a MIDI device!
Note
You'll inevitably run into situations where things don't work exactly as described. There are a few ways to tackle this. First, check the Raspberry Pi forums and search for the problems you are experiencing: it is likely that you aren't the only one. The Raspberry Pi community is active in helping other users figure things out. Another approach is to start clean. If you have an extra SD card, put a fresh copy of Raspbian on it and try your hack again. It's possible that all of the prior changes and updates are causing a conflict in some way. If you have your Samba share set up, it is easy enough to copy anything you might want to keep on your current SD card before starting fresh.
- 工業機器人虛擬仿真實例教程:KUKA.Sim Pro(全彩版)
- Clojure Data Analysis Cookbook
- 軟件架構設計
- 網上生活必備
- 永磁同步電動機變頻調速系統及其控制(第2版)
- 網絡組建與互聯
- 可編程序控制器應用實訓(三菱機型)
- 電氣控制與PLC技術應用
- 工業機器人維護與保養
- Salesforce Advanced Administrator Certification Guide
- 機床電氣控制與PLC
- 基于Quartus Ⅱ的數字系統Verilog HDL設計實例詳解
- 智能小車機器人制作大全(第2版)
- 數據庫基礎:Access
- Mastering Android Game Development with Unity