2025年5月14日 星期三

Ensure SD Card Backups Are Flashable to Any Same-Size Card for Jetson Nano

 ※if you are not interested in the manual steps as below, there is a script to deal with the equivalent procedures for 64GB. It might also work for the other sizes if you adjust the variable LABELED_GB to match your SD card labeled capacity in GB, but this has not been tested. 
# Make the script executable (only needed once)
# chmod +x backup_jetson_nano_64GB_img.sh
sudo ./backup_jetson_nano_64GB_img.sh jetson.img /dev/sdX
# Run without arguments to show help
# ./backup_jetson_nano_64GB_img.sh
Morerover, it may be necesary to read the last session 補說 to know how to use the fully SD card capacity before flashing.

Cloning a Jetson Nano SD card with dd creates a full-size image — but not all SD cards labeled with the same size have the identical usable capacity. Some may be a few megabytes smaller, which will lead to the flashing error : Target device is too small. 

On the other hand,  if you flash the image to the larger SD card(e.g. from 64 GB to 128 GB), The system is likely to fail to boot.

This guide shows how to safely trim the unused space at the end of the imae, align it properly, and fix GPT headers — so your .img backup can be reliably flashed to other SD cards of the same advertised size (e.g. 64GB) using Etcher or dd.

零. Full backup SD card to .img

Insert the SD card and identify the device name:
lsblk -dpno NAME,SIZE,MODEL | grep -Ev 'loop|nvme|sda|boot'
Example output:
/dev/sdb     58.2G   SD/MMC

In this case, the SD card is /dev/sdb.

Create a full image backup:

sudo dd if=/dev/sdb of=jetson_backup.img bs=16M status=progress conv=fsync

Replace /dev/sdX with your SD card device. You can rename jetson_backup.img as needed.


壹. Shrink main partition and truncate the image size

Attach image as loop device:
sudo losetup -Pf --show jetson_backup.img
Example output:
/dev/loop47

Use GParted to shrink the main partition:
sudo gparted /dev/loop47

To shrink the largest ext4 partition (usually labeled "APP" — typically the root partition in Jetson Nano images), follow the steps below:

  • Right-click on the APP partition (such as /dev/loop47p1, ext4, 59.51 GiB in the screenshot).

  • Select "Resize/Move" from the context menu.

  • In the "Resize/Move" dialog:

    • Set "Free space following (MiB)" to 8192 (i.e., 8 GiB).

    • Do NOT change "Free space preceding(MiB)" value.

    • Click the "Resize/Move" button to return to the main GParted window and confirm the change.

  • Go to Edit → Apply All Operations to apply the shrink operation.

Detach the loop device:
sudo losetup -d /dev/loop47



貳. Extend image by 1 MB buffer and align to 512 bytes

Find the partition end (in bytes):
sudo parted jetson_backup.img unit B print
Example output:
Number  Start       End            Size            File system  Name
1       14680064B   63892881407B   63878201344B    ext4         APP
In this example, the end is 63892881407.

Then:
# Add 1MB buffer (1,048,576 bytes)
# 63892881407 + 1048576 = 63893929983
sudo truncate -s 63893929983 jetson_backup.img

Align to next 512-byte boundary (required for GPT):
sudo truncate -s $(( ( $(stat -c %s jetson_backup.img) / 512 + 1 ) * 512 )) \
jetson_backup.img 



參. Fix GPT backup header and verify partition table

Start gdisk:
sudo gdisk jetson_backup.img
In the gdisk prompt, type:
w
y

Inspect the partition table to ensure there are no errors
sudo gdisk -l jetson_backup.img
sudo fdisk -l jetson_backup.img 


肆. (Optional) Compress image 

Zip the image file:
zip -9 -v jetson_backup.img.zip jetson_backup.img

Then, remove the image:
rm jetson_backup.img


補說. Flash the image to the SD card and fully use its capacity.

You can flash either the .img or .img.zip file using:

Etcher (balenaEtcher)
Or via terminal for flashing the .img:
sudo dd if=jetson_backup.img of=/dev/sdX bs=16M status=progress conv=fsync

Optional but recommended: After flashing, you may run the following to relocate the GPT backup header to the actual end of the card (avoids partition warnings):
sudo sgdisk -e /dev/sdX # Relocate the GPT backup header

Optional: Extend the ext4 partition ("APP") to use the full capacity of the SD card.
sudo growpart  /dev/sdX 1     # Expand the APP partition
sudo resize2fs -f /dev/sdX1   # Resize the ext4 filesystem
sudo gdisk -l /dev/sdX        # Verify that GPT looks correct


沒有留言:

張貼留言