Tömeges VM létrehozó szkript

Minta konfig fájl:

#OVS_NAME       REPO_NAME               POOL_NAME       VM_NAME         MEMORY_GB       VCPU_COUNT      BOOT_DISK_GB    CD_NAME         NETWORK_NAME
#!/bin/bash
. ovm_functions.sh
kulcs_ellenorzes

[[ -e $1 ]] || { echo "Add meg a konfig fajlt parameterkent."; exit 1; }
        while read LINE; do
        OVS_NAME=$(echo $LINE | cut -d' ' -f 1)
        REPO_NAME=$(echo $LINE | cut -d' ' -f 2)
        POOL_NAME=$(echo $LINE | cut -d' ' -f 3)
        VM_NAME=$(echo $LINE | cut -d' ' -f 4)
        MEMORY_GB=$(echo $LINE | cut -d' ' -f 5)
        VCPU_COUNT=$(echo $LINE | cut -d' ' -f 6)
        BOOT_DISK_GB=$(echo $LINE | cut -d' ' -f 7)
        CD_NAME=$(echo $LINE | cut -d' ' -f 8)
        NETWORK_NAME=$(echo $LINE | cut -d' ' -f 9)
        echo "###### $VM_NAME letrehozasa ######"
        echo "Ellenorizd a parametereket:"
        echo -e "OVS: ${grn}${OVS_NAME}${end}, Repo: ${grn}${REPO_NAME}${end}, Pool: ${grn}${POOL_NAME}${end}, VM neve: ${grn}${VM_NAME}${end},"
        echo -e "Memoria [GB]: ${grn}${MEMORY_GB}${end}, vCPU szam: ${grn}${VCPU_COUNT}${end}, Boot disk [GB]: ${grn}${BOOT_DISK_GB}${end}, Boot CD image: ${grn}${CD_NAME}${end}, Network: ${grn}${NETWORK_NAME}${end}"
# Megnezzuk, hogy ha letrehozzuk a vdisk-et, nem megy-e a repo meret 90% fole
        REPO_TOTAL_SIZE=$(ovm_exec "show repository name=$REPO_NAME" "File System Total \(GiB\)" | sed 's/\..*$//')
        REPO_USED_SIZE=$(ovm_exec "show repository name=$REPO_NAME" "File System Used \(GiB\)" | sed 's/\..*$//')
        REPO_USAGE_IF=$(echo "scale=2;($REPO_USED_SIZE + $BOOT_DISK_GB) / $REPO_TOTAL_SIZE * 100 / 1" | bc)
        REPO_USAGE_IF=$(echo $REPO_USAGE_IF/1 | bc)
        if [[ $REPO_USAGE_IF -gt 90 ]]; then
                echo -e "${red}A vdisk letrehozasaval a repo telitettsege 90% fole menne (${REPO_USAGE_IF}%), es az OCFS2 6% metaadatot is foglalhat.${end}"
                exit 1
        else
                echo -e "Repo telitettseg az uj vdisk letrehozasa utan: ${REPO_USAGE_IF}%"
        fi

        echo "Ha ez nem stimmel, nyomj egy CTRL+C -t 10mp-en belül!"
        echo "-----------------"
        sleep 15
        VM_ID=$(ovm_exec "create Vm name=\"$VM_NAME\" repository=\"$REPO_NAME\" domainType=XEN_HVM_PV_DRIVERS server=\"$OVS_NAME\" startPolicy=CURRENT_SERVER highAvailability=No restartActionOnCrash=STOP osType=\"Red Hat Enterprise Linux 7\" on ServerPool name=\"$POOL_NAME\"")
        [[ -z $VM_ID ]] && { echo -e "${red}create Vm HIBA!${end}"; continue; } ||echo "VM Id: ${grn}$VM_ID ${end}"
        VDISK_ID=$(ovm_exec "create VirtualDisk name=\"${VM_NAME}_rootvg\" size=$BOOT_DISK_GB sparse=No shareable=No on Repository name=\"$REPO_NAME\"")
        [[ -z $VDISK_ID ]] && { echo -e "${red}create VirtualDisk HIBA!${end}"; continue; } || echo "Vdisk Id: ${grn}$VDISK_ID ${end}"
        VDISK_MAP1=$(ovm_exec "create VmDiskMapping slot=0 virtualDisk=\"$VDISK_ID\" name=\"Boot Disk\" on Vm id=\"$VM_ID\"" "Status")
        [[ -z $VDISK_MAP1 ]] && { echo -e "${red}create VmDiskMapping HIBA!${end}"; continue; } || echo "Vdisk mapping: ${grn}$VDISK_MAP1 ${end}"
        VDISK_MAP2=$(ovm_exec "create VmDiskMapping slot=1 virtualCd=\"$CD_NAME\" name=\"CDROM Drive\" on Vm id=\"$VM_ID\"" "Status")
        [[ -z $VDISK_MAP2 ]] && { echo -e "${red}create VmDiskMapping HIBA!${end}"; continue; } || echo "CD mapping: ${grn}$VDISK_MAP2 ${end}"
        VNIC_ID=$(ovm_exec "create Vnic name=\"${VM_NAME}_vnic\" network=\"$NETWORK_NAME\" on Vm id=\"$VM_ID\"")
        [[ -z $VNIC_ID ]] && { echo -e "${red}create Vnic HIBA!${end}"; continue; } || echo "create Vnic: ${grn}$VNIC_ID ${end}"
        VNIC_MAC=$(ovm_exec "show vnic id=\"$VNIC_ID\"" "Mac Address")
        [[ -z $VNIC_ID ]] && { echo -e "${red}show Vnic HIBA!${end}"; continue; } || echo "Vnic MAC: ${grn}$VNIC_MAC ${end}"
        VNIC_ID=$(ovm_exec "edit vnic id=\"$VNIC_ID\" name=\"$VNIC_MAC\"" "Status")
        [[ -z $VNIC_ID ]] && { echo -e "${red}edit Vnic HIBA!${end}"; } || echo "edit Vnic: ${grn}$VNIC_ID ${end}"
        VM_EDIT=$(ovm_exec "edit Vm id=\"$VM_ID\" bootOrder='DISK,CDROM' memory=$(($MEMORY_GB * 1024)) memoryLimit=$(($MEMORY_GB*1024)) cpuCount=$VCPU_COUNT cpuCountLimit=$VCPU_COUNT" "Status")
        [[ -z $VM_EDIT ]] && { echo -e "${red}edit Vm HIBA!${end}"; } || echo "edit Vm: ${grn}$VM_EDIT ${end}"
        echo "###### A $VM_NAME kesz. ######"
        echo "10mp varakozas..."
        sleep 10
done <<< "`cat $1 | grep -v '^#' | sed 's/[[:space:]]\+/ /g' | tr -s '[:blank:]'`"