3

if only 1/3 of a 300GB hard disk is used. Is it possible to use dd to clone the drive (but only the 100GB that is used)?

dd if=/dev/sda of=clone.img

3 Answers3

3

Can not dd parts of filesystem

You can use dd to copy a whole partition, which could be only a part of the harddisk.

You are asking whether you can use dd to copy part of a partition.

In the general case, that can not work:
It would mean you copy only a part of a file system. There is no reason the actual data should be at the start of the available space. And even if it was, there will still be relevant information in other parts in general.

It something like this would work - possibly with a different tool than dd, that tool would need to understand how the data is structured on the partition - that is, it needs to understand the filesystem.

It sounds more useful to let the normal filesystem driver understand how the data is arranged on the disk, and use tar or similar to pack up that data to put it somewhere else.

Resize filesystem, use dd then

One way to do something similar to what you ask for would be to resize the partition.

In a first step, resize the 300GB partition that is only filled to one third to a partition of slightly above 100GB which is almost full.

gparted, a GNOME-focused GUI front-end for the libparted library, is a commonly recommended partitioning manager, but the parted CLI front-end also exists (and is actually the reference implementation for libparted). cfdisk, fdisk and the like are also CLI options, and while I'd say they're less friendly than gparted most Linux distros should have them as part of the core utilities package.

After resizing, you can use dd in the standard way on a complete partition, containing a complete file system.

Filesystem archive tool

Depending on what you want to do, this may also be helpful:

fsarchiver is a tool that creates custom images from filesystems in a compressed format, that need to be unpacked with the same tool.

I would not recommend the tool in general to create images because of the custom format, but as an intermediate state it may make sense. You would create an image of the existing partition, that will not need space for the empty part of the file system. Then, you can use it to unpack it to a different partition and a different file system. The filesystem is generated by fsarchiver as part of the unpacking.

Volker Siegel
  • 17,283
1

partclone (from the same authors as CloneZilla) can do that. It does not seem to use a DD compatible image format though.

partimage can also do that, with less supported filesystems.

You should be able to convert the images to DD compatible format by restoring the images to a file. (You might want to convert it to sparse file as well)

You won't be able to restore the image to a smaller disk, since it is still a block image, but it uses knowledge of the filesystem to skipped the unused parts.

Exact commands needs more info, like the source filesystem, since partimage comes in filesystem-specific versions.

1

If you want to not waste the extra 200GB of space making the backup, what you can do is use the GNU utility zerofree which sets all non used space on a file system to the specified value. use 0xFF as that value to save wear and tear on your solid state drives and improve the performance. (since erased pages in flash are default to all 0xFF. This also improves the speed of performing the operation on the drive drastically) Once that is done, all of the unused bytes on your partition will be the bytes 0xFF. Now you can simply use a compression algorithm that supports run length compression, and all those big blocks of 0xFF bytes will be reduced to a few megabytes. so your commands would be something like this:(as root)

zerofree -v -f 0xFF /dev/nvme1n1p4
dd if=/dev/nvme1n1p4 | lzma > ./backup-image.img.lzma

The resulting image should not be much bigger (if not smaller than) the used space on the drive.

To restore this backup to a disk later you can simply do something like:

dd if=./backup-image.img.lzma | unlzma | dd of=/dev/sda1