About attaching and detaching Xen domU mounts

If you ever need to replace a mount of a Xen domU guest without rebooting the server, you can’t just remount it and be done. In my case, I needed to increase the size of the swap file a certain DomU was using.

Independent from mounting or unmounting a partition inside the DomU, Xen saves its virtual device information inside a logical structure in an information storage called the XenStore. So to change partition information without rebooting the image, you need to inform Xen that you’ll be changing things, and want to detach the virtual device (in our case, a swap file) from the DomU. This is the way:

In the domU that needs to be changed, turn off the swap space:
# swapoff /dev/sda2

Then, in Dom0, list the domains for an overview:
xen1:~# xm list
Name ID Mem(MiB) VCPUs State Time(s)
myVM 17 2000 1 -b---- 134487.4

List the virtual block devices that are coupled to the specified domain:
xen1:~# xm block-list 17
Vdev BE handle state evt-ch ring-ref BE-path
2049 0 0 4 6 8 /local/domain/0/backend/vbd/17/2049
2050 0 0 4 7 9 /local/domain/0/backend/vbd/17/2050

Read what vdev is coupled to what device name inside the domU:
xen1:~# xenstore-read /local/domain/0/backend/vbd/17/2050/dev

Detach the swap partition:
xen1:~# xm block-detach 17 2050

Create the new swap space (to create a 1G swap file):
xen1:~# dd if=/dev/zero of=/xen/swap/myVM.swap bs=1024k count=100

Attach the new swap partition back to the domain:
xen1:~# xm block-attach 17 file:/xen/swap/myVM.swap sda2 w

Then, with this in place, go back to the domU console, create the swap filesystem and activate it:
# mkswap /dev/sda2
# swapon /dev/sda2

That’s about it for updating a mount point inside the DomU. This can be done for every block device that a Xen DomU may use, as long as it isn’t system-critical, such as the root filesystem).

This procedure also makes use of some lesser known commands such as xm block-attach, xm block-detach and xenstore-read.
Although there isn’t much documentation about these commands, they are worth checking out in case you would need them.