summaryrefslogtreecommitdiff
path: root/others/vm3/compute
diff options
context:
space:
mode:
authorroot <root@sg1.noml.ch>2025-05-22 18:36:01 +0800
committerroot <root@sg1.noml.ch>2025-05-22 18:36:01 +0800
commit3a52e92fbbbab4df949740f89a389d68281a9428 (patch)
tree5cb3b864eb0e3f3b90a1efd9cbdee1fb9f158f4a /others/vm3/compute
parent8cb260002db7c46ea5fe2c3f589b485e5f5d970d (diff)
Diffstat (limited to 'others/vm3/compute')
-rwxr-xr-xothers/vm3/compute/create.sh123
-rwxr-xr-xothers/vm3/compute/ls.sh45
-rwxr-xr-xothers/vm3/compute/rm.sh27
-rwxr-xr-xothers/vm3/compute/shutdown.sh6
-rwxr-xr-xothers/vm3/compute/start.sh20
5 files changed, 0 insertions, 221 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
deleted file mode 100755
index ab481cd..0000000
--- a/others/vm3/compute/create.sh
+++ /dev/null
@@ -1,123 +0,0 @@
-#!/bin/bash
-
-# takes in vm name, os type, vcpu, ram, disk as argument
-# takes in already generated seed iso and downloaded vm.iso file
-
-# default values
-vcpu=8
-ram_gb=8
-disk_gb=64
-os="fedora40"
-ostype="linux"
-
-# 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
-
-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_config_dir="/var/lib/libvirt/images/${vmname}.config"
-new_vm="/var/lib/libvirt/images/${vmname}.qcow2"
-
-# 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 ""
- echo "Usage: $0 <vm-name> [--image <os>] [--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:"
- sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//'
- echo ""
- echo "Available images to download:"
- sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//'
- exit 1
-fi
-
-# run the script to make the cloud init files
-sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}"
-
-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 mkdir -p $new_vm_config_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
- echo -e "\n$vmname already exist. Delete it before using the same name."
- exit 1
-fi
-
-if [[ ${os,,} == *"freebsd"* ]]; then
- ostype="generic"
-fi
-
-# Define the disk options based on OS type
-if [ "$os" = "debian12" ]; then
- disk_opts="--disk path=${new_vm},format=qcow2"
- virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname"
-else
- disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom"
-fi
-
-generate_mac() {
- printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))
-}
-
-# Use the conditional disk options in virt-install
-sudo virt-install --name $vmname \
- --vcpus $vcpu \
- --memory "$((ram_gb * 1024))" \
- $disk_opts \
- --os-type $ostype \
- --os-variant $os \
- --virt-type kvm \
- --graphics none \
- --network bridge=virbr0,model=virtio,mac=$(generate_mac) \
- --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; }
-# if you want this in a new storage pool, move it to a new storage pool after initialisation
-
-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}"/*
-
diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh
deleted file mode 100755
index b821fd1..0000000
--- a/others/vm3/compute/ls.sh
+++ /dev/null
@@ -1,45 +0,0 @@
-
-#!/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}.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/rm.sh b/others/vm3/compute/rm.sh
deleted file mode 100755
index f77831c..0000000
--- a/others/vm3/compute/rm.sh
+++ /dev/null
@@ -1,27 +0,0 @@
-#!/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/shutdown.sh b/others/vm3/compute/shutdown.sh
deleted file mode 100755
index 081499b..0000000
--- a/others/vm3/compute/shutdown.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/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
deleted file mode 100755
index d64dbb1..0000000
--- a/others/vm3/compute/start.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-
-# starts vm
-# 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