diff options
| author | root <root@sg1.noml.ch> | 2025-05-22 18:36:01 +0800 |
|---|---|---|
| committer | root <root@sg1.noml.ch> | 2025-05-22 18:36:01 +0800 |
| commit | 3a52e92fbbbab4df949740f89a389d68281a9428 (patch) | |
| tree | 5cb3b864eb0e3f3b90a1efd9cbdee1fb9f158f4a /others/vm3/compute/create.sh | |
| parent | 8cb260002db7c46ea5fe2c3f589b485e5f5d970d (diff) | |
Diffstat (limited to 'others/vm3/compute/create.sh')
| -rwxr-xr-x | others/vm3/compute/create.sh | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh deleted file mode 100755 index ab481cd..0000000 --- a/others/vm3/compute/create.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash - -# takes in vm name, os type, vcpu, ram, disk as argument -# takes in already generated seed iso and downloaded vm.iso file - -# default values -vcpu=8 -ram_gb=8 -disk_gb=64 -os="fedora40" -ostype="linux" - -# 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 - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -workingdir="/var/lib/libvirt/images/.temp" -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_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!! -if [ -z "$vmname" ] ; then - echo "" - echo "Usage: $0 <vm-name> [--image <os>] [--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:" - sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//' - echo "" - echo "Available images to download:" - sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//' - exit 1 -fi - -# run the script to make the cloud init files -sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}" - -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 - -sudo mkdir -p $new_vm_config_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 - echo -e "\n$vmname already exist. Delete it before using the same name." - exit 1 -fi - -if [[ ${os,,} == *"freebsd"* ]]; then - ostype="generic" -fi - -# Define the disk options based on OS type -if [ "$os" = "debian12" ]; then - disk_opts="--disk path=${new_vm},format=qcow2" - virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname" -else - disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" -fi - -generate_mac() { - printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) -} - -# Use the conditional disk options in virt-install -sudo virt-install --name $vmname \ - --vcpus $vcpu \ - --memory "$((ram_gb * 1024))" \ - $disk_opts \ - --os-type $ostype \ - --os-variant $os \ - --virt-type kvm \ - --graphics none \ - --network bridge=virbr0,model=virtio,mac=$(generate_mac) \ - --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; } - -sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null - -sudo virsh start $vmname - -sudo rm "${workingdir}"/* - |
