Centos6.5安装kvm虚拟化

By | December 18, 2018

KVM简介:
1.KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机)。
2.是x86架构且硬件支持虚拟化技术(如 intel VT 或 AMD-V)的Linux全虚拟化解决方案。
3.KVM能在不改变linux或windows镜像的情况下同时运行多个虚拟机,(它的意思是多个虚拟机使用同一镜像)并为每一个虚拟机配置个性化硬件环境(网卡、磁盘、图形适配器……)

一、设置虚拟机支持虚拟化

二、先关闭防火墙和selinux


[root@cany ~]<span class="hljs-comment"># service iptables stop    关闭防火墙</span>
[root@cany ~]<span class="hljs-comment"># setenforce 0                关闭selinux(临时生效)</span>
setenforce: SELinux <span class="hljs-keyword">is</span> disabled

永久生效方法


[root@cany ~]<span class="hljs-comment"># vim /etc/sysconfig/selinux   SELINUX=enforcing 改为disabled 并reboot生效</span>

三、检查服务器是否支持虚拟化


[root@cany ~]<span class="hljs-comment"># grep -E -o 'vmx|svm' /proc/cpuinfo</span>
vmx    如果有输出VMX就支持虚拟化

四、安装KVM所需组件,VM使用桥接网络,使用yum安装方式(配置好yum源)


[root@cany ~]<span class="hljs-comment"># yum install -y kvm virt-* libvirts bridge-utils qemu-img</span>

五、加载kvm模块,查看kvm模块是否被加载


[root@cany ~]<span class="hljs-comment"># modprobe kvm-intel</span>
[root@cany ~]<span class="hljs-comment"># lsmod | grep kvm</span>
kvm_intel             <span class="hljs-number">54285</span>  <span class="hljs-number">0</span>
kvm                    <span class="hljs-number">333172</span>  <span class="hljs-number">1</span> kvm_intel     成功加载!

六、配置网卡,具体参数如下
创建桥接网卡命令 : virsh iface-bridge eth0 br0 或者直接编辑网卡配置


[root@cany ~]<span class="hljs-comment"># virsh iface-bridge eth0 br0     出现下面的错误 重启一下libvirt服务就好了</span>
error: Failed to reconnect to the hypervisor
error: <span class="hljs-keyword">no</span> valid connection
error: Failed to <span class="hljs-keyword">connect</span> <span class="hljs-keyword">socket</span> to <span class="hljs-string">'/var/run/libvirt/libvirt-sock'</span>: No such file <span class="hljs-keyword">or</span> directory
[root@cany ~]<span class="hljs-comment"># /etc/init.d/libvirtd restart</span>
Stopping libvirtd daemon:                                  [  OK  ]
Starting libvirtd daemon:                                  [  OK  ]
[root@cany ~]<span class="hljs-comment"># virsh iface-bridge eth0 br0</span>
Created bridge br<span class="hljs-number">0</span> with attached device eth<span class="hljs-number">0</span>
Bridge interface br<span class="hljs-number">0</span> started                              成功桥接
[root@cany ~]<span class="hljs-comment"># service network restart           重启网卡</span>

此时的网卡信息


注意 :因为eth0网卡因为与br0 网卡进行桥接,所以eth0不显示ip是正常的

七、查看目前所有的网桥接口


[root@cany ~]<span class="hljs-comment"># brctl show</span>
bridge name bridge id       STP enabled interfaces
br0     <span class="hljs-number">8000.000</span>c29eb8697   <span class="hljs-literal">yes</span>     eth0
virbr0      <span class="hljs-number">8000.525400941201</span>   <span class="hljs-literal">yes</span>     virbr0-nic

八、修改VNC配置


[root@cany ~]<span class="hljs-comment">#  vim /etc/libvirt/qemu.conf</span>
    <span class="hljs-comment">#vnc_listen = "0.0.0.0"  去掉  # 号并wq保存</span>

九、重启libvirtd和messagebus服务


