Take the 2-minute tour ×

I have a disk, say /dev/sda.

Here is fdisk -l:

 Disk /dev/sda: 64.0 GB, 64023257088 bytes
255 heads, 63 sectors/track, 7783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0000e4b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          27      209920   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              27         525     4000768    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5              27         353     2621440   83  Linux
/dev/sda6             353         405      416768   83  Linux
/dev/sda7             405         490      675840   83  Linux
/dev/sda8             490         525      282624   83  Linux

I need to make an image to store on our file server for use in flashing other devices we are manufacturing so I only want the used space (only about 4gb). I want to keep the mbr etc... as this device should be boot ready as soon as the copy is finished.

Any ideas? I previously had been using dd if=/dev/sda of=[//fileserver/file], but at that time, my master copy was on a 4gb flash ide.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Back in the day I ran into a similar problem with embedded Linux distributions - get rid of all the junk before compressing the image.

dd if=/dev/zero of=asdf.txt. Wait until it dies. Delete asdf.txt.

You've just written zeros to all free space on the device.

Now take a disk image and run it through gzip. Voila, sparse image.

Probably doesn't scale very well and could cause problems if you actually need to write to the disk, but hey.

You could take an rsync snapshot of the disk to another volume, zero that, and then take that disk image.

share|improve this answer
if I run it through gzip, will I have to unzip it before using it? And by run it through gzip, do I just pipe it during the dd process? –  Jonathan Henson Oct 17 '12 at 15:30
Yes. dd if=sda2.gz | gunzip > /dev/sda2 and dd if=/dev/sda2 | gzip > sda2.gz –  Rob Bos Oct 20 '12 at 15:56
Thanks! that did it. –  Jonathan Henson Oct 22 '12 at 22:27
add comment

You can't. dd is a very low level tool and it has no means of distinguishing between files and empty space.

On the other hand the empty space will compress very, very nicely so if you are only concerned about storage space, not for example write time, then just pipe it through gzip.

share|improve this answer
Assuming the free space hasn't previously been used. You can zero fill the free space first to ensure the compression works as expected. –  Sirex Oct 16 '12 at 23:41
True. And it only complicates the process and makes it take even longer. –  c2h5oh Oct 16 '12 at 23:52
add comment

Assuming the rest of the drive is empty (all zeros) you could pipe your DD through gzip, which should compress the empty space quite nicely. You can use a tool like zerofree to make sure your empty space is actually blank so it compresses nicely.

If you use a tool like partimage, clonezilla or some of the other linux cloning tools, they would handle most of this for you automatically.

share|improve this answer
add comment

Your Answer


