아래 스크립트는 여러개의 VM을 KVM Host의 쉘에서 Clone 하기 위한 스크립이다.

사전에 kvm 구성 및 template vm에 대한 xml 및 image(qcow2)가 준비되어 있어야 동작된다.

1. clone-vm.sh

#!/bin/bash

######## USER DEFINE DEFAULT VARIABLE ########
ORIGINALVM=template
ORIGINALXMLFILE=/root/template.xml
VMIMAGESAVEDIR=/home/vm-images
DEFAULTMKVMS=10
##############################################

VERSION="0.1"

# Default Options
function defaultopt()
{
    if [ x$ORIGINALVM == x ]; then
        ORIGINALVM=$ORIGINALVM
    fi
    if [ x$ORIGINALXMLFILE == x ]; then
        ORIGINALXMLFILE=$ORIGINALXMLFILE
    fi
    if [ x$VMIMAGESAVEDIR == x ]; then
        VMIMAGESAVEDIR=$VMIMAGESAVEDIR
    fi
    if [ x$DEFAULTMKVMS == x ]; then
        DEFAULTMKVMS=$DEFAULTMKVMS
    fi
}

defaultopt

# Options Function
function usage()
{
    echo -e "\e[34m\e[1m*** The options are :\e[21\033[0m"
    echo "-h or --help    : Help"
    echo "-v or --version : Show script version"
    echo "--source-vm     : Original vm"
    echo "--source-xml    : Source XML file"
    echo "--storage       : Where qcow2 file will be saved"
    echo "--mk-vms        : Number of vm to clone"
    echo ""
    echo -e "\e[34m\e[1m*** Default options are :\e[21\033[0m"
    echo "--source-vm     : $ORIGINALVM"
    echo "--source-xml    : $ORIGINALXMLFILE"
    echo "--storage       : $VMIMAGESAVEDIR"
    echo "--mk-vms        : $DEFAULTMKVMS"
    echo ""
    echo -e "\e[34m\e[1m*** Usage :\e[21\033[0m"
    echo "example 1 : $0"
    echo "            => If no options are set, it is cloned as Default options"
    echo ""
    echo "example 2 : $0 --mk-vms=5"
    echo "            => 5 vm will be cloned. Default values apply to options except the'--mk-vms' option."
    echo ""
    echo "example 3 : $0 --source-vm=template --source-xml=/mnt/rep/template.xml --storage=/mnt/rep --mk-vms=5"
    echo "            => 5 vm will be cloned to /mnt/rep/`hostname` based template vm and /mnt/rep/tmplate.xml"
    echo -e "\033[0m"
}

while [ "$1" != "" ]; do
    PARAM=`echo $1 | awk -F= '{print $1}'`
    VALUE=`echo $1 | awk -F= '{print $2}'`
    case $PARAM in
        -h | --help)
            usage
            exit
            ;;
        -v | --version)
            echo "$0 Version: $VERSION"
            exit
            ;;
        --source-vm)
            ORIGINALVM=$VALUE
            if [ x$ORIGINALVM == x ]; then
                ORIGINALVM=$ORIGINALVM
            fi
            ;;
        --source-xml)
            ORIGINALXMLFILE=$VALUE
            if [ x$ORIGINALXMLFILE == x ]; then
                ORIGINALXMLFILE=$ORIGINALXMLFILE
            fi
            ;;
        --storage)
            VMIMAGESAVEDIR=$VALUE
            if [ x$VMIMAGESAVEDIR == x ]; then
                VMIMAGESAVEDIR=$VMIMAGESAVEDIR
            fi
            ;;
        --mk-vms)
            DEFAULTMKVMS=$VALUE
            if [ x$DEFAULTMKVMS == x ]; then
                DEFAULTMKVMS=$DEFAULTMKVMS
            fi
            ;;
        *)
            echo -e "\033[1;31m\e[5mError\e[25m: unknown option parameter \"$PARAM\"\033[0m"
            echo ""
            usage
            exit 1
            ;;
    esac
    shift
done

