"On-The-Go" OTG-Ports allow USB connector operate in two different modes
First start by downloading the USBBoot utility and follow the instructions to compile the program.
(requires apt-get install git, build-essential)
git clone --depth=1 https://github.com/raspberrypi/usbboot
cd usbboot
sudo apt-get install libusb-1.0-0-dev
make
Download latest Raspbian Stretch image to directory "downloads".
cd ~
mkdir downloads
cd downloads
wget https://downloads.raspberrypi.org/raspbian_lite_latest
Unzip image:
unzip raspbian_lite_latest
Setup loop device (and verify "losetup -a"):
cd ~
losetup -P /dev/loop0 downloads/[20..-..-..]-raspbian-stretch-lite.img
losetup -a
Create two mountpoints for boot and rootfs directories...
mkdir -p /pi/{boot,root}
...and mount the loop devices there:
mount /dev/loop0p1 /pi/boot
mount /dev/loop0p2 /pi/root
Install nfs-server:
apt install nfs-kernel-server
Make the /pi/root folder a NFS mount point, add this in the NFS config file `/etc/exports`
echo "/pi/root 192.168.100.10(rw,sync,no_subtree_check,no_root_squash)" | tee -a /etc/exports
Save and restart the NFS server.
systemctl start nfs-server.service
exportfs -a
Edit config.txt
in the boot directory (/pi/boot) to enable USB OTG mode and add some instructions to add initramfs. This will load extra kernel modules, so the Pi Zero can use the USB cable as an ethernet connection.
Add these 4 lines in config.txt
:
# enable OTG
dtoverlay=dwc2
# set initramfs
initramfs initrd.img followkernel
Then add and make your cmdline.txt
look like this:
otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/nfs nfsroot=10.42.0.1:/pi/root rw ip=10.42.0.14:10.42.0.1::255.255.255.0:pi:usb0:static elevator=deadline modules-load=dwc2,g_ether fsck.repair=yes rootwait g_ether.host_addr=5e:a1:4f:5d:cf:d2
Let’s explain this. We need NFS mount (root=/dev/nfs), we set the network address (nfsroot=10.42.0.1:/pi/root rw) rw is read/write. We need the modules dwc2 ethernet gadget and g_ether and we set a fixed ethernet MAC address to ease, and we set the network addresses.
ip=10.42.0.14:10.42.0.1::255.255.255.0:pi:usb0:static
ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>
Remember Ubuntu/Linux will set USB networks on the 10.42.0.1 address range, and we give the pi the fixed address 10.42.0.14. Please read my other posts about setting up an USB connnecting with the pi Zero, if you need more information.
Now we need to make a initramfs to supply some needed kernel modules. This is the best done on another Pi 1 or Pi Zero. We need to add the g_ether module and it dependencies in initramfs.
First install the needed tools.
sudo apt install initramfs-tools
Add these modules to the modules file:
sudo vi /etc/initramfs-tools/modules
g_ether
libcomposite
u_ether
udc-core
usb_f_rndis
usb_f_ecm
Save and create the initramfs .
sudo update-initramfs -c -k `uname -r`
It will be saved (initrd.img-4.9.80+ or newer) in the boot directory. Copy that file to the boot directory on your laptop: /pi/boot/
, and name it initrd.img
. As long it is the same name as in the config.txt
file, it’s OK.
Now where nearly done.
Modify /pi/root/etc/fstab
in the Pi root directory, and comment out (or delete) the last two lines. The Pi shouldn’t try to mount SD-card images anymore.
Update: Then we need SSH to start automatically. (Thanks to Charlie for pointing this out. I forgot to document this step at first). An empty ssh file in boot will not do the trick this time, so we need to make it start it by adding this symlink in pi/root
:
ln -s /lib/systemd/system/ssh.service /etc/systemd/system/multi-user.target.wants/ssh.service
Connect your Pi. Run the utility rpiboot:
usbboot/rpiboot -d /pi/boot/
Make sure you set the connection on shared
. And your Pi Zero will boot, a bit slowly.
ssh pi@10.42.0.14
Does your Pi connect to the internet?
apt update && apt upgrade -y
Yes, and actually quite fast!
The Raspberry Pi Zero is really a 5 dollar computer in the end. No SD card, no keyboard, no monitor, no mouse needed. Just a Zero and an (old Phone) USB micro cable.
All your changes will persist. The image in your directory will update. When you decide you need a SD card in the end, just flash the image to a SD card.