How to shrink a ddrescue-created image or copy to new image?

0

I have rescued data from a 2TB hdd (and it turned out that it was not faulty - but this is a different story) and I got a 2TB image. Mounting this image under Linux, I can see (using df) that in this image, 512GB are free. Now, I would like get this space back, i.e., either shrink the image, which seems to be slightly complicated (as several questions and answers on Super User and other sites pointed out) or put the files into a new image.

My question is: What is the easiest way to get a mountable image which contains the data, but not the free space? I prefer to keep all the data in one image file.

Rainer

Posted 2015-03-26T18:58:53.333

Reputation: 205

Answers

1

You need to punch holes into it to make a sparse image, however, the only way to do this is to copy the file ...

cp --sparse=always fromfile tofile

Which takes a little bit of disk space.

But before you do this the 'free' space in the image MUST be zeroed: mount the image and cat /dev/zero > /media/loopmount/hugefile then sync and rm /media/loopmount/hugefile before unmounting.

Another option is to convert it into a qcow2 file, this has the advantage that the used data can be compressed. To mount it you use the qemu-nbd command to create a 'network block device' that can be mounted as any other device. If you're lucky this will only need a terabyte of free disk space.

user3710044

Posted 2015-03-26T18:58:53.333

Reputation: 259

Thanks - this sounds promising. I think I will go with a sparse qcow2 as the image contains a lot of text files which should compress quite nicely. I found https://rwmj.wordpress.com/2010/10/19/tip-making-a-disk-image-sparse/ and this lead me also to libguestfs and the tool virt-sparsify which looks very nice, as it can also do the conversion to qcows.

– Rainer – 2015-03-27T09:06:46.513