summaryrefslogtreecommitdiff
path: root/others/vm3/compute/create.sh
diff options
context:
space:
mode:
Diffstat (limited to 'others/vm3/compute/create.sh')
-rwxr-xr-xothers/vm3/compute/create.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
new file mode 100755
index 0000000..6d83a03
--- /dev/null
+++ b/others/vm3/compute/create.sh
@@ -0,0 +1,60 @@
+#!/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 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>"
+ echo "seed.iso and image file have to be present!"
+ 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_path,size=$disk_gb,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}"/*
+