[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-users] newbie question re block-attach on ubuntu 11.10



For completeness, here's a script that shows the smallest possible example
of using xen and lvm together to create and format a blank virtual disk,
and then to tear it all down.   It works with ubuntu-11.10.
There's nothing at all special about it, it's just an first example for newbies
trying to work out how volume management works,
expressed purely as a shell script that uses xm in dom0.
It assumes you're using a 32 bit kernel.
(This is http://kegel.com/linux/xen/demo-lvm-xen.sh.txt )

#!/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 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

_______________________________________________
Xen-users mailing list
Xen-users@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-users


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.