OpenWRT and Bare Metal BackUps
For this article, the x86_64 (referred to as just x86 in this article) version of OpenWRT on an EXT 4 installed on a BIOS / MBR / DOS partitioned Disk / SSD will be the main focus. SquashFS will be addressed too. All of the below information was tested on an OpenWRT 19.07.10 EXT4 "Combined" (as in MBR, GRUB, Boot, and Root Partition (no Overlay)) based system.
The overall objective beyond a bare metal backup is to eventually move an x86_64 SquashFS based system to an LVM EXT4 based system.
Backing Up
The "Backing Up" or BackUp Process is the first part of the cloning a system too.
Relevant "Partitions" of an OpenWRT x86
- MBR (first Sector, 512 Bytes in size, Sector Number 0 (zero), contains stage 1.0 of GRUB and Partition table)
- "MBR Gap" (contains stage 1.5 of GRUB)
- Notes on "MBR Gap": Research indicates this "MBR Gap" can vary quite a bit depending on the OS. Even different versions of Windows have different size "Gaps". IE, the size is arbitrary and not set in stone. Lesson? Use a Partition Utility to look and see how things are arranged for an OS. It also seems that the GRUB 1.5 Stage is made to fit in 32K so it will fit into the MBR Gap.
- Boot Partition (contains grub.cfg file with settings and VMLINUZ (a compressed Kernel Image in "Boot Executable bzImage" format)
- Root Partition (all the files that comprise an OpenWRT installation, plus user added files, packages, settings, etc. on an EXT4 installation)
- Overlay "Partition" (only for SquashFS, not relevant to EXT4 based installations)
What to do / How to do it / Examples of...
Copy the MBR and "MBR Gap" (GRUB Stage 1, GRUB Stage 1.5 (containing the CORE.IMG file)): dd if=/dev/sdX of=/dev/sdY bs=512 count=511
- Breaking it down into two steps, see below (but watch out, because for some reason DD doesn't seem to do something right regardless of the order the below are done, so better to get the MBR and MBR Gap all at the same time, so just giving the below as an option / example); ...and figured it out for the 'writing to partition' part, syntax error, should have used seek, not skip.
- Copy the "MBR Gap" (GRUB Stage 1.5, containing the CORE.IMG file):
dd if=/dev/sdX of=/dev/sdY bs=512 skip=1 count=511
- Copy the MBR (GRUB Stage 1 and Partition Table):
dd if=/dev/sdX of=/dev/sdY bs=512 count=1
- Copy the "MBR Gap" (GRUB Stage 1.5, containing the CORE.IMG file):
- Example of saving to an Image File:
dd if=/dev/sdb of=/mnt/sda1/MBR_and_MBR-Gap.img conv=noerror bs=512 count=511 status=progress
- Example of saving to a Disk / SSD from an Image File:
dd if=/run/media/root/NTFS/MBR_and_MBR-Gap.img of=/dev/sdc conv=noerror bs=512 status=progress
- Why 511 sectors? Use CFDISK (or any other Partitioning Utility) and you'll see that the first Sector of the first Partition starts at Sector 512. That is just the arrangement the OpenWRT developers chose. Research indicates this "MBR Gap" can vary quite a bit depending on the OS. Even different versions of Windows have different size "Gaps". IE, the size is arbitrary and not set in stone. Lesson? Use a Partition Utility to look and see how things are arranged. Oh, one other note on the research. It seems that the GRUB 1.5 Stage is made to fit in 32K. So why did the OpenWRT developers that focus so much on refinement and minimization use a whopping 256K for the MBR Gap? The answer appears to be known only to them as there is no direct way to ask them. ChatGPT was asked and the answer was so bad, it was way worse than not knowing.
Copy the First Partition (/boot Directory): dd if=/run/media/root/NTFS/Boot.img of=/dev/sdc skip=511 conv=noerror bs=512 status=progress
- Example of saving to a Disk / SSD from an Image File:
dd if=/dev/sdb of=/mnt/sda1/MBR_and_MBR-Gap.img conv=noerror bs=512 count=511 status=progress
Copy the Second Partition (/root equivalent): dd if=/dev/sdX2 of=/dev/sdY2 bs=128M conv=noerror progress=status
Flush any buffered information to disk: sync
Syntax Notes;
- if: Input File (or Device)
- of: Output File (or Device)
- bs: "How much to copy from one device to another at a time"
- conv=noerror: "Don't stop, just do what I told you to do, and don't give an excuse"
- progress = status: Show the progress of the copy process (only for a certain version of DD and up)
- sync: Write all buffered blocks to disk (just to be safe)
Other Notes
GRUB
OpenWRT specifically mentions GRUB 2 in their documentation. But the way it works on an x86_64 EXT4 installation of OpenWRT would convince anyone that it is Legacy GRUB (except for the GNU GRUB version 2.02 heading at the top of the screen). The way it is configured via /etc/grub/grub.cfg would also convince anyone that Legacy GRUB was being used. So, the conclusion is that GRUB 2.02 is indeed being used, but since it is backwards compatible, it is in 'Legacy Mode'.