Dd - How to clone root filesystem from LVM?

I use LVM on top of LUKS.

I want to know how I can clone my root filesystem EXACTLY and restore it using dd .

My layout is as follows:

$  sudo vgs
  VG       #PV #LV #SN Attr   VSize   VFree  
  vgfedora   1   1   0 wz--n- 929.99g 229.99g

$  sudo lvs
  LV     VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  fedora vgfedora -wi-ao---- 700.00g 

I don't have a separate home and root partition, it is all in one logical volume.

I want to know how I can clone and restore this partition exactly using dd.

Will something like this work:

dd if=/dev/vgfedora/fedora of=disk.img status=progress

My questions are as follows:

  1. What will be the block size for dd ?

This seems to be the most important question. What will be the block size for dd ? It needs to match, so that an exact copy can be restored without any problems.

  1. What will be the count parameter for dd ?

What do I put as my count parameter ? Again, it needs to match my filesystem exactly.

  1. Can I use gzip in some way to compress the image ?

I will be storing the image on my external HDD, thus the more I can save on space the better.

  1. How am I going to restore the image ?

I am doing this because I want to migrate to thin LVMs.

How do I restore the image to a logical volume after I have the image ? I want to restore it to a logical volume.

Thanks.

You should be able to calculate the block size on Linux filesystems from the following:

linux# cat /proc/partitions
major minor  #blocks  name

   8       16  488386584 sdb
   8       17     248832 sdb1
   8       18     499712 sdb2
   8       19    8000512 sdb3
   8       20  479635456 sdb4
   8        0  488386584 sda
   8        1     248832 sda1
   8        2     499712 sda2
   8        3    8000512 sda3
   8        4  479635456 sda4
   9        2  479503360 md2
   9        0     498688 md0
   9        1    7995392 md1

Or you can use other utilities to determine blocksizes, for example:

linux# blockdev --getsize64 /dev/sda
500107862016

linux# cat /sys/class/block/sda/size
976773168

Is this what you are looking for? How to calcluate the block size?

Well yes, this is definitely part of my question.

But please note I have to calculate the block size within a logical volume, that is not listed in /sys/class/block

This is the output of /sys/class/block on my system:

total 0
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 dm-0 -> ../../devices/virtual/block/dm-0
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 dm-1 -> ../../devices/virtual/block/dm-1
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 pktcdvd0 -> ../../devices/virtual/block/pktcdvd0
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sda -> ../../devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sda1 -> ../../devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sda2 -> ../../devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda2
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sda3 -> ../../devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda3
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sdb -> ../../devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host2/target2:0:0/2:0:0:0/block/sdb
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sdb1 -> ../../devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sdb2 -> ../../devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb2
lrwxrwxrwx. 1 root root 0 Nov 23 13:00 sr0 -> ../../devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sr0

As you can see there is no mention of my LVM devices here.

Also reading the man page for blockdev you come across this peculiar sentence:

--getbsz
              Print blocksize in bytes.  This size does not describe device topology.  It's size used internally by kernel and  it  maybe  modified  (for  example)  by
              filesystem driver on mount.

Should I use the kernel block size or the filesystem block size ?

As they both might be different ?

Most sources on the net mention the LVM blocksize is calculated from the filesystem block size, which makes sense to me.

See for example:

If I were you, I would just try it. Normally we stage these kinds of moves, so I would just pick the params I think are correct and try to clone it; using trial-and-error, on my staging platform.

Do you have a reason for this? We use 'dd' as you say to get an exact mirror of the filesystem, warts and all. Do you have software keys you are trying to copy? Is there a need for inode numbers not to change? Or what?

In this context (clone a disk) it needs to be the same, or for better performance a multiple of, the disk sector size (typically 512 or 1024). In this context it will control how many sectors are hoovered up per payload. Larger block size will mean less I/O work and therefore a bit faster.

You won't need to specify 'count' if you are copying the whole filesystem device. By default, 'dd' will copy the whole device (and report the count to you at the end). You would use 'count' to only copy a given number of sectors.

You would have to use 'dd' to restore it. The 'dd' image will copy the whole filesystem sector-by-sector including free space (so it will be larger than a 'tar' or 'cpio').

I assume that you are regularly backing up your production filesystem, so if you needed to disaster recover (oh, I mean clone it) you would restore from that.

So why do you need to use 'dd'?

Another thought I've had is.............

If you are using LUKS disk encryption you need to check whether 'dd' would copy encrypted disk sectors or de-encrypt them first. I'm not sure how you would recover from a 'dd' file which was sector-by-sector encrypted. You need to check that.

Perhaps someone else on this forum may know what will happen in this instance.

Example:

dd if=/dev/vgfedora/fedora | gzip /path_to_disk/disk.img.gz status=progress

I ended up using partclone for my copy.

I cloned after I decrypted and mounted, so I should be getting unencrypted data in my image right ?

Ended up using partclone. That way free space is not copied.