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/compute/create.sh | 68 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'others/vm3/compute/create.sh') 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}"/* -- cgit v1.2.3-70-g09d2