OpenWRT x86 Style Disks and Booting
SEE THIS ARTICLE INSTEAD: OpenWRT and Bare Metal BackUps - Wiki.TerraBase.info
For this experiment EXT 4 will be used (as opposed to SquashFS).
For Cloning a Drive
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
- Copying the MBR and MBR Gap can be combined into a single step (it was done to clarify and make an explicit point that there is important code stored in the MBR Gap necessary to boot OpenWRT with GRUB):
dd if=/dev/sdX of=/dev/sdY bs=512 count=512
. And for some reason, doing the MBR Gap first, messes up the MBR. Not a clue why. - The "MBR Gap" for OpenWRT's x86 "flavor" is 511 Sectors (Sectors 2 - 511, 512 is the beginning of the first partition, AKA /boot Partition). That's just how it is for OpenWRT because it is what they decided. And just as a historical note, the "MBR Gap" is almost twice as big as the entire capacity of a Commodore 1541 disk (single sided is 170K). And don't think OpenWRT is being wasteful of space either. If one uses CFDISK (or equivalent), the MBR GAP is 2048 Sectors (4 times that of OpenWRT, still 512 Bytes per Sector).
- If conv=sync is used an out of space error will occur.
To Convert a SquashFS version of OpenWRT to an EXT4 version of OpenWRT
- Copy the MBR, "MBR Gap", and First Partition ( /boot Directory ) per the "Cloning a Drive" Section
- Edit the /boot/grub/grub.cfg file, changing portion of the menuentry, from
rootfstype=squashfs
torootfstype=ext4
- Format the Second Partition:
mkfs.ext4 -L WhatEverName -v /dev/sda2 (-L = Label (can be named ROOT for clarity), -v = Verbose)
- Resize the Second Partition (if cloned from a "Virgin OpenWRT" install) to a larger size using GParted or CFDISK (16 GB is more than enough)
Tips;
- Use GParted to Locate Mount Points and to resize Partitions (if using a Live Rocky Linux, CentOS, etc. USB Based , the mount points for Rocky Linux, etc, end up in /run/media/root/WhatEverMountPoint by default.
A Custom MBR (GRUB Stage 1, 1.5, and 2)
Start with a new Disk / SSD / Virtual Drive, connected to a bootable OS (Rocky Linux, other "Live" Linux Distribution, etc.) then (Examples are given, rather than stating WhatEverDrive, etc.);
- wipefs -a /dev/sda (not necessary if it is a new device, but this will wipe out partition information (and oddly, not formatting information if one recreates the same size partitions)
- Use CFDISK or FDISK to create Partitions
- mkfs.ext4 -L BOOT /dev/sda1 (where -L=Label and BOOT is the label name, also note that if this is going to be an LVM Disk, the /boot Partition should not be part of the LVM stuff)
- mkdir /mnt/sda1 (creates mount point), mount /dev/sda1 /mnt/sda1 (mounts the drive)
- grub-install --boot-directory=/mnt/sda1 /dev/sda (note, that last one is /dev/sda, not /dev/sda1 which can cause a funky error message, --boot-directory=where to create the /grub2/grub.cfg stuff, because if it isn't included then grub-install will think one is referring to /boot, which is probably where the OS that these commands are being issued from rather than the OpenWRT installation)
- grub-mkconfig -o /mnt/sda1/grub2/grub.cfg (not needed for OpenWRT as there are no grub tools available to install via OPKG)
- Use the /grub/grub.cfg (yes, /grub/grub.cfg, where as above it is /grub2/grub.cfg, because all of the above was done with Rocky Linux GRUB2 style) file included with OpenWRT's x86 version as a base for making a new config file. There are no tools in OpenWRT to dynamically create a config file from settings in /etc/default, so just edit the config file by hand as the GRUB2 'understands' Legacy GRUB configuration style.
And from there, one OpenWRT system can be copied to another;
- cp -a /Source /Destination --verbose (where -a="Archive" and is equivalent to 'retain permissions', plus it does all of the symbolic link copying too as they're essentially "ShortCuts")