環境介紹:
linux
redhat6.5_x64 basic_server安裝windows
iptables及selinux禁用centos
光盤做爲yum源api
一、BIOS開啓虛擬化bash
Linux中x64的環境中才能使用kvm(若是是vmware,須要在CPU那裏啓用虛擬化)網絡
二、檢測是否支持虛擬化架構
egrep --color "(vmx|svm)" /proc/cpuinfo
VMX:它是Intel處理器虛擬機技術標誌;app
SVM:它是AMD處理器虛擬機技術的標誌;dom
三、加載並查看內核模塊socket
modprobe kvm modprobe kvm_intel(AWD加載kvm_amd) lsmod|grep kvm
四、安裝kvm 和虛擬化管理軟件包
yum -y install kvm virt-* libvirts bridge-utils qemu-img service libvirtd start;chkconfig libvirtd on service acpid start; chkconfig acpid on
五、配置網絡橋接
cd /etc/sysconfig/network-scripts/ cp ifcfg-eth0 ifcfg-br0 作相應修改 #cat ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none NM_CONTROLLED=no ONBOOT=yes TYPE=Ethernet BRIDGE=br0 #cat ifcfg-br0 DEVICE=br0 BOOTPROTO=none NM_CONTROLLED=no ONBOOT=yes TYPE=Bridge IPADDR=192.168.1.2 NETMASK=255.255.255.0 GATEWAY=192.168.1.253
配置橋接網卡br0後,物理網卡eth0不須要得到ip地址,其功能被br0取代,所以在配置的時候不管是靜態ip仍是動態獲取都用br0取代eth0的ip地址,
而實際上數據發送仍是經過本機的物理網卡。
六、複製安裝光盤鏡像
mkdir /iso dd if=/dev/cdrom of=/iso/centos6.5.iso
七、建立虛擬磁盤存放目錄
mkdir /data/vmdisk -p
八、開啓VNC監聽IP
sed -i 's/^#vnc_listen.*/vnc_listen = "0.0.0.0"/' /etc/libvirt/qemu.conf /etc/init.d/libvirtd restart
九、建立磁盤鏡像文件
#virt-install --name=centos6.5-01 --ram=4096 --vcpus=2 --disk=/data/vmdisk/centos6.5-01.raw,size=40,bus=virtio,cache=writeback --network bridge=br0\ --accelerate --cdrom=/iso/centos6.5.iso --vnc --vncport=5900 -d
默認鏡像文件爲raw格式,可用qemu-img info IMG 查看
--name 指定虛擬機名字
--ram 內存,單位M
--vcpus 分配虛擬機cpu個數
--disk 指定虛擬機安裝文件路徑
size 虛擬機文件大小,單位G
bus 虛擬機網卡驅動類型
cache 虛擬機磁盤cache類型
--network bridge 指定橋接網卡
--accelerate 使用內核加速功能
--cdrom 安裝鏡像全路徑
--vnc 能夠用vnc鏈接
--vncport 指定vnc端口,-1表示隨機生成
-d debug打印調試信息到終端
VNC鏈接192.168.1.2:5900端口進行後續安裝
Other:
kvm安裝Windows與裝linux相似,可是Windows須要安裝額外的驅動文件,一個是硬盤的,一個是網卡的
分別爲virtio-win-0.1.96_amd64.vfg和virtio-win-0.1-100.iso
不一樣的版本與架構,名稱也不同,可在這兩個地址下載
硬盤驅動 https://fedoraproject.org/wiki/Windows_Virtio_Drivers#Direct_download.com
網卡驅動 https://fedorapeople.org/groups/virt/virtio-win/deprecated-isos/
一、建立磁盤鏡像
qemu-img create -f qcow2 windows7.qcow2 40G
二、建立xml文件(/etc/libvirt/qemu)
xml內容如末尾所示
三、定義虛擬機,讓kvm來管理
virsh define windows7.xml
四、啓動虛擬機
virsh start windows7
下邊是windows的xml文件,重點在於兩個virto文件的加載。固然,也能夠用命令生產xml文件,不過我更喜歡複製修改xml
通常主要修改或註釋UUID、虛擬機名字及配置、MAC地址、網橋名字、磁盤類型、鏡像或驅動或iso路徑、vnc或spice端口號
[root@controller1 ~]# cat /etc/libvirt/qemu/win7-02.xml <!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit windows7 or other application using the libvirt API. --> <domain type='kvm'> <name>windows7</name> <uuid>6ce14f21-33ac-dc79-5444-f64ccc80ea17</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>1</vcpu> <os> <type arch='x86_64' machine='rhel6.5.0'>hvm</type> <boot dev='cdrom'/> </os> <features> <acpi/> <apic/> <pae/> </features> <cpu mode='host-passthrough'> <topology sockets='1' cores='4' threads='1'/> </cpu> <clock offset='localtime'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/data/vmdisk/win7-02.qcow2'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/iso/win7.iso'/> <target dev='hda' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/iso/virtio-win-0.1-100.iso'/> <target dev='hdb' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='1'/> </disk> <disk type='file' device='floppy'> <driver name='qemu' type='raw'/> <source file='/iso/virtio-win-0.1.96_amd64.vfg'/> <target dev='fda' bus='fdc'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <controller type='usb' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> </controller> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <controller type='fdc' index='0'/> <controller type='virtio-serial' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </controller> <interface type='bridge'> <mac address='fa:c6:3e:a3:e4:a7'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='file'> <source path='/tmp/console.log'/> <target port='0'/> </serial> <serial type='pty'> <target port='1'/> </serial> <console type='file'> <source path='/tmp/console.log'/> <target type='serial' port='0'/> </console> <channel type='spicevmc'> <target type='virtio' name='com.redhat.spice.0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='spice' port='6789' autoport='no' listen='0.0.0.0' keymap='en-us'> <listen type='address' address='0.0.0.0'/> <channel name='main' mode='insecure'/> <channel name='display' mode='insecure'/> <channel name='inputs' mode='insecure'/> <channel name='cursor' mode='insecure'/> <channel name='playback' mode='insecure'/> <channel name='record' mode='insecure'/> <channel name='smartcard' mode='insecure'/> <channel name='usbredir' mode='insecure'/> <p_w_picpath compression='auto_glz'/> <streaming mode='all'/> <clipboard copypaste='yes'/> </graphics> <video> <model type='qxl' ram='65536' vram='65536' heads='1'> <acceleration accel3d='yes' accel2d='yes'/> </model> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </memballoon> </devices> </domain> [root@controller1 ~]#