kvm 虚拟化 搭建

首先更新下yum 源:yum update

查看 系统cpu是否支持虚拟化

cat /proc/cpuinfo |grep vmx

[redhat@opman network-scripts]$ cat /proc/cpuinfo |grep vmx

flags          : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat xsaveopt invpcid_single spec_ctrl ibpb_support pti tpr_shadow vnmi ept vpid fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx

flags          : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat xsaveopt invpcid_single spec_ctrl ibpb_support pti tpr_shadow vnmi ept vpid fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx

查看是否安装KVM模块

[redhat@opman network-scripts]$

[redhat@opman network-scripts]$ lsmod  |grep kvm

[redhat@opman network-scripts]$

发现没有,所以我们开始安装kvm模块

[root@opman ~]# yum -y install qemu-kvm



[root@opman ~]# lsmod  |grep kvm

kvm_intel              56296  0

kvm                   348558  1 kvm_intel

保证一下的东西全部装上

yum – y install qemu-kvm    KVM模块

yum –y install qemu-kvm-tools    KVM调试工具

yum – y install qemu-img     用于创建磁盘,启动虚拟机等

yum –y install bridge-utils    

yum –y install libvirt          虚拟机管理工具

yum –y install virt-manager     图形化管理虚拟机

设置KVM 网桥

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
BRIDGE="br0"
NAME=eth0
TYPE=Ethernet
NM_CONTROLLED=no #备注这条一定要加,否则在启动的过程中会报错
vim /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
TYPE=Bridge
ONBOOT=yes
IPADDR=192.168.153.150
NETMASK=255.255.255.0
BOOTPROTO=static
NM_CONTROLLED=no

现象如下表示正确配置:

未分类

使用图形化管理–virt-manager;注意启动Virt-Manager时报错:unsupported format character 'Uffffffff(0xffffffef) at index 30 ,很可能是 virt-manager rpm包版本的问题;卸载原有的的包

卸载virt-manager-0.9.0-34

yum remove virt-manager

安装virt-manager-0.9.0-31

安装采用本地安装,先下载virt-manager-0.9.0-31的安装包,再使用如下命令安装。

yum localinstall virt-manager-0.9.0-31.el6.x86_64.rpm

virt-manager-0.9.0-31的包,我分享在这.

链接:https://pan.baidu.com/s/1fPU7VqF_SR3WfNzA_zuRiA

提取码:7lmw

未分类

分配两个磁盘空间,分别存放虚拟机的文件和镜像文件

未分类

右击QEMU,选择属性

未分类

新建存储池用于虚拟机的数据文件

未分类

按照我们设置的,选择存储路径(即之前预留的磁盘空间)

未分类

再创建一个存放镜像的存储池, 方法类似

未分类

未分类

未分类

上传镜像到kvm_iso(自己定义的),不管用什么方法,把镜像复制到我们设定的镜像存储池中;

这里我利用rz 来上传 (没有安装的话,需要安装lrzsz)

未分类

上传后,刷新,发现镜像成功上传了。进行下一步

未分类

在QEMU里选择新建虚拟机

未分类

选择合适的镜像文件

未分类

选择合适的存储路径(按照我们的规划)

未分类

新建卷

未分类

空间多给点 给个10G

未分类

未分类

最后给虚拟机配置一些参数

未分类

开始安装,安装的过程就不赘述了

未分类

最后测试能不能和网关通(192.168.153.2),我这边也可以和外网通

未分类

以上就是整个安装过程,如果你觉得有用,请分享给你的盆友并注明出处,谢谢!

在 Ubuntu 18.04 LTS 上使用 KVM 配置无头虚拟化服务器

