summaryrefslogtreecommitdiff
path: root/others/vm3/compute
diff options
context:
space:
mode:
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 @@
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
6# default values
7vcpu=8
8ram_gb=8
9disk_gb=64
10os="fedora40"
11ostype="linux"
12
13# parse arguments
14while [[ $# -gt 0 ]]; do
15 case $1 in
16 --vcpu)
17 vcpu="$2"
18 shift 2
19 ;;
20 --ram)
21 ram_gb="$2"
22 shift 2
23 ;;
24 --disk-size)
25 disk_gb="$2"
26 shift 2
27 ;;
28 --image)
29 os="$2"
30 shift 2
31 ;;
32 *)
33 # Handle positional arguments (vmname and os)
34 if [ -z "$vmname" ]; then
35 vmname="$1"
36 else
37 echo "Unknown argument: $1"
38 exit 1
39 fi
40 shift
41 ;;
42 esac
43done
44
45scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
46workingdir="/var/lib/libvirt/images/.temp"
47seed_iso="${workingdir}/seed.iso"
48xml="${workingdir}/xml"
49
50image_dir_path="/var/lib/libvirt/images/.image_store"
51src_file="${image_dir_path}/${os}.qcow2"
52new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config"
53new_vm="/var/lib/libvirt/images/${vmname}.qcow2"
54
55# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
56# image is os!!
57if [ -z "$vmname" ] ; then
58 echo ""
59 echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]"
60 echo "seed.iso and image file have to be present! Default os is fedora"
61 echo ""
62 echo "Available images:"
63 sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//'
64 echo ""
65 echo "Available images to download:"
66 sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//'
67 exit 1
68fi
69
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}"
72
73if [ ! -f "${src_file}" ]; then
74 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
75 exit 1
76fi
77
78sudo mkdir -p $new_vm_config_dir
79
80sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
81
82if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
83 echo -e "\n$vmname already exist. Delete it before using the same name."
84 exit 1
85fi
86
87if [[ ${os,,} == *"freebsd"* ]]; then
88 ostype="generic"
89fi
90
91# Define the disk options based on OS type
92if [ "$os" = "debian12" ]; then
93 disk_opts="--disk path=${new_vm},format=qcow2"
94 virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname"
95else
96 disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom"
97fi
98
99generate_mac() {
100 printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))
101}
102
103# Use the conditional disk options in virt-install
104sudo virt-install --name $vmname \
105 --vcpus $vcpu \
106 --memory "$((ram_gb * 1024))" \
107 $disk_opts \
108 --os-type $ostype \
109 --os-variant $os \
110 --virt-type kvm \
111 --graphics none \
112 --network bridge=virbr0,model=virtio,mac=$(generate_mac) \
113 --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; }
114# if you want this in a new storage pool, move it to a new storage pool after initialisation
115
116sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
117
118sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
119
120sudo virsh start $vmname
121
122sudo rm "${workingdir}"/*
123
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 @@
1
2#!/bin/bash
3printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name"
4printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------"
5
6# Get all VMs
7vms=$(sudo virsh list --name --all)
8
9# Cache the network leases once
10default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null)
11
12for 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}.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"
45done
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 @@
1#!/bin/bash
2
3# Check if VMs are provided
4if [ $# -eq 0 ]; then
5 echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3"
6 exit 1
7fi
8
9# List VMs to be removed
10echo "The following VMs will be permanently destroyed:"
11for vm in "$@"; do
12 echo "- $vm"
13done
14
15# Confirmation prompt
16read -p "Are you sure you want to remove these VMs? (y/N): " confirmation
17if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then
18 echo "Operation cancelled."
19 exit 1
20fi
21
22for 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")
27done
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 @@
1#!/bin/bash
2
3for 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")
6done
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 @@
1#!/bin/bash
2
3# starts vm
4# remove seed.iso first if vm have the seed iso
5
6vmname=$1
7
8# Get the target device for seed.iso
9target_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#
16if [ ! -z "$target_dev" ]; then
17 sudo virsh detach-disk $vmname $target_dev --config
18fi
19
20sudo virsh start $vmname