当前位置: > 虚拟化 Virtualization > KVM >

KVM源码安装

时间:2020-04-20 11:23来源:未知 作者:IT

系统环境
CentOS 5.5 x64
安装基本系统和开发工具
编译内核

  1. mkdir /root/kvm
  2. cd /root/kvm
  3. wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
  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. <domain type='kvm'>
  2.   <name>win2k89</name>
  3.   <uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
  4.   <memory>1048576</memory>
  5.   <currentMemory>1048576</currentMemory>
  6.   <vcpu>1</vcpu>
  7.   <os>
  8.     <type arch='x86_64' machine='pc-0.12'>hvm</type>
  9.     <boot dev='hd'/>
  10.     <boot dev='cdrom'/>
  11.     <bootmenu enable='yes'/>
  12.   </os>
  13.   <features>
  14.     <acpi/>
  15.     <apic/>
  16.     <pae/>
  17.   </features>
  18.   <clock offset='localtime'/>
  19.   <on_poweroff>destroy</on_poweroff>
  20.   <on_reboot>restart</on_reboot>
  21.   <on_crash>restart</on_crash>
  22.   <devices>
  23.     <emulator>/usr/bin/qemu-kvm</emulator>
  24.     <disk type='file' device='disk'>
  25.       <driver name='qemu' type='qcow2'/>
  26.       <source file='/home/os/vdisk.img'/>
  27.       <target dev='hda' bus='ide'/>
  28.       <address type='drive' controller='0' bus='0' unit='0'/>
  29.     </disk>
  30.     <disk type='file' device='disk'>
  31.       <driver name='qemu' device='qcow2'/>
  32.       <source file='/home/os/vdisk5.img'/>
  33.       <target dev='hdb' bus='ide'/>
  34.     </disk>
  35.     <disk type='file' device='cdrom'>
  36.       <source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
  37.       <target dev='hdz' bus='ide'/>
  38.       <readonly/>
  39.       <address type='drive' controller='0' bus='1' unit='0'/>
  40.     </disk>
  41.     <controller type='ide' index='0'>
  42.       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  43.     </controller>
  44.     <interface type='bridge'>
  45.       <mac address='52:54:00:4a:37:ee'/>
  46.       <source bridge='br0'/>
  47.       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  48.     </interface>
  49.     <serial type='pty'>
  50.       <target port='0'/>
  51.     </serial>
  52.     <console type='pty'>
  53.       <target type='serial' port='0'/>
  54.     </console>
  55.     <input type='tablet' bus='usb'/>
  56.     <input type='mouse' bus='ps2'/>
  57.     <graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
  58.     <video>
  59.       <model type='vga' vram='9216' heads='1'/>
  60.       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
  61.     </video>
  62.     <memballoon model='virtio'>
  63.       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
  64.     </memballoon>
  65.   </devices>
  66. </domain>

结合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.     <disk type='file' device='disk'>
  2.       <driver name='qemu' device='qcow2'/>
  3.       <source file='/home/os/vdisk5.img'/>
  4.       <target dev='hdb' bus='ide'/>
  5.     </disk>

服务器启动时自动启动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

相关网站

http://www.linux-kvm.org
http://www.linux-kvm.com
http://www.libvirt.org
http://www.sf.net

(源码在这里下载)系统环境

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

  1. mkdir /root/kvm
  2. cd /root/kvm
  3. wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
  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. <domain type='kvm'>
  2.   <name>win2k89</name>
  3.   <uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
  4.   <memory>1048576</memory>
  5.   <currentMemory>1048576</currentMemory>
  6.   <vcpu>1</vcpu>
  7.   <os>
  8.     <type arch='x86_64' machine='pc-0.12'>hvm</type>
  9.     <boot dev='hd'/>
  10.     <boot dev='cdrom'/>
  11.     <bootmenu enable='yes'/>
  12.   </os>
  13.   <features>
  14.     <acpi/>
  15.     <apic/>
  16.     <pae/>
  17.   </features>
  18.   <clock offset='localtime'/>
  19.   <on_poweroff>destroy</on_poweroff>
  20.   <on_reboot>restart</on_reboot>
  21.   <on_crash>restart</on_crash>
  22.   <devices>
  23.     <emulator>/usr/bin/qemu-kvm</emulator>
  24.     <disk type='file' device='disk'>
  25.       <driver name='qemu' type='qcow2'/>
  26.       <source file='/home/os/vdisk.img'/>
  27.       <target dev='hda' bus='ide'/>
  28.       <address type='drive' controller='0' bus='0' unit='0'/>
  29.     </disk>
  30.     <disk type='file' device='disk'>
  31.       <driver name='qemu' device='qcow2'/>
  32.       <source file='/home/os/vdisk5.img'/>
  33.       <target dev='hdb' bus='ide'/>
  34.     </disk>
  35.     <disk type='file' device='cdrom'>
  36.       <source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
  37.       <target dev='hdz' bus='ide'/>
  38.       <readonly/>
  39.       <address type='drive' controller='0' bus='1' unit='0'/>
  40.     </disk>
  41.     <controller type='ide' index='0'>
  42.       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  43.     </controller>
  44.     <interface type='bridge'>
  45.       <mac address='52:54:00:4a:37:ee'/>
  46.       <source bridge='br0'/>
  47.       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  48.     </interface>
  49.     <serial type='pty'>
  50.       <target port='0'/>
  51.     </serial>
  52.     <console type='pty'>
  53.       <target type='serial' port='0'/>
  54.     </console>
  55.     <input type='tablet' bus='usb'/>
  56.     <input type='mouse' bus='ps2'/>
  57.     <graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
  58.     <video>
  59.       <model type='vga' vram='9216' heads='1'/>
  60.       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
  61.     </video>
  62.     <memballoon model='virtio'>
  63.       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
  64.     </memballoon>
  65.   </devices>
  66. </domain>

