From c33f0979f0312e7a6d43645aa0a5d5b1481751f5 Mon Sep 17 00:00:00 2001 From: hc Date: Thu, 2 Jan 2025 09:26:32 +0800 Subject: udpated --- others/vm3/compute/create.sh | 14 ++++++++------ others/vm3/compute/delete.sh | 27 --------------------------- others/vm3/compute/ls.sh | 2 +- others/vm3/compute/rm.sh | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 34 deletions(-) delete mode 100755 others/vm3/compute/delete.sh create mode 100755 others/vm3/compute/rm.sh (limited to 'others/vm3/compute') diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh index a1d84f6..1a0411b 100755 --- a/others/vm3/compute/create.sh +++ b/others/vm3/compute/create.sh @@ -49,8 +49,8 @@ xml="${workingdir}/xml" image_dir_path="/var/lib/libvirt/images/.image_store" src_file="${image_dir_path}/${os}.qcow2" -new_vm_dir="/var/lib/libvirt/images/${vmname}" -new_vm="${new_vm_dir}/${vmname}.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!! @@ -75,7 +75,7 @@ if [ ! -f "${src_file}" ]; then exit 1 fi -sudo mkdir -p $new_vm_dir +sudo mkdir -p $new_vm_config_dir sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } @@ -90,11 +90,12 @@ fi # Define the disk options based on OS type if [ "$os" = "debian12" ]; then - disk_opts="--disk path=$new_vm,format=qcow2" + disk_opts="--disk path=${new_vm},format=qcow2" else - disk_opts="--disk path=$new_vm,format=qcow2 --disk path=$seed_iso,device=cdrom" + disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" fi + # Use the conditional disk options in virt-install sudo virt-install --name $vmname \ --vcpus $vcpu \ @@ -105,7 +106,8 @@ sudo virt-install --name $vmname \ --virt-type kvm \ --graphics none \ --network bridge=virbr0,model=virtio \ - --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; } + --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; } diff --git a/others/vm3/compute/delete.sh b/others/vm3/compute/delete.sh deleted file mode 100755 index dd65379..0000000 --- a/others/vm3/compute/delete.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/ls.sh b/others/vm3/compute/ls.sh index b62ef14..b821fd1 100755 --- a/others/vm3/compute/ls.sh +++ b/others/vm3/compute/ls.sh @@ -27,7 +27,7 @@ for vm in $vms; do sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ & echo "$xml" | grep "]' '{print $3}' > /tmp/vcpus.$$ & echo "$xml" | grep "]' '{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.$$ & + sudo du -sk "/var/lib/libvirt/images/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ & wait diff --git a/others/vm3/compute/rm.sh b/others/vm3/compute/rm.sh new file mode 100755 index 0000000..f77831c --- /dev/null +++ b/others/vm3/compute/rm.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 -- cgit v1.2.3-70-g09d2