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 /others/vm3/compute/create.sh | |
| parent | 2916e3eff503473f6bf8b90e0921b0ccd347166f (diff) | |
vm.shupdate
Diffstat (limited to 'others/vm3/compute/create.sh')
| -rwxr-xr-x | others/vm3/compute/create.sh | 68 |
1 files changed, 53 insertions, 15 deletions
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}"/* |