我们已经讲解了 在 Ubuntu 18.04 无头服务器上配置 Oracle VirtualBox (https://www.ostechnix.com/install-oracle-virtualbox-ubuntu-16-04-headless-server/)。在本教程中,我们将讨论如何使用 KVM 去配置无头虚拟化服务器,以及如何从一个远程客户端去管理访客系统。正如你所知道的,KVM(Kernel-based virtual machine)是开源的,是 Linux 上的全虚拟化。使用 KVM,我们可以在几分钟之内,很轻松地将任意 Linux 服务器转换到一个完全的虚拟化环境中,以及部署不同种类的虚拟机,比如 GNU/Linux、*BSD、Windows 等等。

使用 KVM 配置无头虚拟化服务器

我在 Ubuntu 18.04 LTS 服务器上测试了本指南,但是它在其它的 Linux 发行版上也可以使用,比如,Debian、CentOS、RHEL 以及 Scientific Linux。这个方法完全适合哪些希望在没有任何图形环境的 Linux 服务器上,去配置一个简单的虚拟化环境。

基于本指南的目的,我将使用两个系统。

KVM 虚拟化服务器:

  • 宿主机操作系统 – 最小化安装的 Ubuntu 18.04 LTS(没有 GUI)
  • 宿主机操作系统的 IP 地址:192.168.225.22/24
  • 访客操作系统(它将运行在 Ubuntu 18.04 的宿主机上):Ubuntu 16.04 LTS server

远程桌面客户端:

  • 操作系统 – Arch Linux

安装 KVM

首先,我们先检查一下我们的系统是否支持硬件虚拟化。为此,需要在终端中运行如下的命令:

$ egrep -c '(vmx|svm)' /proc/cpuinfo

假如结果是 zero (0),说明系统不支持硬件虚拟化,或者在 BIOS 中禁用了虚拟化。进入你的系统 BIOS 并检查虚拟化选项,然后启用它。

假如结果是 1 或者 更大的数,说明系统将支持硬件虚拟化。然而,在你运行上面的命令之前,你需要始终保持 BIOS 中的虚拟化选项是启用的。

或者,你也可以使用如下的命令去验证它。但是为了使用这个命令你需要先安装 KVM。

$ kvm-ok

示例输出:

INFO: /dev/kvm exists
KVM acceleration can be used

如果输出的是如下这样的错误,你仍然可以在 KVM 中运行访客虚拟机,但是它的性能将非常差。

INFO: Your CPU does not support KVM extensions
INFO: For more detailed results, you should run this as root
HINT: sudo /usr/sbin/kvm-ok

当然,还有其它的方法来检查你的 CPU 是否支持虚拟化。更多信息参考接下来的指南。

  • 如何知道 CPU 是否支持虚拟技术(VT)https://www.ostechnix.com/how-to-find-if-a-cpu-supports-virtualization-technology-vt/

接下来,安装 KVM 和在 Linux 中配置虚拟化环境所需要的其它包。

在 Ubuntu 和其它基于 DEB 的系统上,运行如下命令:

$ sudo apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker

KVM 安装完成后,启动 libvertd 服务(如果它没有启动的话):

$ sudo systemctl enable libvirtd
$ sudo systemctl start libvirtd

创建虚拟机

所有的虚拟机文件和其它的相关文件都保存在 /var/lib/libvirt/ 下。ISO 镜像的默认路径是 /var/lib/libvirt/boot/。

首先,我们先检查一下是否有虚拟机。查看可用的虚拟机列表,运行如下的命令:

$ sudo virsh list --all

示例输出:

Id Name State
----------------------------------------------------

未分类

正如上面的截屏,现在没有可用的虚拟机。

现在,我们来创建一个。

例如,我们来创建一个有 512 MB 内存、1 个 CPU 核心、8 GB 硬盘的 Ubuntu 16.04 虚拟机。

$ sudo virt-install --name Ubuntu-16.04 --ram=512 --vcpus=1 --cpu host --hvm --disk path=/var/lib/libvirt/images/ubuntu-16.04-vm1,size=8 --cdrom /var/lib/libvirt/boot/ubuntu-16.04-server-amd64.iso --graphics vnc

请确保在路径 /var/lib/libvirt/boot/ 中有一个 Ubuntu 16.04 的 ISO 镜像文件,或者在上面命令中给定的其它路径中有相应的镜像文件。

示例输出:

WARNING Graphics requested but DISPLAY is not set. Not running virt-viewer.
WARNING No console to launch for the guest, defaulting to --wait -1

Starting install...
Creating domain... | 0 B 00:00:01
Domain installation still in progress. Waiting for installation to complete.
Domain has shutdown. Continuing.
Domain creation completed.
Restarting guest.

未分类

我们来分别讲解以上的命令和看到的每个选项的作用。

  • –name:这个选项定义虚拟机名字。在我们的案例中,这个虚拟机的名字是 Ubuntu-16.04。
  • –ram=512:给虚拟机分配 512MB 内存。
  • –vcpus=1:指明虚拟机中 CPU 核心的数量。
  • –cpu host:通过暴露宿主机 CPU 的配置给访客系统来优化 CPU 属性。
  • –hvm:要求完整的硬件虚拟化。
  • –disk path:虚拟机硬盘的位置和大小。在我们的示例中,我分配了 8GB 的硬盘。
  • –cdrom:安装 ISO 镜像的位置。请注意你必须在这个位置真的有一个 ISO 镜像。
  • –graphics vnc:允许 VNC 从远程客户端访问虚拟机。

使用 VNC 客户端访问虚拟机

现在,我们在远程桌面系统上使用 SSH 登入到 Ubuntu 服务器上(虚拟化服务器),如下所示。

$ ssh sk@192.168.225.22

在这里,sk 是我的 Ubuntu 服务器的用户名,而 192.168.225.22 是它的 IP 地址。

运行如下的命令找出 VNC 的端口号。我们从一个远程系统上访问虚拟机需要它。

$ sudo virsh dumpxml Ubuntu-16.04 | grep vnc

示例输出:

<graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>

未分类

记下那个端口号 5900。安装任意的 VNC 客户端应用程序。在本指南中,我们将使用 TigerVnc。TigerVNC 是 Arch Linux 默认仓库中可用的客户端。在 Arch 上安装它,运行如下命令:

$ sudo pacman -S tigervnc

在安装有 VNC 客户端的远程客户端系统上输入如下的 SSH 端口转发命令。

$ ssh sk@192.168.225.22 -L 5900:127.0.0.1:5900

再强调一次,192.168.225.22 是我的 Ubuntu 服务器(虚拟化服务器)的 IP 地址。

然后,从你的 Arch Linux(客户端)打开 VNC 客户端。

在 VNC 服务器框中输入 localhost:5900,然后点击 “Connect” 按钮。

未分类

然后就像你在物理机上安装系统一样的方法开始安装 Ubuntu 虚拟机。

未分类

未分类

同样的,你可以根据你的服务器的硬件情况配置多个虚拟机。

或者,你可以使用 virt-viewer 实用程序在访客机器中安装操作系统。virt-viewer 在大多数 Linux 发行版的默认仓库中都可以找到。安装完 virt-viewer 之后,运行下列的命令去建立到虚拟机的访问连接。

$ sudo virt-viewer --connect=qemu+ssh://192.168.225.22/system --name Ubuntu-16.04

管理虚拟机

使用管理用户接口 virsh 从命令行去管理虚拟机是非常有趣的。命令非常容易记。我们来看一些例子。

查看运行的虚拟机,运行如下命令:

$ sudo virsh list

或者,

$ sudo virsh list --all

示例输出:

Id Name State
----------------------------------------------------
 2 Ubuntu-16.04 running

未分类

启动一个虚拟机,运行如下命令:

$ sudo virsh start Ubuntu-16.04

或者,也可以使用虚拟机 id 去启动它。

未分类

正如在上面的截图所看到的,Ubuntu 16.04 虚拟机的 Id 是 2。因此,启动它时,你也可以像下面一样只指定它的 ID。

$ sudo virsh start 2

重启动一个虚拟机,运行如下命令:

$ sudo virsh reboot Ubuntu-16.04

示例输出:

Domain Ubuntu-16.04 is being rebooted

未分类

暂停一个运行中的虚拟机,运行如下命令:

$ sudo virsh suspend Ubuntu-16.04

示例输出:

Domain Ubuntu-16.04 suspended

让一个暂停的虚拟机重新运行,运行如下命令:

$ sudo virsh resume Ubuntu-16.04

示例输出:

Domain Ubuntu-16.04 resumed

关闭一个虚拟机,运行如下命令:

$ sudo virsh shutdown Ubuntu-16.04

示例输出:

Domain Ubuntu-16.04 is being shutdown

完全移除一个虚拟机,运行如下的命令:

$ sudo virsh undefine Ubuntu-16.04
$ sudo virsh destroy Ubuntu-16.04

示例输出:

Domain Ubuntu-16.04 destroyed

未分类

关于它的更多选项,建议你去查看 man 手册页:

$ man virsh

今天就到这里吧。开始在你的新的虚拟化环境中玩吧。对于研究和开发者、以及测试目的,KVM 虚拟化将是很好的选择,但它能做的远不止这些。如果你有充足的硬件资源,你可以将它用于大型的生产环境中。如果你还有其它好玩的发现,不要忘记在下面的评论区留下你的高见。

VMware ESXI 迁移至KVM

1. ESXI将虚拟机导出

未分类

导出ova模板

未分类

将导出的ova模板导入到KVM环境中。

2. 配置KVM环境

安装所需要的组件

[root@clsn7 ~]# yum install libvirt* virt-* qemu-kvm* -y

配置桥接网卡

[root@clsn7 ~]# virsh iface-bridge eth1 br1 
使用附加设备 br1 生成桥接 eth1 失败
已启动桥接接口 br1

3. 导入虚拟机

启动kvm管理程序,并设置开机自启动

[root@clsn7 ~]# systemctl start libvirtd.service
[root@clsn7 ~]# systemctl enable libvirtd.service

创建虚拟机存放目录

[root@clsn7 ~]# virt-v2v -i ova centos-dev-test01-v2v.ova -o local -os /vmhost/dev/dev-test01  -of qcow2 
[   0.0] Opening the source -i ova centos-dev-test01-v2v.ova
virt-v2v: warning: making OVA directory public readable to work around 
libvirt bug https://bugzilla.redhat.com/1045069
[  23.1] Creating an overlay to protect the source from being modified
[  23.4] Initializing the target -o local -os /vmhost/dev/dev-test01
[  23.4] Opening the overlay
[  41.4] Inspecting the overlay
[  57.5] Checking for sufficient free disk space in the guest
[  57.5] Estimating space required on target for each disk
[  57.5] Converting CentOS release 6.9 (Final) to run on KVM
virt-v2v: This guest has virtio drivers installed.
[ 178.6] Mapping filesystem data to avoid copying unused and blank areas
[ 178.9] Closing the overlay
[ 179.4] Checking if the guest needs BIOS or UEFI to boot
[ 179.4] Assigning disks to buses
[ 179.4] Copying disk 1/1 to /vmhost/dev/dev-test01/centos-dev-test01-v2v-sda (qcow2)
    (100.00/100%)
[ 216.1] Creating output metadata
[ 216.1] Finishing off
导入完成后在 /vmhost/dev/dev-test01 目录下会生成文件
[root@clsn7 dev-test01]# pwd
/vmhost/dev/dev-test01
[root@clsn7 dev-test01]# ls
centos-dev-test01-v2v-sda  centos-dev-test01-v2v.xml

修改网卡配置

修改网卡source network 为桥接

修改网卡使用virtio

[root@clsn7 dev-test01]# cat centos-dev-test01-v2v.xml 
<?xml version='1.0' encoding='utf-8'?>
<domain type='kvm'>
  <!-- generated by virt-v2v 1.36.3rhel=7,release=6.el7_4.3,libvirt -->
  <name>centos-dev-test01-v2v</name>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu>1</vcpu>
  <features>
    <acpi/>
    <apic/>
  </features>
  <os>
    <type arch='x86_64'>hvm</type>
  </os>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/vmhost/dev/dev-test01/centos-dev-test01-v2v-sda'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <disk device='cdrom' type='file'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
    </disk>
    <disk device='floppy' type='file'>
      <driver name='qemu' type='raw'/>
      <target dev='fda'/>
    </disk>
    <interface type='bridge'>
      <source bridge='br1'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <video>
      <model type='qxl' ram='65536' heads='1'/>
    </video>
    <graphics type='vnc' autoport='yes' port='-1'/>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <console type='pty'/>
  </devices>
</domain>

4. 启动虚拟机

导入主机

[root@clsn7 dev-test01]# virsh  define centos-dev-test01-v2v.xml  
定义域 centos-dev-test01-v2v(从 centos-dev-test01-v2v.xml)

查看主机列表

[root@clsn7 dev-test01]#  virsh list --all 
 Id    名称                         状态
----------------------------------------------------
 -     centos-dev-test01-v2v          关闭

启动主机

[root@clsn7 dev-test01]#  virsh start centos-dev-test01-v2v 
域 centos-dev-test01-v2v 已开始

5. 测试可用性

登陆迁移后的虚拟机测试

[root@clsn7 ~]# ssh root@192.168.19.123
The authenticity of host '192.168.19.123 (192.168.19.123)' can't be established.
RSA key fingerprint is SHA256:iRmghFzgRIJy5+v8p4lqi8DyUG8F0hXR/qNdDZ2J6RY.
RSA key fingerprint is MD5:37:b8:56:3b:b7:85:fa:cb:d9:55:a7:44:d5:de:f8:d9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.19.123' (RSA) to the list of known hosts.
root@192.168.19.123's password: 
Last login: Mon Mar  5 10:30:02 2018 from 192.168.19.9
[root@dev-test01 ~]# hostname  -I
192.168.19.123

设置虚拟机开机自启动。

[root@clsn7 ~]# virsh autostart centos-dev-test01-v2v 
域 centos-dev-test01-v2v标记为自动开始

至此第一台机器的迁移完成,后续按照相同的方式将其他的虚拟机进行迁移即可。

centos7上测试部署kvm虚拟机

华为的云计算已经摒弃xen架构了,使用kvm。目前kvm已经是一种主流虚拟化架构,所以学习kvm是很有必要的。

一、部署环境

使用vmware workstation部署centos7虚拟机,采用最小化安装,硬盘50G,内存2G,开启cpu虚拟化。网络模式采用桥接,ip地址192.168.3.1,kvm
对虚拟机进行初始化操作,包括关闭selinux 等

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
systemctl disable firewalld
systemctl stop firewalld

二、安装kvm

验证cpu是否支持kvm,如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的

egrep '(vmx|svm)' /proc/cpuinfo

未分类

2.最小化安装依赖包

yum install epel-release net-tools vim unzip zip wget ftp -y

3.安装kvm及依赖项

yum install qemu-kvm libvirt virt-install bridge-utils -y

4.验证安装结果

lsmod | grep kvm

未分类

5.开启kvm服务

systemctl start libvirtd

systemctl enable libvirtd

6.确认服务运行

systemctl status libvirtd

未分类

7.配置网桥模式
先将 /etc/sysconfig/network-scripts/ 目录下的网卡配置文件备份一份(不要备在当前目录以及子目录下,其他目录随意)

cp ifcfg-ens33 /root/   #这里物理网卡配置名称为ens33,复制到/root/

创建 ifcfg-br0 文件,内容如下

cat >>ifcfg-br0 <<EOF
BOOTPROTO=static
DEVICE=br0
TYPE=Bridge
NM_CONTROLLED=no
IPADDR=192.168.3.50
NETMASK=255.255.255.0
GATEWAY=192.168.3.1
DNS1=202.103.24.68
DNS2=202.103.44.150
EOF

未分类

移除掉原来的 ifcfg-ens33 ,重新创建该文件,内容如下:

BOOTPROTO=none
DEVICE=ens33
NM_CONTROLLED=no
ONBOOT=yes
BRIDGE=br0

未分类

重启网络服务

systemctl restart network

使用ifconfig查看,此时多了块网卡br0

未分类

注意virbr0是kvm内部作为nat使用的,没有配置则自动分配192.168.122.1地址,暂时不用管

三、安装虚拟机

准备操作系统,kvm可以很好的支持linux虚拟机,windows虚拟机需要另外装驱动。创建/home/iso目录,使用xftp上传镜像centos7镜像

未分类

未分类

上传完毕后如下图所示

未分类

2.创建虚拟机存放文件目录

mkdir -p /home/kvm

3.使用virt-install创建虚拟机

virt-install --name centos7_kvm --memory 512 --vcpus=1 --disk /home/kvm/centos7_kvm.img,format=qcow2,size=20 --network bridge=br0 --os-type=linux --os-variant=rhel7.3 --cdrom /home/iso/CentOS-7-x86_64-Minimal-1611.iso --noautoconsole --vnc --vncport=5910 --vnclisten=0.0.0.0

未分类

注意,这里参数最好加上–noautoconsole,不然本tty就会一直被占用,会误以为失败死机
检查虚拟机状态,确认运行

virsh list

未分类

virsh工具的相关选项说明
--name 虚拟机名称
--memory 内存大小
--vcpus= vcpu
--disk 创建硬盘 制定硬盘路径,格式,大小
--network 指定网络
--os-type 指定操作系统类型
--os-variant= 指定操作系统版本
--cdrom 指定光驱安装操作系统
--noautoconsosle 虚拟机创建完毕后不会自动切换tty
--vnc 使用vnc
--vncport vnc端口

4.使用vnc登录虚拟机安装操作系统

未分类

安装操作系统

未分类

因为连接br0,虚拟机可以使用dhcp获取ip地址

未分类

kvm中虚拟机创建成功,可以直接使用ssh连接kvm虚拟机进行配置

centos7.2 kvm虚拟化管理平台WebVirtMgr部署

在服务器上部署kvm虚拟化,虚出多台VM出来,以应对新的测试需求。
当KVM宿主机越来越多,需要对宿主机的状态进行调控,决定采用WebVirtMgr作为kvm虚拟化的web管理工具,图形化的WEB,让人能更方便的查看kvm 宿主机的情况和操作
WebVirtMgr是近两年来发展较快,比较活跃,非常清新的一个KVM管理平台,提供对宿主机和虚机的统一管理,它有别于kvm自带的图形管理工具(virtual machine manager),让kvm管理变得更为可视化,对中小型kvm应用场景带来了更多方便。
WebVirtMgr采用几乎纯Python开发,其前端是基于Python的Django,后端是基于Libvirt的Python接口,将日常kvm的管理操作变的更加的可视化。

WebVirtMgr特点

  • 操作简单,易于使用
  • 通过libvirt的API接口对kvm进行管理
  • 提供对虚拟机生命周期管理

WebVirtMgr 功能

宿主机管理支持以下功能

  • CPU利用率
  • 内存利用率
  • 网络资源池管理
  • 存储资源池管理
  • 虚拟机镜像
  • 虚拟机克隆
  • 快照管理
  • 日志管理
  • 虚机迁移

虚拟机管理支持以下功能

  • CPU利用率
  • 内存利用率
  • 光盘管理
  • 关/开/暂停虚拟机
  • 安装虚拟机
  • VNC console连接
  • 创建快照

下面对部署过程进行记录,希望能帮助到有用到的朋友们。
这里我将webvirtmgr服务器和kvm服务器放在同一台机器上部署的,即单机部署

  • 系统:Centos 7.2
  • 内存:64G
  • CPU:32核
  • ip:192.168.56.50(内网),192.168.0.29(外网)

一、基础环境

#close firewalld and NetworkManager
[root@linux-node1 ~]# systemctl disable firewalld
[root@linux-node1 ~]# systemctl disable NetworkManager

#关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

1.1 开启blos 开启vt,检查

1)查看是否支持虚拟机
说明1:半虚拟化是不能运行与安装KVM虚拟机的。
[root@ops ~]#egrep '(vmx|svm)' --color=always /proc/cpuinfo

