在Ubuntu下编译安装kvm

1、下载包
我的内核是2.6.32,需要下载对应的软件包
http://sourceforge.net/projects/kvm/files/qemu-kvm/0.12.5/qemu-kvm-0.12.5.tar.gz/download
内核默认有的kvm,CPU占用过大,我们自己编译最新版的

其他的库 zlib sdl alsa gnutls等等
[shell]
sudo apt-get install gcc libsdl1.2-dev zlib1g-dev libasound2-dev linux-kernel-headers pkg-config libgnutls-dev libpci-dev pciutils-dev
[/shell]

2、编译qemu-kvm
[shell]
tar xzf qemu-kvm-0.12.5.tar.gz
cd qemu-kvm-0.12.5
./configure –prefix=/usr/local/kvm
make
sudo make install
sudo /sbin/modprobe kvm-intel
[/shell]

3、创建镜像
[shell]
/usr/local/kvm/bin/qemu-img create -f qcow2 centos.img 10G
[/shell]

4、启动虚拟机(安装操作系统)
[shell]
#安装虚拟机
sudo /usr/local/kvm/bin/qemu-system-x86_64 -m 256 -boot d -drive file=/home/liheyuan/虚拟机/kvm/centos.img,cache=writeback -localtime -cdrom /media/4C088BD8088BBF86/虚拟镜像/OS/CentOS-5.4-i386-bin-DVD.iso
[/shell]

[shell]
#平时启动
sudo /usr/local/kvm/bin/qemu-system-x86_64 -m 256 -drive file=/home/liheyuan/虚拟机/kvm/centos.img,cache=writeback -localtime
[/shell]

5、网络桥接
请参考《KVM虚拟化的多个网卡桥接配置》
[shell]
#安装/usr/sbin/brctl和/usr/sbin/tunctl
sudo apt-get install bridge-utils uml-utilities

#这里我们只设置public bridge,即可以和真实网络通信的,否则没有意义
vim /etc/network/interfaces
#酌情添加如下内容
auto eth0
iface eth0 inet dhcp

auto tap0
iface tap0 inet manual
up ifconfig $IFACE 0.0.0.0 up
down ifconfig $IFACE down
tunctl_user liheyuan

auto br0
iface br0 inet dhcp
bridge_ports eth0 tap0

#重启网络
/etc/init.d/networking restart
#正常情况下br0将分配到原来eth0应该得到的IP,eth0没有得到IP

#之后的启动
sudo /usr/local/kvm/bin/qemu-system-x86_64 -m 256 -drive file=/home/liheyuan/虚拟机/kvm/centos.img,cache=writeback -localtime -net nic,macaddr=00-11-22-33-44-55 -net tap,ifname=tap0,script=no
[/shell]
6、创建网络console
[shell]
#启动虚拟机
sudo /usr/local/kvm/bin/qemu-system-x86_64 -m 256 -drive file=/home/liheyuan/虚拟机/kvm/centos.img,cache=writeback -localtime -net nic,macaddr=00-11-22-33-44-55 -net tap,ifname=tap0,script=no

#设置menu.lst
vim /boot/grub/menu.lst
#头部添加如下两行
serial –unit=0 –speed=9600 –word=8 –parity =no –stop=1
terminal –timeout=1 –dumb serial console
#并在kernel的一行加入
console=tty0 console=ttyS0,9600n8

#设置
vim /etc/inittab
#添加或者修改这一行:
T0:123:respawn:/sbin/getty -L ttyS0 9600 vt100

#保存,退出,一般就OK了。再次重启时候
sudo /usr/local/kvm/bin/qemu-system-x86_64 -m 256 -drive file=/home/liheyuan/虚拟机/kvm/centos.img,cache=writeback -localtime -nographic
[/shell]

7、进一步提升性能
virtio (提升I/O性能)
参考:
http://www.heboliang.cn/archive/kvm-windows.html
http://www.linux-kvm.org/page/Virtio

Leave a Reply

Your email address will not be published.