#!/bin/bash # takes in vm name, os type, vcpu, ram, disk as argument # takes in already generated seed iso and downloaded vm.iso file vmname=$1 os=$2 vcpu=$3 ram_gb=$4 disk_gb=$5 #echo "$vmname $os $vcpu $ram_gb $disk_gb" if [ $# -ne 5 ]; then echo "Usage: $0 " echo "seed.iso and image file have to be present!" echo "" echo "Available images" sudo ls /var/lib/libvirt/images/.image_store exit 1 fi scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir workingdir="/var/lib/libvirt/images/.temp" seed_iso="${workingdir}/seed.iso" xml="${workingdir}/xml" image_dir_path="/var/lib/libvirt/images/.image_store" src_file="${image_dir_path}/${os}.qcow2" new_vm="/var/lib/libvirt/images/${vmname}.qcow2" if [ ! -f "${src_file}" ]; then echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" exit 1 fi sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then echo -e "\n$vmname already exist. Delete it before using the same name." exit 1 fi sudo virt-install --name $vmname \ --vcpus $vcpu \ --memory "$((ram_gb * 1024))"\ --disk path=$new_vm,format=qcow2 \ --disk path=$seed_iso,device=cdrom \ --os-type linux \ --os-variant $os \ --virt-type kvm \ --graphics none \ --network bridge=virbr0,model=virtio \ --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; } sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null sudo virsh start $vmname #sudo rm "${workingdir}"/*