首页 > 代码库 > installation and administration Vms on centos

installation and administration Vms on centos

                         installation and administration Vms on centos

1.Compile qemu

wget http://wiki.qemu.org/download/qemu-0.15.1.tar.gz
tar zxf qemu-0.15.1.tar.gz
cd qemu-0.15.1
./configure --prefix=/opt/qemu-0.15.1
make
make install

ln -s /opt/qemu-0.15.1/bin/qemu-nbd /usr/local/bin/

2.Installvirtualization tools

 yum install libvirt

yum install virt-manger

3.Install template OS(using "virt-manage" seems easy)

I don‘t decribe it.you can install any OS step by step by virt-manager.

we assume that the name of disk is ubuntu12.04.img.

I suggest you rather use raw format for virtual disk as a template image file.I think "qemu-img" also is a good tool to create an image,like this:

qemu-img create -f raw ubuntu12.04.img 20G

4.dump VMs and create new

virsh dumpxml ubuntu12.04 >ubuntu2.xml

virsh undefine ubuntu12.04

create a new disk image for the other OS

qemu-img create -f qcow2 -o\ cluster_size=2M,backing_file=ubuntu12.04.img ubuntu2.qcow2 30G

5. mount ubuntu2.qcow2 to change OS settings

yum install kmod-nbd

qemu-nbd -c /dev/nbd0 ubuntu2.qcow2

partprobe /dev/nbd0
fdisk -l /dev/nbd0

proxy?url=http%3A%2F%2F2.bp.blogspot.com

 mount /dev/nbd0p1 /mnt

Now you should do some work for the new OS

1)change hostname

sed -i "s,127.0.1.1.*,127.0.1.1 ubuntu2,g" /mnt/etc/hosts

sed -i "s,ubuntu,ubuntu2,g" /mnt/etc/hostname

MAC="52:54:$(dd if=/dev/urandom count=1 2>/dev/null \


          | md5sum | sed s/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/)"


vim /mnt/etc/udev/rules.d/70-persistent-net.rules

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="%MAC%", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

REMEMBER:PLEASE USE MAC TO REPLACE %MAC%

2) set network

vim /mnt/etc/network/interfaces

 auto lo

 iface lo inet loopback

 auto eth0

 iface eth0 inet static

 address 192.168.122.222

 netmask 255.255.255.0

 broadcast 192.168.122.255

 gateway 192.168.122.1

6.umount nbd device

umount /mnt

qemu-nbd -d /dev/nbd0

7.create vm‘s xml file

cp template.xml ubuntu2.xml

modify "%VM_NAME","%UUID%","%MAC%","%IMAGE_PATH%"

sed -i "s,%VM_NAME,ubuntu2,g" ubuntu2.xml

UUID=`uuidgen`

sed -i "s,%UUID%,$UUID,g" ubuntu2.xml

OK,please refer Section 5 to modify MAC.change "%IMAGE_PATH%" to the path where your image is in.


8.define VM via virsh command

virsh define ubuntu2.xml

virsh start ubuntu2

attention:if you installed kvm by yum,the emulator path is "/usr/libexec/qemu-kvm" not "/usr/bin/kvm" in ubuntu2.xml file.

check the vnc port

virsh vncdisplay ubuntu2


Ok,That‘s all.you have already been able to create new Vms quickly now.


本文出自 “技术为王” 博客,转载请与作者联系!

installation and administration Vms on centos