Mounting a dd image compressed with pixz involves two main steps: decompressing the image and then mounting the specific partition(s) within the uncompressed image.
Since a dd image copies the entire drive, it includes the partition table. You can't directly mount the cloned.img file itself as a filesystem. You need to identify the partitions within the image and mount them.
Here are the most common and recommended ways to do this:
Prerequisites:
pixz: For decompression.
kpartx: Highly recommended for managing partitions within disk images. It makes mounting much easier.
fdisk or parted: To inspect the partition table if kpartx is not available or if you want to mount manually.
sudo: You'll need root privileges for mounting.
Method 1: Decompress to a file, then mount (Recommended for simplicity)
This is generally the easiest approach if you have enough disk space for the uncompressed image.
Decompress the image:
This will create a large uncompressed dd image file. Make sure you have enough free space.
pixz -d < cloned.img > uncompressed_cloned.img
Replace uncompressed_cloned.img with your desired output filename.
This will take some time depending on the size of your drive and the speed of your CPU/disk.
Inspect the partitions within the uncompressed image:
You have a few options here. kpartx is the easiest.
a) Using kpartx (Highly Recommended):kpartx will create device mappings for each partition found inside the image.
sudo kpartx -av uncompressed_cloned.img
This command will output something like:
add map loop0p1 (253:0): 0 204800 linear 7:0 2048
add map loop0p2 (253:1): 0 123456789 linear 7:0 206848
It creates symbolic links in /dev/mapper/ for each partition (e.g., /dev/mapper/loop0p1, /dev/mapper/loop0p2).
b) Using fdisk (Manual Offset Calculation):
If you don't have kpartx, you can use fdisk to find the partition offsets.
sudo fdisk -l uncompressed_cloned.img
Look for the Start column for each partition. Note the Start sector and multiply it by 512 (common sector size) to get the byte offset.
Example fdisk output:
Device Start End Sectors Size Type
uncompressed_cloned.img1 2048 206847 204800 100M EFI System
uncompressed_cloned.img2 206848 251658189 251451342 119.9G Linux filesystem
For uncompressed_cloned.img1, the offset is 2048 * 512 = 1048576 bytes.
For uncompressed_cloned.img2, the offset is 206848 * 512 = 105906176 bytes.
Mount the desired partition:
a) Using kpartx (Easiest):
mkdir /mnt/cloned_drive_part1
sudo mount /dev/mapper/loop0p1 /mnt/cloned_drive_part1
# Or for the second partition:
mkdir /mnt/cloned_drive_part2
sudo mount /dev/mapper/loop0p2 /mnt/cloned_drive_part2
Replace loop0p1, loop0p2 with the actual device names kpartx created.
It's highly recommended to mount read-only first, especially if you're doing forensics or don't want to accidentally modify the clone:
sudo mount -o ro /dev/mapper/loop0p1 /mnt/cloned_drive_part1
b) Using Manual Offset (if kpartx is not used):
mkdir /mnt/cloned_drive_part2
sudo mount -o loop,offset=105906176 /path/to/uncompressed_cloned.img /mnt/cloned_drive_part2
Replace 105906176 with the actual byte offset you calculated.
Again, consider adding ,ro for read-only.
You might need to specify the filesystem type if mount can't auto-detect it (e.g., -t ext4, -t ntfs).
Method 2: Mount on the fly (Without full decompression to a file)
This method is more advanced and can be trickier, but it saves disk space by decompressing and piping to a loop device. It usually works best for single partition images or when kpartx can interpret the piped stream. However, for full disk images with partition tables, it's significantly more complex.
For a full drive dd image (with partition table), the kpartx step still relies on a seekable file. So, piping directly to kpartx is generally not possible.
Instead, you'd typically decompress to a temporary file or use losetup with the find option and then run kpartx on that loop device.
# 1. Decompress and connect to a loop device
# This creates a loop device directly from the output of pixz.
# However, this loop device will represent the *entire disk image*, not individual partitions.
sudo pixz -d < cloned.img | sudo losetup -f --show
# Example output: /dev/loop0
# Let's assume it output /dev/loop0
# 2. Use kpartx to create device mappings for the partitions within /dev/loop0
sudo kpartx -av /dev/loop0
# This will create /dev/mapper/loop0p1, /dev/mapper/loop0p2 etc.
# Now, mount as in Method 1, step 3a:
mkdir /mnt/cloned_drive_part1
sudo mount /dev/mapper/loop0p1 /mnt/cloned_drive_part1 -o ro
# Don't forget to clean up later!
Important Considerations:
Disk Space: Decompressing cloned.img will require free disk space equal to the original size of the drive. If your drive was 1TB, you need 1TB free.
Read-Only (RO): Always try to mount with -o ro (read-only) first. This prevents accidental changes to your cloned image, which could be critical for data recovery or forensic purposes.
Filesystem Type: If mount complains, you might need to explicitly specify the filesystem type with -t, e.g., sudo mount -t ext4 ....
Clean Up: Don't forget to unmount and remove the loop devices when you're done.
Cleaning Up
After you're finished accessing the image:
Unmount the mounted partition(s):
sudo umount /mnt/cloned_drive_part1
sudo umount /mnt/cloned_drive_part2 # If you mounted multiple
Remove kpartx mappings (if used):
sudo kpartx -d uncompressed_cloned.img # For Method 1
# OR
sudo kpartx -d /dev/loop0 # For Method 2 (or if kpartx was used on the loop device)
Remove the loop device (if used manually or via losetup):
sudo losetup -d /dev/loop0 # Replace /dev/loop0 with the actual loop device
Remove the uncompressed image file (if you don't need it anymore):
rm uncompressed_cloned.img
Choose the method that best suits your needs and available disk space. Method 1 with kpartx is usually the most straightforward.
User
Will mounting the compressed version take up the full storage of the drive?
Assistant
Yes, in effect, it will take up the full storage of the drive that was cloned.
Here's why and what that means:
Compressed vs. Decompressed:
Your cloned.img file is compressed using pixz. This means it occupies less space on your current disk than the original drive's capacity.
However, mount (and the kernel's loop device driver) cannot directly read and understand a compressed disk image. Before a filesystem can be mounted from it, the data must be decompressed.
How Mounting Works (and its implications for space):
Method 1: Decompress to a file first (most common):
pixz -d < cloned.img > uncompressed_cloned.img
In this scenario, uncompressed_cloned.img will be a file on your disk that is exactly the same size as the original drive you cloned. If you cloned a 1TB drive, this file will be 1TB. You then mount partitions from this uncompressed file. So, yes, it will take up the full storage space of the original drive on your current disk.
Method 2: Decompress to a loop device on the fly (more advanced, but still implies full size):
Even with this method, the losetup command creates a virtual block device (e.g., /dev/loop0) that represents the entire uncompressed drive image. While a persistent full-size file isn't written to disk in this specific command, the system still needs to process and hold (either in memory, or by buffering to a temporary spool file behind the scenes, or by smart block-level decompression if pixz could do that randomly which it largely can't easily) the decompressed data. The kernel sees and treats this loop device as if it were a physical disk of the original full size. When you access data, pixz is decompressing it, and the kernel is operating on that decompressed stream or data.
In summary:
You cannot directly mount a pixz compressed image without decompressing it first. Whether you decompress it to a new file or stream it directly to a loop device, the system will need to access and process the data in its full, uncompressed size. Therefore, you must have enough free disk space on the system you're working on to accommodate the original full size of the drive you cloned, even if only temporarily.
User
How do I create an image like that?
Assistant
Creating a dd image compressed with pixz is a powerful way to back up an entire drive. However, it requires extreme caution because using dd incorrectly can easily wipe your data.
Key Principle: You will read the entire source drive block by block using dd, pipe that raw data directly into pixz for compression, and pixz will write the compressed output to your specified file.
CRITICAL WARNINGS BEFORE YOU START:
IDENTIFY THE CORRECT DRIVE: This is the most crucial step. Using the wrong input drive (if=/dev/sdX) will result in cloning the wrong drive. Using the wrong output file (of=/path/to/your/important/file) can overwrite existing data.
BOOT FROM A LIVE SYSTEM: To ensure data consistency and prevent issues with mounted filesystems, it's highly recommended to boot your computer from a Linux Live USB/DVD (e.g., Ubuntu Live, GParted Live, SystemRescueCD). Do NOT clone a drive that is currently mounted and in use by the running operating system.
DESTINATION SPACE: While the output will be compressed, ensure you have enough free space on the destination drive to hold the compressed image. The compression ratio depends on the data on the drive.
Steps to Create the cloned.img file:
1. Prepare Your Environment
Boot from a Live USB/DVD: Insert your Linux Live media, boot from it, and choose "Try Ubuntu" (or similar) to get to a desktop environment.
Open a Terminal: Once in the live environment, open a terminal window (usually Ctrl+Alt+T).
2. Identify the Source Drive
This is the drive you want to clone.
Use lsblk: This command shows block devices in a tree-like format, making it easy to identify disks and their partitions.
lsblk -f
Look for your source drive (e.g., /dev/sda, /dev/sdb, /dev/nvme0n1). Pay attention to its size and the partitions it contains to confirm it's the right one.
Yes, you absolutely can use a dd image (even compressed with pixz) as a backup of your system. In fact, dd is often called the "disk duplicator" for this exact purpose, as it creates a bit-for-bit copy of the entire drive.
However, using it effectively requires understanding two main scenarios:
Restoring the entire system to its previous state (or to new hardware).
Extracting specific files or directories from the backup without a full restore.
Let's break down how to use it for both.
Scenario 1: Restoring the Entire System from the dd Image
This is the "disaster recovery" scenario where you want to bring your entire system back to the state it was in when the image was created.
CRITICAL WARNINGS:
This process will ERASE ALL DATA on the target drive (/dev/sdX). Double-check, triple-check that you are writing to the correct destination drive.
Boot from a Live USB/DVD: Just like when you created the image, you must boot from a Linux Live environment (e.g., Ubuntu Live, SystemRescueCD, GParted Live). You cannot restore to a disk that is currently in use by your running OS.
Target Drive Size: The target drive must be equal to or larger than the original drive you cloned. If it's larger, the extra space will appear as unallocated space at the end of the disk after the restore, which you can then expand into later. If it's smaller, dd will fail or truncate the image.
Steps for Full System Restore:
Boot from a Linux Live USB/DVD: Get to a terminal window.
Identify the Target Drive: Use lsblk -f or sudo fdisk -l to identify the drive you want to restore to. This will be your of=/dev/sdX.
Example: If you want to restore to your new empty SSD, and lsblk shows it as /dev/sdb, then of=/dev/sdb.
Seriously, verify this. One wrong letter can destroy data.
Locate Your cloned.img Backup: Make sure the drive containing your cloned.img file is connected and mounted (if necessary). Note its full path (e.g., /mnt/backup/cloned.img).
User
Is there a way to do it for a single partition (my home folder)?
Assistant
Yes, you absolutely can use dd to create an image of a single partition, such as your home folder partition, and compress it with pixz. This is actually a very common and efficient use case for dd when you only care about specific data.
The process is very similar to creating a full disk image, but with a few crucial differences that make it simpler for single partitions.
CRITICAL WARNINGS:
UNMOUNT THE SOURCE PARTITION: For a consistent backup, the partition you are cloning (/home in this case) must not be mounted when you run dd. If it's your active /home directory, you'll need to boot from a Linux Live USB/DVD to perform the backup.
IDENTIFY THE CORRECT PARTITION: Ensure you're selecting the correct partition (e.g., /dev/sda3, /dev/nvme0n1p5). Using the wrong partition will backup incorrect data or potentially overwrite something if you're not careful with of=.
DESTINATION SPACE: While compressed, you still need enough space on your destination drive for the compressed image.
Scenario 1: Creating the Compressed Image of Your Home Partition
Let's assume your /home directory is on a separate partition, for example, /dev/sda3.
1. Prepare Your Environment
Boot from a Linux Live USB/DVD: This is crucial if /home is your active home directory. If /home is not your active home (e.g., it's an extra data partition you access), you can skip this and just ensure it's unmounted in your running OS.
Open a Terminal: Once in the live environment (or your running OS).
2. Identify Your /home Partition
You need the actual device name of the partition.
Using lsblk -f:
lsblk -f
Look for a partition with a MOUNTPOINT of /home (or similar) or a LABEL that identifies it. Note its NAME (e.g., /dev/sda3).
Example Output:
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 Boot a1b2... /
└─sda2 ext4 Home_Data d4e5... /home <-- This is what you're looking for
If you want to clone the entire /dev/sda drive in this example, your input for dd would be if=/dev/sda.
Use sudo fdisk -l: Another option, but lsblk is often more user-friendly for this purpose.
sudo fdisk -l
This lists all disk partitions. Look for /dev/sdX or /dev/nvmeXn1 (the whole disk, not a partition like /dev/sdX1).
3. Choose a Destination for the Image
You'll need a place to save the cloned.img file. This is usually an external USB hard drive or a network share.
Mount the Destination Drive (if necessary): If your external drive isn't automatically mounted, you'll need to mount it.
First, identify its device name (e.g., /dev/sdd1) using lsblk -f.
Create a mount point: sudo mkdir /mnt/backup
Mount it: sudo mount /dev/sdd1 /mnt/backup (Replace /dev/sdd1 with your actual partition).
sudo: Executes the command with root privileges, which are required to read raw disk devices.
dd: The disk duplicator utility.
if=/dev/sdX: Input File. Replace /dev/sdX with the actual device name of the drive you identified in Step 2 (e.g., /dev/sda, /dev/sdb, /dev/nvme0n1). Be absolutely sure this is correct!
bs=4M: Block Size. This sets the read/write block size to 4 Megabytes. A larger block size generally speeds up the copy process for large drives.
status=progress: Shows the progress of the dd command, which is very helpful for long operations.
conv=noerror,sync:
noerror: Instructs dd to continue even if it encounters read errors on the input drive.
sync: Fills input blocks with zeros if read errors occur, maintaining block size and making the output image consistent in size. This is useful for potentially failing drives.
|: The pipe symbol. It takes the standard output of dd (the raw drive data) and redirects it as standard input to the pixz command.
pixz: The parallel xz compressor.
-c: Instructs pixz to write its compressed output to standard output, which is then redirected to the cloned.img file.
-T $(nproc): Tells pixz to use all available CPU cores for compression. $(nproc) is a command substitution that expands to the number of CPU cores. This significantly speeds up compression.
>: Redirects the standard output of pixz to the specified file.
/path/to/destination/cloned.img: Output File. This is the full path and name of the compressed image file you want to create (e.g., /mnt/backup/my_drive_backup.img).
Example:
If your source drive is /dev/sdb and your external backup drive is mounted at /mnt/backup, the command would be:
The status=progress flag will show you real-time updates of how much data has been copied.
The process will take a significant amount of time, depending on the size of your drive, the read/write speed of your disks, and your CPU's compression speed.
Once the command finishes, you will see a final summary from dd and return to the command prompt.
Verification (Optional but Recommended):
You can list the contents of the pixz file to get some basic info without decompressing:
pixz -l /path/to/destination/cloned.img
This will show basic information like the compressed size.
Congratulations! You've now created a compressed dd image of your entire drive.
pixz -d < /path/to/your/cloned.img: Decompresses the image and sends its output to standard output.
|: Pipes the decompressed data to dd.
sudo dd of=/dev/sdX: Writes the decompressed data to the specified target drive (/dev/sdX). Replace /dev/sdX with your actual target drive.
bs=4M: Uses a 4MB block size for efficient writing.
status=progress: Shows progress.
conv=noerror,sync: Continues on read errors (though unlikely here as pixz would have read it first) and pads blocks.
Post-Restore Steps (Crucial for Bootability):
After dd completes, your drive now has the exact byte-for-byte copy. However, for a bootable system, especially if you moved to different hardware or simply restored, you might need to fix the bootloader (GRUB) and potentially UUIDs.
a. Check Partitions:
Run sudo fdisk -l /dev/sdX (where /dev/sdX is your restored drive) to verify the partition table looks correct.
You can also use sudo gparted /dev/sdX for a graphical view. If the target disk was larger, you might see unallocated space at the end, which you can now extend your existing partitions into.
b. Reinstall GRUB (Bootloader):
This is often necessary to make the system bootable, especially if you restored to a different drive or if the previous GRUB installation was corrupted.
Mount the restored root partition:
First, identify your root partition (e.g., /dev/sdX1, /dev/sdX2).
sudo mount /dev/sdX# /mnt # Replace /dev/sdX# with your root partition
Mount other necessary partitions (e.g., EFI, /boot) if applicable:
sudo mount /dev/sdY# /mnt/boot/efi # For EFI partition
sudo mount /dev/sdZ# /mnt/boot # If you have a separate /boot partition
Bind mount virtual filesystems:
sudo mount --bind /dev /mnt/dev
sudo mount --bind /sys /mnt/sys
sudo mount --bind /proc /mnt/proc
Chroot into the restored system:
sudo chroot /mnt
Reinstall GRUB and update config:
grub-install /dev/sdX # Replace /dev/sdX with the target physical disk (not partition)
update-grub
c. Update /etc/fstab and GRUB config (if UUIDs changed):
If you restored to a different physical disk (e.g., from an old HDD to a new SSD), the Partition UUIDs (PARTUUIDs) and Filesystem UUIDs (UUIDs) might change.
Check UUIDs:lsblk -f or sudo blkid
Edit /mnt/etc/fstab: Mount your restored root partition again, then edit /mnt/etc/fstab to reflect the new UUIDs/PARTUUIDs.
Update GRUB config (again): After editing fstab, if UUIDs were changed, chroot back in and run update-grub again to ensure it picks up the correct boot paths. Also, update-initramfs -u -k all might be helpful.
Reboot: Remove the Live USB/DVD and try booting from your restored drive.
Scenario 2: Extracting Specific Files or Directories
This is for when you just need to grab a few files without performing a full system restore. This uses the mounting process described in the previous answer.
Steps for File Extraction:
Decompress (or stream to loop device):
Option A (Recommended if space allows): Decompress to a file.
pixz -d < cloned.img > uncompressed_cloned.img
Option B (Advanced, temporary): Stream to a loop device.
Delete the uncompressed image file (if using Option A): rm uncompressed_cloned.img
Pros and Cons of Using dd Images for System Backup
Pros:
Complete Block-Level Copy: Captures everything, including boot sectors, partition tables, hidden partitions, filesystem metadata, and even unused/deleted data (which can be good for forensics). It's truly a bit-for-bit duplicate.
Filesystem Agnostic:dd doesn't care what filesystem is on the disk (ext4, NTFS, XFS, etc.) because it operates at the raw block level.
Simplicity of Concept: Once you understand the dd command, it's a very straightforward way to clone a disk.
Cons:
Size: Even with compression, the uncompressed image is always the full size of the original drive, including empty space. This means large backup files and long restore times.
No Incremental Backups: Every dd backup is a full backup. You cannot easily capture only changes since the last backup.
Complexity of Partial Restore: As shown above, recovering a single file requires decompressing (or streaming) and mounting the image, which can be cumbersome.
No File Selection: You cannot tell dd to exclude certain files or directories.
UUID Issues: Restoring to different hardware or a different drive can lead to UUID mismatches in /etc/fstab and GRUB configuration, requiring manual intervention to fix bootability.
Resizing Issues: Restoring to a smaller disk is impossible without pre-shrinking partitions on the original. Restoring to a larger disk leaves unallocated space that needs to be manually expanded into.
Bad Blocks: If your source drive has bad sectors, dd with conv=noerror,sync will copy them (replacing unreadable data with zeros), but these "corrupted" sections will be in your backup.
When dd is a Good Choice for System Backup:
Initial Baseline Backup: As a very first backup of a freshly installed system.
Forensic Purposes: To get an exact, immutable copy of a drive for analysis.
Full Drive Migration: When moving an entire system to an identical or larger physical disk.
Specific Niche Cases: Where a precise block-level copy is absolutely paramount, regardless of empty space.
Alternatives for System Backup (Often More Practical):
For everyday system backups, especially for Linux systems, other tools are often more practical:
Clonezilla / Foxclone: These are dedicated disk imaging tools that are more intelligent than dd. They can compress, handle different target drive sizes (shrinking/expanding partitions automatically), and manage bootloader installations. They often create filesystem-aware backups, skipping empty blocks.
Timeshift: Excellent for Linux system snapshots, focusing on system files, not user data. It allows easy rollbacks.
Rsync: A versatile file synchronization tool. You can use it to create highly customizable file-level backups of your entire system (excluding certain directories like /dev, /proc, /sys, etc.).
BorgBackup / Duplicity: Powerful de-duplicating and encrypted backup tools that are great for incremental backups and efficient storage.
While dd is a fundamental tool, for general system backup and recovery, a more specialized imaging tool or a file-level backup strategy might offer more flexibility, efficiency, and ease of use. However, for a truly bit-for-bit clone, dd is the way to go.
In this example, your partition would be /dev/sda2.
Using df -h (if mounted):
df -h /home
This will show you the device path associated with /home.
3. Unmount the Partition
This is critical for consistency.
sudo umount /dev/sda2 # Replace /dev/sda2 with your actual home partition device
If you're in a Live environment and that partition isn't automatically mounted, this step might not be strictly necessary, but it's good practice.
If you get "target is busy" errors, ensure no processes are accessing it. Sometimes, killing processes or rebooting into a Live environment is the only way.
4. Choose a Destination for the Image
You'll save the compressed image file here (e.g., an external USB drive).
Mount your destination drive if it's not already:
sudo mkdir /mnt/backup_dest
sudo mount /dev/sdXN /mnt/backup_dest # Replace /dev/sdXN with your external drive partition
Note down the full path for your output file, e.g., /mnt/backup_dest/home_partition_backup.img.pixz.
of=/dev/sda2: Output File. Replace /dev/sda2 with your actual target partition.
Post-Restore (Filesystem Check & Re-mount):
After dd completes, it's a good idea to run a filesystem check, especially if conv=noerror was used or if the original partition was ungracefully unmounted.
sudo e2fsck -f /dev/sda2 # For ext2/3/4 filesystems
# Or for XFS: sudo xfs_repair /dev/sda2
Then you can mount it back to /home (if you're configuring it to be your actual home partition again).
Why this is better for a Home Folder than a Full Disk Image:
Smaller Size: The image file will only be the size of the partition, not the entire disk, saving storage space.
Simpler Mounting: No complex kpartx needed; just mount -o loop.
Direct Restoration: You restore directly to a partition, avoiding bootloader or partition table complications.
More Focused: It's a backup specifically for your data, not the OS.
However, for day-to-day /home folder backups, file-level tools like rsync, tar, borgbackup, or GUI tools that use these under the hood are often more practical as they allow incremental backups, don't copy empty space, and are easier to browse. dd is great for an exact, forensic-style snapshot of a partition.