KVM简介:
1.KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机)。
2.是x86架构且硬件支持虚拟化技术(如 intel VT 或 AMD-V)的Linux全虚拟化解决方案。
3.KVM能在不改变linux或windows镜像的情况下同时运行多个虚拟机,(它的意思是多个虚拟机使用同一镜像)并为每一个虚拟机配置个性化硬件环境(网卡、磁盘、图形适配器……)
一、开启设备支持虚拟化
BIOS 高级设置里面 选择CPU 选择Intel Virtual Technology并回车。注意:若无VT选项或不可更改,则表示你的电脑不支持VT技术。
二、先关闭防火墙和selinux
1 2 3 |
[root@localhost ~]# service iptables stop 关闭防火墙 [root@localhost ~]# setenforce 0 关闭selinux(临时生效) setenforce: SELinux is disabled |
永久生效方法
1 |
[root@localhost ~]# vim /etc/sysconfig/selinux SELINUX=enforcing 改为disabled 并reboot生效 |
三、检查服务器是否支持虚拟化
1 2 |
[root@localhost ~]# grep -E -o 'vmx|svm' /proc/cpuinfo vmx 如果有输出VMX就支持虚拟化 |
四、安装KVM所需组件,使用yum安装方式(配置好yum源)
1 2 3 4 5 6 7 |
[root@localhost ~]# yum install -y kvm virt-* libvirts bridge-utils qemu-img [root@localhost ~]# /etc/rc.d/init.d/libvirtd start Starting libvirtd daemon: [ OK ] [root@localhost ~]# /etc/rc.d/init.d/messagebus start Starting system message bus: [ OK ] [root@localhost ~]# chkconfig libvirtd on [root@localhost ~]# chkconfig messagebus on |
五、加载kvm模块,查看kvm模块是否被加载
1 2 3 4 |
[root@localhost ~]# modprobe kvm-intel [root@localhost ~]# lsmod | grep kvm kvm_intel 54285 0 kvm 333172 1 kvm_intel 成功加载! |
六、配置网卡,具体参数如下
创建桥接网卡命令 : virsh iface-bridge eth0 br0
1 2 3 4 5 6 7 8 9 10 11 |
[root@localhost ~]# virsh iface-bridge eth0 br0 出现下面的错误 重启一下libvirt服务就好了 error: Failed to reconnect to the hypervisor error: no valid connection error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory [root@localhost ~]# /etc/init.d/libvirtd restart Stopping libvirtd daemon: [ OK ] Starting libvirtd daemon: [ OK ] [root@localhost ~]# virsh iface-bridge eth0 br0 Created bridge br0 with attached device eth0 Bridge interface br0 started 成功桥接 [root@localhost ~]# service network restart 重启网卡 |
此时的网卡信息
注意 :因为eth0网卡因为与br0 网卡进行桥接,所以eth0不显示ip是正常的
七、查看目前所有的网桥接口
1 2 3 4 |
[root@localhost ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29eb8697 yes eth0 virbr0 8000.525400941201 yes virbr0-nic |
八、修改VNC配置
1 2 |
[root@localhost ~]# vim /etc/libvirt/qemu.conf #vnc_listen = "0.0.0.0" 去掉 # 号并wq保存 |
九、重启libvirtd和messagebus服务
1 2 3 4 5 6 |
[root@localhost ~]# service libvirtd restart Stopping libvirtd daemon: [ OK ] Starting libvirtd daemon: [ OK ] [root@localhost ~]# service messagebus restart Stopping system message bus: [ OK ] Starting system message bus: [ OK ] |
十、创建、安装LVM虚拟机
1 2 3 4 5 6 7 8 9 10 11 |
[[root@localhost ~]# mkdir /home/image 创建镜像存放目录 [root@localhost ~]# mkdir -p /home/kvm 创建虚拟磁盘存放目录 [root@localhost ~]# dd if=/root/CentOS-6.10-x86_64-bin-DVD1.iso of=/home/image/Centos6.iso 把Centos6.5系统的镜像文件拷贝到abc目录下 8726528+0 records in 8726528+0 records out 4467982336 bytes (4.5 GB) copied, 157.25 s, 28.4 MB/s [root@localhost ~]# cd /home/kvm 进入虚拟磁盘目录创建img [root@localhost ~]# qemu-img create -f qcow2 -o preallocation=metadata kvm1.img 5G Formatting 'kvm_cany.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='metadata' [root@localhost ~]# ls kvm1.img |
安装虚拟机
1 |
[root@localhost ~]# virt-install --name=kvm1 --ram 1024 --vcpus=1 --check-cpu --os-type linux --network network=default -f /home/kvm/kvm1.img --location /home/image/Centos6.iso --extra-args 'console=ttyS0,115200n8 serial' --autostart |
相关参数解释
1 2 3 4 5 6 7 8 9 10 11 |
--name 虚拟机名称 --ram 内存大小MB --vcpus cpu核数 --check-cpu 检查指定的虚拟CPU数不要超过无论CPU,如果超过将返回警告信息。 --os-type linux 指定系统类型 --network network=default 连接到nat default网络,桥接:--network bridge=BRIDGE (连接到BRIDGE的桥接网卡上) -f /home/kvm/kvm1.img 指定虚拟机文件地址 --graphics none --location /home/image/Centos6.iso 指定安装源镜像路径 --extra-args 'console=ttyS0,115200n8 serial' 执行从"--location"选项指定位置的客户机安装时,附加内核命令行参数到安装程序。(设置console连接所需参数) --autostart 指定虚拟机在物理机启动后自动启动; |
图形安装教程
- skip
- 选择安装语言
- Re-initialize all 初始化磁盘
- 选择时区 默认就行
- 输入root密码
- 系统安装位置(刚才创建的5G磁盘)
- 确认初始化磁盘
- 然后系统开始安装
- 等待系统安装包安装完成,重启系统。
虚拟机的连接
查看所有的虚拟机(关闭和运行的虚拟机)
1 |
virsh list --all |
连接虚拟机
1 |
virsh console +域名(虚拟机的名称) |