[XEN] Shrink / Reduce Windows Guest Filesystem

1) Shrink the filesystem via windows as needed.

2) Extract the corresponding VDI at the hypervisor (xenserver):
2.1) First look up the VDI UUID

xe vbd-list 

2.2) Export VDI – Format default: raw -> Set with format=…

xe vdi-export uuid=VDI_UUID filename=fs.vdi

3.) Mount VDI on another Maschine (e.g. VM via XenServer (recommended))

apt-get install qemu-utils kpartx
modprobe nbd
qemu-nbd -c /dev/nbd0 /path/to/file.vdi

3.1) Create device maps from partition tables -> Typical for windows guests: nbd0p1 (system-reserved) and nbd0p2 (data)

kpartx -a /dev/nbd0

4) Add a new virtual disk image to your VM and create corresponding partition tables on the target device -> HowTo:Fdisk

5.1) Copy boot partition with dd:

dd if=/dev/nbd0p1 of=/dev/xvdX1

5.2) Copy data partition with dd:

dd if=/dev/nbd0p2 of=/dev/xvdX2

Export our new small vdi at the hypervisor (xenserver):

xe vdi-export uuid=VDI_UUID filename=fs.vdi

Create new VBD (Connector between VM and VDI)

xe vdb-create vm-uuid=WINDOWS_GUEST_VM_UUID device=0 bootable=yes mode=RW type=Disk

Plug new VDB to VM