一 准备模板主机和XML文件 /var/lib/libvirt/images/下
.ce7_template.img .centos7.xml
二 编写克隆脚本clone-centos7 /var/lib/libvirt/images/bin/下
#!/bin/bash
#65 -> user input nothing
#66 -> user input is not a number
#67 -> user input out of range
#68 -> vm disk image exists
IMG_DIR=/var/lib/libvirt/images //定义变量IMG_DIR--虚拟机目录
BASEVM=ce7_template //定义变量BASEVM--虚拟主机模板
read -p "Enter VM number: " VMNUM
if [ $VMNUM -le 9 ];then
VMNUM=0$VMNUM //编号小于9,前面加0,保持两位
fi
if [ -z "${VMNUM}" ]; then
echo "You must input a number." //输入为空值时的输出结果
exit 65
elif [ $(echo ${VMNUM}*1 | bc) = 0 ]; then
echo "You must input a number." //输入为0时的输出结果
exit 66
elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then
echo "Input out of range"