DEV Community

Yigal Korman
Yigal Korman

Posted on

2 1

Access VM disk image from the host

Many times there are multiple solutions to a problem. This statement is driven to an extreme by the number of tools provided in a standard Linux distribution.

Most of the time, I'd like to find the fastest solution.
In Linux, the fastest solution usually includes installing a tool that does the job.

But sometimes I prefer an elegant solution - one that uses the tools at hand, one that will teach me more about the versatility of the environment I'm already using.

The task at hand was to install an upstream kernel on a fedora VM.
The catch - the VM has no network connectivity.
Why? no reason really. I just wanted a simple test VM that didn't need any network adapters in the first place.

How do I copy the kernel sources and all related build tools to the VM?

Possible solutions could have been - adding a network adapter, sharing the host FS using some modern solution like virtio-fs, or using a specific tool for the job - something like libguestfs.

But if I want a more elegant solution?

Assuming I'm already using Qemu/KVM as the hypervisor, it's likely I already have a tool for the job. It's called qemu-nbd.
NBD stands for "Network Block Device" - it's a protocol for accessing block devices over the network.
qemu-nbd provides a way to access the VM disk image as if it was a block device on a remote host.
The Linux kernel has a built-in driver for NBD, so accessing a disk image is fairly simple.

Load the nbd driver module:

sudo modprobe nbd

nbd provides some module parameters: nbds_max and max_part, see modinfo nbd for their description.

Then use qemu-nbd to map the disk image to an nbd device:

sudo qemu-nbd -c /dev/nbd0 image.qcow2

The disk image most likely contained a partitioned disk, so the result will be multiple partitions - /dev/nbd0p1, /dev/nbd0p2, etc.
Now we can mount and access the filesystem on the image by mounting the nbd partion:

sudo mount /dev/nbd0p1 /mnt

That's it.

Don't forget to clean up after you're done:

sudo umount /mnt
sudo qemu-nbd -d /dev/nbd0
sudo rmmod nbd

In future posts, I'll discuss how to install an upstream kernel on a mounted image and also how to create an image from scratch.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more