前面写了一系列文章,现在在这里用一篇文章概括一下,方便查看。本文针对Ubuntu 13.10 server。
root@kvmhost:~# egrep '(vmx|svm)' --color=always /proc/cpuinfo flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 sse4_2 popcnt lahf_lm dts tpr_shadow vnmi flexpriority ept vpid flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 sse4_2 popcnt lahf_lm dts tpr_shadow vnmi flexpriority ept vpid
apt-get install ubuntu-virt-server python-vm-builder kvm-ipxe
adduser `id -un` libvirtd adduser `id -un` kvm
然后reboot后,再登录,运行下面的命令检查
root@kvmhost:~# virsh -c qemu:///system list Id Name State ----------------------------------
如下设置/etc/network/interfaces
# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth1 iface eth1 inet manual auto br0 iface br0 inet static address 10.150.140.103 network 10.150.140.0 netmask 255.255.0.0 broadcast 10.150.255.255 gateway 10.150.0.254 dns-nameservers 10.150.0.253 8.8.8.8 bridge_ports eth1 bridge_fd 9 bridge_hello 2 bridge_maxage 12 bridge_stp off
br0下面的dns, netmask, broadcast都来自原来eth1的设置。
KVM宿主机设置完毕,重新启动吧。
从Ubuntu 12.10开始,KVM Builder就有一个bug,导致创建虚拟机的时候遇到如下错误:
Selecting previously unselected package linux-image-virtual. Unpacking linux-image-virtual (from .../linux-image-virtual_3.5.0.17.19_amd64.deb) ... , stderr: grep: /proc/cpuinfo: No such file or directory这个是Ubuntu 12.10/13.10自带的vmbuilder的bug,原因是没有mount /proc目录,这里讨论了这个问题,并提供了临时解决方案:
https://bugs.launchpad.net/ubuntu/+source/vm-builder/+bug/1037607
打补丁的命令如下:
wget https://launchpadlibrarian.net/120169451/usr_share_pyshared_VMBuilder_plugins_ubuntu_dapper.py.diff cp /usr/share/pyshared/VMBuilder/plugins/ubuntu/dapper.py ./dapper.py_bk patch /usr/share/pyshared/VMBuilder/plugins/ubuntu/dapper.py ./usr_share_pyshared_VMBuilder_plugins_ubuntu_dapper.py.diff
创建好的虚拟机的/etc/apt/sources.list文件需要修改,最好从宿主机复制一份。
apt-get install virtinst
比如已经有了vm_base虚拟机,用virsh shutdown关闭它,然后开始clone。
首先创建要存放的目录
mkdir -p /var/lib/libvirt/images/infobright
virt-clone -o vm_base -n infobright --file /var/lib/libvirt/images/infobright/a.img --file /var/lib/libvirt/images/infobright/b.img
apt-get install guestfish
apt-get install libguestfs-tools
update-guestfs-appliance
好,现在可以修改几个文件
virt-edit -d infobright /etc/hostname virt-edit -d infobright /etc/hosts
修改网络配置,注意,VM内部可以使用其他的子网配置,不一定和KVM宿主机相同。
virt-edit -d infobright /etc/network/interfaces
auto eth0 iface eth0 inet static address 10.150.144.23 netmask 255.255.0.0 network 10.0.0.0 gateway 10.150.0.1 # dns-* options are implemented by the resolvconf package, if installed dns-nameservers 10.150.0.253 8.8.8.8 up route add default gw 10.150.0.254 up route add -net 115.182.51.0 netmask 255.255.255.0 gw 10.150.0.1 dev eth1 up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.150.0.1 dev eth1
复制虚拟机数据文件,比如:
scp -r [email protected]:/var/lib/libvirt/images/vm_base /var/lib/libvirt/images/
scp -r [email protected]:/etc/libvirt/qemu/vm_base.xml /etc/libvirt/qemu/
现在添加虚拟机:
virsh define /etc/libvirt/qemu/vm_base.xml
现在可以用virt-edit修改网络配置文件了,不再赘述。
用virsh edit vm_name 命令修改对应的xml文件,如果想使用emacs,则需要将emacs设置为默认编辑器
在/root/.bashrc文件中添加:
export EDITOR=/usr/bin/emacs
参考我的朋友的文章:
http://blog.csdn.net/ubuntu64fan/article/details/12385707