From f7f159a04671690786de2f84e34046c103521d58 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 22 Dec 2024 19:03:49 +0800 Subject: vm.shupdate --- others/vm3/.config/cloud-init-files/fedora40.sh | 65 +++++++++++ others/vm3/.docs.swp | Bin 0 -> 12288 bytes others/vm3/compute/create.sh | 68 ++++++++--- others/vm3/compute/delete.sh | 27 +++++ others/vm3/compute/list.sh | 45 +++++++ others/vm3/compute/shutdown.sh | 6 + others/vm3/compute/start.sh | 18 ++- others/vm3/docs | 32 ++++- others/vm3/image/fedora.sh | 65 ----------- others/vm3/test.sh | 49 ++++++++ others/vm3/vm.sh | 148 ++++++++++++++++++------ 11 files changed, 400 insertions(+), 123 deletions(-) create mode 100755 others/vm3/.config/cloud-init-files/fedora40.sh create mode 100644 others/vm3/.docs.swp create mode 100755 others/vm3/compute/delete.sh create mode 100755 others/vm3/compute/list.sh create mode 100755 others/vm3/compute/shutdown.sh mode change 100644 => 100755 others/vm3/compute/start.sh delete mode 100755 others/vm3/image/fedora.sh create mode 100755 others/vm3/test.sh mode change 100644 => 100755 others/vm3/vm.sh (limited to 'others') diff --git a/others/vm3/.config/cloud-init-files/fedora40.sh b/others/vm3/.config/cloud-init-files/fedora40.sh new file mode 100755 index 0000000..cd05e10 --- /dev/null +++ b/others/vm3/.config/cloud-init-files/fedora40.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# generates user data, meta data, and seed.iso for cloud init. for fedora +# requires the name of the vm as an argument +# +# osinfo-query os to list all available vm types to deploy. fedora is fedora 40 + + +image_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2" + +sshkeysdir="/root/k" + +scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir +dir_path="/var/lib/libvirt/images/.image_store" +src_file="${dir_path}/fedora40.qcow2" +config_dir="/var/lib/libvirt/images/.temp" +user_data="${config_dir}/user-data" +meta_data="${config_dir}/meta-data" +seed_iso="${config_dir}/seed.iso" +# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. + +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +sudo mkdir -p "$dir_path" +sudo mkdir -p "$config_dir" + +[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" + +cat > "$user_data" << EOF +#cloud-config +users: + - name: user + ssh-authorized-keys: +EOF + +for key in $sshkeysdir/*.pub; do + echo " - $(cat "$key")" >> $user_data +done + +cat >> "$user_data" << 'EOF' + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + groups: wheel + shell: /bin/bash + password: 123 +runcmd: + - sudo growpart /dev/sda 1 + - sudo xfs_growfs / + - sudo dnf install -y vim git + - cd /home/user + - #git clone https://git.0nom.ch/setup + - #sudo ./setup/setup.sh + - touch /home/user/runcmd_done +EOF + +cat > "$meta_data" << EOF +instance-id: vm_id +local-hostname: $1 +EOF + +genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } + +echo "Configuration files generated successfully" diff --git a/others/vm3/.docs.swp b/others/vm3/.docs.swp new file mode 100644 index 0000000..849e1f3 Binary files /dev/null and b/others/vm3/.docs.swp differ 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 " - 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 [--image .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 "/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.$$ & + + 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 old mode 100644 new mode 100755 index 5a2b3aa..d64dbb1 --- 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 /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 --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/image/fedora.sh b/others/vm3/image/fedora.sh deleted file mode 100755 index cd05e10..0000000 --- a/others/vm3/image/fedora.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/bash - -# generates user data, meta data, and seed.iso for cloud init. for fedora -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. fedora is fedora 40 - - -image_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2" - -sshkeysdir="/root/k" - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/fedora40.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. - -if [ $# -ne 1 ]; then - echo "Usage: $0 " - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" - -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash - password: 123 -runcmd: - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - sudo dnf install -y vim git - - cd /home/user - - #git clone https://git.0nom.ch/setup - - #sudo ./setup/setup.sh - - touch /home/user/runcmd_done -EOF - -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF - -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } - -echo "Configuration files generated successfully" 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 [--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 old mode 100644 new mode 100755 index 1072569..30168ba --- 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 [args...]\n" + echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n" + echo "Run ./vm 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 -- cgit v1.2.3-70-g09d2