系统环境
CentOS 5.5 x64
安装基本系统和开发工具
编译内核
-
mkdir /root/kvm
-
cd /root/kvm
-
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
-
tar -jxf linux-2.6.34.4.tar.bz2 -C /usr/src
-
cd /usr/src/linux-2.6.34.4
-
make menuconfig
vi .config
-
增加
-
CONFIG_SYSFS_DEPRECATED_V2=y
-
CONFIG_STP=m
-
CONFIG_BRIDGE=m
-
修改
-
CONFIG_STP=y
-
CONFIG_BRIDGE=y
-
-
make -j30 all
-
make modules_install
-
make install
安装kvm_mod
-
tar -jxf kvm-kmod-2.6.34.1.tar.bz2
-
cd kvm-kmod-2.6.34.1
-
./configure
-
make && make install
-
reboot
确认kvm_kmod安装成功
lsmod |grep kvm
-
kvm_intel 40286 0
-
kvm 266256 1 kvm_intel
安装qemu-kvm
-
cd /root/kvm
-
tar -zxf qemu-kvm-0.12.5.tar.gz
-
cd qemu-kvm-0.12.5
-
./configure --prefix=/usr/local/qemu --disable-vnc-tls --disable-vnc-sasl --disable-curl --enable-system --with-kvm-trace --static
-
make && make install
-
-
ln -s /usr/local/kvm/bin/qemu-img /usr/bin/qemu-img
-
ln -s /usr/local/kvm/bin/qemu-io /usr/bin/qemu-io
-
ln -s /usr/local/kvm/bin/qemu-nbd /usr/bin/qemu-nbd
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/kvm
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu-kvm
安装libvirt
-
cd /root/kvm
-
tar -zxf libvirt-0.8.3.tar.gz
-
cd libvirt-0.8.3
-
./configure --prefix=/usr/local/libvirt --enable-debug=no --with-storage-lvm=no
-
make && make install
-
-
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
-
yum install gnutls-devel -y
问题
-
[root@vpstest bin]# /usr/local/libvirt/sbin/libvirtd
-
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
-
-
19:49:56.982: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
-
yum install bridge-utils -y
问题
-
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
-
-
19:51:11.368: error : networkAddMasqueradingIptablesRules:637 : failed to add iptables rule to allow forwarding to 'virbr0': Invalid argument
-
19:51:11.420: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
安装virtinst
-
cd /root/kvm
-
tar -zxf virtinst-0.500.3.tar.gz
-
cd virtinst-0.500.3
-
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
-
cp -rfp /usr/local/libvirt/lib/* /usr/lib/
-
cp -rfp /usr/local/libvirt/lib64/* /usr/lib64/
设置桥接网络
vi /etc/sysconfig/network-scripts/ifcfg-eth0
-
DEVICE=eth0
-
-
HWADDR=6C:F0:49:7F:DD:6F
-
-
ONBOOT=yes
-
BRIDGE=br0
vi /etc/sysconfig/network-scripts/ifcfg-br0
-
DEVICE=br0
-
TYPE=Bridge
-
BOOTPROTO=static
-
BROADCAST=192.168.0.255
-
#HWADDR=6C:F0:49:7F:DD:6F
-
IPADDR=192.168.0.155
-
NETMASK=255.255.255.0
-
NETWORK=192.168.0.0
-
ONBOOT=yes
配置完成
安装guestOS命令行
-
./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
-
win2k8
-
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
创建新的虚拟主机
-
/usr/bin/qemu-img create -f qcow2 /home/os/vdisk.img 30G
vi newhost.xml
-
<domain type='kvm'>
-
<name>win2k89</name>
-
<uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
-
<memory>1048576</memory>
-
<currentMemory>1048576</currentMemory>
-
<vcpu>1</vcpu>
-
<os>
-
<type arch='x86_64' machine='pc-0.12'>hvm</type>
-
<boot dev='hd'/>
-
<boot dev='cdrom'/>
-
<bootmenu enable='yes'/>
-
</os>
-
<features>
-
<acpi/>
-
<apic/>
-
<pae/>
-
</features>
-
<clock offset='localtime'/>
-
<on_poweroff>destroy</on_poweroff>
-
<on_reboot>restart</on_reboot>
-
<on_crash>restart</on_crash>
-
<devices>
-
<emulator>/usr/bin/qemu-kvm</emulator>
-
<disk type='file' device='disk'>
-
<driver name='qemu' type='qcow2'/>
-
<source file='/home/os/vdisk.img'/>
-
<target dev='hda' bus='ide'/>
-
<address type='drive' controller='0' bus='0' unit='0'/>
-
</disk>
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
-
<disk type='file' device='cdrom'>
-
<source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
-
<target dev='hdz' bus='ide'/>
-
<readonly/>
-
<address type='drive' controller='0' bus='1' unit='0'/>
-
</disk>
-
<controller type='ide' index='0'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
-
</controller>
-
<interface type='bridge'>
-
<mac address='52:54:00:4a:37:ee'/>
-
<source bridge='br0'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
-
</interface>
-
<serial type='pty'>
-
<target port='0'/>
-
</serial>
-
<console type='pty'>
-
<target type='serial' port='0'/>
-
</console>
-
<input type='tablet' bus='usb'/>
-
<input type='mouse' bus='ps2'/>
-
<graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
-
<video>
-
<model type='vga' vram='9216' heads='1'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
-
</video>
-
<memballoon model='virtio'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
-
</memballoon>
-
</devices>
-
</domain>
结合arptables防止IP更改
vi /etc/sysctl.conf
-
net.ipv4.conf.eth0.rp_filter = 0
-
net.ipv4.conf.default.rp_filter = 0
-
net.ipv4.ip_forward = 0
-
net.ipv4.conf.lo.arp_ignore = 1
-
-
net.ipv4.conf.lo.arp_announce = 2
-
net.ipv4.conf.all.arp_ignore = 1
-
net.ipv4.conf.all.arp_announce = 2
vi /root/arptables.sh
-
#!/bin/sh
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
sleep 5
-
-
#/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.133 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.173 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.1 --src-mac 00:3c:50:20:18:9e -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.11 --src-mac 00:13:20:61:45:00 -j ACCEPT
-
-
-
/usr/local/sbin/arptables -A FORWARD -j DROP
限制带宽
限制vnet0的下行带宽为50kbit
-
/sbin/tc qdisc add dev vnet0 root tbf rate 50kbit latency 50ms burst 1000
增加硬盘
增加一个400G的磁盘
-
dd if=/dev/zero of=/home/os/vdisk2.img bs=1M seek=409600 count=0
vi vdisk.xml
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
服务器启动时自动启动domain
-
virsh autostart domain-name
关机
-
virsh destroy domain-name
开机
-
virsh start domain-domain
挂起(暂停)
-
virsh suspend domain-domain
恢复
-
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
安装基本系统和开发工具
编译内核
-
mkdir /root/kvm
-
cd /root/kvm
-
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
-
tar -jxf linux-2.6.34.4.tar.bz2 -C /usr/src
-
cd /usr/src/linux-2.6.34.4
-
make menuconfig
vi .config
-
增加
-
CONFIG_SYSFS_DEPRECATED_V2=y
-
CONFIG_STP=m
-
CONFIG_BRIDGE=m
-
修改
-
CONFIG_STP=y
-
CONFIG_BRIDGE=y
-
-
make -j30 all
-
make modules_install
-
make install
安装kvm_mod
-
tar -jxf kvm-kmod-2.6.34.1.tar.bz2
-
cd kvm-kmod-2.6.34.1
-
./configure
-
make && make install
-
reboot
确认kvm_kmod安装成功
lsmod |grep kvm
-
kvm_intel 40286 0
-
kvm 266256 1 kvm_intel
安装qemu-kvm
-
cd /root/kvm
-
tar -zxf qemu-kvm-0.12.5.tar.gz
-
cd qemu-kvm-0.12.5
-
./configure --prefix=/usr/local/qemu --disable-vnc-tls --disable-vnc-sasl --disable-curl --enable-system --with-kvm-trace --static
-
make && make install
-
-
ln -s /usr/local/kvm/bin/qemu-img /usr/bin/qemu-img
-
ln -s /usr/local/kvm/bin/qemu-io /usr/bin/qemu-io
-
ln -s /usr/local/kvm/bin/qemu-nbd /usr/bin/qemu-nbd
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/kvm
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu-kvm
安装libvirt
-
cd /root/kvm
-
tar -zxf libvirt-0.8.3.tar.gz
-
cd libvirt-0.8.3
-
./configure --prefix=/usr/local/libvirt --enable-debug=no --with-storage-lvm=no
-
make && make install
-
-
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
-
yum install gnutls-devel -y
问题
-
[root@vpstest bin]# /usr/local/libvirt/sbin/libvirtd
-
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
-
-
19:49:56.982: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
-
yum install bridge-utils -y
问题
-
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
-
-
19:51:11.368: error : networkAddMasqueradingIptablesRules:637 : failed to add iptables rule to allow forwarding to 'virbr0': Invalid argument
-
19:51:11.420: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
安装virtinst
-
cd /root/kvm
-
tar -zxf virtinst-0.500.3.tar.gz
-
cd virtinst-0.500.3
-
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
-
cp -rfp /usr/local/libvirt/lib/* /usr/lib/
-
cp -rfp /usr/local/libvirt/lib64/* /usr/lib64/
设置桥接网络
vi /etc/sysconfig/network-scripts/ifcfg-eth0
-
DEVICE=eth0
-
-
HWADDR=6C:F0:49:7F:DD:6F
-
-
ONBOOT=yes
-
BRIDGE=br0
vi /etc/sysconfig/network-scripts/ifcfg-br0
-
DEVICE=br0
-
TYPE=Bridge
-
BOOTPROTO=static
-
BROADCAST=192.168.0.255
-
#HWADDR=6C:F0:49:7F:DD:6F
-
IPADDR=192.168.0.155
-
NETMASK=255.255.255.0
-
NETWORK=192.168.0.0
-
ONBOOT=yes
配置完成
安装guestOS命令行
-
./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
-
win2k8
-
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
创建新的虚拟主机
-
/usr/bin/qemu-img create -f qcow2 /home/os/vdisk.img 30G
vi newhost.xml
-
<domain type='kvm'>
-
<name>win2k89</name>
-
<uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
-
<memory>1048576</memory>
-
<currentMemory>1048576</currentMemory>
-
<vcpu>1</vcpu>
-
<os>
-
<type arch='x86_64' machine='pc-0.12'>hvm</type>
-
<boot dev='hd'/>
-
<boot dev='cdrom'/>
-
<bootmenu enable='yes'/>
-
</os>
-
<features>
-
<acpi/>
-
<apic/>
-
<pae/>
-
</features>
-
<clock offset='localtime'/>
-
<on_poweroff>destroy</on_poweroff>
-
<on_reboot>restart</on_reboot>
-
<on_crash>restart</on_crash>
-
<devices>
-
<emulator>/usr/bin/qemu-kvm</emulator>
-
<disk type='file' device='disk'>
-
<driver name='qemu' type='qcow2'/>
-
<source file='/home/os/vdisk.img'/>
-
<target dev='hda' bus='ide'/>
-
<address type='drive' controller='0' bus='0' unit='0'/>
-
</disk>
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
-
<disk type='file' device='cdrom'>
-
<source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
-
<target dev='hdz' bus='ide'/>
-
<readonly/>
-
<address type='drive' controller='0' bus='1' unit='0'/>
-
</disk>
-
<controller type='ide' index='0'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
-
</controller>
-
<interface type='bridge'>
-
<mac address='52:54:00:4a:37:ee'/>
-
<source bridge='br0'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
-
</interface>
-
<serial type='pty'>
-
<target port='0'/>
-
</serial>
-
<console type='pty'>
-
<target type='serial' port='0'/>
-
</console>
-
<input type='tablet' bus='usb'/>
-
<input type='mouse' bus='ps2'/>
-
<graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
-
<video>
-
<model type='vga' vram='9216' heads='1'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
-
</video>
-
<memballoon model='virtio'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
-
</memballoon>
-
</devices>
-
</domain>
结合arptables防止IP更改
vi /etc/sysctl.conf
-
net.ipv4.conf.eth0.rp_filter = 0
-
net.ipv4.conf.default.rp_filter = 0
-
net.ipv4.ip_forward = 0
-
net.ipv4.conf.lo.arp_ignore = 1
-
-
net.ipv4.conf.lo.arp_announce = 2
-
net.ipv4.conf.all.arp_ignore = 1
-
net.ipv4.conf.all.arp_announce = 2
vi /root/arptables.sh
-
#!/bin/sh
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
sleep 5
-
-
#/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.133 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.173 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.1 --src-mac 00:3c:50:20:18:9e -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.11 --src-mac 00:13:20:61:45:00 -j ACCEPT
-
-
-
/usr/local/sbin/arptables -A FORWARD -j DROP
限制带宽
限制vnet0的下行带宽为50kbit
-
/sbin/tc qdisc add dev vnet0 root tbf rate 50kbit latency 50ms burst 1000
增加硬盘
增加一个400G的磁盘
-
dd if=/dev/zero of=/home/os/vdisk2.img bs=1M seek=409600 count=0
vi vdisk.xml
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
服务器启动时自动启动domain
-
virsh autostart domain-name
关机
-
virsh destroy domain-name
开机
-
virsh start domain-domain
挂起(暂停)
-
virsh suspend domain-domain
恢复
-
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
安装基本系统和开发工具
编译内核
-
mkdir /root/kvm
-
cd /root/kvm
-
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.4.tar.bz2
-
tar -jxf linux-2.6.34.4.tar.bz2 -C /usr/src
-
cd /usr/src/linux-2.6.34.4
-
make menuconfig
vi .config
-
增加
-
CONFIG_SYSFS_DEPRECATED_V2=y
-
CONFIG_STP=m
-
CONFIG_BRIDGE=m
-
修改
-
CONFIG_STP=y
-
CONFIG_BRIDGE=y
-
-
make -j30 all
-
make modules_install
-
make install
安装kvm_mod
-
tar -jxf kvm-kmod-2.6.34.1.tar.bz2
-
cd kvm-kmod-2.6.34.1
-
./configure
-
make && make install
-
reboot
确认kvm_kmod安装成功
lsmod |grep kvm
-
kvm_intel 40286 0
-
kvm 266256 1 kvm_intel
安装qemu-kvm
-
cd /root/kvm
-
tar -zxf qemu-kvm-0.12.5.tar.gz
-
cd qemu-kvm-0.12.5
-
./configure --prefix=/usr/local/qemu --disable-vnc-tls --disable-vnc-sasl --disable-curl --enable-system --with-kvm-trace --static
-
make && make install
-
-
ln -s /usr/local/kvm/bin/qemu-img /usr/bin/qemu-img
-
ln -s /usr/local/kvm/bin/qemu-io /usr/bin/qemu-io
-
ln -s /usr/local/kvm/bin/qemu-nbd /usr/bin/qemu-nbd
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/kvm
-
ln -s /usr/local/kvm/bin/qemu-system-x86_64 /usr/bin/qemu-kvm
安装libvirt
-
cd /root/kvm
-
tar -zxf libvirt-0.8.3.tar.gz
-
cd libvirt-0.8.3
-
./configure --prefix=/usr/local/libvirt --enable-debug=no --with-storage-lvm=no
-
make && make install
-
-
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
-
yum install gnutls-devel -y
问题
-
[root@vpstest bin]# /usr/local/libvirt/sbin/libvirtd
-
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
-
-
19:49:56.982: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
-
yum install bridge-utils -y
问题
-
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
-
-
19:51:11.368: error : networkAddMasqueradingIptablesRules:637 : failed to add iptables rule to allow forwarding to 'virbr0': Invalid argument
-
19:51:11.420: warning : qemudStartup:1832 : Unable to create cgroup for driver: No such device or address
安装virtinst
-
cd /root/kvm
-
tar -zxf virtinst-0.500.3.tar.gz
-
cd virtinst-0.500.3
-
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
-
cp -rfp /usr/local/libvirt/lib/* /usr/lib/
-
cp -rfp /usr/local/libvirt/lib64/* /usr/lib64/
设置桥接网络
vi /etc/sysconfig/network-scripts/ifcfg-eth0
-
DEVICE=eth0
-
-
HWADDR=6C:F0:49:7F:DD:6F
-
-
ONBOOT=yes
-
BRIDGE=br0
vi /etc/sysconfig/network-scripts/ifcfg-br0
-
DEVICE=br0
-
TYPE=Bridge
-
BOOTPROTO=static
-
BROADCAST=192.168.0.255
-
#HWADDR=6C:F0:49:7F:DD:6F
-
IPADDR=192.168.0.155
-
NETMASK=255.255.255.0
-
NETWORK=192.168.0.0
-
ONBOOT=yes
配置完成
安装guestOS命令行
-
./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
-
win2k8
-
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
创建新的虚拟主机
-
/usr/bin/qemu-img create -f qcow2 /home/os/vdisk.img 30G
vi newhost.xml
-
<domain type='kvm'>
-
<name>win2k89</name>
-
<uuid>116cb1cc-0505-f707-b90a-af1ab2fac4d7</uuid>
-
<memory>1048576</memory>
-
<currentMemory>1048576</currentMemory>
-
<vcpu>1</vcpu>
-
<os>
-
<type arch='x86_64' machine='pc-0.12'>hvm</type>
-
<boot dev='hd'/>
-
<boot dev='cdrom'/>
-
<bootmenu enable='yes'/>
-
</os>
-
<features>
-
<acpi/>
-
<apic/>
-
<pae/>
-
</features>
-
<clock offset='localtime'/>
-
<on_poweroff>destroy</on_poweroff>
-
<on_reboot>restart</on_reboot>
-
<on_crash>restart</on_crash>
-
<devices>
-
<emulator>/usr/bin/qemu-kvm</emulator>
-
<disk type='file' device='disk'>
-
<driver name='qemu' type='qcow2'/>
-
<source file='/home/os/vdisk.img'/>
-
<target dev='hda' bus='ide'/>
-
<address type='drive' controller='0' bus='0' unit='0'/>
-
</disk>
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
-
<disk type='file' device='cdrom'>
-
<source file='/home/iso/Windows_Svr_DC_EE_SE_Web_2008R2_64-bit_X15-59777.ISO'/>
-
<target dev='hdz' bus='ide'/>
-
<readonly/>
-
<address type='drive' controller='0' bus='1' unit='0'/>
-
</disk>
-
<controller type='ide' index='0'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
-
</controller>
-
<interface type='bridge'>
-
<mac address='52:54:00:4a:37:ee'/>
-
<source bridge='br0'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
-
</interface>
-
<serial type='pty'>
-
<target port='0'/>
-
</serial>
-
<console type='pty'>
-
<target type='serial' port='0'/>
-
</console>
-
<input type='tablet' bus='usb'/>
-
<input type='mouse' bus='ps2'/>
-
<graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0' passwd='123456' keymap='en-us'/>
-
<video>
-
<model type='vga' vram='9216' heads='1'/>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
-
</video>
-
<memballoon model='virtio'>
-
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
-
</memballoon>
-
</devices>
-
</domain>
结合arptables防止IP更改
vi /etc/sysctl.conf
-
net.ipv4.conf.eth0.rp_filter = 0
-
net.ipv4.conf.default.rp_filter = 0
-
net.ipv4.ip_forward = 0
-
net.ipv4.conf.lo.arp_ignore = 1
-
-
net.ipv4.conf.lo.arp_announce = 2
-
net.ipv4.conf.all.arp_ignore = 1
-
net.ipv4.conf.all.arp_announce = 2
vi /root/arptables.sh
-
#!/bin/sh
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
/usr/local/sbin/arptables -F
-
sleep 5
-
-
#/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.133 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.173 --src-mac 52:54:00:4a:37:ee -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.1 --src-mac 00:3c:50:20:18:9e -j ACCEPT
-
/usr/local/sbin/arptables -A FORWARD --src-ip 192.168.0.11 --src-mac 00:13:20:61:45:00 -j ACCEPT
-
-
-
/usr/local/sbin/arptables -A FORWARD -j DROP
限制带宽
限制vnet0的下行带宽为50kbit
-
/sbin/tc qdisc add dev vnet0 root tbf rate 50kbit latency 50ms burst 1000
增加硬盘
增加一个400G的磁盘
-
dd if=/dev/zero of=/home/os/vdisk2.img bs=1M seek=409600 count=0
vi vdisk.xml
-
<disk type='file' device='disk'>
-
<driver name='qemu' device='qcow2'/>
-
<source file='/home/os/vdisk5.img'/>
-
<target dev='hdb' bus='ide'/>
-
</disk>
服务器启动时自动启动domain
-
virsh autostart domain-name
关机
-
virsh destroy domain-name
开机
-
virsh start domain-domain
挂起(暂停)
-
virsh suspend domain-domain
恢复
-
virsh resume domain-domain
相关网站
http://www.linux-kvm.org
http://www.linux-kvm.com
http://www.libvirt.org
http://www.sf.net
(源码在这里下载)
(责任编辑:IT) |