Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

In Linux, files with the .img.xz extension are commonly used to distribute compressed storage medium images such as a hard disk. From back to front, the xz part of the extension typically refers to the xz data compression algorithm and tool. The latter is a general-purpose utility with a command-line interface similar to gzip and bzip2. Specifically, xz compresses and decompresses given files based on the selected operation mode. In general, the .img extension represents the image file, while the .xz denotes the compression algorithm.

In this tutorial, we’ll outline the process of extracting and using .img.xz files on a Linux system, including the necessary commands and steps involved.

2. Prerequisites

First and foremost, let’s ensure we have all the required tools installed within the current Linux system:

  • xz-utils package with tools for handling .xz files
  • bzip2 package in case the .xz file is compressed using other formats
  • gzip package, crucial for handling .gz files

To proceed, let’s use the native package manager to install these utilities.

For instance, on Debian-based systems like Ubuntu, we use APT:

$ sudo apt install xz-utils bzip2 gzip

On the other hand, on Red Hat-based systems like Fedora, we can use DNF:

$ dnf install xz-utils bzip2 gzip

Finally, we employ Pacman on Arch Linux:

$ pacman -S xz-utils bzip2 gzip

After the installation is complete, we should be able to extract images from the img.xz file.

3. Extracting the .img.xz File

To begin with, we open the terminal and navigate to the directory that contains the .img.xz files.

Then, let’s use the xz command to decompress the file:

$ xz -d filename.img.xz

The -d option stands for decompress. As a result, this command produces an uncompressed file named filename.img in the same directory.

Alternatively, we can also use the unxz command:

$ unxz filename.img.xz

This command is a shorthand for xz -d.

4. Verifying the Extracted Image

Since it usually contains a large amount of data, it’s often essential to verify the integrity of an .img file.

In this case, we use the file command to check the file type:

$ file filename.img
filename.img: DOS/MBR boot sector; partition 1 : ID=0x83, starthead 32, startsector 2048, 524288 sectors, extended partition table (last)

This output confirms that filename.img is a disk image with a Master Boot Record (MBR) and a partition table. Furthermore, the details of the partitions are also displayed, indicating that it should be a valid disk image that can be used for deployment or mounting.

5. Dealing With Incomplete or Corrupt File

Encountering issues like decompression failures can be frustrating. In particular, errors indicating issues with the file format recognition often suggest that the file may be incomplete or corrupt.

5.1. Basic Checks

If we encounter an error message stating that the file format isn’t recognized or that the file isn’t valid, it could mean it is incomplete or wasn’t downloaded fully. Alternatively, the file might be corrupted, damaged, or improperly formatted.

To address this issue, we can start by verifying the file size of the extracted image:

$ ls -l filename.img
-rw-rw-r-- 1 user user 128331200 Wax 23 19:52 filename.img

The output includes a line showing the file size in bytes. In this example, the file size is 128331200 bytes.

Next, we can obtain the expected size of the extracted .img file using the xz command:

$ xz -l filename.img.xz

Finally, we can compare the size of the extracted image with the expected size to find out if they match.

If the filename.img file size doesn’t align with the expected size, the file might be incomplete or corrupted. Consequently, we may need to take further steps to address the issue.

5.2. Handling Incomplete or Corrupted Files

If the file size doesn’t match the expected length, we have some options to address the issue:

  • re-download the file and ensure it comes from a credible and reliable source
  • consider using a download manager that can resume interrupted downloads for large files
  • verify the file integrity with checksums

An example integrity check involves computing the SHA256 checksum of the filename.img.xz file:

$ sha256sum filename.img.xz

Then, we can compare it with the checksum value from the website where we downloaded the file.

Of course, if the file is broken at the source or if we’re unable to download it fully, this should be addressed separately.

5.3. Using a Different Tool

If the xz tool fails to decompress the file, we might try using alternatives to see if they can handle the extraction.

For example, 7zip is available by default on most Linux distros, so we often don’t need to worry about installation.

Let’s use 7z to decompress the filename.img.xz file:

$ 7z x filename.img.xz

This command should again output a file named filename.img in the same directory.

6. Using the Extracted .img File

After successfully extracting the .img file, we might want to use it in several ways.

Some of the most common tasks we may consider include mounting the image or flashing it to a storage device. Since it’s one of the most common, let’s discuss mounting.

First, create a mount point. Specifically, this involves creating a directory where we mount the image:

$ mkdir /mnt/myimage

Then, we can use the mount command to attach the image file to the directory. Additionally, we need to specify the loop device:

$ mount -o loop filename.img /mnt/myimage

After mounting, we can access the image’s contents by navigating to the /mnt/image directory.

When ready, we can unmount the image by using the unmount command:

$ umount /mnt/myimage

Thus, we’ve worked with the contents of the image and then unmounted the filename.img file.

7. Conclusion

In this article, we examined how to extract an img.xz file in Linux via the xz and 7zip commands. Additionally, we verified the extracted image by comparing its size to the expected file size. After the extraction process, we also looked at how to deal with corrupted or incomplete files by verifying the file integrity with checksums.

Finally, we mounted the extracted .img file using the mount command. By properly articulating the steps outlined in this guide, we can effectively manage compressed image files and integrate them into different Linux workflows.