官术网_书友最值得收藏!

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...

  1. 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.

  2. Let's make a mini "filesystem" for our Zero flash drive:
    sudo dd if=/dev/zero of=/opt/cookbook.share bs=1Ml count=512
    
  3. Then, we will format this into a FAT32 system with the mkfs.fat command:
            sudo mkfs.fat /opt/cookbook.share
    
  4. Set permissions:
            sudo chwon 755 /opt/cookbook.share
    
  5. Finally, reboot and connect your USB cable:
    sudo reboot
    
  6. 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
    
  7. On a Windows machine, you will probably see drivers loading, and then you will see a new icon under USB devices:
  8. From, here, looking at disk management, we will see we have a new, initialized volume:
  9. 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.

主站蜘蛛池模板: 普兰县| 东阳市| 那坡县| 灵寿县| 正定县| 禄劝| 湛江市| 江津市| 崇仁县| 寿阳县| 贵州省| 上高县| 和林格尔县| 怀远县| 阜康市| 大田县| 鲜城| 周至县| 罗城| 邹城市| 台北市| 新民市| 南川市| 皋兰县| 象山县| 师宗县| 喀喇| 中江县| 疏附县| 郸城县| 渭南市| 聂拉木县| 密山市| 内江市| 昆明市| 罗平县| 农安县| 温州市| 安国市| 藁城市| 天峻县|