结合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.     <disk type='file' device='disk'>
  2.       <driver name='qemu' device='qcow2'/>
  3.       <source file='/home/os/vdisk5.img'/>
  4.       <target dev='hdb' bus='ide'/>
  5.     </disk>

服务器启动时自动启动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

相关网站

http://www.linux-kvm.org
http://www.linux-kvm.com
http://www.libvirt.org
http://www.sf.net

(源码在这里下载)系统环境

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

  1. mkdir /root/kvm
  2. cd /root/kvm
  3. wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
  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. <domain type='kvm'>
  2.   <name>win2k89</name>
  3.   <uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
  4.   <memory>1048576</memory>
  5.   <currentMemory>1048576</currentMemory>
  6.   <vcpu>1</vcpu>
  7.   <os>
  8.     <type arch='x86_64' machine='pc-0.12'>hvm</type>
  9.     <boot dev='hd'/>
  10.     <boot dev='cdrom'/>
  11.     <bootmenu enable='yes'/>
  12.   </os>
  13.   <features>
  14.     <acpi/>
  15.     <apic/>
  16.     <pae/>
  17.   </features>
  18.   <clock offset='localtime'/>
  19.   <on_poweroff>destroy</on_poweroff>
  20.   <on_reboot>restart</on_reboot>
  21.   <on_crash>restart</on_crash>
  22.   <devices>
  23.     <emulator>/usr/bin/qemu-kvm</emulator>
  24.     <disk type='file' device='disk'>
  25.       <driver name='qemu' type='qcow2'/>
  26.       <source file='/home/os/vdisk.img'/>
  27.       <target dev='hda' bus='ide'/>
  28.       <address type='drive' controller='0' bus='0' unit='0'/>
  29.     </disk>
  30.     <disk type='file' device='disk'>
  31.       <driver name='qemu' device='qcow2'/>
  32.       <source file='/home/os/vdisk5.img'/>
  33.       <target dev='hdb' bus='ide'/>
  34.     </disk>
  35.     <disk type='file' device='cdrom'>
  36.       <source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
  37.       <target dev='hdz' bus='ide'/>
  38.       <readonly/>
  39.       <address type='drive' controller='0' bus='1' unit='0'/>
  40.     </disk>
  41.     <controller type='ide' index='0'>
  42.       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  43.     </controller>
  44.     <interface type='bridge'>
  45.       <mac address='52:54:00:4a:37:ee'/>
  46.       <source bridge='br0'/>
  47.       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  48.     </interface>
  49.     <serial type='pty'>
  50.       <target port='0'/>
  51.     </serial>
  52.     <console type='pty'>
  53.       <target type='serial' port='0'/>
  54.     </console>
  55.     <input type='tablet' bus='usb'/>
  56.     <input type='mouse' bus='ps2'/>
  57.     <graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
  58.     <video>
  59.       <model type='vga' vram='9216' heads='1'/>
  60.       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
  61.     </video>
  62.     <memballoon model='virtio'>
  63.       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
  64.     </memballoon>
  65.   </devices>
  66. </domain>

结合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.     <disk type='file' device='disk'>
  2.       <driver name='qemu' device='qcow2'/>
  3.       <source file='/home/os/vdisk5.img'/>
  4.       <target dev='hdb' bus='ide'/>
  5.     </disk>

服务器启动时自动启动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

相关网站

http://www.linux-kvm.org
http://www.linux-kvm.com
http://www.libvirt.org
http://www.sf.net

(源码在这里下载)

(责任编辑:IT)
------分隔线----------------------------