1、创建一个基础镜像文件

[root@server1  images]# pwd
/var/lib/libvirt/images              //虚拟机默认创建的位置
[root@server1  images]# qemu-img create -f qcow2 rhel6test.img 80G
Formatting  '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-rhel6 

Startinginstall...
Retrievingfile vmlinuz...                              | 7.7 MB     00:00 ... 
Retrievingfile initrd.img...                           |  60 MB     00:00 ... 
Creatingdomain...                                      |    0 B     00:00    
Domaininstallation still in progress. You can reconnect to 
theconsole to complete the installation process.
[root@server1images]#

注:详细的install-rhel6文件请参考:http://murongqingqqq.blog.51cto.com/2902694/1396080



3、基于基础镜像文件创建增量镜像文件---克隆机器

[root@server1images]#  qemu-img create -b rhel6test.img -f qcow2 rhel6testnode1.img 
Formatting'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.xml

5、修改配置文件,以满足克隆虚拟机的要求

[root@server1images]#  uuidgen 
3b69e07e-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/bash
qemu-img  create -b /var/lib/libvirt/images/rhel6test.img -f qcow2  /var/lib/libvirt/images/rhel6testnode1.img

virsh  dumpxml rhel6test > /tmp/rhel6test.xml

sed  -i '/<name>rhel6test/s/rhel6test/rhel6testnode1/' /tmp/rhel6test.xml
sed  -i "/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/"  /tmp/rhel6test.xml
sed  -i "/rhel6test\.img/s/rhel6test\.img/rhel6testnode1\.img/"  /tmp/rhel6test.xml
sed  -i "/mac address/s/00'/03'/" /tmp/rhel6test.xml

virsh  define /tmp/rhel6test.xml



经过优化后的脚本

实现功能:

1,支持判断用户输入的字符判断,可用范围为(1-254)

2,多余输出信息导入到/dev/null中

3,支持输出错误编码$?

4,如果需要修改原虚拟机名,直接修改$BASE_VM的值即可

5,支出输出信息[OK]高亮显示

[root@server1 ~]# cat  bin/clone-rhel6.sh 
#!/bin/bash

IMG_DIR=/var/lib/libvirt/images           //定义变量,虚拟机文件存放的位置
BASE_VM=rhel6test                    //定义变量,基础虚拟机的名字
E_NOINPUT=65                          //定义输出错误编码
E_NOTNUM=66
E_OUT_OF_RANGE=67
E_VM_EXISTS=68

read -p "please input a vm  number(0-254): " NEW_VMNU                //交互式输入增量虚拟机的编号
if [ -z $NEW_VMNU ]; then                                  //判断编号是否为空
   echo "You must input a number."
   exit $E_NOINPUT
fi

if [ $NEW_VMNU != $(echo  "$NEW_VMNU*1" | bc) ]; then         //判断编号是否为字符
   echo "You must input a number."
   exit $E_NOTNUM
fi

if [ $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_RANGE
fi

NEW_VM=${BASE_VM}node${NEW_VMNU}                    //定义增量虚拟机的名字

if [ -e $IMG_DIR/$NEW_VM.img ]; then                 //判断增量虚拟机编号是否重复
   echo "$NEW_VM already exists"
   exit $E_VM_EXISTS
fi

echo -en "creating disk  image......\t\t"         //-en选项,不换行输出,为了连接下面的ok
qemu-img create -b  $IMG_DIR/$BASE_VM.img -f qcow2 $IMG_DIR/$NEW_VM.img &> /dev/null
echo -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.xml
sed -i  "/$BASE_VM\.img/s/$BASE_VM\.img/$NEW_VM\.img/" /tmp/$BASE_VM.xml
sed -i "/mac  address/s/00'/$MAC_ADDR'/" /tmp/$BASE_VM.xml

virsh define  /tmp/$BASE_VM.xml &> /dev/null               //定义新的虚拟机

echo "$NEW_VM created successful  !!!"
[root@server1 ~]#