summaryrefslogtreecommitdiff
path: root/others/vm3/compute
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-12-22 19:03:49 +0800
committerYour Name <you@example.com>2024-12-22 19:03:49 +0800
commitf7f159a04671690786de2f84e34046c103521d58 (patch)
tree22b45e904c1aa6800250ec6e998d0022fd018ade /others/vm3/compute
parent2916e3eff503473f6bf8b90e0921b0ccd347166f (diff)
vm.shupdate
Diffstat (limited to 'others/vm3/compute')
-rwxr-xr-xothers/vm3/compute/create.sh68
-rwxr-xr-xothers/vm3/compute/delete.sh27
-rwxr-xr-xothers/vm3/compute/list.sh45
-rwxr-xr-xothers/vm3/compute/shutdown.sh6
-rwxr-xr-x[-rw-r--r--]others/vm3/compute/start.sh18
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 @@
# 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"
+# default values
+vcpu=8
+ram_gb=4
+disk_gb=32
+os="fedora40.qcow2"
+# parse arguments
+while [[ $# -gt 0 ]]; do
+ case $1 in
+ --vcpu)
+ vcpu="$2"
+ shift 2
+ ;;
+ --ram)
+ ram_gb="$2"
+ shift 2
+ ;;
+ --disk-size)
+ disk_gb="$2"
+ shift 2
+ ;;
+ --image)
+ os="$2"
+ shift 2
+ ;;
+ *)
+ # Handle positional arguments (vmname and os)
+ if [ -z "$vmname" ]; then
+ vmname="$1"
+ else
+ echo "Unknown argument: $1"
+ exit 1
+ fi
+ shift
+ ;;
+ esac
+done
-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!"
+# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
+# image is os!!
+if [ -z "$vmname" ] ; then
+ echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]"
+ echo "seed.iso and image file have to be present! Default os is fedora"
echo ""
- echo "Available images"
+ echo "Available images:"
sudo ls /var/lib/libvirt/images/.image_store
exit 1
fi
@@ -26,14 +58,20 @@ 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"
+src_file="${image_dir_path}/${os}"
+new_vm_dir="/var/lib/libvirt/images/${vmname}"
+new_vm="${new_vm_dir}/${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
+# run the script to make the cloud init files
+sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}"
+
+sudo mkdir -p $new_vm_dir
+
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
@@ -47,11 +85,11 @@ sudo virt-install --name $vmname \
--disk path=$new_vm,format=qcow2 \
--disk path=$seed_iso,device=cdrom \
--os-type linux \
- --os-variant $os \
+ --os-variant $(basename "$os" .qcow2) \
--virt-type kvm \
--graphics none \
--network bridge=virbr0,model=virtio \
- --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; }
+ --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; }
sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
@@ -59,5 +97,5 @@ sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
sudo virsh start $vmname
-#sudo rm "${workingdir}"/*
+sudo rm "${workingdir}"/*
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 @@
+#!/bin/bash
+
+# Check if VMs are provided
+if [ $# -eq 0 ]; then
+ echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3"
+ exit 1
+fi
+
+# List VMs to be removed
+echo "The following VMs will be permanently destroyed:"
+for vm in "$@"; do
+ echo "- $vm"
+done
+
+# Confirmation prompt
+read -p "Are you sure you want to remove these VMs? (y/N): " confirmation
+if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then
+ echo "Operation cancelled."
+ exit 1
+fi
+
+for vm in "$@"; do
+ sudo virsh destroy "$vm" &>/dev/null
+ sudo virsh undefine "$vm" --remove-all-storage &>/dev/null
+ sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null
+ printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
+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 @@
+
+#!/bin/bash
+printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name"
+printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------"
+
+# Get all VMs
+vms=$(sudo virsh list --name --all)
+
+# Cache the network leases once
+default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null)
+
+for vm in $vms; do
+ # Get XML once and use it multiple times
+ xml=$(sudo virsh dumpxml "$vm" 2>/dev/null)
+
+ # Extract all data from the cached XML
+ mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}')
+ net=$(echo "$xml" | grep "<source network" | awk -F\' '{print $2}')
+ if [ -z "$net" ]; then
+ net="default"
+ fi
+
+ # Use cached leases
+ ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/')
+
+ # Run commands in background and save to temp files
+ sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ &
+ echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ &
+ echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ &
+ sudo du -sk "/var/lib/libvirt/images/${vm}/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ &
+
+ wait
+
+ # Read from temp files
+ state=$(cat /tmp/state.$$ 2>/dev/null)
+ vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null)
+ ram=$(cat /tmp/ram.$$ 2>/dev/null)
+ disk=$(cat /tmp/disk.$$ 2>/dev/null)
+
+ # Clean up temp files
+ rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$
+
+ printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \
+ "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm"
+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 @@
+#!/bin/bash
+
+for vm in "$@"; do
+ sudo virsh shutdown "$vm" > /dev/null 2>&1
+ printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
+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 @@
#!/bin/bash
# starts vm
-# need to remove seed.iso first if vm have the seed iso
+# remove seed.iso first if vm have the seed iso
+
+vmname=$1
+
+# Get the target device for seed.iso
+target_dev=$(sudo virsh domblklist $vmname | grep 'seed.iso' | awk '{print $1}')
+
+# If seed.iso is found, detach it,
+# --config means it is persistent but do not affect the state now.
+# --config --live means online remove
+# no option means that the changes are not persistent
+#
+if [ ! -z "$target_dev" ]; then
+ sudo virsh detach-disk $vmname $target_dev --config
+fi
+
+sudo virsh start $vmname