diff options
Diffstat (limited to 'others/vm3/compute')
| -rwxr-xr-x | others/vm3/compute/create.sh | 68 | ||||
| -rwxr-xr-x | others/vm3/compute/delete.sh | 27 | ||||
| -rwxr-xr-x | others/vm3/compute/list.sh | 45 | ||||
| -rwxr-xr-x | others/vm3/compute/shutdown.sh | 6 | ||||
| -rwxr-xr-x[-rw-r--r--] | others/vm3/compute/start.sh | 18 |
5 files changed, 148 insertions, 16 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh index 649cd86..0bb2e63 100755 --- a/others/vm3/compute/create.sh +++ b/others/vm3/compute/create.sh | |||
| @@ -2,20 +2,52 @@ | |||
| 2 | 2 | ||
| 3 | # takes in vm name, os type, vcpu, ram, disk as argument | 3 | # takes in vm name, os type, vcpu, ram, disk as argument |
| 4 | # takes in already generated seed iso and downloaded vm.iso file | 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 | 5 | ||
| 11 | #echo "$vmname $os $vcpu $ram_gb $disk_gb" | 6 | # default values |
| 7 | vcpu=8 | ||
| 8 | ram_gb=4 | ||
| 9 | disk_gb=32 | ||
| 10 | os="fedora40.qcow2" | ||
| 12 | 11 | ||
| 12 | # parse arguments | ||
| 13 | while [[ $# -gt 0 ]]; do | ||
| 14 | case $1 in | ||
| 15 | --vcpu) | ||
| 16 | vcpu="$2" | ||
| 17 | shift 2 | ||
| 18 | ;; | ||
| 19 | --ram) | ||
| 20 | ram_gb="$2" | ||
| 21 | shift 2 | ||
| 22 | ;; | ||
| 23 | --disk-size) | ||
| 24 | disk_gb="$2" | ||
| 25 | shift 2 | ||
| 26 | ;; | ||
| 27 | --image) | ||
| 28 | os="$2" | ||
| 29 | shift 2 | ||
| 30 | ;; | ||
| 31 | *) | ||
| 32 | # Handle positional arguments (vmname and os) | ||
| 33 | if [ -z "$vmname" ]; then | ||
| 34 | vmname="$1" | ||
| 35 | else | ||
| 36 | echo "Unknown argument: $1" | ||
| 37 | exit 1 | ||
| 38 | fi | ||
| 39 | shift | ||
| 40 | ;; | ||
| 41 | esac | ||
| 42 | done | ||
| 13 | 43 | ||
| 14 | if [ $# -ne 5 ]; then | 44 | # Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null |
| 15 | echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>" | 45 | # image is os!! |
| 16 | echo "seed.iso and image file have to be present!" | 46 | if [ -z "$vmname" ] ; then |
| 47 | echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]" | ||
| 48 | echo "seed.iso and image file have to be present! Default os is fedora" | ||
| 17 | echo "" | 49 | echo "" |
| 18 | echo "Available images" | 50 | echo "Available images:" |
| 19 | sudo ls /var/lib/libvirt/images/.image_store | 51 | sudo ls /var/lib/libvirt/images/.image_store |
| 20 | exit 1 | 52 | exit 1 |
| 21 | fi | 53 | fi |
| @@ -26,14 +58,20 @@ seed_iso="${workingdir}/seed.iso" | |||
| 26 | xml="${workingdir}/xml" | 58 | xml="${workingdir}/xml" |
| 27 | 59 | ||
| 28 | image_dir_path="/var/lib/libvirt/images/.image_store" | 60 | image_dir_path="/var/lib/libvirt/images/.image_store" |
| 29 | src_file="${image_dir_path}/${os}.qcow2" | 61 | src_file="${image_dir_path}/${os}" |
| 30 | new_vm="/var/lib/libvirt/images/${vmname}.qcow2" | 62 | new_vm_dir="/var/lib/libvirt/images/${vmname}" |
| 63 | new_vm="${new_vm_dir}/${vmname}.qcow2" | ||
| 31 | 64 | ||
| 32 | if [ ! -f "${src_file}" ]; then | 65 | if [ ! -f "${src_file}" ]; then |
| 33 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" | 66 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" |
| 34 | exit 1 | 67 | exit 1 |
| 35 | fi | 68 | fi |
| 36 | 69 | ||
| 70 | # run the script to make the cloud init files | ||
| 71 | sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}" | ||
| 72 | |||
| 73 | sudo mkdir -p $new_vm_dir | ||
| 74 | |||
| 37 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } | 75 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } |
| 38 | 76 | ||
| 39 | if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then | 77 | if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then |
| @@ -47,11 +85,11 @@ sudo virt-install --name $vmname \ | |||
| 47 | --disk path=$new_vm,format=qcow2 \ | 85 | --disk path=$new_vm,format=qcow2 \ |
| 48 | --disk path=$seed_iso,device=cdrom \ | 86 | --disk path=$seed_iso,device=cdrom \ |
| 49 | --os-type linux \ | 87 | --os-type linux \ |
| 50 | --os-variant $os \ | 88 | --os-variant $(basename "$os" .qcow2) \ |
| 51 | --virt-type kvm \ | 89 | --virt-type kvm \ |
| 52 | --graphics none \ | 90 | --graphics none \ |
| 53 | --network bridge=virbr0,model=virtio \ | 91 | --network bridge=virbr0,model=virtio \ |
| 54 | --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; } | 92 | --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; } |
| 55 | 93 | ||
| 56 | sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } | 94 | sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } |
| 57 | 95 | ||
| @@ -59,5 +97,5 @@ sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null | |||
| 59 | 97 | ||
| 60 | sudo virsh start $vmname | 98 | sudo virsh start $vmname |
| 61 | 99 | ||
| 62 | #sudo rm "${workingdir}"/* | 100 | sudo rm "${workingdir}"/* |
| 63 | 101 | ||
diff --git a/others/vm3/compute/delete.sh b/others/vm3/compute/delete.sh new file mode 100755 index 0000000..dd65379 --- /dev/null +++ b/others/vm3/compute/delete.sh | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # Check if VMs are provided | ||
| 4 | if [ $# -eq 0 ]; then | ||
| 5 | echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3" | ||
| 6 | exit 1 | ||
| 7 | fi | ||
| 8 | |||
| 9 | # List VMs to be removed | ||
| 10 | echo "The following VMs will be permanently destroyed:" | ||
| 11 | for vm in "$@"; do | ||
| 12 | echo "- $vm" | ||
| 13 | done | ||
| 14 | |||
| 15 | # Confirmation prompt | ||
| 16 | read -p "Are you sure you want to remove these VMs? (y/N): " confirmation | ||
| 17 | if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then | ||
| 18 | echo "Operation cancelled." | ||
| 19 | exit 1 | ||
| 20 | fi | ||
| 21 | |||
| 22 | for vm in "$@"; do | ||
| 23 | sudo virsh destroy "$vm" &>/dev/null | ||
| 24 | sudo virsh undefine "$vm" --remove-all-storage &>/dev/null | ||
| 25 | sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null | ||
| 26 | printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") | ||
| 27 | done | ||
diff --git a/others/vm3/compute/list.sh b/others/vm3/compute/list.sh new file mode 100755 index 0000000..b62ef14 --- /dev/null +++ b/others/vm3/compute/list.sh | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | |||
| 2 | #!/bin/bash | ||
| 3 | printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name" | ||
| 4 | printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------" | ||
| 5 | |||
| 6 | # Get all VMs | ||
| 7 | vms=$(sudo virsh list --name --all) | ||
| 8 | |||
| 9 | # Cache the network leases once | ||
| 10 | default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null) | ||
| 11 | |||
| 12 | for vm in $vms; do | ||
| 13 | # Get XML once and use it multiple times | ||
| 14 | xml=$(sudo virsh dumpxml "$vm" 2>/dev/null) | ||
| 15 | |||
| 16 | # Extract all data from the cached XML | ||
| 17 | mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}') | ||
| 18 | net=$(echo "$xml" | grep "<source network" | awk -F\' '{print $2}') | ||
| 19 | if [ -z "$net" ]; then | ||
| 20 | net="default" | ||
| 21 | fi | ||
| 22 | |||
| 23 | # Use cached leases | ||
| 24 | ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/') | ||
| 25 | |||
| 26 | # Run commands in background and save to temp files | ||
| 27 | sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ & | ||
| 28 | echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ & | ||
| 29 | echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ & | ||
| 30 | sudo du -sk "/var/lib/libvirt/images/${vm}/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ & | ||
| 31 | |||
| 32 | wait | ||
| 33 | |||
| 34 | # Read from temp files | ||
| 35 | state=$(cat /tmp/state.$$ 2>/dev/null) | ||
| 36 | vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null) | ||
| 37 | ram=$(cat /tmp/ram.$$ 2>/dev/null) | ||
| 38 | disk=$(cat /tmp/disk.$$ 2>/dev/null) | ||
| 39 | |||
| 40 | # Clean up temp files | ||
| 41 | rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$ | ||
| 42 | |||
| 43 | printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \ | ||
| 44 | "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm" | ||
| 45 | done | ||
diff --git a/others/vm3/compute/shutdown.sh b/others/vm3/compute/shutdown.sh new file mode 100755 index 0000000..081499b --- /dev/null +++ b/others/vm3/compute/shutdown.sh | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | for vm in "$@"; do | ||
| 4 | sudo virsh shutdown "$vm" > /dev/null 2>&1 | ||
| 5 | printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") | ||
| 6 | done | ||
diff --git a/others/vm3/compute/start.sh b/others/vm3/compute/start.sh index 5a2b3aa..d64dbb1 100644..100755 --- a/others/vm3/compute/start.sh +++ b/others/vm3/compute/start.sh | |||
| @@ -1,4 +1,20 @@ | |||
| 1 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | 2 | ||
| 3 | # starts vm | 3 | # starts vm |
| 4 | # need to remove seed.iso first if vm have the seed iso | 4 | # remove seed.iso first if vm have the seed iso |
| 5 | |||
| 6 | vmname=$1 | ||
| 7 | |||
| 8 | # Get the target device for seed.iso | ||
| 9 | target_dev=$(sudo virsh domblklist $vmname | grep 'seed.iso' | awk '{print $1}') | ||
| 10 | |||
| 11 | # If seed.iso is found, detach it, | ||
| 12 | # --config means it is persistent but do not affect the state now. | ||
| 13 | # --config --live means online remove | ||
| 14 | # no option means that the changes are not persistent | ||
| 15 | # | ||
| 16 | if [ ! -z "$target_dev" ]; then | ||
| 17 | sudo virsh detach-disk $vmname $target_dev --config | ||
| 18 | fi | ||
| 19 | |||
| 20 | sudo virsh start $vmname | ||
