OpenWRT and Backing Up

Wiki.TerraBase.info
Jump to navigation Jump to search

SEE THIS ARTICLE INSTEAD: OpenWRT and Bare Metal BackUps - Wiki.TerraBase.info

This article is mostly about doing a bare metal backup of an x86 based OpenWRT system. There are some notes about the additional items that need to be done for a more typical installation on a Router with with Flash based storage (IE, using the

Rant on Backing Up OpenWRT (Not just the configuration files, but an entire 'Drive' (Flash, SSD, etc.))

How can the entire OpenWRT installation be backed up in a fashion similar to Acronis products, Clonezilla, etc.?

The first answer is that there is no built in facility to do that. Dang! Why not? Well, first of all, OpenWRT is intended to be installed on small router devices where this type of backup (Disk / Drive Imaging, Bare Metal BackUp, etc.) just isn't a thing. Second, and largely because of something mentioned in the first item, is that a program like that can't be crammed into a small enough space to fit on small router device.

Spoiler Alert: RESTIC

What about Baquacil, err, wait, that's a pool chemical. Bactine! No, that's the antiseptic. RESTIC! Yup, that's the utility for backing up in OpenWRT (the only one)

Sadly it's like EDLIN VS Microsoft Word. No offense to the nice RETIC people. It does it's job and works reliably. But seems as if was made in the 1980s (it was actually done in the 20 teens). Again, no offense, it has a ton of features and seems bullet proof.

And most of all, RESTIC is for backing up files on a Disk, Volume, or Partition. It is not capable of (nor is it intended for) backing up an entire disk or partition, including boot sectors, file tables, etc. It is not a Disk / SSD or Partition Imaging device.

Wah! But what if I want to do it anyway because... Well, because I want to

Other Choices

Clonezilla? Yup, great, but one issue. One has to boot from the Clonezilla CD / DVD / USB Flash Drive / ISO to do it. Router off, Clonezilla on.

Again; WAH! I want to do it with the Router still fully functional, IE from within OpenWRT.

What About DD?

Perfect! Except for one thing: Files might change right in the middle of a backup (because it takes a long time). Remember DD has no capability or concept of "Volume Shadow Copy" (Note: RESTIC doesn't have the capability, but it does have the concept of it, however that would require converting the entire OpenWRT install over to an LVM based Disk / SSD / Whatever 'Layout'.)

Ideal Objective

Ideally, the only solution would seem to be something like Acronis for Linux (or some other commercial product). Or something like RUBackUp and / or FSArchiver. The latter requiring the above noted conversion of OpenWRT over to an LVM based Disk / SSD / FlashDrive system. That's a lot of work. The best option would be making DD aware of Volume Snapshots and doing it that way. Ongoing work with this experiment will be to attempt changing the disk layout to include an LVM volume that allows for "Volume Shadow Copy" (Volume Snapshot, etc., IE, the Linux equivalent of that Windows feature) and using DD for a Bare Metal BackUp!

Cop Out, err, Respect!

Please remember, none of this is rant is intended to be disrespectful to the OpenWRT, RESTIC, or anyone else that might have felt slighted. It is simply a colorful way of noting how in the software world, once someone is given something, the inevitable question and desire of 'What else can it do?" comes up. That's what this is about

DD as the Chosen Solution

Back It Up

To make sure the Image is as small as possible: dd if=/dev/zero bs=1M conv=noerror,sync status=progress of=/overlay/ZeroByteFile (this will fill all unused areas of the Disk / SSD / USB Flash Drive / etc. with Zeros)

dd if=/dev/WhatEverSDx of=/mnt/WhatEverSDx/WhatEverImageName.img conv=sync,noerror bs=64K status=progress (Example: dd if=/dev/sdb of=/mnt/sda1/ALL.img conv=noerror,sparse bs=1M status=progress)

Remember, using DD to clone /dev/WhatEverSDx is not enough. There's also the /overlay Directory / Loop Device to consider. See the "Overlay" Section below.

Create a "Device" to Mount, IE Associate a File with a Loop Device Node

losetup -fP WhatEverImageName.img (To see mounted "Devices": losetup -a), (f = File Name to configure as a Loop Device, P = Scan Partition Size, a = Show All Loop Devices, b = sector size)

"Man" Page for LOSETUP: https://man7.org/linux/man-pages/man8/losetup.8.html

Loop Device: https://en.wikipedia.org/wiki/Loop_device

Mount the Image

mount -t squashfs /dev/loop7 /mnt/squash

Keep in mind this only works with IMG Files created from Partitions. If an entire disk drive is imaged, the above method won't work. However...

Mount an Image of an Entire Disk

If an entire disk is imaged with DD, then a "map" of it can be viewed with CFDISK: cfdisk /dev/WhatEverLoopDevice (Example: cfdisk /dev/loop5) (Remember, use losetup -a to see "Devices")

Examining the output of CFDISK will show the names of the individual Partitions available: /dev/WhatEverLoopDevicePx (Example: /dev/loop6p1).

With the above information garnered from CFDISK, then mount individual Partitions: mount -t WhatEverFileSystem (squashfs, ext4, ntfs-3g, etc.) /dev/WhatEverLoopDevice /mnt/WhatEverMountPoint: mount -t squashfs /dev/loop6p2 /mnt/Loop6/p2 (Also: losetup -d /dev/WhatEverLoopDevice will delete the loop device)

Note on: LOSETUP -d WhatEverLoopDeviceName

Bad news. The delete command doesn't always work. After issuing the command on a legitimate Loop Device, and with no errors, the Loop Device will persist. Checking it with LOSETUP -a shows it is still there. Accessing it with CFDISK or MOUNT still works. Oh, well. Windows solution time. Reboot.

Overlay, OverlayFS, Union Mount, SquashFS, F2FS, and more.

"QuickD"

Real quick, here's what's going on with a typical Router based installation of OpenWRT that is stored on Flash Memory Storage.

OpenWRT is essentially installed on devices as if all of it's code were stored in an EPROM and just plugged into the board of a router. There are a bunch of abstraction layers that make this possible, both in the installation and function of OpenWRT, but that's what is happening.

How is it done? Using the Overlay / Union Mount capabilities of Linux. Sorry, not here to explain that fully, hence the above links.

Backing up the /overlay Directory

Ideally (since LVM isn't typically available to do "Volume Shadow Copies"), one would boot to an alternative OS, use DD to clone the /overlay "Partition" / "Device". Easy with an x86_64 based install of OpenWRT, but also very possible with a Router based on Flash Memory Storage too. For the Router scenario, again ideally, if the /overlay "Partition" / "Device" is on an external USB Flash Drive (as it should be if a router has a USB 3.0 port), just pop it into a "Linux Box" (virtual or otherwise) for backing up. It can also be done "live" without much chance of something being modified during the cloning process.

  • Make a BackUp: While booted to an alternate OS (Rocky Linux, another OpenWRT install on a USB Flash Drive, etc.), in this example, booted to another OS, do... Wait, but first;
    • Locate where the /overlay Partition is;
      • LSBLK will show the connection between the /overlay Directory and what it is "connected to" (hint for x86 systems, look in the /boot/grub/grub.cfg file)
        • For an OpenWRT install with /overlay on a USB Flash Drive: /overlay will be on /dev/sdXy (where X is a letter and y is a number, example: sda1)
        • For an OpenWRT install with /overlay on a SATA Disk Drive / SSD: /overlay will be on a Loop Device, roughly equivalent to a VHD or VHDX File where a file represents a storage device
          • Use this command: losetup -a (it will display something like this: /dev/loop0: (Some Numbers) (/sda2), offset 3014656 (/dev/loop0 is the device and /sda2 is the "file", actually a real drive))
    • "Create" a LOOP Device (For an x86 (used interchangeably with x86_64, yes, yes, don't say it));
      • Generic Syntax: losetup -o OffsetInBytes /dev/loopX /WhatEverPath/WhatEverFileName
      • OpenWRT specific Syntax: losetup -o 3014656 -fP /WhatEverPath/WhatEverFileName (3014656* is the offset picked by the OpenWRT developers, losetup will pick the next available LOOP#)
      • OpenWRT Syntax Example: losetup -o 3014656 -fP /mnt/sda1/OpenWRT_BackUp.img
    • Mount the LOOP Device (again for an x86 system)
      • Example: mount -t f2fs /dev/loop1 /mnt/loop1 (mkdir /mnt/loop1 before this command of course)
    • ...and that's it for x86 devices. From there, just cp

*If you haven't spotted it yet, but you've been asking, "Where is that file that OpenWRT mounts a Directory named /overlay as a LOOP Device?" Answer: There is no file. It is simply a sector on the Disk / SSD Drive to the end of it. And you might ask, "But won't it be overwritten?" Answer: Nope, because everything before it is considered ROM (as in no write), so it won't expand. And yes that means the entire disk drive (well, at least the OpenWRT partitions) is considered a File that breaks down into a ROM Section and an /overlay Section.

Why 3014656 Bytes as the offset? Well, it works out to 5888 Sectors (512 Bytes / Sector, $1700 (that's Commodore Hexadecimal Notation)), 1472 Sectors (if translated to 2048 Bytes / Sector, $5CO), so nothing really jumps out. If it was the 1980s, there might be some unique point in the CHS format where this makes sense. Some other alignment? Doesn't make any sense with SSDs, but maybe something with HDDs. PI ( π )? Nope, that's 3.141592. Maximum "ROM" size (with room left for expansion)? That seems so much like Bill Gate's comment on 640K should probably be about enough for anyone, when putting important stuff above the top of RAM, thus blocking easy expansion and requiring workarounds.


mount -t f2fs /dev/loop1 /mnt/loop1

MBR Gap

And it seemed like all the backup stuff was done. Nope. There's also a 'hidden' (not on purpose) region in the MBR Gap that contains stage 1.5 of the GRUB Boot Loader. See this: OpenWRT x86 Style Disks and Booting - Wiki.TerraBase.info

Summary of Everything Backed Up

  • MBR
  • MBR Gap
  • Boot Partition
  • Root Partition
  • Overlay "Partition"

And More...

What about taking that RAW Image created by DD and turning it into a VHD File (or just about any other virtual disk format)