My goal is to create a bootable backup of my system without restarting, I will use qemu-img to convert a raw dd image to vhdx and run it in Hyper-V.

If I boot into a liveCD and run:

dd if=/dev/sda of=/nfs/sda.raw conv=noerror,sync,notrunc

I can convert that to a vhdx and run it directly in Hyper-V without much effort.

fdisk -l output:

Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 7555D04B-D6DC-4ABB-8863-529E874BADAD

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   2000895   1998848   976M EFI System
/dev/sda2  2000896 500117503 498116608 237.5G Linux filesystem

However you can't use that command while the system is online, so I am using the dattobd driver to copy my entire ext4 filesystem like this:

dd if=/dev/datto0 of=/nfs/sda2.raw bs=1M

This effectively copies all the data on /dev/sda2 correctly, I've verified that because the hash is the same if I where to use the liveCD and dd the /dev/sda2 partition. Now I can boot into a liveCD and copy /dev/sda1, that's the EFI partition so it doesn't change very often unless I update grub or something Linux kernel related, I'm fine with restarting just to backup that partition once in a while, but if someone knows a way to do that online please tell me.

Now I have /dev/sda1 and /dev/sda2 in two separate files, my question is how can I assemble these two into a single file that would essentially give the same ouput as running dd if=/dev/sda of=/nfs/sda.raw conv=noerror,sync,notrunc on a liveCD? Would you just need to align the bytes perfectly? I've search for this everywhere and no luck. Any help is very much appreciated!

| improve this question | | | | |
  • You can use LVM to assemble sda1, sda2 into one logical / "virtual" disk. You also need have another section for the partition table. I did this once based on information I found here on SE, but I fail to find it now. The closest answer is this one, but the external link is now broken. – Hermann Mar 21 at 23:52

You could loop-mount the whole-disk image with partition support, and then dd the updated sda2 image into place within it:

losetup -P /dev/loop0 /nfs/sda.raw  #this will create /dev/loop0p1 and loop0p2
dd if=/nfs/sda2.raw of=/dev/loop0p2
losetup -d /dev/loop0

Alternatively, your partition table indicates that your first partition starts at sector #2048, so your disk has 2048 * 512 bytes = 1 MiB of space before the first partition ("head"), and 500118192 - 500117503 - 1 = 688 sectors (344 kiB) of space at the end ("tail") outside of any partition. ( -1 because the first sector is #0, not #1)

The first partition ends at sector #2000895 and the second one starts at #2000896, so there is no unused space between them.

You're using GPT partition table, which includes a backup copy of the partition table at the end of the disk, so the "tail" part will be necessary to capture also. It starts from sector #500117504 and continues to the end of the disk.

So, you could capture the contents of the "head" and "tail" parts separately, then put it all together:

dd if=/dev/sda of=/nfs/head.raw bs=512 count=2048
dd if=/dev/sda of=/nfs/tail.raw bs=512 skip=500117504
cat /nfs/head.raw /nfs/sda1.raw /nfs/sda2.raw /nfs/tail.raw > /nfs/sda.raw

Unless you modify the partition table, you'll need to capture the "head" and "tail" portions only once: they should not be changing during normal use of the system.

| improve this answer | | | | |
  • Your method works perfectly, even the sha1sum matches the original. Thank you very much! Now I know this isn't part of the original question, but do you know why the sda1 partition doesn't match when I DD it on a liveCD and when I DD it online? What's different and what changes between online and offline? I would love to fully backup without restarting but so far this is 100% acceptable, thank you again. – polyemia Mar 22 at 13:12
  • sda1 or your EFI system partition is typically FAT32, which includes a bit that will be cleared on mounting the filesystem and set again when the filesystem is cleanly unmounted. That allows detection of unclean mounts (i.e. system crashes), but it is also enough to completely change the sha1sum of the partition if you dd it while it's mounted. But that partition is really only needed at boot time and when installing kernel updates: you can simply unmount it for dd'ing while the system is running, and then mount it again once the imaging is complete. – telcoM Mar 22 at 13:22
  • Just tried to unmount and DD it online, it now matches the offline raw image with sha1sum, now I can make a full backup without restarting, thank you again! – polyemia Mar 22 at 13:59
  • We both learned new things today: I was not aware of the existence of the dattobd driver. Thank you for mentioning it; it looks useful! – telcoM Mar 22 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.