1.2 系统版本

[root@webvirtmgr-node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

[root@webvirtmgr-node1 ~]# uname -r
3.10.0-327.el7.x86_64

1.3 安装epel源

#备份源
yum install wget -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum clean all
yum makecache
#install software
yum install net-tools vim lrzsz -y

1.4 安装kvm软件

yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager python-virtinst libvirt-client virt-viewer -y

1.5 本机网络

[root@webvirtmg ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:68:4b:e3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.50/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe68:4be3/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:68:4b:ed brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.50/24 brd 192.168.56.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe68:4bed/64 scope link 
       valid_lft forever preferred_lft forever

1.6 配置桥接网络,(备注:br0绑定eth1)

[root@webvirtmg ~]# cd /etc/sysconfig/network-scripts/

[root@webvirtmg network-scripts]# cat ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=192.168.56.50
PREFIX=24
GATEWAY=192.168.56.2
DNS1=192.168.56.2
BRIDGE=br0 
NM_CONTROLLED=no

[root@webvirtmg network-scripts]# cat ifcfg-br0
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth1
ONBOOT=yes
IPADDR=192.168.56.50
PREFIX=24
GATEWAY=192.168.56.2
DNS1=192.168.56.2

#重启网络服务

systemctl restart network

1.7 启动libvirt

[root@webvirtmgr-node1 ~]# systemctl restart libvirtd
[root@webvirtmgr-node1 ~]# systemctl status libvirtd

1.8 测试

[root@webvirtmgr-node1 ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

[root@webvirtmgr-node1 ~]# virsh --version
3.9.0
[root@webvirtmgr-node1 ~]# virt-install --version
1.4.3
[root@webvirtmgr-node1 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@webvirtmgr-node1 ~]# lsmod |grep kvm
kvm_intel 162153 0 
kvm 525259 1 kvm_intel

1.9 查看网桥

[root@webvirtmg ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29684bed   no      eth1
virbr0      8000.000000000000   yes

二、部署webvirtmgr

参考官网:https://github.com/retspen/webvirtmgr/wiki/Install-WebVirtMgr

1.1 安装依赖包

yum install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx -y

1.2 从git-hub中下载相关的webvirtmgr代码

[root@openstack ops]# cd /usr/local/src/
[root@openstack src]# git clone git://github.com/retspen/webvirtmgr.git    (下载地址:https://pan.baidu.com/s/1pLS3kCj      获取密码:8efm)

1.3 安装webvirtmgr

[root@openstack src]# cd webvirtmgr/
[root@openstack webvirtmgr]# pip install -r requirements.txt

1.4 检查sqlite3 (备注:自带不需要安装,导入模块检查一下。)

[root@webvirtmg webvirtmgr]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()

1.5、初始化账号

[root@webvirtmg webvirtmgr]# pwd
/usr/local/src/webvirtmgr<br>
[root@webvirtmg webvirtmgr]# ./manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): admin
Email address: 1034611705@qq.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)

1.6 拷贝web到 相关目录

[root@openstack ops]# mkdir -pv /var/www
[root@openstack ops]# cp -Rv /usr/local/src/webvirtmgr /var/www/webvirtmgr

1.7 设置ssh

[root@openstack ops]# ssh-keygen -t rsa             //产生公私钥
[root@openstack ops]# ssh-copy-id 192.168.1.17        //由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么这个是它的ip
[root@openstack ops]# ssh 192.168.1.17 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60

1.8 编辑nginx配置文件

#添加这行代码: include /etc/nginx/conf.d/*.conf;

[root@webvirtmg ~]# cd /etc/nginx/
[root@webvirtmg  nginx]# mv nginx.conf /tmp
[root@webvirtmg  nginx]#cp nginx.conf.default nginx.conf

#编辑配置文件
[root@webvirtmg  nginx]#vi nginx.conf

[root@webvirtmg nginx]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

#添加 /etc/nginx/conf.d/webvirtmgr.conf 配置文件

[root@webvirtmg nginx]# vim /etc/nginx/conf.d/webvirtmgr.conf  
server {
listen 80 default_server;

server_name $hostname;
#access_log /var/log/nginx/webvirtmgr_access_log;

location /static/ {
root /var/www/webvirtmgr/webvirtmgr; # or /srv instead of /var
expires max;
}

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $remote_addr;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M; # Set higher depending on your needs
}
}

#重启nginx服务

systemctl restart nginx

1.9 修改防火墙规则

#修改防火墙规则
[root@ops ~]# vim /etc/sysconfig/selinux 
......
SELINUX=disabled

#临时生效
[root@ops ~]# setenforce 0
setenforce: SELinux is disabled

#查看状态
[root@ops ~]# getenforce 
Disabled

#直接执行这行
/usr/sbin/setsebool httpd_can_network_connect true

2.0 授权

chown -R nginx:nginx /var/www/webvirtmgr

2.1 设置 supervisor (如果iptables防火墙开启的话,就必须要开通80、8000、6080端口访问)

[root@test]# vim /etc/supervisord.conf     //在文件末尾添加,注意将默认的python改为python2,因为上面只有用这个版本执行才不报错!
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py                     //启动8000端口
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=nginx

[program:webvirtmgr-console]
command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console                               //启动6080端口(这是控制台vnc端口)
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx

#检查

#检查
[root@test]#vim /var/www/webvirtmgr/conf/gunicorn.conf.py    //确保下面bind绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
bind = '127.0.0.1:8000'

#设置开机启动
[root@webvirtmg nginx]# systemctl enable supervisord.service

#设置开机加载
[root@webvirtmg nginx]#vim /etc/rc.local /usr/sbin/setsebool httpd_can_network_connect true

#重启服务
[root@webvirtmg nginx]# systemctl restart supervisord
[root@webvirtmg nginx]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-06-28 09:37:15 CST; 6s ago
Process: 19369 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
Main PID: 19372 (supervisord)
CGroup: /system.slice/supervisord.service
├─19372 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
├─19373 /usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
├─19374 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19380 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19381 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19382 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19383 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19384 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19385 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19386 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19387 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19388 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19389 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19390 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19391 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19392 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19393 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19394 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
├─19395 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...
└─19396 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py...

Jun 28 09:37:15 webvirtmg.com systemd[1]: Starting Process Monitoring and Control Daemon...
Jun 28 09:37:15 webvirtmg.com systemd[1]: Started Process Monitoring and Control Daemon.

2.2 查看端口 备注:6080和8000已经启动

#查看端口  备注:6080和8000已经启动
[root@webvirtmg nginx]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19287/nginx: master 
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 7498/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1631/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1828/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 18977/sshd: root@pt 
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 18977/sshd: root@pt 
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 19374/python2
tcp 0 0 0.0.0.0:6080 0.0.0.0:* LISTEN 19373/python2
tcp6 0 0 :::22 :::* LISTEN 1631/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1828/master
tcp6 0 0 ::1:6010 :::* LISTEN 18977/sshd: root@pt 
tcp6 0 0 ::1:6011 :::* LISTEN 18977/sshd: root@pt

2.3 访问地址:http://192.168.56.50/login/

账号信息:

username: admin

passwd:************

未分类

未分类

未分类

未分类

未分类

2.4 登录后会报错

解决措施:
1)在webvirtmgr服务器(服务端)上(这里kvm和WebVirtMgr部署在同一台机器上)创建nginx用户家目录(默认nginx服务安装时是没有nginx家目录的),生成nginx的公私钥
[root@test]# cd /home/
[root@test home]# mkdir nginx
[root@test home]# chown nginx.nginx nginx/
[root@test home]# chmod 700 nginx/ -R
[root@test home]# su - nginx -s /bin/bash
-bash-4.1$ ssh-keygen                             #期间输入yes后直接回车,回车
-bash-4.1$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=nonUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.1$ chmod 0600 ~/.ssh/config

#在webvirtmgr服务器(服务端)上(这里kvm和WebVirtMgr部署在同一台机器上),将nginx用户的ssh-key上传到kvm服务器上(这里kvm和WebVirtMgr部署在同一台机器上)

[root@openstack ops]# su - nginx -s /bin/bash
-bash-4.1$ ssh-copy-id root@192.168.0.50
Warning: Permanently added '192.168.0.50' (RSA) to the list of known hosts.
root@192.168.0.50's password: #输入192.168.0.50即本机的root账号
Now try logging into the machine, with "ssh 'root@192.168.0.50'", and check in:
.ssh/authorized_keys

#在kvm(客服端)服务器上(这里kvm和WebVirtMgr部署在同一台机器上)配置 libvirt ssh授权

[root@test]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root #注意这里采用的是root用户
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[root@test]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla

#重启服务

systemctl restart nginx

systemctl restart libvirtd

#其它参考:

http://www.cnblogs.com/kevingrace/p/5737724.html

https://www.jianshu.com/p/160272d81ac3

三、WebVirtMgr的日常配置

参考:https://yq.aliyun.com/articles/46358

3.1 上传Centos6.8的镜像到这个目录中

[root@webvirtmg images]# cd /var/lib/libvirt/images

[root@webvirtmg images]# ll
total 6162064
-rw-------. 1 qemu qemu 21478375424 Jun 28 11:13 Centos6.8.img
-rw-r--r--. 1 qemu qemu  3916431360 Jun 28 11:01 CentOS-6.8-x86_64-bin-DVD1.iso

3.2 配置完成

未分类

KVM虚拟网络

宿主服务器安装完成KVM,首先要设定网络,以便和主机网络,客户机之间的网络通信在libvirt中运行KVM网络有两种方法:NAT和Bridge,默认为NAT

KVM 客户机网络连接有两种方式:

(1)用户模式(NAT):这种方式是默认网络,数据包由NAT方式通过主机的接口进行传送,可以访问外网,但是无法从外网访问虚拟机网络
(2)桥接模式(Bridge):这种模式允许虚拟机像一台独立的主机一样拥有网络,外部的主机可以直接访问到虚拟机内部

一、基于NAT的虚拟网络

KVM安装时默认的网络配置

未分类

默认宿主机会有一个虚拟网卡virbr0,其实是一个虚拟交换机,并绑定一个网卡virbr0-nic

未分类

这时候主机就变成了一个路由器,可以看到路由功能已经打开,并做了SNAT

[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
[root@localhost ~]# iptables -t nat -L

未分类

当有虚拟机启动,如果采用的是默认NAT模式,那么virbr0交换机下就会增加接口,可以看到有2个虚拟接口,代表有2个虚拟机接到了这个交换机上

未分类

可以通过virsh命令查看虚拟机的网卡情况

未分类

向虚拟机添加虚拟机网络
可以通过xml文档添加:

<interface type='network'>
      <mac address='52:54:00:aa:18:d1'/>
      <source network='default'/>    //网络模式
      <model type='virtio'/>        //网卡类型
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

二、基于网桥的虚拟网络

未分类

首先需要创建虚拟交换机,方式很多,可以通过图形化,nmcli命令等方式,下面通过直接该配置文件创建虚拟交换机

(1)在宿主机添加配置文件

未分类

重新启动网络,让虚拟交换机br0激活

未分类

(2)为虚拟机添加网络

如果虚拟机是关机情况下,执行以下命令

virsh attach-interface vm1  bridge  br0 --model virtio --config        // 下次启动生效

如果虚拟机是开机情况下,执行以下命令

virsh attach-interface vm2  bridge  br0 --model virtio --current       // 立即生效

cd /etc/libvirt/qemu
virsh dumpxml vm2 > vm2.xml      //为了让每次开机都生效,新生成一次配置文件替换原来的配置文件

KVM虚拟化-qemu-kvm杂记

一、安装KVM

1、VMware Workstation开启虚拟化功能

未分类

1.1 查看自己的物理CPU是否支持硬件辅助虚拟化

只要标记有vmx或svm,就说明支持硬件虚拟化
[root@node2 ~]# grep -E --color=auto "(vmx|svm)" /proc/cpuinfo 
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave hypervisor lahf_lm arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave hypervisor lahf_lm arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep

2、安装kvm

2.1 装载kvm模块

装载kvm核心模块:

[root@node2 ~]# modprobe kvm

因为我是Intel的CPU,所以装载intel的kvm模块:
[root@node2 ~]# modprobe kvm-intel

2.2 查看装载模块

此时内核已经成为kvm-hypervisor
[root@node2 ~]# lsmod | grep kvm
kvm_intel              55496  0 
kvm                   337772  1 kvm_intel
[root@node2 ~]# 

验证是否有KVM文件,存在说明内核已经被kvm寄宿了
[root@node2 ~]# ll /dev/kvm 
crw-rw---- 1 root root 10, 232 Sep 15 23:14 /dev/kvm

2.3 查看虚拟化管理包组

查看包组:
[root@node2 ~]# yum grouplist | grep -i "virtualization"
   Virtualization
   Virtualization Client
   Virtualization Platform
   Virtualization Tools

2.4 由于我们暂时先学习qemu管理虚拟机,只安装 Virtualization包组里的qemu组件即可

[root@node2 ~]# yum install qemu-kvm

2.5 查看qemu的文件

[root@node2 ~]# rpm -ql qemu-kvm | grep qemu-kvm | head -2

2.6 执行程序位置不在环境变量下,需要创建链接

[root@node2 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/
`/usr/bin/qemu-kvm' -> `/usr/libexec/qemu-kvm'

3、CentOS7的虚拟化包组

只要安装Virtualization包组即可

[root@master1 ~]# yum grouplist | grep -i "virtualization"
   Virtualization Host
   Virtualization

3.1 CentOS7安装KVM

装载kvm
# modprobe kvm
# modprobe kvm-intel

查看kvm模块:
modinfo kvm
[root@master1 ~]# modinfo kvm-intel

二、qemu-kvm虚拟机管理(CentOS7)

1、安装qemu

[root@master1 ~]# yum install qemu-kvm

1.1 创建软链接

[root@master1 ~]# rpm -ql qemu-kvm | grep /libexec/
/usr/libexec/qemu-kvm

[root@master1 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/
‘/usr/bin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’

1.2 查看使用帮助

[root@master1 ~]# qemu-kvm -h

2、创建一个简单的虚拟机

2.1 去互联网搜索开源的镜像文件

访问:https://launchpad.net/cirros

查看该网站发现只更新到0.33,获取源码已经移交到github托管。这边下载0.30做实验

[root@master1 ~]# ls
cirros-no_cloud-0.3.0-x86_64-disk.img

2.1.1 查看磁盘映像文件格式

[root@master1 ~]# qemu-img info /root/cirros-no_cloud-0.3.0-x86_64-disk.img 
image: /root/cirros-no_cloud-0.3.0-x86_64-disk.img
file format: qcow2
virtual size: 39M (41126400 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
    compat: 0.10

2.2 启动微缩版虚拟机

内存128M,CPU 1个
[root@master1 ~]# qemu-kvm -m 128 -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img 
VNC server running on `::1:5900'

2.2.2 默认用VNC打开,需要在宿主机安装一个VNC客户端进行查看

Centos7需要先安装图形界面,并切换到图形界面
yum groupinstall "GNOME Desktop" "Graphical Administration Tools"

查看运行级别:
[root@master1 ~]# systemctl get-default
multi-user.target

修改运行级别为图形化界面:
systemctl set-default graphical.target
或者
ln -s /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target

运行图形界面:
startx &

还是需要重启宿主机,才能转发X11图形调用到xmanager
# reboot

安装vnc客户端:
[root@master1 ~]# yum install tigervnc

连接虚拟机:
[root@master1 ~]# vncviewer :5900

未分类

未分类

切换为管理员

未分类

切换qemu监控接口:Ctrl+Alt+2

未分类

显示当前虚拟机的名字

未分类

显示当前虚拟机的状态

未分类

虚拟机切换回命令行:Ctrl+Alt+1

未分类

关闭虚拟机,不是断电,而是向里面发送关机信号:

[root@master1 ~]# ps aux | grep qemu-kvm
root      2504 36.5  1.6 561512 65812 pts/0    Sl+  19:03   0:24 qemu-kvm -m 128 -smp 1 -name test -hda cirros-no_cloud-0.3.0-i386-disk.img
root      2539  0.0  0.0 112660   976 pts/1    S+   19:04   0:00 grep --color=auto qemu-kvm
[root@master1 ~]# 
[root@master1 ~]# kill -9 2504

2.2.3 杂项

查看可以模拟的主机类型:
[root@master1 ~]# qemu-kvm -M ?

查看可以模拟的CPU类型:
[root@master1 ~]# qemu-kvm -cpu ?

默认模拟的是QEMU的虚拟CPU,不是底层硬件CPU

未分类

想要启动虚拟机时,模拟底层硬件CPU需要加-cpu参数:

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img 

未分类

3、选项

3.1 device

磁盘映像高级选项:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback

硬盘格式已经显示为VDA

未分类

未分类

3.1.1 安装window-xp

[root@master1 ~]# ls -lht xp.iso 
-rw-r--r-- 1 root root 499M Mar 17 20:17 xp.iso

创建目录:
[root@master1 ~]# mkdir -pv /images/windows/

创建磁盘映像文件(20G大小,磁盘类型为元数据类型【稀疏格式】):
[root@master1 ~]# qemu-img create -o size=20G,preallocation=metadata -f qcow2 /images/windows/winxp.qcow2
[root@master1 ~]# ll -lht /images/windows/winxp.qcow2 
-rw-r--r-- 1 root root 21G Mar 17 21:01 /images/windows/winxp.qcow2
[root@master1 ~]# du -sh /images/windows/winxp.qcow2 
4.1M    /images/windows/winxp.qcow2

启动安装window-xp(512内存,1核CPU,CPU使用底层硬件模拟,使用XP镜像安装,启动顺序为光盘-硬盘,第一次重启剔除光盘启动)
[root@master1 ~]# qemu-kvm -m 512 -smp 1 -cpu host -drive file=/images/windows/winxp.qcow2,media=disk -drive file=/root/xp.iso,media=cdrom -boot order=dc,once=d

登陆VNC查看:

[root@master1 ~]# vncviewer :5900

未分类

未分类

安装系统中

未分类

4、KVM-显示

4.1 SDL功能

[root@master1 ~]# yum install -y SDL SDL-devel SDL-static

[root@master1 ~]# qemu-kvm -m 128 -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img -sdl
SDL support is disabled

由于qemu-kvm编译默认没有加入SDL功能,所以该实验跳过

4.2 vnc更改监听端口5901

默认0号面板端口为5900,以此类推
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1

[root@master1 ~]# vncviewer 10.201.106.131:5901

未分类

4.3 启动虚拟机直接显示monitor控制台设置VNC密码

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1,password -monitor stdio
QEMU 1.5.3 monitor - type 'help' for more information
(qemu) 

设置VNC密码
(qemu) change vnc password
Password: ******

4.4 通过VNC密码登陆

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1,password

未分类

未分类

4.4.1 控制台切换

未分类

切换控制台或系统界面
Ctrl+a,c

未分类

显示控制台帮助:Ctrl+a,h

C-a h    print this help
C-a x    exit emulator
C-a s    save disk data back to file (if -snapshot)
C-a t    toggle console timestamps
C-a b    send break (magic sysrq)
C-a c    switch between console and monitor
C-a C-a  sends C-a

4.4.2 指定显卡接口类型

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -vga cirrus

5、网络

5.1 创建管理桥

查看桥模块,桥是在内核中实现的:
[root@master1 ~]# modinfo bridge

安装桥管理命令:
# yum install -y bridge-utils

查看其文件路径:
[root@master1 ~]# rpm -ql bridge-utils

创建桥:
[root@master1 ~]# brctl addbr br0

关闭桥:
[root@master1 ~]# brctl stp br0 off
[root@master1 ~]# brctl show

启动桥:
[root@master1 ~]# ip link set br0 up

其他:可以用nmtui图形界面添加桥
# nmtui

5.2 网卡

5.2.1 查看当前支持的网卡类型

[root@master1 ~]# qemu-kvm -net nic,model=?
qemu: Supported NIC models: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio

5.2.2 启动虚拟机指定网卡类型

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif0.0,script=no

内部的系统网卡类型默认是e1000
# lsmod | grep e1000
e1000                 101773  0 

设置宿主机的后端网卡
首先配置启动和关闭脚本:
[root@master1 ~]# vim /etc/qemu-ifup 

#!/bin/bash
#
bridge=br0

if [ -n "$1"  ]; then
    ip link set $1 up
    brctl addif $bridge $1
    [ $? -eq 0 ] && exit 0 || exit 1
else
    echo "Error,no interface specified"
    exit 1
fi

[root@master1 ~]# chmod +x /etc/qemu-ifup

启动测试:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif0.0,script=/etc/qemu-ifup

[root@master1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.66fc76ef2ced   no      vif0.0

5.3 开启第二台虚拟机

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test1" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif1.0,script=/etc/qemu-ifup

[root@master1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.66fc76ef2ced   no      vif0.0
                            vif1.0

5.4 配置两台虚拟机IP为同个网段通信(隔离网络模型)

# hostname
test
# ifconfig eth0 192.168.1.1/24

# hostname
test1
# ifconfig eth0 192.168.1.2/24

不通,原因是MAC地址相同,需要随机生成:

[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test1" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic,macaddr=52:54:00:12:34:57 -net tap,ifname=vif1.0,script=/etc/qemu-ifup

网络可以通了:

未分类

5.5 (路由网络模型)

5.5.1 宿主机添加虚拟网卡

[root@master1 ~]# ip link add veth0.0 type veth peer veth1.0

[root@master1 ~]# ifconfig -a | grep veth
veth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
veth1.0: flags=4098<BROADCAST,MULTICAST>  mtu 1500

veth0留在物理机,veth1添加到br0

激活两块网卡:
[root@master1 ~]# ip link set veth0 up
[root@master1 ~]# ip link set veth1.0 up

将vet1.0添加至br0
[root@master1 ~]# brctl addif br0 veth1.0
[root@master1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.c2e8d51d8e78   no      veth1.0

[root@master1 ~]# ifconfig veth0 192.168.1.254/24

在KVM上的虚拟机测试ping
# hostname
test1
# ping 192.168.1.254
PING 192.168.1.254 (192.168.1.254): 56 data bytes
64 bytes from 192.168.1.254: seq=0 ttl=64 time=13.576 ms
64 bytes from 192.168.1.254: seq=1 ttl=64 time=2.025 ms

KVM客户机添加去往物理机地址的网关:
# route add default gw 192.168.1.254
已经可以ping通物理机:
# ping 10.201.106.131
PING 10.201.106.131 (10.201.106.131): 56 data bytes
64 bytes from 10.201.106.131: seq=0 ttl=64 time=1.828 ms
64 bytes from 10.201.106.131: seq=1 ttl=64 time=2.080 ms

5.6 路由网络模型

5.6.1 物理机打开路由转发

[root@master1 ~]# cat /proc/sys/net/ipv4/ip_forward
0

[root@master1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

但是这时候还是通不了外网,因为包发出去回不来:

未分类

未分类

5.6.2 使用nat模式转发到外网

root@master1 ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.201.106.131

KVM虚拟机已经可以ping通外网

未分类

未分类

5.6.3 上面方式太繁琐,多了一层虚拟桥,其实只需把网关地址配置到br0即可实现外网转发

[root@master1 ~]# ip link set veth0 down
[root@master1 ~]# ip link set veth1.0 down

[root@master1 ~]# ifconfig br0 192.168.1.254/24 up

KVM测试外网联通性:
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=127 time=72.507 ms
64 bytes from 8.8.8.8: seq=1 ttl=127 time=114.481 ms

抓包,br0上没有转换

未分类

是在物理网卡eth0转换的

未分类

5.7 桥接模型

5.7.1 取消之前br0地址

[root@master1 ~]# ip addr del 192.168.1.254/24 dev br0

5.7.2 把物理网卡添加到br0

一旦物理网卡添加到br0,物理网卡就变成了交换机

首先拆除物理网卡地址,然后将物理网卡添加至br0,最后在br0配置原先的物理网卡的IP地址。由于担心xshell断掉,直接在终端操作:

未分类

[root@master1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29a77aec   no      eth0
                            vif0.0
                            vif1.0

5.7.3 KVM虚拟机配置跟宿主机同个网段IP和网关

# ifconfig eth0 10.201.106.81/24
# route add default gw 10.201.106.2

# ifconfig eth0 10.201.106.82/24
# route add default gw 10.201.106.2

又可以ping通外网了:

未分类

抓包物理机网卡,源地址全是KVM虚拟机IP发出的包:

未分类

5.8 一旦KVM虚拟机停止,网卡会自动删除

关机前:
bridge name bridge id       STP enabled interfaces
br0     8000.000c29a77aec   no      eth0
                            vif0.0
关机                          vif1.0
# poweroff

会自动拆除网卡:
[root@master1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29a77aec   no      eth0
                            vif1.0

6、基于PXE环境安装虚拟机

6.1 创建映像文件

[root@master1 ~]# mkdir /images/centos

[root@master1 ~]# qemu-img create /images/centos/centos6.img -o size=120G,preallocation=metadata -f qcow2

6.2 启动虚拟机

[root@master1 ~]# qemu-kvm -m 512 -smp 1 -name centos -drive file=/images/centos/centos6.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:66:66:66 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n

我没有PXE环境,所以一直是获取IP地址界面

未分类

7、其他操作

7.1 用光盘方式安装一个CentOS6.6

[root@master1 ~]# qemu-kvm -m 512 -smp 1 -name centos -drive file=/images/centos/centos6.img,media=disk,if=virtio -drive file=/root/centos6.iso,media=cdrom -boot order=dc,once=d -net nic,model=virtio,macaddr=52:54:00:66:66:66 -net tap,ifname=centos6.0,script=/etc/qemu-ifup

安装完毕查看模块,网卡等设备是半虚拟化的virtio

未分类

7.2 宿主机热迁移,需要共享存储

在待迁入目的宿主机使用(指定哪个端口接收待迁移虚拟机进入)
    # qemu-kvm -vnc :N -incoming tcp:0:7777
    # vncviewer :590N

在源主机使用monitor接口,指定要迁入的宿主机信息:
        migrate tcp:DEST_IP:DEST:PORT

7.3 切换至控制台

Ctrl+Alt+2 切换至控制台

Ctrl+Alt+1 切换回系统操作界面

未分类

7.3.1 查看当前虚拟机CPU信息

CPU是用线程模拟的

未分类

7.3.2 qemu缺陷,无法远程管理虚拟机

kvm实战应用–kvm基础部署

虚拟化基本上是一个企业里边必不可少的一项技能了,但是测试环境当中,虚拟化又是很简单的。

一、安装KVM

1、验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的。

egrep '(vmx|svm)' /proc/cpuinfo

未分类

2、关闭SELinux,防火墙等。

setenforce 0        #永久关闭修改配置文件
iptables -F
chkconfig iptables o
service NetworkManager stop
chkconfig NetworkManager off

3、如果是最小化安装的CentOS的话,那么有一些基础工具没有,可以先安装一下。

yum -y install epel-release net-tools vim unzip zip wget ftp

4、安装KVM及依赖

yum -y install qemu-kvm libvirt virt-install bridge-utils

5、验证安装结果

lsmod | grep kvm

未分类

6、开启KVM服务,设置开机自启

service libvirtd start
chkconfig libvirtd on

7、查看运行状态

service libvirtd status

未分类

8、配置网卡为网桥模式。配置前先备份一下网卡文件

A、创建ifcfg-br0文件,内容如下:

mkdir /backup
cp /etc/sysconfig/network-scripts/ /backup/ -a
cd /etc/sysconfig/network-scripts/
vim ifcfg-br0

DEVICE="br0"  
ONBOOT="yes"  
TYPE="Bridge"  
BOOTPROTO="static"  
IPADDR="192.168.96.10"    #这个就是服务器的IP地址
NETMASK="255.255.255.0"  
GATEWAY="192.168.96.100"  
DNS="192.168.96.101"  
#IPV6INIT="yes"  
#IPV6_AUTOCONF="yes"  
DHCPV6C="no"  
STP="on"  
DELAY="0"  

B、替换ifcfg-em1(就是服务器在用的第一个网卡)里边的内容为下边的

DEVICE=em1
ONBOOT=yes  
BRIDGE="br0"  

C、重启网络服务。

service network restart

然后ifconfig查看,发现多出了br0网卡,这个br0也成为了系统现在所使用的网卡。

未分类

二、安装虚拟机

1、准备操作系统安装所需的镜像文件

mkdir -p /home/kvm/ios

把想要安装的系统镜像拷到此目录之下。

2、创建虚拟机文件存放的目录

mkdir -p /home/kvm/image

3、直接创建虚拟机。这里先创建一个模板虚拟机,方便以后直接克隆使用,而不需要再重复安装系统

virt-install -name=centos7_2H4G --ram 4096 --vcpus=2 --disk path=/home/kvm/image/centos7_2H4G.img,size=200 --cdrom /home/kvm/ios/CentOS-7-x86_64-DVD-1611.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0,port=6666,keymap=en_us --accelerate --force  --autostart  

事实上创建的时候名字用-n就可以了,但是我当时不知道,这样创建了一个名字为ame=centos7_2H4G的虚拟机,等会儿再说说给虚拟机改名的事儿。

简单说明下各选项的意义:

-n                        #指定创建虚拟机名称
--ram                     #指定内存大小
--vcpus                   #指定CPU个数
--disk path               #指定虚拟机文件存储名称及位置
size                      #指定虚拟机大小
--cdrom                   #指定镜像位置
--network bridge          #指定网桥
vnc,listen=0.0.0.0,port   #指定远程连接端口

4、直接远程连接安装操作系统

  1. 使用VNC进行远程连接,如果是初次使用vnc可能会有问题,解决办法网上难找,不过在另外一个地方有记录点我解决。
  2. 用IP+端口就能够登陆安装操作系统了。
  3. 有时候安装完之后重启,会有找不到系统盘的报错,这个暂时无解,把刚才的虚拟机删掉,重新创建咯。
  4. 装完操作系统,配置IP之类的,就可以直接远程连接使用了。

5、刚才的安装好系统,关机,作为模板文件

virsh shutdown ame=centos7_2H4G

6、使用模板克隆出一个新的虚拟机

virt-clone -o 原虚拟机 -n 新虚拟机 -f 新img文件
virt-clone  -o CentOS7_2H4G -n test -f /home/kvm/image/test.img
  1. 以模板为蓝本创建一个名为maopao(可自定义)的虚拟机,新虚拟机文件位置为。
  2. 创建完成之后,需要修改它的远程连接端口
virsh edit test
搜索/vnc
端口更改成另外一个,yes要改成no,这个如果不改,则无法使用。

未分类

7、启动新的虚拟机,vnc远程连接,可以设置IP了

virsh start test

在vnc中使用宿主机IP+端口的方式访问。

现在,一个虚拟机的使用创建流程就是这样。

CentOS 7下如何配置KVM虚拟化环境

KVM是一个开源的虚拟化模块,是基于硬件的完全虚拟化,所以需要硬件的支持。废话不多说,操作步骤如下:

0x01 硬件检测

$ egrep '(vmx|svm)' /proc/cpuinfo   ##检测CPU是否支持虚拟化

如果输出的结果包含vmx,则是Intel处理器虚拟机技术标志;如果包含SVM,则是AMD处理器虚拟机技术的标志;如果什么都没有,则需要在BISO中开启VT(Virtual Technolege)功能。

0x02 软件包安装

$ yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer

安装完成后,检查kvm模块有没有加载上

$  lsmod |grep kvm   ##有输出类似下面结果则正常

kvm_intel 148081 0

kvm 461126 1 kvm_intel

0x03 创建网桥

1、创建br0文件

$ cp ifcfg-eth0 ifcfg-br0  ##如果管理IP配置再eth0,则这就复制一个br0即可

2、编辑br0

TYPE=Bridge

BOOTPROTO=static

DEFROUTE=yes

PEERDNS=yes

PEERROUTES=yes

DEVICE=br0

ONBOOT=yes

IPADDR=192.168.2.10

NETMASK=255.255.255.0

GATEWAY=192.168.2.1

3、编辑eth0

TYPE=Ethernet

BOOTPROTO=none

NM_CONTROLLED=no

NAME=eth0

DEVICE=eth0

BRIDGE=br0

ONBOOT=yes

配置完成后重启下机器,查看下网桥状态

$ brctl show

bridge name bridge id STP enabled interfaces

br0 8000.90b11c5a7c89 no eth0

virbr0 8000.5254000908ac yes virbr0-nic

至此,KVM环境配置完成。可以创建虚拟机尽情玩耍了。

修改kvm虚拟机镜像文件大小

1、查看kvm镜像的格式信息

[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201
image: 10.1.12.201_base-12-201
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 88G
cluster_size: 65536

2、给镜像增加100G空间

[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201 +100G
Image resized.
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201
image: 10.1.12.201_base-12-201
file format: qcow2
virtual size: 300G (322122547200 bytes)
disk size: 88G
cluster_size: 65536

3、给镜像减少100G空间

qcow2格式降低空间

[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201-- -100G
This image format does not support resize

由于qcow2格式不支持空间减少,所以要使用raw格式。

[root@base-12-199 bakup]# qemu-img convert -O raw 10.1.12.201_base-12-201 10.1.12.201_base-12-201.raw

查看镜像信息

[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.raw
image: 10.1.12.201_base-12-201.raw
file format: raw
virtual size: 300G (429496729600 bytes)
disk size: 88G
[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201.raw -- 100G
Image resized.
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.raw
image: 10.1.12.201_base-12-201.raw
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 88G

转换为qcow2

[root@base-12-199 bakup]# qemu-img convert -O qcow2 10.1.12.201_base-12-201.raw 10.1.12.201_base-12-201.qcow2
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.qcow2
image: 10.1.12.201_base-12-201.qcow2
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 88G