In this tutorial you wil learn how to mount VM partitions within a disk image. If you use virtualisation technologies, such as KVM or XEN, there will be occasions where you need to access the data within a partition, in particular when the disk image has been partitioned by an OS installer such as Debian.

A good example of when this might be neccessary is if a VM (Virtual Machine) cannot boot and you need to access the data.

Requirements

  • You will need root access to the Linux distribution
  • You will need a disk image that is not in use or can be mounted.

Before you begin

For this tutorial, we will be using Debian 11, you can use other versions of Linux such as CentOS or AlmaLinux. If you require a server for testing, please visit our website.

Create A Disk Image

For our tutorial we will now create a new disk image to demonstrate the process of how to create disk image file, then to mount it and create an empty file within the image.

Change to a directory that has disk space available, for example /home.

Next create the disk image by running the following command:

dd if=/dev/zero of=mydisk.img bs=1024k count=0 seek=5000

You should now have a disk image called mydisk.img which is 5GB in size.

Now we can format the volume by running the following command:

mkfs.ext4 mydisk.img

We can now easily mount this volume to a mount point and create files. To do this run the following command:

mount -o loop mydisk.img /mnt/

If we now change to the mount point directory, we can create an empty file:

cd/mnt
touch myfile

We can now view the new file within the new disk image

ls -la

As you can see, this is a simple process, the disk image is created, we have mounted the disk image and created an empty file.

When using VM creation tools such as bootstrap from the main hypervisor, this is the normal process for mounting the disk image, the partition is create by the hypervisor and is accessible.

Mounting HVM Disk Images

The problem comes when you create a new HVM and the OS installer is run from an ISO or net install. The installer will usually create multiple partitions within the image, therefore when you attempt to mount the disk from the hypervisor, it will not work.

mount -o loop mydisk.img /mnt/disk1: you must specify the filesystem type

We can examine the file further by running the following command:

fdisk -lu mydisk.img

This gives us an output of

You must set cylinders.
You can do this from the extra functions menu.

Disk hda: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot      Start         End      Blocks   Id  System
  centos.img   *     1060290    17848214     8393962+  83  Linux
Partition 1 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1110, 254, 63)
  hda2        17848215    20964824     1558305   83  Linux
Partition 2 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 254, 63) logical=(1111, 0, 1)
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1304, 254, 63)

The ‘u’ flag inside fdisk tells us the partition table sizes in sectors rather than cylinders.  From here we will need this information so we can calculate the correct offset.

Calculate the Offset in “Bytes”

In order to mount the two partitions from the image, we need to know the starting Byte from where each partition starts.

To calculate this offset, we have to mulitply start_sector * sector_byte_size. So for Partition 1 we have (1060290 * 512) = 542868480 and for Partition 2 we have (17848215 * 512) = 9138286080.

With is information we can now mount each partition to a mount point on our main host.

First we will create 2 new mount point directories by running the following commands:

mkdir /mnt/partition1
mkdir /mnt/partition2

Next, lets now mount the disk image paritions by using the information we gathered from the fdisk output and the calculation by running the following commands:

mount -o loop,offset=542868480 mydisk.img /mnt/partition1
mount -o loop,offset=9138286080 mydisk.img /mnt/partition2

As you can see, the partitions mounted successfully.

We can now change into each mount point directory and view files within the disk image.

You have now learned how to mount VM partitions within a disk image. Caution should be taken when carrying out these steps as data could be lost if you incorrectly mount the partition.

Share.

Leave A Reply