Index ¦ Archives ¦ Atom

Unpacking img files ubuntu

Introduction

This is an example of how to unpack an image and access the files from an img file. The problem is that depending on the actual file, different procedures (and tools) need to be used.

Preparation

A ubuntu 22.04 cloud image from https://cloud-images.ubuntu.com/jammy/current/ is used to create a VM that runs the commands outlined in this document.

The cloud image at https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img is used as the example image to be unpacked.

Checking the file type of the image

The file command helpfully demistifies the file (despite the extension, which is a mere suggestion really) and reads the magic file number to figure out what file type it is:

file jammy-server-cloudimg-amd64.img 
jammy-server-cloudimg-amd64.img: QEMU QCOW2 Image (v2), 2361393152 bytes

If the img file type is raw and not qcow2, the img can be directly mounted using the following command:

sudo mkdir -p /mnt/mymount
sudo mount -o loop my.img /mnt/mymount

Installing dependencies

Install kpartx and libguestfs-tools:

sudo apt-get -y install kpartx libguestfs-tools

Mounting the image using libguestfs-tools

sudo mkdir -p /mnt/mymount
sudo guestmount -a /opt/jammy-server-cloudimg-amd64.img -i --ro /mnt/mymount/

Copy data from the Linux system

Windows 10+ has an integrated SSH client which can be used to copy data from a virtual machine. It might need to be enabled to be available from Powershell, in which case check instructions on the net to do so. With the integrated SSH client, one can use scp to copy the files directly from the Linux system to a local (Windows) machine:

scp user@233.252.1.10:/home/myfile ./

Alternative options to the SSH client are WinSCP and Putty.

Unmounting and cleaning up

Once you are done copying files, unmount the image using:

sudo umount /mnt/mymount

© Bruno Henc. Built using Pelican. Theme by Giulio Fidente on github.