Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1509202
  • 博文数量: 487
  • 博客积分: 161
  • 博客等级: 入伍新兵
  • 技术积分: 5064
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-01 07:37
个人简介

只有偏执狂才能生存

文章分类

全部博文(487)

文章存档

2016年(10)

2015年(111)

2014年(66)

2013年(272)

2012年(28)

分类: LINUX

2013-05-27 10:36:20

系统环境
CentOS 5.5 x64

必须安装CentOS 64位,才可以安装KVM虚拟机。32位系统的内核默认没有把KVM模块编译进去,而且源中也没有与虚拟化相关的软件包,如Qemu等。

安装基本系统和开发工具
编译内核

  1. mkdir /root/kvm
  2. cd /root/kvm
  3. wget
  4. tar -jxf linux-2.6.34.4.tar.bz2 -C /usr/src
  5. cd /usr/src/linux-2.6.34.4
  6. make menuconfig

vi .config

  1. 增加
  2. CONFIG_SYSFS_DEPRECATED_V2=y
  3.  CONFIG_STP=m
  4. CONFIG_BRIDGE=m
  5. 修改
  6. CONFIG_STP=y
  7. CONFIG_BRIDGE=y
  8.  
  9. make -j30 all
  10. make modules_install
  11. make install

安装kvm_mod

  1. tar -jxf kvm-kmod-2.6.34.1.tar.bz2
  2. cd kvm-kmod-2.6.34.1
  3. ./configure
  4. make && make install
  5. reboot

确认kvm_kmod安装成功

lsmod |grep kvm

  1. kvm_intel              40286  0
  2. kvm                   266256  1 kvm_intel

安装qemu-kvm

  1. cd /root/kvm
  2. tar -zxf qemu-kvm-0.12.5.tar.gz
  3. cd qemu-kvm-0.12.5
  4. ./configure --prefix=/usr/local/qemu  --disable-vnc-tls  --disable-vnc-sasl  --disable-curl   --enable-system    --with-kvm-trace   --static
  5. make && make install
  6.  
  7. ln -s /usr/local/kvm/bin/qemu-img /usr/bin/qemu-img
  8. ln -s /usr/local/kvm/bin/qemu-io /usr/bin/qemu-io
  9. ln -s /usr/local/kvm/bin/qemu-nbd /usr/bin/qemu-nbd
  10. ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu
  11. ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/kvm
  12. ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu-kvm

安装libvirt

  1. cd /root/kvm
  2. tar -zxf libvirt-0.8.3.tar.gz
  3. cd libvirt-0.8.3
  4. ./configure --prefix=/usr/local/libvirt --enable-debug=no --with-storage-lvm=no
  5. make && make install
  6.  
  7. ln -s /usr/local/libvirt/bin/virsh /usr/bin/virsh

常见问题

configure: error: You must install the GnuTLS library in order to compile and run libvirt
  1. yum install gnutls-devel -y

问题

  1. [root@vpstest bin]# /usr/local/libvirt/sbin/libvirtd
  2. 19:49:56.942: error : virRunWithHook:857 : internal error 'brctl setfd virbr0 0' exited with non-zero status 1 and signal 0: libvir: error : cannot execute binary brctl: No such file or directory
  3.  
  4. 19:49:56.982: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
  1. yum install  bridge-utils -y

问题

  1. 19:51:11.368: error : virRunWithHook:857 : internal error '/sbin/iptables --table filter --insert FORWARD --destination 192.168.122.0/255.255.255.0 --out-interface virbr0 --match state --state ESTABLISHED,RELATED --jump ACCEPT' exited with non-zero status 1 and signal 0: iptables: Unknown error 18446744073709551615
  2.  
  3. 19:51:11.368: error : networkAddMasqueradingIptablesRules:637 : failed to add iptables rule to allow forwarding to 'virbr0': Invalid argument
  4. 19:51:11.420: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
  1. 如果使用桥接网络独立IP可以忽略

安装virtinst

  1. cd /root/kvm
  2. tar -zxf virtinst-0.500.3.tar.gz
  3. cd virtinst-0.500.3
  4. python setup.py install

常见问题

virt-install
Traceback (most recent call last):
  File "/usr/bin/virt-install", line 34, in ?
    import libvirt
