Create OpenWrt partition layout for an sd card in a Pi 4


I am using rsync to make backups of the boot and root file systems on my OpenWrt which is running on a Pi 4 (sd card). I want to script those backed up boot and root file systems back on to a raw image file that I created with dd and set up with a device block at /dev/loop0 then mounted to /mnt/boot and /mnt/root on another (Ubuntu) OS.

My question:
I got this working on Ubuntu and was using Parted there to create the OpenWrt partition layout for boot and root. But Parted is not in the opkg feed.

How should I make the partition layout on my raw image file when running the script on OpenWrt itself? I've tried gdisk but could not get the sd card to boot.

What partition type do I need for the boot partition? I tried EF00, but no joy. I formatted the bootfs as Fat 16 like:
# mkfs.fat -F 16 /dev/loop0p1

Maybe I should have made it fat32? Boot disks for EFI-based systems.



I think the boot partition must be FAT32. Pi booting is not EFI, it's strange.

When I view the OpenWrt Pi firmware in Gparted the first partition is fat 16 and has the partition labels - boot and lba.

I can reproduce that in a script on a system (Ubuntu) that has parted. But I want to run such a script on OpenWrt itself. I think my only option is to use gdisk or sgdisk which I'm not sure how to use.

This is the layout of an image file I made... when I dd it to an sd card it doesn't boot:

root@OpenWrt:/mnt/usb/openwrt/BackupOfImages# gdisk /dev/loop0
GPT fdisk (gdisk) version 1.0.6

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/loop0: 2457600 sectors, 1.2 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5479C6D4-8D1D-4449-B797-95B35816C97C
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 2457566
Partitions will be aligned on 2048-sector boundaries
Total free space is 16350 sectors (8.0 MiB)

Number Start (sector) End (sector) Size Code Name
1 8192 139263 64.0 MiB EF00 EFI system partition
2 147456 2457566 1.1 GiB 8300 Linux filesystem

Command (? for help):

I formatted the boot Partition 1:
# mkfs.fat -F 16 /dev/loop0p1

And the root Partition 2:
# mkfs.ext4 /dev/loop0p2

I'll try with fat32 on Partition 1. Am I using the correct gdisk partition codes (EF00 & 8300)?



Every one I've looked at is 0x0c, FAT32 (LBA). Also the partition table is MBR only. I'm not sure if GPT is supported.

I create an image file like this:
# dd if=/dev/zero of=loopbackfile.img bs=4M count=300

I create a loopback device for it (e.g: /dev/loop0):
# losetup -fP loopbackfile.img

On Ubuntu I can partition this "drive"...

parted --script /dev/loop0 \
		mklabel msdos \
		mkpart primary fat16 4MiB 68MiB \
		mkpart primary ext4 72MiB 100% \
		set 1 lba on \
		set 1 boot on

Then I can format those partitions, mount them etc.. I can then restore backed up OpenWrt boot and root file systems and write that image to an sd card and it boots in a Pi4.

Is it possible to do this all on OpenWrt? So far I think NO because Parted is not available. Fdisk can only make one primary partition and no ext partitions. That's the only reason I was working with gdisk and a GPT partition table.

Thank you,


As far as I can remember, raspberries use the partition UUID to define the root partition. Look at your /boot/cmdline.txt and the param root=.

The copy of your root partition must have the same partition UUID as the original partition.

I got it working with sfdisk. I wasn't sure if fdisk could do it but it can it seems. Here is what I did...

I created an image file and hooked it up with a loopback device (/dev/loop0)

# dd if=/dev/zero of=$imgName bs=4M count=300
# losetup -fP loopbackfile5.img

# opkg install sfdisk

I copied the partition structure from a working OpenWrt installation I have running in a Pi4.
# sfdisk -d /dev/mmcblk0 > mmcblk0.sfdisk

I restore this partition layout to loopbackfile5.img
# sfdisk /dev/loop0 < mmcblk0.sfdisk

-- Now I formatted the bootfs
# mkfs.fat -F 16 /dev/loop0p1
-- And the rootfs
# mkfs.ext4 /dev/loop0p2

-- Mount the partitions
# mount /dev/loop0p1 /mnt/boot
# mount /dev/loop0p2 /mnt/root

After that I could restore rsync'd boot and root file systems to those partitions and it booted up!

I can also compress the .img file... (goes from 1.3GB to 180MB)
# gzip -k loopbackfile5.img

Then can use Balena etcher to flash the .img.gz file to an sd card and it also boots.



If your problem is solved, please consider marking this topic as [Solved]. See How to mark a topic as [Solved] for a short how-to.

This topic was automatically closed 0 minutes after the last reply. New replies are no longer allowed.