if [ ! -d $VMIMAGESAVEDIR/`hostname` ]; then
    mkdir -p $VMIMAGESAVEDIR/`hostname`
fi

ENDVMSNUM=$(ls $VMIMAGESAVEDIR/`hostname`/ | grep 'vm[0-9][0-9][0-9]' | awk -F '.' '{print $1}' | tail -n1 | awk -F 'vm' '{print $2}' | sed 's/^0*//')

NUM=0
for i in $(seq 1 $DEFAULTMKVMS)
    do let ENDVMSNUM+=1
    virt-clone --original $ORIGINALVM --original-xml $ORIGINALXMLFILE --name `printf vm%03d $ENDVMSNUM` --file $VMIMAGESAVEDIR/`hostname`/`printf vm%03d $ENDVMSNUM`.qcow2
    virsh start `printf vm%03d $ENDVMSNUM`
    sleep 1
    echo "==================================================="
    echo ""
    NUM=$((NUM+1));
    if [ $NUM == $DEFAULTMKVMS ]; then
        echo "++++++++++++++++ vm clone finished +++++++++++++++++"
        virsh list --all
        echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
    fi
done

 

2. operate-vm.sh

#!/bin/bash

VERSION="0.1"

# Options Function
function usage()
{
    echo -e "\e[34m\e[1m*** The options are :\e[21\033[0m"
    echo "-h or --help    : Help"
    echo "-v or --version : Show script version"
    echo "--action        : start / shutdown / destroy / undefine"
    echo ""
    echo -e "\e[34m\e[1m*** Usage :\e[21\033[0m"
    echo "example 1 : $0 --action=shutdown 1 10"
    echo "            => vm001 ~ vm010 will be shutdown."
    echo ""
    echo "example 2 : $0 --action=start 3"
    echo "            => vm003 will be started."
    echo ""
    echo "example 3 : $0 --action=destroy 11 13"
    echo "            => vm011 ~ vm013 will be shutdown(power off)."
    echo ""
    echo "example 4 : $0 --action=undefine 10 20"
    echo "            => vm010 ~ vm020 will be remove(delete vm's image data)."
    echo -e "\033[0m"
}

START=$2
END=$3

if [ x$3 == x ]; then
    END=$START
fi

while [ "$1" != "" ]; do
    PARAM=`echo $1 | awk -F= '{print $1}'`
    VALUE=`echo $1 | awk -F= '{print $2}' | awk -F ' ' '{print $1}'`
    case $PARAM in
        -h | --help)
            usage
            exit
            ;;
        -v | --version)
            echo "$0 Version: $VERSION"
            exit
            ;;
        --action)
            ACTION=$VALUE
            if [ x$ACTION == x ]; then
                ACTION=$ACTION
            fi
            ;;
    esac
    shift
done


if [ "$ACTION" = "start" ] || [ "$ACTION" = "shutdown" ] || [ "$ACTION" = "destroy" ] || [ "$ACTION" = "undefine" ]; then
    if [ -z $START ]; then
        echo "You must enter VM Number value."
        echo "Refer to below help, please."
        echo ""
        usage
        exit
    elif [ $END -lt $START ]; then
        echo "You must enter $END value less than $START value."
        echo "Refer to below help, please."
        echo ""
        usage
        exit
    else
	if [ $ACTION = "undefine" ]; then
            while true; do
                read -p "Do you wish to undefine these vm? (*WARN: It will be removed vm's image data. OK?)" yn
                case $yn in
                    [Yy]* ) for i in $(seq -f "%03g" $START $END) ; do virsh destroy vm$i; done; for i in $(seq -f "%03g" $START $END) ; do virsh $ACTION vm$i --remove-all-storage; done ; break;;
                    [Nn]* ) exit;;
                * ) echo "Please answer yes or no.";;
                esac
            done 
            exit
        else
            for i in $(seq -f "%03g" $START $END)
                do virsh $ACTION vm$i
            done
        fi
    fi
else
    echo "You must enter one of the three --action options. Refer to below help, please."
    echo ""
    usage
    echo $START $END
    exit
fi

 

Clone and Operate script for VM on the KVM

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다