Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 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
All the answers below are wrong except for @sudoer. The correct answer is to use dd conv=sparse. – bahamat Jan 6 '15 at 22:07
@bahamat, no, gzip is better since it will compress the data. – psusi Jan 7 '15 at 0:38
That's not the same thing as sparse. – bahamat Jan 7 '15 at 0:39
@bahamat, the question isn't asking specifically for sprase; just how to make the image take up less space. – psusi Jan 7 '15 at 0:40
up vote 19 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.

Note: Could be hazardous for SSD, user should consider this operation befor committing.

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
"You've just written zeros to all free space on the device". You mean partition, not device, I think. So you'd need to run that command with an of path for each partition. – jiggunjer Feb 5 at 4:46
If the physical media is an SSD, it may now think every sector on the device has been used. This will give the SSD fewer spare sectors to work with and possibly decrease performance as a result. If the driver and firmware has TRIM support that condition will only apply until you delete the file again. If you keep the file in place while you create the image, you will have to delete the file again after restoring the image. That may be useful if the image is restored to SSD. – kasperd Feb 19 at 11:14

Assuming you want to save /dev/sdXN to /tgtfs/image.rawand you are root:

  1. mkdir /srcfs && mount /dev/sdXN /srcfs

  2. Use zerofill or just: dd if=/dev/zero of=/srcfs/tmpzero.txt to fill unused blocks with zero (wait for it to fill the file system completely then rm /srcfs/tmpzero.txt)

  3. Take the image with dd and use conv=sparse to punch zeros on-the-fly: dd conv=sparse if=/dev/sdxn of=/tgtfs/image.raw

If you want to use compression you don't need to punch the zeros with dd as zero blocks are highly compressible:

dd if=/dev/sdxn | gz -c | dd of=/tgtfs/image.raw

PS: You should note that this is not a good idea on a flash memory based storage media (i.e. your source file system be SSD)

share|improve this answer
This is the correct answer. Use dd conv=sparse. – bahamat Jan 6 '15 at 22:06

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

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
partimage and clonezilla are actually smart enough to skip reading the free space, rather than relying on you to write zeros to it, and then have dd or gzip drop or compress the zeros after reading them in. – psusi Jan 7 '15 at 0:39

While /dev/zeroing the free-disk-space and use dd conv=sparse/gz -c is possible, on huge disks with empty space running in 100s of GBs, /dev/zeroing is painfully slow - not to mention that as other answers noted, /dev/zeroing an SDD till EOF.

Here's what I did when I ran into this situation:

  • On a lubuntu live CD, used gparted to 'shrink' the disk to minimum possible size, leaving rest of the space unallocated

  • Used
    dd bs=1M count=<size_in_MBs> if=/dev/sdX | gzip -c --fast| dd of=/path/to/image.gz to create the fast-compressed image (needless to say, you may want to skip the compression if you have sufficient space to store raw data (or are otherwise inclined to reduce CPU loading)

  • Used
    dd if=/path/to/image.gz | gunzip -c | dd bs=1M of=/dev/sdY to copy the data back onto different disk
  • Used gparted again to 'expand' the partition

I haven't tried it for multiple partitions but I believe the process above can be adapted to copy 'partitions' if partition-table on destination disk is created first and only the data contained in the partition is copied via dd - reading/writing offsets (skip/seek option of dd, respectively) would be required as appropriate.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.