diff options
| author | Your Name <you@example.com> | 2024-12-21 15:11:19 +0800 |
|---|---|---|
| committer | Your Name <you@example.com> | 2024-12-21 15:11:19 +0800 |
| commit | be3dde6ce14698c5818f21f65037a4beef1199cf (patch) | |
| tree | 79dbd14ec4a07d6b41561931d15a4c03406d5836 /others/vm3/compute/create.sh | |
| parent | 47303626554d42356dfd1b61ad3f9de97929cc87 (diff) | |
betavm3
Diffstat (limited to 'others/vm3/compute/create.sh')
| -rwxr-xr-x | others/vm3/compute/create.sh | 60 |
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 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # takes in vm name, os type, vcpu, ram, disk as argument | ||
| 4 | # takes in already generated seed iso and downloaded vm.iso file | ||
| 5 | vmname=$1 | ||
| 6 | os=$2 | ||
| 7 | vcpu=$3 | ||
| 8 | ram_gb=$4 | ||
| 9 | disk_gb=$5 | ||
| 10 | |||
| 11 | #echo "$vmname $os $vcpu $ram_gb $disk_gb" | ||
| 12 | |||
| 13 | |||
| 14 | if [ $# -ne 5 ]; then | ||
| 15 | echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>" | ||
| 16 | echo "seed.iso and image file have to be present!" | ||
| 17 | exit 1 | ||
| 18 | fi | ||
| 19 | |||
| 20 | scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir | ||
| 21 | workingdir="/var/lib/libvirt/images/.temp" | ||
| 22 | seed_iso="${workingdir}/seed.iso" | ||
| 23 | xml="${workingdir}/xml" | ||
| 24 | |||
| 25 | image_dir_path="/var/lib/libvirt/images/.image_store" | ||
| 26 | src_file="${image_dir_path}/${os}.qcow2" | ||
| 27 | new_vm="/var/lib/libvirt/images/${vmname}.qcow2" | ||
| 28 | |||
| 29 | if [ ! -f "${src_file}" ]; then | ||
| 30 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" | ||
| 31 | exit 1 | ||
| 32 | fi | ||
| 33 | |||
| 34 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } | ||
| 35 | |||
| 36 | if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then | ||
| 37 | echo -e "\n$vmname already exist. Delete it before using the same name." | ||
| 38 | exit 1 | ||
| 39 | fi | ||
| 40 | |||
| 41 | sudo virt-install --name $vmname \ | ||
| 42 | --vcpus $vcpu \ | ||
| 43 | --memory "$((ram_gb * 1024))"\ | ||
| 44 | --disk path=$new_vm_path,size=$disk_gb,format=qcow2 \ | ||
| 45 | --disk path=$seed_iso,device=cdrom \ | ||
| 46 | --os-type linux \ | ||
| 47 | --os-variant $os \ | ||
| 48 | --virt-type kvm \ | ||
| 49 | --graphics none \ | ||
| 50 | --network bridge=virbr0,model=virtio \ | ||
| 51 | --print-xml > $xml || { echo "Failed to print XML."; exit 1; } | ||
| 52 | |||
| 53 | sudo virsh define $xml &> /dev/null || { echo "Failed to define the new VM."; exit 1; } | ||
| 54 | |||
| 55 | sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null | ||
| 56 | |||
| 57 | sudo virsh start $vmname | ||
| 58 | |||
| 59 | sudo rm "${workingdir}"/* | ||
| 60 | |||
