Switch from raw images to qcow2 format

This allows to use minimal copy-on-write clones of the base image as
guest images, which in turn saves a lot of disk space.
This commit is contained in:
Reto Buerki
2013-01-17 16:54:54 +01:00
committed by Tobias Brunner
parent 7fa2719185
commit 8ed98c1373
15 changed files with 113 additions and 46 deletions
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/alice.img'/> <source file='/var/lib/libvirt/images/alice.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/bob.img'/> <source file='/var/lib/libvirt/images/bob.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/carol.img'/> <source file='/var/lib/libvirt/images/carol.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/dave.img'/> <source file='/var/lib/libvirt/images/dave.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/moon.img'/> <source file='/var/lib/libvirt/images/moon.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/sun.img'/> <source file='/var/lib/libvirt/images/sun.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/venus.img'/> <source file='/var/lib/libvirt/images/venus.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+3 -3
View File
@@ -7,7 +7,7 @@
<os> <os>
<type arch='x86_64' machine='pc-1.1'>hvm</type> <type arch='x86_64' machine='pc-1.1'>hvm</type>
<kernel>/var/run/kvm-swan-kernel</kernel> <kernel>/var/run/kvm-swan-kernel</kernel>
<cmdline>root=/dev/vda loglevel=1</cmdline> <cmdline>root=/dev/vda1 loglevel=1</cmdline>
<boot dev='hd'/> <boot dev='hd'/>
</os> </os>
<features> <features>
@@ -22,8 +22,8 @@
<devices> <devices>
<emulator>/usr/bin/kvm</emulator> <emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'> <disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='writethrough'/> <driver name='qemu' type='qcow2' cache='writethrough'/>
<source file='/var/lib/libvirt/images/winnetou.img'/> <source file='/var/lib/libvirt/images/winnetou.qcow2'/>
<target dev='vda' bus='virtio'/> <target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk> </disk>
+31 -10
View File
@@ -10,6 +10,7 @@ echo "Building base image"
EXTRAS=build-essential,gperf,libgmp-dev,libldap2-dev,libcurl4-openssl-dev,libxml2-dev,libtspi-dev,libsqlite3-dev,openssh-server,tcpdump,psmisc,openssl,vim,sqlite3,conntrack,gdb,cmake,libxerces-c2-dev,libltdl-dev,liblog4cxx10-dev,libboost-thread-dev,libboost-system-dev,git-core EXTRAS=build-essential,gperf,libgmp-dev,libldap2-dev,libcurl4-openssl-dev,libxml2-dev,libtspi-dev,libsqlite3-dev,openssh-server,tcpdump,psmisc,openssl,vim,sqlite3,conntrack,gdb,cmake,libxerces-c2-dev,libltdl-dev,liblog4cxx10-dev,libboost-thread-dev,libboost-system-dev,git-core
SERVICES="isc-dhcp-server apache2 slapd" SERVICES="isc-dhcp-server apache2 slapd"
PACKAGES=$EXTRAS,${SERVICES// /,} PACKAGES=$EXTRAS,${SERVICES// /,}
CACHEDIR=$BUILDDIR/cache CACHEDIR=$BUILDDIR/cache
APTCACHE=$LOOPDIR/var/cache/apt/archives APTCACHE=$LOOPDIR/var/cache/apt/archives
@@ -19,28 +20,45 @@ mkdir -p $CACHEDIR
rm -f $ROOTFS rm -f $ROOTFS
echo "`date`, building $ROOTFS" >>$LOGFILE echo "`date`, building $ROOTFS" >>$LOGFILE
load_qemu_nbd
log_action "Creating image $ROOTFS" log_action "Creating image $ROOTFS"
execute "dd if=/dev/null of=$ROOTFS bs=1M seek=$ROOTFSSIZE count=1" execute "qemu-img create -f qcow2 $ROOTFS ${ROOTFSSIZE}M"
log_action "Connecting image to NBD device $NBDEV"
execute "qemu-nbd -c $NBDEV $ROOTFS"
sync
log_action "Partitioning disk"
sfdisk /dev/nbd0 -D -uM >>$LOGFILE 2>&1 << EOF
;
EOF
if [ $? != 0 ]
then
log_status 1
exit 1
else
log_status 0
fi
log_action "Creating ext3 filesystem" log_action "Creating ext3 filesystem"
execute "mkfs.ext3 -F $ROOTFS" execute "mkfs.ext3 $NBDPARTITION"
log_action "Mounting image to $LOOPDIR" log_action "Mounting $NBDPARTITION to $LOOPDIR"
execute "mount -o loop $ROOTFS $LOOPDIR" execute "mount $NBDPARTITION $LOOPDIR"
log_action "Mounting cache to $CACHEDIR" log_action "Using $CACHEDIR as archive for apt"
mkdir -p $APTCACHE mkdir -p $APTCACHE
execute "mount -o bind $CACHEDIR $APTCACHE" execute "mount -o bind $CACHEDIR $APTCACHE"
do_on_exit umount -l $APTCACHE do_on_exit graceful_umount $APTCACHE
do_on_exit umount -l $LOOPDIR do_on_exit graceful_umount $LOOPDIR
do_on_exit qemu-nbd -d $NBDEV
log_action "Running debootstrap ($ROOTFSSUITE, $ROOTFSARCH)" log_action "Running debootstrap ($ROOTFSSUITE, $ROOTFSARCH)"
execute "debootstrap --arch=$ROOTFSARCH --include=$PACKAGES $ROOTFSSUITE $LOOPDIR $ROOTFSMIRROR" execute "debootstrap --arch=$ROOTFSARCH --include=$PACKAGES $ROOTFSSUITE $LOOPDIR $ROOTFSMIRROR"
log_action "Disabling root password"
execute_chroot "passwd -d root"
for service in $SERVICES for service in $SERVICES
do do
log_action "Stopping service $service" log_action "Stopping service $service"
@@ -48,3 +66,6 @@ do
log_action "Disabling service $service" log_action "Disabling service $service"
execute_chroot "update-rc.d -f $service remove" execute_chroot "update-rc.d -f $service remove"
done done
log_action "Disabling root password"
execute_chroot "passwd -d root"
+10 -5
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# create UML host file systems # create specific guest images
# #
# Copyright (C) 2004 Eric Marchionni, Patrik Rayo # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
# Zuercher Hochschule Winterthur # Zuercher Hochschule Winterthur
@@ -20,12 +20,14 @@ echo "Creating guest images"
[ `id -u` -eq 0 ] || die "You must be root to run $0" [ `id -u` -eq 0 ] || die "You must be root to run $0"
BASE=$BUILDDIR/base.img BASE=$BUILDDIR/base.qcow2
HOSTSDIR=$PWD/hosts HOSTSDIR=$PWD/hosts
[ -f $BASE ] || die "Base image $BASE not found" [ -f $BASE ] || die "Base image $BASE not found"
[ -f $HOSTDIR ] || die "Hosts directory $HOSTSDIR not found" [ -f $HOSTDIR ] || die "Hosts directory $HOSTSDIR not found"
load_qemu_nbd
mkdir -p $BUILDDIR mkdir -p $BUILDDIR
mkdir -p $LOOPDIR mkdir -p $LOOPDIR
@@ -41,8 +43,10 @@ do_on_exit umount $LOOPDIR
for host in $HOSTS for host in $HOSTS
do do
log_action "Creating guest image for $host" log_action "Creating guest image for $host"
execute "cp $BASE $ROOTFSDIR/$host.img" 0 execute "qemu-img create -b $BASE -f qcow2 $ROOTFSDIR/$host.qcow2" 0
execute "mount -o loop $ROOTFSDIR/$host.img $LOOPDIR" 0 execute "qemu-nbd -c $NBDEV $ROOTFSDIR/$host.qcow2" 0
sync
execute "mount $NBDPARTITION $LOOPDIR" 0
execute "cp -rf $HOSTSDIR/${host}/etc $LOOPDIR" 0 execute "cp -rf $HOSTSDIR/${host}/etc $LOOPDIR" 0
execute "cp -rf $HOSTSDIR/default/etc $LOOPDIR" 0 execute "cp -rf $HOSTSDIR/default/etc $LOOPDIR" 0
if [ "$host" = "winnetou" ] if [ "$host" = "winnetou" ]
@@ -58,6 +62,7 @@ do
execute_chroot "chown -R openldap:openldap /var/lib/ldap" 0 execute_chroot "chown -R openldap:openldap /var/lib/ldap" 0
fi fi
sync sync
execute "umount $LOOPDIR" 0
execute "qemu-nbd -d $NBDEV" 0
log_status 0 log_status 0
umount $LOOPDIR
done done
+12 -4
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Create UML root filesystem # Create guest root image
# #
# Copyright (C) 2004 Eric Marchionni, Patrik Rayo # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
# Zuercher Hochschule Winterthur # Zuercher Hochschule Winterthur
@@ -22,17 +22,24 @@ echo "Building root image"
[ -f "$ROOTFS" ] || die "Root image $ROOTFS not found" [ -f "$ROOTFS" ] || die "Root image $ROOTFS not found"
load_qemu_nbd
mkdir -p $ROOTFSDIR mkdir -p $ROOTFSDIR
mkdir -p $LOOPDIR mkdir -p $LOOPDIR
mkdir -p $ROOTFSCOMPILEDIR mkdir -p $ROOTFSCOMPILEDIR
cd $ROOTFSDIR cd $ROOTFSDIR
BASE=$BUILDDIR/base.img BASE=$BUILDDIR/base.qcow2
log_action "Creating $BASE" log_action "Creating $BASE"
execute "cp $ROOTFS $BASE" execute "cp $ROOTFS $BASE"
log_action "Mounting base image $BASE" log_action "Connecting base image to NBD device $NBDEV"
execute "mount -o loop $BASE $LOOPDIR" execute "qemu-nbd -c $NBDEV $BASE"
sync
log_action "Mounting $NBDPARTITION to $LOOPDIR"
execute "mount $NBDPARTITION $LOOPDIR"
log_action "Mounting proc filesystem to $LOOPDIR/proc" log_action "Mounting proc filesystem to $LOOPDIR/proc"
execute "mount -t proc none $LOOPDIR/proc" execute "mount -t proc none $LOOPDIR/proc"
@@ -43,6 +50,7 @@ execute "mount -o bind $ROOTFSCOMPILEDIR $LOOPDIR/root/compile"
do_on_exit umount $LOOPDIR/root/compile do_on_exit umount $LOOPDIR/root/compile
do_on_exit umount $LOOPDIR/proc do_on_exit umount $LOOPDIR/proc
do_on_exit umount $LOOPDIR do_on_exit umount $LOOPDIR
do_on_exit qemu-nbd -d $NBDEV
echo "Installing software from source" echo "Installing software from source"
RECPDIR=$TESTDIR/testing/scripts/recipes RECPDIR=$TESTDIR/testing/scripts/recipes
+31
View File
@@ -117,6 +117,37 @@ do_on_exit()
fi fi
} }
# wait for a mount to disappear
# $1 - device/image to wait for
# $2 - maximum time to wait in seconds, default is 5 seconds
graceful_umount()
{
secs=$2
[ ! $secs ] && secs=5
let steps=$secs*100
for i in `seq 1 $steps`
do
umount $1 >>$LOGFILE 2>&1
mount | grep $1 >/dev/null 2>&1
[ $? -eq 0 ] || return 0
sleep 0.01
done
return 1
}
# load qemu NBD kernel module, if not already loaded
load_qemu_nbd()
{
lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
if [ $? != 0 ]
then
log_action "Loading NBD kernel module"
execute "modprobe nbd max_part=16"
fi
}
############################################# #############################################
# search and replace strings throughout a # search and replace strings throughout a
# whole directory # whole directory
+1 -1
View File
@@ -22,7 +22,7 @@ done
for host in $STRONGSWANHOSTS for host in $STRONGSWANHOSTS
do do
ln -fs $ROOTFSDIR/$host.img $VIRTIMGSTORE/$host.img ln -fs $ROOTFSDIR/$host.qcow2 $VIRTIMGSTORE/$host.qcow2
log_action "Guest $host" log_action "Guest $host"
execute "virsh create $CONFDIR/$host.xml" execute "virsh create $CONFDIR/$host.xml"
done done
+1 -1
View File
@@ -19,7 +19,7 @@ for host in $STRONGSWANHOSTS
do do
log_action "Guest $host" log_action "Guest $host"
execute "virsh destroy $host" execute "virsh destroy $host"
rm -f $VIRTIMGSTORE/$host.img rm -f $VIRTIMGSTORE/$host.qcow2
done done
log_action "Removing kernel $KERNEL" log_action "Removing kernel $KERNEL"
+3 -1
View File
@@ -40,7 +40,7 @@ LOOPDIR=$BUILDDIR/loop
ROOTFSSIZE=1024 ROOTFSSIZE=1024
ROOTFSSUITE=wheezy ROOTFSSUITE=wheezy
ROOTFSARCH=amd64 ROOTFSARCH=amd64
ROOTFS=$BUILDDIR/debian-$ROOTFSSUITE-$ROOTFSARCH.img ROOTFS=$BUILDDIR/debian-$ROOTFSSUITE-$ROOTFSARCH.qcow2
ROOTFSMIRROR=http://cdn.debian.net/debian ROOTFSMIRROR=http://cdn.debian.net/debian
ROOTFSCOMPILEDIR=$BUILDDIR/compile ROOTFSCOMPILEDIR=$BUILDDIR/compile
@@ -48,6 +48,8 @@ ROOTFSCOMPILEDIR=$BUILDDIR/compile
ROOTFSDIR=$BUILDDIR/root-fs ROOTFSDIR=$BUILDDIR/root-fs
# libvirt config # libvirt config
NBDEV=/dev/nbd0
NBDPARTITION=${NBDEV}p1
VIRTIMGSTORE=/var/lib/libvirt/images VIRTIMGSTORE=/var/lib/libvirt/images
# Directory where test results will be stored # Directory where test results will be stored