kvm虚拟化环境搭建

环境
centos6.5

安装kvm

yum -y install qemu-kvm.x86_64 qemu-kvm-tools.x86_64 python-virtinst.noarch qemu-img.x86_64 bridge-utils.x86_64 libvirt virt-manager libguestfs-tools-c

查看cpu是否支持虚拟化

[root@zyn ~]# cat /proc/cpuinfo |grep vmx
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 pdpe1gb rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
[root@zyn ~]# lsmod |grep kvm
kvm_intel              54285  0 
kvm                   333172  1 kvm_intel

配置网络
在libvirt中运行KVM网络有两种方法:NAT和bridge,默认是NAT.
将以bridge(桥接模式)为例:

[root@localhost 桌面]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
HWADDR=00:0C:29:BB:CA:36
TYPE=Ethernet
UUID=5697689c-b71a-47b9-8a4b-de1c3d4c0a03
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
BRIDGE="br0"
---------------------------------------------------------
[root@localhost 桌面]# vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
NAME=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=
NETMASK=
GATEWAY=
DNS1=
ONBOOT=yes
--------------------------------------------------------------------
[root@localhost 桌面]# service network restart
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0:                                            [确定]
弹出界面 br0: Determining if ip address 192.168.10.1 is already in use for device br0...
                                                           [确定]
-----------------------------------------------------
确定IP地址信息
[root@localhost 桌面]# ifconfig 
br0       Link encap:Ethernet  HWaddr 00:0C:29:BB:CA:36  
          inet addr:192.168.10.1  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:febb:ca36/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:33 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1772 (1.7 KiB)  TX bytes:636 (636.0 b)
eth0      Link encap:Ethernet  HWaddr 00:0C:29:BB:CA:36  
          inet6 addr: fe80::20c:29ff:febb:ca36/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3091 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:797555 (778.8 KiB)  TX bytes:1182 (1.1 KiB)

kvm管理
创建kvm存储池目录

[root@localhost 桌面]# mkdir -p /data_kvm/{store,iso}

打开图形化

[root@localhost 桌面]# virt-manager 

双击创建卷存储池
在这里插入图片描述
在这里插入图片描述

以同样的方式创建一个镜像存储池
在这里插入图片描述
创建存储卷
在这里插入图片描述
在这里插入图片描述
新建虚拟机
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
kvm命令集
查看命令帮助

[root@localhost ~]# virsh -h

查看KVM的配置文件存放目录

[root@localhost ~]# ls /etc/libvirt/qemu
autostart  Centos6.5.xml  networks

查看虚拟机状态

