I have a partition sdb1 of size 3.3 GB. From 3.3 GB only 1.9 GB is used and 1.2 GB is an empty space. I want to create an image of this partition. But I want the image to only have the used space i.e. 1.9 GB

Now when I run a command using dd:

dd if=/dev/sdb1 of=/home/data/Os.img bs=1M status=progress

I saw behavior that dd is making the image of 3.3 GB While I don't want the unallocated space to be the part of the OS image.

So I tried different solutions. I found one solution:

dd if=/dev/sdb1 of=/home/data/OS.img bs=1M count=1946 status=progress

This solution created an image of 1.9 GB as I have defined a block size of 1M and count is 1946 which will give 1.9 GB totally.

Question I am unable to determine if this dd command only made an image of the used space or it just created an image of size 1.9 GB in which there are both used and unused spaces?

And is there any other way in which the image can be created for the used space only?

  • 1
    In general, there are free blocks (not occupied with file content) at various locations on the partition, and the file content is not only in the head end of the partition. In other words, it does not work to copy only the head end of the partition corresponding to what is used. – sudodus Mar 13 at 12:36
  • 1
    sudodus is spot on with his comments here and knowledgeable in this subject. What do you want to do with the image? Just store it and move it around? Do you need to mount the image? Check out this Q/A for more information. – jc__ Mar 13 at 13:33

This has been answered here.

First make an image of the partition.

Then use losetup to mount the image. As it's not a disk but a partition, you don't need the partscan flag.

# lsblk
# losetup --find foo.img

Lookup the newly created loop device

# lsblk
loop1    7:1    0   4096M  0 loop 

Zero out the remaining space by writing a large empty file.

# mount /dev/loop1 /mnt
# dd if=/dev/zero of=/mnt/filler conv=fsync bs=1M
# rm /mnt/filler
# umount /dev/loop1

Now compress the image.

# ls -s
4096M foo.img
# gzip foo.img
# ls -s
11M foo.img.gz

So far for the partition image.

If there is an ext4 file system on the partition, you can resize it with resize2fs to the minimum size. This moves all data to the first blocks.

e2fsck -f /dev/loop1
resize2fs -M /dev/loop1

You can use Clonezilla and create a Clonezilla image, which is a directory with a set of files, where the biggest files are compressed. Clonezilla is smart enough to copy only the blocks from the partition that are marked as used by the file system, which makes it faster than dd. It also means that the image will be smaller than if the free drive space is also copied.

But when you restore the content of the image into a partition, the target must be a partition of [at least] the same size, so if you want the size of the target smaller after the restore operation, you must use another method.

If you want a smaller size of the partition after the imaging and restore operation, you should work at the file level. You can

  • create a tarball, a [compressed] tarfile of the whole file system,

  • create a new partition with a file system with the size that you want

  • extract the directory tree with all the files from the tarball.

Boot from another operating system, so that the partition to be copied is not active, and run tar with elevated permissions, for example

  • change directory to the top of the partition that you want to copy

    sudo tar -cvzf /path-to/file.tar.gz .

When you restore (also booted from another operating system)

  • change directory to the top of the partition where you want to extract from the tarfile

    sudo tar -xvf /path-to/file.tar.gz

Please notice an important difference between this result and the result of a cloning operation,

  • The UUID of the partition will be different. If the copied partition contains a system partition, you must either change the UUID to that of the original partition or change the references to the UUID in /etc/fstab and /boot/grub/grub.cfg to match the new UUID.
  • I am looking for a command line tool like dd for making the image of a partition and it should be inbuilt in the Linux by default. – Sharvin Shah Mar 13 at 13:13
  • @SharvinShah, tar is built-in in most if not all linux distros. If you still want dd, you can write zeros to all free space (create a huge file 'blank' until the file system is full) and remove it. After that you can clone with dd and pipe the result via gzip or xz, which writes also the zeros, but compresses that space very efficiently. So effectively you will get almost what you want. But Clonezilla will be much more efficient (speedwise). – sudodus Mar 13 at 13:20
  • 1
    For a CLI experience you can use partclone, which is one of the tools clonezilla uses. – Emmanuel Rosa Mar 14 at 4:33

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.