If I make a disk image and compress it with gzip/xz/etc is there a way to mount it directly without first uncompressing it?
Say I've used
sudo dd if=/dev/sdc1 | gzip -9 > image1.dd.gz
how can I mount the original image, without creating an uncompressed copy first?
Or I've used
sudo dd if=/dev/sdc | gzip -9 > wholedisk.dd.gz
and the disk has multiple partitions, would that make it any harder?
With an uncompressed image of a whole disk then using kpartx
or newer versions of losetup
with it's -P
flag should create a loop for each partition.
But is there a way to mount/losetup/read the compressed image?
If it won't work for gzip/xz, is there any compression method this would work for?
Note on duplicate Q
The currently suggested duplicate
- Mount single partition from image of entire disk (device) , while being an excellent useful Q
DOES NOT USE COMPRESSION, and IS NOT A DUPLICATE.
mount
will not mount a compressed image by itself.
Best Answer
You can use
squashfs
to compress disk images and then mount them.Create the disk image
If you haven't got a disk image yet use
dd
to create one:Compress the image with squashfs
Install
squashfs
:Compress the image:
Or Stream the compression (don't need a temporary dd file)
Compliments to terminator14 at UbuntuForums.org. Definitions/Explanations:
empty-dir
- "source" dir. Basically in our case, just an empty dir to satisfy mksquashfs' input arg formatsquash.img
- the destination and filename of the output squashfs filesda_backup.img
- the name of the dd backup INSIDE the squashfs filef
- specifies that sda_backup.img is a regular file (as opposed to a directory, block device, or char device)444
- permissions of the sda_backup.img file inside the squashfs imageroot root
- UID and GID for the sda_backup.img file inside the squashfs image. Can be specified by decimal numbers, or by namedd if=/dev/sda bs=4M
- the dd command used to read the device we want backed upMount the image
First mount the
squashfs
image:This will present the un-compressed disk image for you to mount:
Or if it's a full drive image (partitioned) you could use
losetup
to attach the dd image to a loop device (possibly optional) and thenkpartx -a
orpartprobe
to find & separate the partitions to separate devices, or evenvgscan
/vgchange -ay
if there's LVM.