[Raspberry Pi 4] USB Boot: Stop Using an SD Card and Boot from an SSD

RaspberryPi

This is a memo from migrating a Raspberry Pi web server that had been running from an SD card to an SSD with USB boot. I had written about something similar before; this time only the data-copying method is different. This procedure may be easier.
The steps are for Ubuntu, but I think almost the same method can be used with Raspberry Pi OS. This is amateur work, so follow it at your own risk if you use it as a reference.

Test Environment

Raspberry Pi 4 4GB Ubuntu 20.10
Migration source: SD card Silicon Power microSD 64GB
Migration destination: portable SSD: SanDisk SSD 480GB

Check the Disk Information Before Migration

Check the current devices with the lsblk command.
Because containers are running, many loopback devices named loopXX are visible, but mmcblk0 is the SD card.

# 現在のブロックデバイスの状況
$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 88.1M  1 loop /snap/core/11996
loop1         7:1    0 37.5M  1 loop /snap/snapd/14296
loop2         7:2    0 41.3M  1 loop /snap/certbot/1671
loop3         7:3    0   49M  1 loop /snap/core18/2252
loop4         7:4    0 57.4M  1 loop /snap/core20/1244
loop5         7:5    0 65.7M  1 loop /snap/lxd/22151
loop6         7:6    0 88.1M  1 loop /snap/core/11803
loop7         7:7    0   49M  1 loop /snap/core18/2248
loop8         7:8    0 40.4M  1 loop /snap/certbot/1583
loop10        7:10   0 36.5M  1 loop /snap/snapd/14063
loop11        7:11   0 57.5M  1 loop /snap/core20/1274
loop12        7:12   0 65.9M  1 loop /snap/lxd/22116
mmcblk0     179:0    0 58.9G  0 disk
├─mmcblk0p1 179:1    0  256M  0 part /boot/firmware
└─mmcblk0p2 179:2    0 58.7G  0 part /

ループバックデバイスを非表示にするには -e 7 をつけて実行する

$ lsblk -e 7 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT mmcblk0 179:0 0 58.9G 0 disk ├─mmcblk0p1 179:1 0 256M 0 part /boot/firmware └─mmcblk0p2 179:2 0 58.7G 0 part /

Check the Destination Disk Information

Connect the SSD to the Raspberry Pi and check again. You can see that a device named sda has been added. (*This SSD had been used once to run a Raspberry Pi, so it already has partitions, but that data will be overwritten.)

$ lsblk -e 7
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 447.1G  0 disk
├─sda1        8:1    0   256M  0 part
└─sda2        8:2    0 446.9G  0 part
mmcblk0     179:0    0  58.9G  0 disk
├─mmcblk0p1 179:1    0   256M  0 part /boot/firmware
└─mmcblk0p2 179:2    0  58.7G  0 part /

Install rpi-clone and Copy the Data

Copy the data from the SD card to the SSD. I used rpi-clone for the copy. Install it by following the official procedure.

$ git clone https://github.com/billw2/rpi-clone.git 
$ cd rpi-clone
$ sudo cp rpi-clone rpi-clone-setup /usr/local/sbin

After installing it, run the clone. Stop daemons and similar services beforehand.
Specify the migration destination sda (SSD) as the argument. For the reverse pattern, copying from SSD to SD card, specify mmcblk0 as the argument.

$ sudo rpi-clone sda

Booted disk: mmcblk0 63.3GB Destination disk: sda 480.1GB

Part Size FS Label Part Size FS Label 1 /boot/firmware 256.0M fat32 – 1 256.0M fat32 – 2 root 58.7G ext4 writable 2 446.9G ext4 writable

== SYNC mmcblk0 file systems to sda == /boot/firmware (153.5M used) : SYNC to sda1 (256.0M size) / (10.0G used) : SYNC to sda2 (446.9G size)

Run setup script : no.

Verbose mode : no.

実行してよいか? yes を入力

Ok to proceed with the clone? (yes/no): yes

実行中・・・

Syncing file systems (can take a long time) Syncing mounted partitions: Mounting /dev/sda2 on /mnt/clone => rsync // /mnt/clone with-root-excludes … Mounting /dev/sda1 on /mnt/clone/boot/firmware => rsync /boot/firmware/ /mnt/clone/boot/firmware …

=============================== Done with clone to /dev/sda Start - 15:34:37 End - 15:45:35 Elapsed Time - 10:58

Cloned partitions are mounted on /mnt/clone for inspection or customizing.

Enterキーを押して、sda をアンマウントして終了。

Hit Enter when ready to unmount the /dev/sda partitions … unmounting /mnt/clone/boot/firmware unmounting /mnt/clone

Configure It to Boot from the SSD

To be safe, check the firmware version and bootloader settings.
Ubuntu does not include rpi-eeprom-update by default, so if the command is missing, install it.

# rpi-eeprom-update でファームウェアのバージョンを確認する。
# 古いファームウェアだとUSBドライブからのブートに対応していないものがある。
# rpi-eeprom-update コマンドがない場合は sudo apt install -y rpi-eeprom でインストールする
$ sudo rpi-eeprom-update
BCM2711 detected
Dedicated VL805 EEPROM detected
BOOTLOADER: up-to-date
CURRENT: 2020年  9月  3日 木曜日 12:11:43 UTC (1599135103)
 LATEST: 2020年  9月  3日 木曜日 12:11:43 UTC (1599135103)
 FW DIR: /lib/firmware/raspberrypi/bootloader/default
VL805: up-to-date
CURRENT: 000138a1
 LATEST: 000138a1

Use the rpi-eeprom-config command to check whether it is configured to boot from the SSD.

# BOOT_ORDER の設定値を確認する
$ rpi-eeprom-config
[all]
BOOT_UART=0
WAKE_ON_GPIO=1
POWER_OFF_ON_HALT=0
DHCP_TIMEOUT=45000
DHCP_REQ_TIMEOUT=4000
TFTP_FILE_TIMEOUT=30000
TFTP_IP=
TFTP_PREFIX=0
BOOT_ORDER=0x1
SD_BOOT_MAX_RETRIES=3
NET_BOOT_MAX_RETRIES=5
[none]
FREEZE_VERSION=0

Because this is BOOT_ORDER=0x1, it only boots from the SD card with this setting.
I used the official setting values as a reference. If you want it to prefer booting from the USB drive even when the SD card is inserted, 0xf14 (Try USB first, followed by SD then repeat) seems good. If you want to prioritize the SD card, set 0xf41.

# BOOT_ORDER の値を変更する
$ env EDITOR=vi sudo -E rpi-eeprom-config --edit

[all] BOOT_UART=0 WAKE_ON_GPIO=1 POWER_OFF_ON_HALT=0 DHCP_TIMEOUT=45000 DHCP_REQ_TIMEOUT=4000 TFTP_FILE_TIMEOUT=30000 TFTP_IP= TFTP_PREFIX=0 BOOT_ORDER=0xf14 # この値を変更して保存。 SD_BOOT_MAX_RETRIES=3 NET_BOOT_MAX_RETRIES=5 [none] FREEZE_VERSION=0

保存したらリブート

$ sudo reboot

After rebooting, it will boot from the USB drive. If you set 0xf41, the procedure is to shut down, remove the SD card, and boot.

It also looks like migrating the WordPress and mail server that had been running in Docker completed without problems.

Me
Me

I had kept running from the SD card because migrating the containers seemed troublesome, but this procedure made it easy.