[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     Centos6.5                      running

开关机

[root@localhost ~]# service acpid status
acpid (pid  1625) 正在运行...
[root@localhost ~]# virsh shutdown Centos6.5
[root@localhost ~]# virsh start Centos6.5

关闭电源

[root@localhost ~]# virsh destroy Centos6.5

通过配置文件启动虚拟机系统实例

[root@localhost ~]# virsh create /etc/libvirt/qemu/Centos6.5.xml 

挂起虚拟机

[root@localhost ~]# virsh suspend Centos6.5
域 Centos6.5 被挂起
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     Centos6.5                      暂停

恢复挂起

[root@localhost ~]# virsh resume Centos6.5
域 Centos6.5 被重新恢复
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     Centos6.5                      running

配置虚拟机实例伴随宿主机自动启动

[root@localhost ~]# virsh autostart Centos6.5
域 Centos6.5标记为自动开始

导出虚拟机配置

[root@localhost ~]# virsh dumpxml Centos6.5 > /etc/libvirt/qemu/Centos6.5_bak.xml

虚拟机的删除与添加
[root@localhost ~]# virsh shutdown Centos6.5
[root@localhost ~]# virsh undefine Centos6.5

修改虚拟机的配置信息
[root@localhost qemu]# virsh edit Centos6.5
[root@localhost qemu]# vim /etc/libvirt/qemu/Centos6.5.xml (效果同上)

KVM文件管理
虚拟机磁盘文件有raw与qcow2格式,KVM 默认格式是:raw;
raw:好处:性能好,速度最快。缺点:不支持一些新的功能。如:镜像、Zlib磁盘压缩,AES加密等。
libgestfs-tools

转换raw格式磁盘至qcow2的格式
查看当前磁盘格式
[root@localhost qemu]# qemu-img info /data_kvm/store/xinwen_kvm.img
image: /data_kvm/store/xinwen_kvm.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 10G
关闭虚拟机:
[root@localhost qemu]# virsh shutdown Centos6.5
域 Centos6.5 被关闭
转换磁盘文件格式:
[root@localhost qemu]# qemu-img convert -f raw -O qcow2 /data_kvm/store/xinwen_kvm.img /data_kvm/store/xinwen_kvm.qcow2

virt-cat命令,类似于cat
[root@localhost qemu]# virt-cat -a /data_kvm/store/xinwen_kvm.qcow2 /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain

virt-edit命令,用于编辑文件,用法与vim基本相同
[root@localhost qemu]# virt-edit -a /data_kvm/store/xinwen_kvm.qcow2 /etc/resolv.conf

virt-df 命令用查看虚拟机磁盘信息
[root@localhost qemu]# virt-df -h Centos6.5
Filesystem Size Used Available Use%
Centos6.5:/dev/sda1 484M 34M 425M 8%
Centos6.5:/dev/VolGroup/lv_root 8.4G 3.3G 4.6G 40%

虚拟机克隆
查看虚拟机的状态:

[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 1     Centos6.5                      running
----------------------------------------------------
[root@localhost ~]# virsh shutdown Centos6.5
域 Centos6.5 被关闭
----------------------------------------------------
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     Centos6.5                      关闭

从Centos6.5 克隆为 test
[root@localhost ~]# virt-clone -o Centos6.5 -n test -f /data_kvm/store/test.qcow2
正在分配 ‘test.qcow2’ | 10 GB 00:00
Clone ‘test’ created successfully.
再查看虚拟机的状态

[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     Centos6.5                      关闭
 -     test                           关闭
6.4 启动虚拟机
[root@localhost ~]# virsh start testtest 已开始
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     test                           running
 -     Centos6.5                      关闭

虚拟机的快照
KVM虚拟机要使用镜像功能,磁盘的格式必须为qcow2

对Centos6.5创建快照
[root@localhost ~]# virsh snapshot-create Centos6.5
Domain snapshot 1420636959 created

查看虚拟机快照版本信息

[root@localhost ~]# virsh snapshot-current Centos6.5

  1420636959 //快照的版本号
  shutoff
.........
7.3 查看快照信息
[root@localhost ~]# virsh snapshot-list Centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1420636959           2015-01-07 21:22:39 +0800 shutoff
7.4 创建新快照
[root@localhost ~]# virsh snapshot-create Centos6.5
Domain snapshot 1420637149 created
[root@localhost ~]# virsh snapshot-list Centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1420636959           2015-01-07 21:22:39 +0800 shutoff
 1420637149           2015-01-07 21:25:49 +0800 shutoff
----------------------------------------------------------------

注意:
[root@localhost ~]# virsh snapshot-create-as Centos6.5 kgc1 123123
注释: 对Centos6.5创建快照名是:kgc1 描述是: 123123

恢复快照状态至1420636959
[root@localhost ~]# virsh snapshot-revert Centos6.5 1420636959
[root@localhost ~]# virsh snapshot-current Centos6.5

1420636959
shutoff

删除快照

[root@localhost ~]# virsh snapshot-list Centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1420636959           2015-01-07 21:22:39 +0800 shutoff
 1420637149           2015-01-07 21:25:49 +0800 shutoff
[root@localhost ~]# virsh snapshot-delete Centos6.5 1420637149
Domain snapshot 1420637149 deleted
[root@localhost ~]# virsh snapshot-list Centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1420636959           2015-01-07 21:22:39 +0800 shutoff
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值