⑤企业虚拟化Kvm 管理、存储配置、自动化脚本管理kvm、第五篇
文章目录KVM存储配置6.存储池相关管理命令kvm快照kvm迁移虚拟机自动化脚本管理kvmKVM存储配置存储池概念:kvm必须要配置一个目录当作他存储磁盘镜像(存储卷)的目录,我们称这个目录为存储池kvm默认存储池的位置:/var/lib/libvirt/images/1.创建基于文件夹的存储池(目录,可自定义)[root@youngfit ~]# mkdir -p /data/vmfs2.定义存
KVM存储配置
存储池
概念:
kvm必须要配置一个目录当作他存储磁盘镜像(存储卷)的目录,我们称这个目录为存储池
kvm默认存储池的位置:
/var/lib/libvirt/images/
1.创建基于文件夹的存储池(目录,可自定义)
[root@youngfit ~]# mkdir -p /data/vmfs
2.定义存储池
[root@youngfit ~]# virsh pool-define-as vmdisk --type dir --target /data/vmfs
Pool vmdisk defined
解释:vmdisk是新建的存储池的名称。可自定义
3.创建已定义的存储池
(1)创建已定义的存储池
[root@youngfit ~]# virsh pool-build vmdisk
Pool vmdisk built
(2)查看已定义的存储池,存储池不激活无法使用
[root@youngfit ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
default active yes
root active yes
vmdisk inactive no
4.激活并自动启动已定义的存储池
[root@youngfit ~]# virsh pool-start vmdisk
Pool vmdisk started
[root@youngfit ~]# virsh pool-autostart vmdisk
Pool vmdisk marked as autostarted
[root@youngfit ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
default active yes
root active yes
vmdisk active yes
这里vmdisk存储池就已经创建好了,可以直接在这个存储池中创建虚拟磁盘文件了。
5.在存储池中创建虚拟机存储卷
[root@youngfit ~]# cd /data/vmfs/
[root@youngfit network]# virsh vol-create-as vmdisk vm99.qcow2 5G --format qcow2
Vol vm99.qcow2 created
[root@youngfit ~]# ll /data/vmfs/ -h
total 196K
-rw------- 1 root root 193K Oct 20 02:32 vm99.qcow2
注1:KVM存储池主要是体现一种管理方式,可以通过挂载存储目录,lvm逻辑卷的方式创建存储池,虚拟机存储卷创建完成后,剩下的操作与无存储卷的方式无任何区别了。
注2:KVM存储池也要用于虚拟机迁移任务。
将存储池的磁盘文件添加到某个虚拟机上
能看到已经存在
6.存储池相关管理命令
(1)在存储池中删除虚拟机存储卷
[root@youngfit ~]# virsh vol-delete --pool vmdisk vm99.qcow2
Vol vm99.qcow2 deleted
(2)取消激活存储池
[root@youngfit ~]# virsh pool-destroy vmdisk
Pool vmdisk destroyed
(3)删除存储池定义的目录/data/vmfs
[root@youngfit ~]# virsh pool-delete vmdisk
Pool vmdisk deleted
(4)取消定义存储池
[root@youngfit ~]# virsh pool-undefine vmdisk
Pool vmdisk has been undefined
到此kvm存储池配置与管理操作完毕.
图形操作方式
如想删除,必须先停用,再删除
kvm快照
为虚拟机rhel5u8-1创建一个快照(磁盘格式必须为qcow2)
# virsh snapshot-create-as vm9 vm9.snap1
error: unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
我做之前vm9虚拟机是关闭的状态,因为开启的状态,文本模式加磁盘总失败(没搞清楚原因,不过可以图形添加)
查看磁盘文件格式
[root@youngfit ~]# qemu-img info /var/lib/libvirt/images/vm9.qcow2
image: /var/lib/libvirt/images/vm9.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 1.5G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
[root@youngfit ~]# virsh snapshot-list vm9 #查看某台虚拟机设备的快照
Name Creation Time State
创建一块磁盘
[root@youngfit ~]# qemu-img create -f raw /var/lib/libvirt/images/vm9-2.raw 5G
Formatting '/var/lib/libvirt/images/vm9-2.raw', fmt=raw size=5368709120
[root@youngfit ~]# ll -h /var/lib/libvirt/images/vm9-2.raw
-rw-r--r-- 1 root root 5.0G Oct 20 03:19 /var/lib/libvirt/images/vm9-2.raw
将其添加到vm9虚拟机上面
[root@youngfit ~]# virsh edit vm9
[root@youngfit images]# qemu-img info /var/lib/libvirt/images/vm9-2.raw
image: /var/lib/libvirt/images/vm9-2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
磁盘格式的转换
由于raw的磁盘格式,不支持快照功能,我们需要将其转换为qcow2的格式
[root@youngfit images]# qemu-img convert -O qcow2 /var/lib/libvirt/images/vm9-2.raw /var/lib/libvirt/images/vm9-2.qcow2
查看
[root@youngfit images]# ll -h
total 14G
-rw-r--r-- 1 root root 193K Oct 20 03:40 vm9-2.qcow2
-rw-r--r-- 1 root root 5.0G Oct 20 03:19 vm9-2.raw
-rw-------. 1 root root 5.1G Oct 20 03:34 vm9.qcow2
[root@youngfit images]# qemu-img info /var/lib/libvirt/images/vm9-2.qcow2
image: /var/lib/libvirt/images/vm9-2.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
然后去修改vm9虚拟机的磁盘格式和名称
[root@youngfit images]# virsh edit vm9
然后我们开始做快照,图形化方式不再介绍
[root@youngfit images]# virsh snapshot-create-as vm9 vm9.snap1
Domain snapshot vm9.snap1 created
[root@youngfit images]# virsh snapshot-list vm9
Name Creation Time State
------------------------------------------------------------
vm9.snap1 2019-10-20 03:48:21 +0800 shutoff
[root@localhost ~]# mkdir /opt/snap
[root@youngfit images]# virsh snapshot-create-as vm9 vm9.snap2
Domain snapshot vm9.snap2 created
[root@localhost ~]# touch /opt/snap/qf.txt
[root@youngfit images]# virsh snapshot-create-as vm9 vm9.snap3
Domain snapshot vm9.snap3 created
[root@youngfit images]# virsh snapshot-list vm9
Name Creation Time State
------------------------------------------------------------
vm9.snap1 2019-10-20 03:48:21 +0800 shutoff
vm9.snap2 2019-10-20 03:50:45 +0800 shutoff
vm9.snap3 2019-10-20 03:51:02 +0800 shutoff
然后将vm9关闭,恢复到快照vm9.snap1
[root@youngfit images]# virsh shutdown vm9
[root@youngfit images]# virsh snapshot-revert vm9 vm9.snap1 #恢复快照到vm9.snap1
[root@youngfit images]# virsh start vm9
Domain vm9 started
在vm9虚拟机上查看
[root@vm9 ~]# ls /opt/
[root@vm9 ~]#
可以再恢复到vm9.snap2测试一下
删除虚拟机快照操作:
[root@youngfit images]# virsh snapshot-list vm9
Name Creation Time State
------------------------------------------------------------
vm9.snap1 2019-10-20 03:48:21 +0800 shutoff
vm9.snap2 2019-10-20 03:50:45 +0800 shutoff
vm9.snap3 2019-10-20 03:51:02 +0800 shutoff
[root@youngfit images]# virsh snapshot-delete --snapshotname vm9.snap2 vm9
Domain snapshot vm9.snap2 deleted
[root@youngfit images]# virsh snapshot-list vm9
Name Creation Time State
------------------------------------------------------------
vm9.snap1 2019-10-20 03:48:21 +0800 shutoff
vm9.snap3 2019-10-20 03:51:02 +0800 shutoff
kvm迁移虚拟机
最简单的方法:
拷贝配置文件,磁盘
0.18.42.202
10.18.42.46
热迁移
192.168.1.1/24 192.168.1.2/24
++++++++++++ ++++++++++++
+ + + +
+ KVM-A + =======> + KVM-B +
+ + + +
++++++++++++ ++++++++++++
images images
/var/lib/libvirt/images /var/lib/libvirt/images
nfs
系统环境:rhel6.4 x86_64 iptables and selinux off
注意:
1.两台机器要做互相解析
2.同一个大版本的系统,从高版本系统上不可以往低版本系统上迁移,反过来可以比如从6.5不能迁移到6.4,但是从6.4可以迁移到6.5
3.两台机器的防火墙和selinux全部开机关闭
将 KVM-A 上的虚拟机镜像文件所在的目录共享出来
[root@localhost1 ~]# getenforce
Permissive
[root@localhost1 ~]# iptables -F
[root@localhost1 ~]# cat /etc/hosts
192.168.1.1 fw-01
192.168.1.2 fw-02
[root@localhost1 ~]# yum -y install nfs-tools rpcbind
[root@localhost1 ~]# vim /etc/exports
/var/lib/libvirt/images 192.168.1.2(rw,sync,no_root_squash)
[root@localhost1 ~]# service nfs start
KVM-B将KVM-A上共享出来的目录挂载在到KVM-B的/var/lib/libvirt/images
[root@localhost2 ~]# mount -t nfs 192.168.1.1:/var/lib/libvirt/images /var/lib/libvirt/images
在KVM-B配置/etc/libvirt/qemu.conf
vim /etc/libvirt/qemu.conf #取消下面选项的注释
user = "root" 第198行
group = "root" 第202行
[root@localhost2 ~]# serivice libvirtd restart
自动化脚本管理kvm
#!/bin/bash
#kvm batch create vm tool
#version:0.1
#author:name
#需要事先准备模板镜像和配置文件模板
echo "1.创建自定义配置单个虚拟机
2.批量创建自定义配置虚拟机
3.批量创建默认配置虚拟机
4.删除虚拟机"
read -p "选取你的操作(1/2/3):" op
batch_self_define() {
kvmname=`openssl rand -hex 5`
sourceimage=/var/lib/libvirt/images/vmmodel.img
sourcexml=/etc/libvirt/qemu/vmmodel.xml
newimg=/var/lib/libvirt/images/${kvmname}.img
newxml=/etc/libvirt/qemu/${kvmname}.xml
cp $sourceimage $newimg
cp $sourcexml $newxml
kvmuuid=`uuidgen`
kvmmem=${1}000000
kvmcpu=$2
kvmimg=$newimg
kvmmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
sed -i "s@kvmname@$kvmname@;s@kvmuuid@$kvmuuid@;s@kvmmem@$kvmmem@;s@kvmcpu@$kvmcpu@;s@kvmimg@$kvmimg@;s@kvmmac@$kvmmac@" $newxml
virsh define $newxml
virsh list --all
}
self_define() {
read -p "请输入新虚机名称:" newname
read -p "请输入新虚机内存大小(G):" newmem
read -p "请输入新虚机cpu个数:" newcpu
sourceimage=/var/lib/libvirt/images/vmmodel.img
sourcexml=/etc/libvirt/qemu/vmmodel.xml
newimg=/var/lib/libvirt/images/${newname}.img
newxml=/etc/libvirt/qemu/${newname}.xml
cp $sourceimage $newimg
cp $sourcexml $newxml
kvmname=$newname
kvmuuid=`uuidgen`
kvmmem=${newmem}000000
kvmcpu=$newcpu
kvmimg=$newimg
kvmmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
sed -i "s@kvmname@$kvmname@;s@kvmuuid@$kvmuuid@;s@kvmmem@$kvmmem@;s@kvmcpu@$kvmcpu@;s@kvmimg@$kvmimg@;s@kvmmac@$kvmmac@" $newxml
virsh define $newxml
virsh list --all
}
case $op in
1)self_define;;
2)
read -p "请输入要创建的虚拟机的个数:" num
read -p "请输入新虚机内存大小(G):" newmem
read -p "请输入新虚机cpu个数:" newcpu
for((i=1;i<=$num;i++))
do
batch_self_define $newmem $newcpu
done;;
3)
read -p "请输入要创建的虚拟机的个数:" num
for((i=1;i<=$num;i++))
do
batch_self_define 1 1
done;;
*)echo "输入错误,请重新执行脚本"
exit;;
esac
更多推荐
所有评论(0)