<span class="hljs-selector-attr">[root@cany ~]</span># <span class="hljs-selector-tag">service</span> <span class="hljs-selector-tag">libvirtd</span> <span class="hljs-selector-tag">restart</span>
<span class="hljs-selector-tag">Stopping</span> <span class="hljs-selector-tag">libvirtd</span> <span class="hljs-selector-tag">daemon</span>:                                  <span class="hljs-selector-attr">[  OK  ]</span>
<span class="hljs-selector-tag">Starting</span> <span class="hljs-selector-tag">libvirtd</span> <span class="hljs-selector-tag">daemon</span>:                                  <span class="hljs-selector-attr">[  OK  ]</span>
<span class="hljs-selector-attr">[root@cany ~]</span># <span class="hljs-selector-tag">service</span> <span class="hljs-selector-tag">messagebus</span> <span class="hljs-selector-tag">restart</span>
<span class="hljs-selector-tag">Stopping</span> <span class="hljs-selector-tag">system</span> <span class="hljs-selector-tag">message</span> <span class="hljs-selector-tag">bus</span>:                               <span class="hljs-selector-attr">[  OK  ]</span>
<span class="hljs-selector-tag">Starting</span> <span class="hljs-selector-tag">system</span> <span class="hljs-selector-tag">message</span> <span class="hljs-selector-tag">bus</span>:                               <span class="hljs-selector-attr">[  OK  ]</span>

十、创建、安装LVM虚拟机


[root@cany ~]<span class="hljs-comment"># mkdir /abc         创建镜像存放目录</span>
[root@cany ~]<span class="hljs-comment"># mkdir -p /data/kvm     创建虚拟磁盘存放目录</span>
[root@cany ~]<span class="hljs-comment"># dd if=/dev/cdrom of=/abc/Centos6.iso     把Centos6.5系统的镜像文件拷贝到abc目录下</span>
<span class="hljs-number">8726528</span>+<span class="hljs-number">0</span> records <span class="hljs-keyword">in</span>
<span class="hljs-number">8726528</span>+<span class="hljs-number">0</span> records out
<span class="hljs-number">4467982336</span> bytes (<span class="hljs-number">4.5</span> GB) copied, <span class="hljs-number">157.25</span> s, <span class="hljs-number">28.4</span> MB/s
[root@cany libvirt]<span class="hljs-comment"># cd /data/kvm/    进入虚拟磁盘目录创建img</span>
[root@cany kvm]<span class="hljs-comment"># qemu-img create -f qcow2 -o preallocation=metadata kvm_cany.img 5G</span>
Formatting <span class="hljs-string">'kvm_cany.img'</span>, fmt=qcow2 size=<span class="hljs-number">5368709120</span> encryption=<span class="hljs-literal">off</span> cluster_size=<span class="hljs-number">65536</span> preallocation=<span class="hljs-string">'metadata'</span>
[root@cany kvm]<span class="hljs-comment"># ls</span>
kvm_cany.img

安装虚拟机


[root@cany kvm]<span class="hljs-comment"># virt-install --name=kvm_cany --ram 512 --vcpus=1 -f /data/kvm/kvm_cany.img --cdrom=/abc/Centos6.iso --graphics vnc,listen=0.0.0.0,port=7788 --force --autostart</span>

Starting install...
Creating domain...                                                                            |    <span class="hljs-number">0</span> B     <span class="hljs-number">00</span>:<span class="hljs-number">00</span>    
Cannot open display:
Run <span class="hljs-string">'virt-viewer --help'</span> to see a full list <span class="hljs-keyword">of</span> available command line options
Domain installation still <span class="hljs-keyword">in</span> progress. You can reconnect to
the <span class="hljs-built_in">console</span> to complete the installation process.

如果遇到安装失败  请对虚拟磁盘存放目录权限  chmod -R <span class="hljs-number">777</span> /data/kvm

VNC客户端连接KVM虚拟机
软件:VNC Viewer

VNC连上了之后,安装系统就ok了!安装结束后,vnc会自动挂断,因为此时虚拟机并未启动!!!

使用 virsh控制台为 KVM 系统管理若干任务 下面就让我们来手动开启刚刚安装的KVM虚拟机


[root@cany kvm]<span class="hljs-comment"># virsh</span>
Welcome to virsh, the virtualization interactive terminal.

Type:  <span class="hljs-string">'help'</span> <span class="hljs-keyword">for</span> help with commands
       <span class="hljs-string">'quit'</span> to quit

virsh <span class="hljs-comment"># list --all</span>
 Id    Name                           State
----------------------------------------------------
 -     kvm_cany                       shut <span class="hljs-literal">off</span>

virsh <span class="hljs-comment"># start kvm_cany</span>
Domain kvm_cany started

重新使用VNC客户端进行连接 并对其网卡进行配置

注意:出现网卡重启失败的话 将eth0网卡配置文件中的 HWADDR修改成MACADDR


<span class="hljs-attribute">rm</span> -rf /etc/udev/rules.d/<span class="hljs-number">70</span>-persistent-net.rules

在虚拟机上查看KVM虚拟机运行状态

Leave a Reply

Your email address will not be published.