Mounting a .vhd disk image using blktap/tapdisk

From Xen
Revision as of 11:04, 25 November 2013 by Dave.scott (talk | contribs)

This page describes how to mount a .vhd disk image on a Linux system, using blktap/tapdisk. This page shows you how to modify and manipulate a .vhd disk image before booting a VM from it.

Overview

A .vhd disk image is a format originally created by Connectix for their Virtual PC and Virtual Server products, sold to Microsoft in 2003. The .vhd format is well-documented and supported by blktap/tapdisk. When using .vhd, blktap/tapdisk enable:

  • thin-provisioning of disks: blocks are only allocated on first write
  • snapshots, clones: gold master images can be used as read-only "parent" disks

Other similar formats exist, most notably [qcow2] used natively by qemu.

How blktap/tapdisk works

"blktap" is a kernel module which is like "tuntap for blocks": it exposes a kernel block device and sends requests to user-space over a shared-memory ring. The only consumer of this ring is "tapdisk": a user-space process which understands the .vhd format (via a shared library called libvhd) and which performs read and write operations via AIO.

Note: when a tapdisk is connected to a blktap, it's important not to interfere with tapdisk (e.g. by killing it) because this will cause in-flight requests on the block device to block.

When tapdisk starts it will call mlockall() to prevent any of its pages being swapped out, which would otherwise lead to deadlocks.

Prereqs

This example assumes Ubuntu 12.04, but similar packages should be available for other distributions. First install the user-space tools and the blktap kernel module (via DKMS):

sudo apt-get install blktap-utils
sudo modprobe blktap

Attaching a disk

First, allocate a minor number in the kernel:

sudo tap-ctl allocate           
/dev/xen/blktap-2/tapdev0

(the minor number is the number following "tapdev", i.e. 0 in this example). This is the path which will be the block device later.

Then, spawn a tapdisk process:

sudo tap-ctl spawn
tapdisk spawned with pid 5646

Now, you need to attach the two together:

sudo tap-ctl attach -m 0 -p 5646

(where -m <minor> and -p <tapdisk pid>)

Finally, you can open a VHD:

sudo tap-ctl open -m 0 -p 5646 -a vhd:/home/djs/smalltest.vhd