How do I convert a Linux disk image into a sparse file?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2
--
Chapters
00:00 How Do I Convert A Linux Disk Image Into A Sparse File?
01:07 Answer 1 Score 2
01:34 Accepted Answer Score 21
02:23 Answer 3 Score 2
02:46 Answer 4 Score 15
03:01 Thank you
--
Full question
https://superuser.com/questions/170125/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #filesystems #mount #compression
#avk47
ACCEPTED ANSWER
Score 21
First of all, sparse files are only handled transparently if you seek, not if you write zeroes.
To make it more clear, the example from Wikipedia
dd if=/dev/zero of=sparse-file bs=1k count=0 seek=5120
does not write any zeroes, it will open the output file, seek (jump over) 5MB and then write zero zeroes (i. e. nothing at all). This command (not from Wikipedia)
dd if=/dev/zero of=sparse-file bs=1k count=5120
will write 5MB of zeroes and will not create a sparse file!
As a consequence, a file that is already non-sparse will not magically become sparse later.
Second, to make a file with lots of zeroes sparse, you have to cp it
cp --sparse=always original sparsefile
ANSWER 2
Score 15
Perhaps the easiest way to sparsify a file in place would be to use fallocate
utility as follows:
fallocate -v --dig-holes {file_name}
fallocate(1) is provided by util-linux package on Debian.
ANSWER 3
Score 2
Do you mean that your ddrescue created image is, say, 50 GB and in reality something much less would suffice?
If that's the case, couldn't you just first create a new image with dd:
dd if=/dev/zero of=some_image.img bs=1M count=20000
and then create a filesystem in it:
mkfsofyourchoice some_image.img
then just mount the image, and copy everything from the old image to new one? Would that work for you?
ANSWER 4
Score 2
PartImage can create disk images that only store the used blocks of a filesystem, thus drastically reducing the required space by ignoring unused block. I don't think you can directly mount the resulting images, but going:
image -> partimage -> image -> cp --sparse=alway
Should produce what you want (might even be possible to stick the last step, haven't tried).