ImportError: No module named libvirt
  1. cp -rfp /usr/local/libvirt/lib/* /usr/lib/
  2. cp -rfp /usr/local/libvirt/lib64/* /usr/lib64/

设置桥接网络

vi /etc/sysconfig/network-scripts/ifcfg-eth0

  1. DEVICE=eth0
  2.  
  3. HWADDR=6C:F0:49:7F:DD:6F
  4.  
  5. ONBOOT=yes
  6. BRIDGE=br0

vi /etc/sysconfig/network-scripts/ifcfg-br0

  1. DEVICE=br0
  2. TYPE=Bridge
  3. BOOTPROTO=static
  4. BROADCAST=192.168.0.255
  5. #HWADDR=6C:F0:49:7F:DD:6F
  6. IPADDR=192.168.0.155
  7. NETMASK=255.255.255.0
  8. NETWORK=192.168.0.0
  9. ONBOOT=yes

配置完成

安装guestOS命令行

  1. ./virt-install --connect=qemu:///session -v --name winxddpd --ram 512  --file /home/os/winxpd2dd3.img --file-size 30 --network bridge=br0 --accelerate  --vnc --vncport=5915 --vnclisten=0.0.0.0 --cdrom /home/iso/zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-74070.iso
  2. win2k8
  3. virt-install --connect=qemu:///session -v --os-variant=win2k8 --name win2k8h --ram 1024 --autostart --file /home/os/win2k8h.img --file-size 30 --network bridge=br0 --accelerate  --vnc --vncport=5920 --vnclisten=0.0.0.0 --cdrom /home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO

创建新的虚拟主机

  1. /usr/bin/qemu-img create -f qcow2 /home/os/vdisk.img 30G

vi newhost.xml

  1.   win2k89
  2.   116cb1cc-0505-f707-b90a-af1ab2fac4d7
  3.   1048576
  4.   1048576
  5.   1
  6.   
  7.     hvm
  8.    
  9.    
  10.    
  11.   
  12.   
  13.    
  14.    
  15.    
  16.   
  17.   
  18.   destroy
  19.   restart
  20.   restart
  21.   
  22.     /usr/bin/qemu-kvm
  23.    
  24.       
  25.       
  26.       
  27.       
  28.    
  29.    
  30.       
  31.       
  32.       
  33.    
  34.    
  35.       
  36.       
  37.       
  38.       
  39.    
  40.    
  41.       
  42.    
  43.    
  44.       
  45.       
  46.       
  47.    
  48.    
  49.       
  50.    
  51.    
  52.       
  53.    
  54.    
  55.    
  56.    
  57.    
  58.       
  59.       
  60.    
  61.    
  62.       
  63.    
  64.   

结合arptables防止IP更改

vi /etc/sysctl.conf

  1. net.ipv4.conf.eth0.rp_filter = 0
  2. net.ipv4.conf.default.rp_filter = 0
  3. net.ipv4.ip_forward = 0
  4. net.ipv4.conf.lo.arp_ignore = 1
  5.  
  6. net.ipv4.conf.lo.arp_announce = 2
  7. net.ipv4.conf.all.arp_ignore = 1
  8. net.ipv4.conf.all.arp_announce = 2

vi /root/arptables.sh

  1. #!/bin/sh
  2. /usr/local/sbin/arptables -F
  3. /usr/local/sbin/arptables -F
  4. /usr/local/sbin/arptables -F
  5. /usr/local/sbin/arptables -F
  6. sleep 5
  7.  
  8. #/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.133 --src-mac 52:54:00:4a:37:ee -j ACCEPT
  9.  
  10. /usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.173 --src-mac 52:54:00:4a:37:ee -j ACCEPT
  11. /usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.1 --src-mac 00:3c:50:20:18:9e -j ACCEPT
  12. /usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.11 --src-mac 00:13:20:61:45:00 -j ACCEPT
  13.  
  14.  
  15. /usr/local/sbin/arptables -A FORWARD -j DROP
  1. chmod +x arptables.sh

限制带宽

限制vnet0的下行带宽为50kbit

  1. /sbin/tc qdisc add dev vnet0 root tbf rate 50kbit latency 50ms burst 1000

增加硬盘

增加一个400G的磁盘

  1. dd if=/dev/zero of=/home/os/vdisk2.img bs=1M seek=409600 count=0

vi vdisk.xml

  1.    
  2.       
  3.       
  4.       
  5.    

服务器启动时自动启动domain

  1. virsh autostart domain-name

关机

  1. virsh destroy domain-name

开机

  1. virsh start domain-domain

挂起(暂停)

  1. virsh suspend domain-domain

恢复

  1. virsh resume domain-domain

相关网站




(源码在这里下载

error: Failed to connect socket to '/usr/local/var/run/libvirt/libvirt-sock': No such file or directory

查看libvirt进程是否启动?  ps -le | grep libvirt*  如果没有启动,那么上面的错误就是这个原因



阅读(3008) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~