KVM2-使用qcow2管理KVM虚拟机磁盘

本文发布时间: 2019-Mar-22
KVM2-使用qcow2管理KVM虚拟机磁盘1、创建一个基础镜像文件[root@server1 images]# pwd/var/lib/libvirt/images //虚拟机默认创建的位置[root@server1 images]# qemu-img create -f qcow2 rhel6test.img 80GFormatting 'rhel6base.img', fmt=qcow2 size=85899345920 encryption=off cluster_size=65536[root@server1 images]# vim ~/bin/install-rhel6[root@server1 images]# cat ~/bin/install-rhel6#!/bin/bash/usr/sbin/virt-install \--vnc \--noautoconsole \--name=rhel6test \--ram=1024 \--arch=x86_64 \--vcpus=1 \--os-type=linux \--os-variant=rhel6 \--hvm \--accelerate \--disk=/var/lib/libvirt/images/rhel6test.img \-m 52:54:00:00:01:02 \-w bridge=br0 \--location=ftp://192.168.3.1/rhel6 \--extra-args="ks=ftp://192.168.3.1/ks.cfg"[root@server1 images]#2、安装系统到镜像文件[root@server1images]# install-rhel6Startinginstall...Retrievingfile vmlinuz... | 7.7 MB 00:00 ...Retrievingfile initrd.img... | 60 MB 00:00 ...Creatingdomain... | 0 B 00:00Domaininstallation still in progress. You can reconnect totheconsole to complete the installation process.[root@server1images]#注:详细的install-rhel6文件请参考:http://murongqingqqq.blog.51cto.com/2902694/13960803、基于基础镜像文件创建增量镜像文件---克隆机器[root@server1images]# qemu-img create -b rhel6test.img -f qcow2rhel6testnode1.imgFormatting'rhel6testnode1.img', fmt=qcow2 size=85899345920 backing_file='rhel6test.img'encryption=off cluster_size=65536[root@server1images]#4、导出原始虚拟机的配置文件说明查看虚拟机rhel6test的配置信息:[root@server1 images]# virsh dumpxml rhel6test将配置信息作为模板导出:[root@server1images]# virsh dumpxml rhel6test > /tmp/rhel6testnode1.xml5、修改配置文件,以满足克隆虚拟机的要求[root@server1images]# uuidgen3b69e07e-95a9-48eb-b95d-52507ad864e0[root@server1images]# vim /tmp/rhel6testnode1.xml //一共修改四处<name>rhel6testnode1</name><uuid>3b69e07e-95a9-48eb-b95d-52507ad864e0</uuid><source file='/var/lib/libvirt/images/rhel6testnode1.img'/><mac address='52:54:00:00:01:03'/>6、根据配置文件定义新的克隆虚拟机[root@server1images]# virsh define /tmp/rhel6testnode1.xml附:实现自动创建克隆虚拟机的脚本:等同步骤3-6[root@server1 ~]# vim bin/clone-rhel6[root@server1 ~]# cat bin/clone-rhel6#!/bin/bashqemu-img create -b /var/lib/libvirt/images/rhel6test.img -f qcow2 /var/lib/libvirt/images/rhel6testnode1.imgvirsh dumpxml rhel6test > /tmp/rhel6test.xmlsed -i '/<name>rhel6test/s/rhel6test/rhel6testnode1/' /tmp/rhel6test.xmlsed -i "/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/" /tmp/rhel6test.xmlsed -i "/rhel6test\.img/s/rhel6test\.img/rhel6testnode1\.img/" /tmp/rhel6test.xmlsed -i "/mac address/s/00'/03'/" /tmp/rhel6test.xmlvirsh define /tmp/rhel6test.xml经过优化后的脚本实现功能:1,支持判断用户输入的字符判断,可用范围为(1-254)2,多余输出信息导入到/dev/null中3,支持输出错误编码$?4,如果需要修改原虚拟机名,直接修改$BASE_VM的值即可5,支出输出信息[OK]高亮显示[root@server1 ~]# cat bin/clone-rhel6.sh#!/bin/bashIMG_DIR=/var/lib/libvirt/images //定义变量,虚拟机文件存放的位置BASE_VM=rhel6test //定义变量,基础虚拟机的名字E_NOINPUT=65 //定义输出错误编码E_NOTNUM=66E_OUT_OF_RANGE=67E_VM_EXISTS=68read -p "please input a vm number(0-254): " NEW_VMNU //交互式输入增量虚拟机的编号if [ -z $NEW_VMNU ]; then //判断编号是否为空echo "You must input a number."exit $E_NOINPUTfiif [ $NEW_VMNU != $(echo "$NEW_VMNU*1" | bc) ]; then //判断编号是否为字符echo "You must input a number."exit $E_NOTNUMfiif [ $NEW_VMNU -lt 1 -o $NEW_VMNU -gt 254 ]; then //判断编号是否超出了1-254范围echo "You must input a number between 1 and 254"exit $E_OUT_OF_RANGEfiNEW_VM=${BASE_VM}node${NEW_VMNU} //定义增量虚拟机的名字if [ -e $IMG_DIR/$NEW_VM.img ]; then //判断增量虚拟机编号是否重复echo "$NEW_VM already exists"exit $E_VM_EXISTSfiecho -en "creating disk image......\t\t" //-en选项,不换行输出,为了连接下面的okqemu-img create -b $IMG_DIR/$BASE_VM.img -f qcow2 $IMG_DIR/$NEW_VM.img &> /dev/nullecho -e "\e[32m[OK]\e[0m" //高亮显示输出内容:[ok]echo //echo后面不接选项和参数,输出空行virsh dumpxml $BASE_VM > /tmp/$BASE_VM.xml //生成克隆虚拟机XML文件MAC_ADDR=$(echo "obase=16;$NEW_VMNU" | bc) //定义mac地址后两位数字sed -i "/<name>$BASE_VM/s/$BASE_VM/$NEW_VM/" /tmp/$BASE_VM.xml //XML文件需要修改的四处sed -i "/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/" /tmp/$BASE_VM.xmlsed -i "/$BASE_VM\.img/s/$BASE_VM\.img/$NEW_VM\.img/" /tmp/$BASE_VM.xmlsed -i "/mac address/s/00'/$MAC_ADDR'/" /tmp/$BASE_VM.xmlvirsh define /tmp/$BASE_VM.xml &> /dev/null //定义新的虚拟机echo "$NEW_VM created successful !!!"[root@server1 ~]#注:其实核心句子就是绿色字体的四个步骤


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2020-Jul-13 01:14am
栏目列表