diff options
| author | Your Name <you@example.com> | 2024-12-22 19:03:49 +0800 |
|---|---|---|
| committer | Your Name <you@example.com> | 2024-12-22 19:03:49 +0800 |
| commit | f7f159a04671690786de2f84e34046c103521d58 (patch) | |
| tree | 22b45e904c1aa6800250ec6e998d0022fd018ade | |
| parent | 2916e3eff503473f6bf8b90e0921b0ccd347166f (diff) | |
vm.shupdate
| -rwxr-xr-x | others/vm3/.config/cloud-init-files/fedora40.sh (renamed from others/vm3/image/fedora.sh) | 0 | ||||
| -rw-r--r-- | others/vm3/.docs.swp | bin | 0 -> 12288 bytes | |||
| -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 | ||||
| -rw-r--r-- | others/vm3/docs | 32 | ||||
| -rwxr-xr-x | others/vm3/test.sh | 49 | ||||
| -rwxr-xr-x[-rw-r--r--] | others/vm3/vm.sh | 148 |
10 files changed, 335 insertions, 58 deletions
diff --git a/others/vm3/image/fedora.sh b/others/vm3/.config/cloud-init-files/fedora40.sh index cd05e10..cd05e10 100755 --- a/others/vm3/image/fedora.sh +++ b/others/vm3/.config/cloud-init-files/fedora40.sh diff --git a/others/vm3/.docs.swp b/others/vm3/.docs.swp Binary files differnew file mode 100644 index 0000000..849e1f3 --- /dev/null +++ b/others/vm3/.docs.swp 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 diff --git a/others/vm3/docs b/others/vm3/docs index 8423f1c..c274dd3 100644 --- a/others/vm3/docs +++ b/others/vm3/docs @@ -10,9 +10,9 @@ vm storage create <name2> /dir/ vm/ - ./vm # Single entry point. one vm at a time(except for compute delete-all) + ./vm.sh # Single entry point. one vm at a time(except for compute delete-all) .config/ # all config files - init/ + init/ # run this everytime. try to think of a way to set a flag that says this device has been initialised install_packages.sh make_rootkey.sh # in /root/k/k1. can also add keys in here /var/lib/libvirt/images/.image_store/ # stores the images @@ -22,23 +22,23 @@ vm/ opensuse.sh debian.sh freebsd.sh + list-all-images.sh compute/ create.sh # default vcpu ram disk storage pool network # allow specification of storage pool and network. # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh delete.sh and all associated virtual machines and block devices on all storage device - delete-all.sh start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore. shutdown.sh - list.sh # list by ip address, (sorted by) network group, then name, then cpu ram disk, total disk(incl all attachedvols) + list.sh # list by (sorted by) network group, ip address, then cpu ram disk, total disk(incl all attachedvols), name network/ attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach list.sh detach.sh create.sh # allow specifying the ip address range. auto make uuid delete.sh - storage/ + disk/ attach.sh detach.sh create.sh @@ -52,4 +52,24 @@ vm/ attach.sh detach.sh view.sh # usbs of all the vms - + +# No arguments shows available options +./vm compute create +Available networks: + prod-net 192.168.1.0/24 + dev-net 192.168.2.0/24 + test-net 192.168.3.0/24 + +Available storage pools: + prod-store 2TB free + dev-store 500GB free + backup 1TB free + +Available images: + fedora40(41) + + +Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool prod-store --network prod-net + +# Then use directly +./vm compute create myvm fedora 2 4 20 prod-net prod-store diff --git a/others/vm3/test.sh b/others/vm3/test.sh new file mode 100755 index 0000000..0dafc23 --- /dev/null +++ b/others/vm3/test.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Default values +vcpu=2 +ram_gb=4 +disk_gb=20 + +# Parse named 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 + ;; + *) + # Handle positional arguments (vmname and os) + if [ -z "$vmname" ]; then + vmname="$1" + elif [ -z "$os" ]; then + os="$1" + else + echo "Unknown argument: $1" + exit 1 + fi + shift + ;; + esac +done + +# Check mandatory arguments +if [ -z "$vmname" ] || [ -z "$os" ]; then + echo "Usage: $0 <vm-name> <os> [--vcpu N] [--ram N] [--disk-size N]" + exit 1 +fi + +# Now you can use the variables +echo "VM Name: $vmname" +echo "OS: $os" +echo "VCPU: $vcpu" +echo "RAM: $ram_gb GB" +echo "Disk: $disk_gb GB" diff --git a/others/vm3/vm.sh b/others/vm3/vm.sh index 1072569..30168ba 100644..100755 --- a/others/vm3/vm.sh +++ b/others/vm3/vm.sh @@ -1,42 +1,118 @@ #!/bin/bash +# Main entry point for VM management +if [ "$(id -u)" != "0" ]; then + echo "This script must be run as root" + exit 1 +fi -case "$1" in - "compute") - shift - case "$1" in - "create") - VM_NAME=$2 - shift 2 - while [[ $# -gt 0 ]]; do - case "$1" in - -vcpu) - VCPU=$2 - shift 2 - ;; - -ram) - RAM_GB=$(($2*1000)) - shift 2 - ;; - -disk) - DISK_GB=$2 - shift 2 - ;; - *) - echo "Unknown argument: $1" - exit 1 - ;; - esac - done - ;; - *) - echo "Unknown compute command: $1" - exit 1 - ;; - esac - ;; - *) - echo "Unknown command: $1" +# Get script directory for relative paths +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# First argument is the category (compute, network, storage, etc) +category=$1 +shift + +# Exit if no category specified +if [ -z "$category" ]; then + echo -e "Usage: ./vm <category> <action> [args...]\n" + echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n" + echo "Run ./vm <category> for available subactions or tree for all available actions." + exit 1 +fi + +# Second argument is the action +action=$1 +shift + +# Handle each category +case $category in + compute) + case $action in + create) + $SCRIPT_DIR/compute/create.sh "$@" + ;; + start) + $SCRIPT_DIR/compute/start.sh "$@" + ;; + list) + $SCRIPT_DIR/compute/list.sh "$@" + ;; + shutdown) + $SCRIPT_DIR/compute/shutdown.sh "$@" + ;; + delete) + $SCRIPT_DIR/compute/delete.sh "$@" + ;; + *) + echo -e "Available compute actions: \ncreate\nstart\nlist\nshutdown\ndelete" + exit 1 + ;; + esac + ;; + + network) + case $action in + attach) + $SCRIPT_DIR/network/attach.sh "$@" + ;; + detach) + $SCRIPT_DIR/network/detach.sh "$@" + ;; + list) + $SCRIPT_DIR/network/list.sh "$@" + ;; + create) + $SCRIPT_DIR/network/create.sh "$@" + ;; + delete) + $SCRIPT_DIR/network/delete.sh "$@" + ;; + *) + echo "Available network actions: \ncreate\nattach\ndetach\nlist\ndelete" + exit 1 + ;; + esac + ;; + + disk) + case $action in + attach) + $SCRIPT_DIR/disk/attach.sh "$@" + ;; + list) + $SCRIPT_DIR/disk/list.sh "$@" + ;; + *) + echo "Available disk actions: \ncreate\nattach\ndetach\nlist\ndelete" exit 1 ;; + esac + ;; + + storage-pool) + case $action in + create) # using a directory as a storage pool + $SCRIPT_DIR/storage-pool/create.sh "$@" + ;; + list) + $SCRIPT_DIR/storage-pool/list.sh "$@" + ;; + create-from-device) # initialise and use a devcie as storage pool + $SCRIPT_DIR/storage-pool/create-from-device.sh "$@" + ;; + + *) + echo "Available disk actions: \ncreate\nlist\ncreate-from-device\ndelete" + exit 1 + ;; + esac + ;; + + + *) + echo "Unknown category: $category" + echo "Available categories: compute, network, storage" + exit 1 + ;; esac |
