"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/usbbootcd usbbootsudo apt-get install libusb-1.0-0-devmakeDownload latest Raspbian Stretch image to directory "downloads".
cd ~mkdir downloadscd downloadswget https://downloads.raspberrypi.org/raspbian_lite_latestUnzip image:
unzip raspbian_lite_latestSetup loop device (and verify "losetup -a"):
cd ~losetup -P /dev/loop0 downloads/[20..-..-..]-raspbian-stretch-lite.imglosetup -aCreate two mountpoints for boot and rootfs directories...
mkdir -p /pi/{boot,root}...and mount the loop devices there:
mount /dev/loop0p1 /pi/bootmount /dev/loop0p2 /pi/rootInstall nfs-server:
apt install nfs-kernel-serverMake 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/exportsSave and restart the NFS server.
systemctl start nfs-server.serviceexportfs -aEdit 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 followkernelThen 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:d2Let’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:staticip=<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-toolsAdd these modules to the modules file:
sudo vi /etc/initramfs-tools/modulesg_ether libcomposite u_ether udc-core usb_f_rndis usb_f_ecmSave 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.serviceConnect 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.14Does your Pi connect to the internet?
apt update && apt upgrade -yYes, 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.