Skip to content

Instantly share code, notes, and snippets.

@Nilpo
Last active December 26, 2022 04:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilpo/314853f8224d493940ee06fd1070c28a to your computer and use it in GitHub Desktop.
Save Nilpo/314853f8224d493940ee06fd1070c28a to your computer and use it in GitHub Desktop.
How to boot RetroPie (or Raspbian) from a USB drive on the Raspberry Pi 3 B(+)

Boot RetroPie from USB on the Raspberry Pi 3 B(+)

You will need a Raspberry Pi 3 B(+) with a microSD card running RetroPie (or Raspbian).

  1. Update Raspbian.

    $ sudo apt-get update && sudo apt-get upgrade -y
  2. Make sure you are running on the latest Raspberry Pi firmware.

    $ sudo BRANCH=next rpi-update
  3. Enable booting from USB. (This feature is experimental and cannot be reversed.)

    $ echo program_usb_boot_mode=1 | sudo tee -a /boot/config.txt
    $ sudo reboot
  4. (Optional) Verify that USB booting was enabled successfully.

    $ vcgencmd otp_dump | grep 17:
    17:3020000a
  5. Set up the USB drive. This should be blank and unintialized. (No existing partitions.) The drive should be mounted as /dev/sda, but you should verify this before continuing.

      # start the partition manager
    $ sudo parted /dev/sda
      # add a FAT partition table
    (parted) mktable msdos
      # create a FAT32 boot partition
    (parted) mkpart primary fat32 0% 100M
      # create an EXT4 partition for RetroPie
    (parted) mkpart primary ext4 100M 100%
      # double check that you are creating two partitions
    (parted) print
    (parted) quit
  6. Format the partitions you just created on the USB drive.

    $ sudo mkfs.vfat -n BOOT -F 32 /dev/sda1
    $ sudo mkfs.ext4 -n /dev/sda2
  7. Install rsync.

    $ sudo apt-get update && sudo apt-get install rsync -y
  8. Copy the files from the SD card to the USB drive. (This step may take some time.)

      # mount the target Linux partition
    $ sudo mkdir /mnt/target
    $ sudo mount /dev/sda2 /mnt/target
      # mount the target boot partition
    $ sudo mkdir /mnt/target/boot
    $ sudo mount /dev/sda1 /mnt/target/boot
      # synchronize the files between the SD card and USB drive
    $ sudo rsync -ax --progress / /boot/ /mnt/target
  9. Mount the virtual filesystem on the USB drive.

    $ sudo mount --bind /dev dev
    $ sudo mount --bind /sys sys
    $ sudo mount --bind /proc proc
  10. Reset the SSH keys on the new installation. (They match the old host.)

    $ sudo chroot /mnt/target
    # rm /etc/ssh/ssh_host*
    # dpkg-reconfigure openssh-server
    # exit
  11. Unmount the virtual filesystem.

    $ sudo umount dev
    $ sudo umount sys
    $ sudo umount proc
  12. Reconfigure the boot files on the USB drive to search for the operating system on the new drive.

    $ sudo sed -i "s,root=/dev/mmcblk0p2,root=/dev/sda2," /mnt/target/boot/cmdline.txt
    $ sudo sed -i "s,/dev/mmcblk0p,/dev/sda," /mnt/target/etc/fstab
  13. Unmount the USB drive and power down.

    $ cd ~
    $ sudo umount /mnt/target/boot
    $ sudo umount /mnt/target
    $ sudo poweroff
  14. Remove the microSD card and boot from the USB drive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment