一,实验环境 OS:CENTOS6.5 X86_64 二,KVM宿主环境配置 1.cat /proc/cpuinfo | egrep 'vmx|svm' //查看是否支持虚拟技术 2.安装KVM相关 yum -y groupinstall "Virtualization" "Virtualization Client" "Virtualization Platform" "Virtualization Tools" //可以先用yum grouplist查看下,为了避免缺少组件,索性一次全装了 三,网络配置 1.安装网桥工具 yum install bridge-utils -y 2.ifconfig virbr0 //确认有类似下面的输出 virbr0 Link encap:Ethernet HWaddr 52:54:00:A0:83:9A inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) 3.设置bridge //bridge模式有个好处,无论是想从vm访问外网还是从外网访问vm都可以,所以建议设置为这种模式 1)修改/etc/sysconfig/network-scripts/ifcfg-eth0,并且在同目录下新建ifcfg-br0,两文件内容如下: [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BRIDGE=br0 [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 TYPE=Bridge ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=Static //如果想使用DHCP方式,把这里换成dhcp,下面的都删掉就行了 IPADDR=192.168.1.80 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 2)sysctl -p //查看如下三项是否都为0,我这里默认就是0,官网说这是为了disabled netfilter net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 3)/etc/init.d/network restart后,确认bridge是否设置成功 [root@localhost ~]# ifconfig br0 Link encap:Ethernet HWaddr 00:0C:29:69:07:FE inet addr:192.168.1.80 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe69:7fe/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:788 errors:0 dropped:0 overruns:0 frame:0 TX packets:369 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:78433 (76.5 KiB) TX bytes:58258 (56.8 KiB) eth0 Link encap:Ethernet HWaddr 00:0C:29:69:07:FE inet6 addr: fe80::20c:29ff:fe69:7fe/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:37360 errors:0 dropped:0 overruns:0 frame:0 TX packets:8899 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:38608283 (36.8 MiB) TX bytes:796592 (777.9 KiB) ....... 或者使用brctl show [root@localhost ~]# brctl show bridge namebridge idSTP enabledinterfaces br08000.000c296907fenoeth0 virbr08000.525400a0839ayesvirbr0-nic 四,安装虚拟机 1)新建两个目录分别存放ISO文件和虚拟磁盘,我这里在/opt下新建了iso和kvmimg目录;有些朋友不知道怎么将PC上的ISO文件传送到服务器上,我这里推荐大家用xshell和xftp,简单好用且免费! 2)利用virt-install开始安装系统,更多参数可以参考--help 安装linux: [root@localhost iso]# virt-install --name www --boot network,cdrom,menu=on --ram 2048 --vcpus=2 --os-variant=rhel6 --accelerate --cdrom=/opt/iso/CentOS-6.4-x86_64-bin-DVD1.iso --disk path=/opt/kvmimg/vm01.img,size=5,bus=virtio --bridge=br0,model=virtio --autostart --vnc --vncport=5900 --vnclisten=0.0.0.0 安装windwos virt-install --name windows-2008 --boot network,cdrom,menu=on --ram 2048 --vcpus=2 --os-variant=win7 --accelerate --cdrom=/opt/iso/cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617598.iso --disk path=/opt/kvmimg/2008server.img,size=50,bus=ide --bridge=br0,model=virtio --autostart --vnc --vncport=5900 --vnclisten=0.0.0.0 Starting install... Creating storage file vm01.img | 5.0 GB 00:00 Creating domain... | 0 B 00:00 Cannot open display: Run 'virt-viewer --help' to see a full list of available command line options Domain installation still in progress. You can reconnect to the console to complete the installation process. 这个时候就是已经OK了,用vnc软件(vnc-viewer/tigervnc)填好IP地址和端口,这里是5900连接,就能看到装系统的界面了,装机过程就省略了。。。 3)安装完毕后的补充 virsh list --all //可以发现vm01状态时shut off virsh start vm01 //手动启动vm01 /etc/init.d/network restart //用vnc登录到vm01,重启网络获取IP地址,至此vm01就可以正常使用了 五,克隆虚拟机 我们有时候需要多台虚拟机以满足需求,这时候只要利用vm01克隆出来就行了,过程简单,方便,快速,但是需要做一些改动 1)virsh suspend vm01 //克隆的时候状态必须是非running,之后把suspend换成resume恢复即可 2)virt-clone -o vm01 -n vm02 -f /opt/kvmimg/vm02.img //克隆vm02 3)virsh edit vm02 //修改vm02.xml文件,因为vm01的vnc已经把5900端口占用所以需要换一下,我这里修改成6900 <graphics type='vnc' port='6900' autoport='no' listen='0.0.0.0'> 4)virsh start vm02 //启动vm02 5)利用vnc登录到vm02之后,有些网络设置需要修改,否则网卡无法启动 cat /etc/sysconfig/network-scripts/ifcfg-eth0 //删除mac地址和uuid那一行 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp cat /etc/udev/rules.d/70-persistent-net.rules //把之前的"SUBSYSTEM=="这一行注释掉,把新的那行的eth1换成eth0 # This file was automatically generated by the /lib/udev/write_net_rules # program, run by the persistent-net-generator.rules rules file. # # You can modify it, as long as you keep each rule on a single # line, and change only the value of the NAME= key. # PCI device 0x1af4:0x1000 (virtio-pci) #SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:72:69:a1", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" # PCI device 0x1af4:0x1000 (virtio-pci) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:f8:e5:86", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" 6)重启vm02,重启网络 //至此,大功告成! 六,常用virsh指令 1)virsh list 列出当前虚拟机列表,不包括未启动的 2)virsh list --all 列出所有虚拟机,包括所有已经定义的虚拟机 3)virsh destroy vm-name 关闭虚拟机 4)virsh start vm-name 启动虚拟机 5)virsh edit vm-name 编辑虚拟机xml文件 6)virsh undefine vm-name 删除虚拟机 7)virsh shutdown vm-name 停止虚拟机 8)virsh reboot vm-name 重启虚拟机 9)virsh autostart vm-name 虚拟机随宿主机启动 转载于:https://www.cnblogs.com/tchroot/p/7247945.html (责任编辑:IT) |