diff options
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 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # takes in vm name, os type, vcpu, ram, disk as argument | ||
| 4 | # takes in already generated seed iso and downloaded vm.iso file | ||
| 5 | |||
| 6 | # default values | ||
| 7 | vcpu=8 | ||
| 8 | ram_gb=8 | ||
| 9 | disk_gb=64 | ||
| 10 | os="fedora40" | ||
| 11 | ostype="linux" | ||
| 12 | |||
| 13 | # parse arguments | ||
| 14 | while [[ $# -gt 0 ]]; do | ||
| 15 | case $1 in | ||
| 16 | --vcpu) | ||
| 17 | vcpu="$2" | ||
| 18 | shift 2 | ||
| 19 | ;; | ||
| 20 | --ram) | ||
| 21 | ram_gb="$2" | ||
| 22 | shift 2 | ||
| 23 | ;; | ||
| 24 | --disk-size) | ||
| 25 | disk_gb="$2" | ||
| 26 | shift 2 | ||
| 27 | ;; | ||
| 28 | --image) | ||
| 29 | os="$2" | ||
| 30 | shift 2 | ||
| 31 | ;; | ||
| 32 | *) | ||
| 33 | # Handle positional arguments (vmname and os) | ||
| 34 | if [ -z "$vmname" ]; then | ||
| 35 | vmname="$1" | ||
| 36 | else | ||
| 37 | echo "Unknown argument: $1" | ||
| 38 | exit 1 | ||
| 39 | fi | ||
| 40 | shift | ||
| 41 | ;; | ||
| 42 | esac | ||
| 43 | done | ||
| 44 | |||
| 45 | scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir | ||
| 46 | workingdir="/var/lib/libvirt/images/.temp" | ||
| 47 | seed_iso="${workingdir}/seed.iso" | ||
| 48 | xml="${workingdir}/xml" | ||
| 49 | |||
| 50 | image_dir_path="/var/lib/libvirt/images/.image_store" | ||
| 51 | src_file="${image_dir_path}/${os}.qcow2" | ||
| 52 | new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config" | ||
| 53 | new_vm="/var/lib/libvirt/images/${vmname}.qcow2" | ||
| 54 | |||
| 55 | # Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null | ||
| 56 | # image is os!! | ||
| 57 | if [ -z "$vmname" ] ; then | ||
| 58 | echo "" | ||
| 59 | echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]" | ||
| 60 | echo "seed.iso and image file have to be present! Default os is fedora" | ||
| 61 | echo "" | ||
| 62 | echo "Available images:" | ||
| 63 | sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//' | ||
| 64 | echo "" | ||
| 65 | echo "Available images to download:" | ||
| 66 | sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//' | ||
| 67 | exit 1 | ||
| 68 | fi | ||
| 69 | |||
| 70 | # run the script to make the cloud init files | ||
| 71 | sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}" | ||
| 72 | |||
| 73 | if [ ! -f "${src_file}" ]; then | ||
| 74 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" | ||
| 75 | exit 1 | ||
| 76 | fi | ||
| 77 | |||
| 78 | sudo mkdir -p $new_vm_config_dir | ||
| 79 | |||
| 80 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } | ||
| 81 | |||
| 82 | if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then | ||
| 83 | echo -e "\n$vmname already exist. Delete it before using the same name." | ||
| 84 | exit 1 | ||
| 85 | fi | ||
| 86 | |||
| 87 | if [[ ${os,,} == *"freebsd"* ]]; then | ||
| 88 | ostype="generic" | ||
| 89 | fi | ||
| 90 | |||
| 91 | # Define the disk options based on OS type | ||
| 92 | if [ "$os" = "debian12" ]; then | ||
| 93 | disk_opts="--disk path=${new_vm},format=qcow2" | ||
| 94 | virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname" | ||
| 95 | else | ||
| 96 | disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" | ||
| 97 | fi | ||
| 98 | |||
| 99 | generate_mac() { | ||
| 100 | printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) | ||
| 101 | } | ||
| 102 | |||
| 103 | # Use the conditional disk options in virt-install | ||
| 104 | sudo virt-install --name $vmname \ | ||
| 105 | --vcpus $vcpu \ | ||
| 106 | --memory "$((ram_gb * 1024))" \ | ||
| 107 | $disk_opts \ | ||
| 108 | --os-type $ostype \ | ||
| 109 | --os-variant $os \ | ||
| 110 | --virt-type kvm \ | ||
| 111 | --graphics none \ | ||
| 112 | --network bridge=virbr0,model=virtio,mac=$(generate_mac) \ | ||
| 113 | --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; } | ||
| 114 | # if you want this in a new storage pool, move it to a new storage pool after initialisation | ||
| 115 | |||
| 116 | sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } | ||
| 117 | |||
| 118 | sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null | ||
| 119 | |||
| 120 | sudo virsh start $vmname | ||
| 121 | |||
| 122 | sudo rm "${workingdir}"/* | ||
| 123 | |||
