#!/bin/sh # Script to demonstrate creating and mounting a disk image using LVM and Xen set -x set -e lvm version || sudo apt-get install lvm2 dmsetup mdadm # After installing Xen, reboot and choose the Xen kernel. # Note that you won't be able to use nvidia graphics cards under Xen, # so you'll probably need to ssh into the box after booting linux in xen. xm help > /dev/null || sudo apt-get install virtinst virt-manager virt-viewer xen-hypervisor-4.1-i386 xenstore-utils xen-tools xen-utils-4.1 xen-utils-common xenwatch test -b /dev/xvda1 || sudo modprobe xen-blkfront # Create a demo physical volume # Make a sparse image file of size 64MB dd if=/dev/zero of=foo.img bs=1024k seek=64 count=0 # Make a loopback device for it sudo losetup /dev/loop0 foo.img # Register it as a physical volume sudo pvcreate -f -M2 /dev/loop0 # List the known physical volumes sudo pvdisplay # Create a demo volume group and put that physical volume in it sudo vgcreate demo_vg /dev/loop0 # List the known volume groups sudo vgdisplay # Create a logical volume in that volume group sudo lvcreate --name foo_lv --size 50MB demo_vg # List the known logical volumes sudo lvdisplay # Attach it to domain 0 sudo xm block-attach 0 phy:/dev/demo_vg/foo_lv /dev/xvda1 w 0 # Show the newly attached block device sudo xm block-list 0 # Create a filesytem on it sudo mkfs.ntfs /dev/xvda1 # Mount it somewhere sudo mkdir /media/foo sudo mount /dev/xvda1 /media/foo # Show that it's real df /media/foo # Unmount it sudo umount /media/foo sudo rmdir /media/foo # Detach it sudo xm block-detach 0 /dev/xvda1 -f # Remove the logical volume # BUG?: in Ubuntu 11.10, lvremove complains "semop failed" # See https://bugs.launchpad.net/ubuntu/+source/lvm2/+bug/846061 sudo lvremove -f /dev/demo_vg/foo_lv # Remove the volume group sudo vgremove demo_vg # Remove the physical volume sudo pvremove /dev/loop0 # BUG?: have to sleep here, else losetup -d fails sometimes? sleep 1 # Remove the loopback device sudo losetup -d /dev/loop0 # Remove the image rm foo.img