cloning with dd - is non-partitioned space considered the same as empty space within filesystem for resulting image size?
sda
is the disk as a whole - 160 GB
sda1
is the first partition on that disk - 8 GB (say)
sda2
is the second partition on that disk - 8 GB (say)
If you only want to clone the 16 GB of filesystem data, use dd
on sda1
and sda2
.
To restore the data to a different disk you'd have to first partition that disk (ideally identically to the original disk). I expect you'd probably have to reinstall the bootloader too.
See, for example, http://www.backuphowto.info/linux-backup-hard-disk-clone-dd
First I want to answer to your question:
what about the situation where i have two partitions that together fill up only 10% of 160G disk. what is the image size going to be for the good ol "dd if=/dev/sda of=disk.img" run: 16G or 160G?
If you run dd on /dev/sda, i.e. the whole disk sda of 160G, then disk.img will be 160G, since dd will just copy all bits on sda one by one. It makes no difference if the space after /dev/sda1 and /dev/sda2 is partitioned or not! Unfortunately, if the hard disk ever was full of data before, even if you try to compress the disk.img, it will not really get smaller because of the fact that the "empty space" on the disk surface in reality is not empty but contains the leftover bits of the previously used file systems and data. I do not know what the case for brand new hard disks that have never been used before is, though!
Additionally to this, I want to answer to your comment that you gave to RedGrittyBrick's answer. If your goal is to make a minimum size whole-disk image, then a possible solution is as follows
Create a third partition with the empty space, in your example: /dev/sda3. There is no need (but also no harm) to format it with a file system since the partition will be overwritten with zeros in the next step, eliminating any file system formatting again
Now fill the entire partition with zeros:
dd if=/dev/zero of=/dev/sda3
. This will take quite a long time and dd has no progress bar, so just be patient. At some point dd will report that the disk is full and some time after that will exit with a final report about the bits copiedIf you do not want this partition to exist, you can delete it again now. The zeros will remain on the disk surface!
Now make the single disk image as you where planning in first place:
dd if=/dev/sda of=disk.img
. By the way: There is no need for any further dd-parameter whatsoever, such asconv=notrunc
, etc. as reported in many other forums. notrunc has absolutely nothing to do with what we want to do here (clone a disk to a file) and is used for something totally different (in short: Insert data in a file that is already existing without cutting off [=truncating] the remains of the existing file should the newly added data be shorter than the existing file). Do not listen to those who write that notrunc is especially needed when cloning disks to a file, that is 100% bollocksThe disk.img will be 160GB again, no surprise here. But the difference now to before is that if you compress the disk.img, this time it will almost become the same size as the sum of the compressed images of /dev/sda1 and /dev/sda2 would be, since the zeros on the remaining unpartitioned disk surface will compress down to practically nothing!
There you go, now you have a single-file full disk image of the whole disk but which is almost only the size of the really used data area of /dev/sda1 and /dev/sda2
In the case that you would not have created /dev/sda1 and /dev/sda2 yet, you could also just write zeros with dd to the entire disk /dev/sda first, or use a disk wiping tool that writes zeros to an entire disk, etc. and then just create /dev/sda1 and /dev/sda2. The result should be the same
HTH! Best regards An anonymous internet native on planet earth