Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I like create an image backup for the first time I'm backing up a system. After this first time I use rsync to do incremental backups.

My usual image backup is as follows:

Mount and zero out the empty space:

dd if=/dev/zero of=temp.dd bs=1M

rm temp.dd

umount and dd the drive while compressing it

dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c > /mnt/sda1/hda.ddimg.gz

to put the system back to normal, i will usually do a

gunzip -c /mnt/sda1/hda.img.gz | dd of=/dev/hda conv=sync,noerror bs=64K

This is really straightforward and allows my to save the 'whole drive' but really just save the used space.

Here is the problem. Lets say I do the above but not on a clean system and don't get the rsync backups going soon enough and there are files that I want to access that are on the image. Let's say I dont have the storage space to actually unzip and dd the image to a drive but want to mount the image to get individual files off of it.... Is this possible?

Normally, one wouldn't compress the dd image, which will allow you to just mount the image using -o loop... but this isn't my case...

Any suggestions for mounting the compressed img on the fly?


Would using AVFS to 'mount' the gz file then mounting the internal dd.img work (I don't think so... but would need verification...)?

share|improve this question
You should use SquashFS for this kind of things. It also de-dupes duplicated files. –  Avio Oct 24 '12 at 8:56
It looks like this fellow is doing what you are asking about:… –  Joshua Jan 2 '13 at 20:47

5 Answers 5

up vote 28 down vote accepted

It depends on whether the disk image is a full disk image, or just a partition.

Washing the partition(s)

If the disk is in good working condition, you will get better compression if you wash the empty space on the disk with zeros. If the disk is failing, skip this step.

If you're imaging an entire disk then you will want to wash each of the partitions on the disk.

CAUTION: Be careful, you want to set the of to a file in the mounted partition, NOT THE PARTITION ITSELF!

mkdir image_source
sudo mount /dev/sda1 image_source
dd if=/dev/zero of=image_source/wash.tmp bs=4M
rm image_source/wash.tmp
sudo umount image_source

Making a Partition Image

mkdir image
sudo dd if=/dev/sda1 of=image/sda1_backup.img bs=4M

Where sda is the name of the device, and 1 is the partition number. Adjust accordingly for your system if you want to image a different device or partition.

Making a Whole Disk Image

mkdir image
sudo dd if=/dev/sda of=image/sda_backup.img bs=4M

Where sda is the name of the device. Adjust accordingly for your system if you want to image a different device.


Make a "squashfs" image that contains the full uncompressed image.

sudo apt-get install squashfs-tools
mksquashfs image squash.img

Streaming Compression

To avoid making a separate temporary file the full size of the disk, you can stream into a squashfs image.

mkdir empty-dir
mksquashfs empty-dir squash.img -p 'sda_backup.img f 444 root root dd if=/dev/sda bs=4M'

Mounting a compressed partition image

First mount the squashfs image, then mount the partition image stored in the mounted squashfs image.

mkdir squash_mount
sudo mount squash.img squash_mount

Now you have the compressed image mounted, mount the image itself (that is inside the squashfs image)

mkdir compressed_image
sudo mount squash_mount/sda1_backup.img compressed_image

Now your image is mounted under compressed_image.

EDIT: If you wanted to simply restore the disk image onto a partition at this point (instead of mounting it to browse/read the contents), just dd the image at squash_mount/sda1_backup.img onto the destination instead of doing mount.

Mounting a compressed full disk image

This requires you to use a package called kpartx. kpartx allows you to mount individual partitions in a full disk image.

sudo apt-get install kpartx

First, mount your squashed partition that contains the full disk image

mkdir compressed_image
sudo mount squash.img compressed_image

Now you need to create devices for each of the partitions in the full disk image:

sudo kpartx -a compressed_image/sda_backup.img

This will create devices for the partitions in the full disk image at /dev/mapper/loopNpP where N is the number assigned for the loopback device, and P is the partition number. For example: /dev/mapper/loop0p1.

Now you have a way to mount the individual partitions in the full disk image:

mkdir fulldisk_part1
sudo mount /dev/mapper/loop0p1 fulldisk_part1
share|improve this answer
interesting take on this problem (squashfs instead of gzip). I am pretty unfamiliar with squashfs tools... can you pipe the output of dd to create a squash partition on the fly as you can with the gzip partition? what is the compression ratios (gzip is okay/good, esp given the fact that I am clearing 'empty space with zeros')? –  g19fanatic May 13 '13 at 3:12
how would you dd the image back to the hard disk? –  g19fanatic May 13 '13 at 3:15
@g19fanatic The uncompressed disk image is "inside" the squashfs image. You mount the squashfs image, then dd the image inside it to the destination disk. –  doug65536 May 13 '13 at 5:27
@g19fanatic The compression was excellent (very nearly the same as gzip in my case). mksquashfs was fast too, it is parallelized. On my 990x (6 core) it was actually limited by the destination disk write speed, around 100MB/sec. –  doug65536 May 13 '13 at 5:29
@g19fanatic You can stream into squashfs using the -p or -pf flags to pass it a pseudo-file. A pseudo file can be used for things like making device nodes which you can't otherwise do without root (useful for building images as part of a build process) or for streaming the output of some command into the image. One of the examples given in the docs (/usr/share/doc/squashfs-tools/examples/pseudo-file.example on Debian/Ubuntu) is input f 444 root root dd if=/dev/sda1 bs=1024 count=10 to copy the first 10K from a disk image into a file named "input" in the squashfs image. –  Brian Campbell Jul 22 '14 at 3:45

Not really. You can't really seek to a specific block in the compressed file without decompressing the whole thing first, which makes it difficult to use the compressed image as a block device.

You could use something like dump and restore (or tar, really), all of which use a streaming you can access invidividual files by effectively scanning through the uncompressed stream. It means if the file you want is at the end of the compressed archive you may have a long time to wait, but it doesn't require you to actually decompress everything onto disk.

Using tar for backups may seem a bit old fashioned, but you get a lot of flexability.

share|improve this answer
The problem lies in the fact that I do not even know if the file of interest is actually on this compressed backup... Do you know of a file explorer that will go through the whole .gz'd image, keep the file/dir structure in memory, provide a simple view of the structure and allow you to 'pick' files (now that it knows where they exist) to extract? Its a very niche specification... but I could see tons of uses for something like this... if it exists. –  g19fanatic Feb 15 '12 at 12:09
If it doesn't, would you be able to point me towards some instruction on how to pull the structure from the gz'd image? I would be able to create such a program (program for a living...) but am blind on the topic of decompressing image data and the specifics of different filesystems. –  g19fanatic Feb 15 '12 at 12:11
I suspect that building your own tool is going to be a larger project than you really want to undertake. However...assuming that you have an ext[234] filesystem, I would suggest the e2fsprogs package, or maybe something like fuse-ext2. Both provide user-space tools for interacting with ext[234] filesystems. –  larsks Feb 15 '12 at 13:59
Also note that what you have doesn't appear to be a filesystem image, it's a whole disk image, which means you'll first have to parse out the partition table and locate the appropriate partition. –  larsks Feb 15 '12 at 14:00
I mistyped in the above question and will fix it. I usually do a partition based dd image and save a copy of the partition table. I used to do whole disk copies but hated needing to mount with options to get to the proper location. –  g19fanatic Feb 15 '12 at 14:51

This answer complements Cristian Ciupitu's answer. If you use xz compression with a reasonable block size, you can access the disk image using guestfish or other libguestfs tools like this:

nbdkit xz file=disk.img.xz --run 'guestfish --format=raw -a $nbd -i'
share|improve this answer

If the image is read-only you can also use nbdkit (man page) and its xz plugin (xz should provide better compression and random access times than gzip).

Create the compressed partition image

dd if=/dev/sda1 bs=16M | xz -9 --block-size=16MiB - > sda1.img.xz

A --block-size option of 16 MiB should provide good random access performance.

Note: you may use alternative xz compression programs such as pixz which provides parallel compression, just make sure it splits the output in multiple small blocks, otherwise nbdkit has to decompress a lot of data. For example as of September 2015, pxz does not support this.

Serve it with nbdkit

nbdkit --no-fork --user nobody --group nobody -i \
       xz file=sda1.img.xz

Connect to the NBD server

nbd-client 10809 /dev/nbd0 -nofork

Mount it read-only

mount -o ro /dev/nbd0 sda1

When done

umount /dev/nbd0
nbd-client -d /dev/nbd0

Stop nbdkit by pressing Ctrl+C (or with kill).

share|improve this answer

Try archivemount

archivemount is a FUSE-based file system for Unix variants, including Linux. Its purpose is to mount archives (i.e. tar, tar.gz, etc.) to a mount point where it can be read from or written to as with any other file system. This makes accessing the contents of the archive, which may be compressed, transparent to other programs, without decompressing them.

After mounting archive you can use it contents like regular file. Maybe get partition table, or convert, mount image with qemu tools.

squashfs useful for booting from image, but much complex for backuping.

share|improve this answer
Perfect! This is the most easy and elegant solution so far. I wonder why there are no votes here. –  TranslucentCloud Apr 28 at 20:54
I think it is because if you mount an archive like disk.img.gz on a folder with archivemount, say /mnt/, you would get a single file /mnt/disk.img, that you then have to mount elsewhere. The question instead wants something able to unwrap both in a single step (and archivemount seems capable to do that on .tar.gz, but not on gzipped raw images). –  p91paul May 21 at 15:04
@p91paul squashfs method is same with packing in tar.gz in this case. You can mount squashfs, but you also can mount tar.gz and skip ten steps in creating image and have 1-two step to see contents –  eri May 21 at 21:50
Of course, but still the original question doesn't talk about a tar.gz. My point was to tell @TranslucentCloud this answer has no votes because it doesn't actually answer the question, it tells "you should have created a tar.gz instead" –  p91paul May 22 at 8:41
@p91paul fair enough. But still the eri's answer sheds light on some interesting tool. –  TranslucentCloud May 22 at 10:20

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.