summaryrefslogtreecommitdiff
path: root/others/vm3/compute/create.sh
diff options
context:
space:
mode:
Diffstat (limited to 'others/vm3/compute/create.sh')
-rwxr-xr-xothers/vm3/compute/create.sh123
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
7vcpu=8
8ram_gb=8
9disk_gb=64
10os="fedora40"
11ostype="linux"
12
13# parse arguments
14while [[ $# -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
43done
44
45scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
46workingdir="/var/lib/libvirt/images/.temp"
47seed_iso="${workingdir}/seed.iso"
48xml="${workingdir}/xml"
49
50image_dir_path="/var/lib/libvirt/images/.image_store"
51src_file="${image_dir_path}/${os}.qcow2"
52new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config"
53new_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!!
57if [ -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
68fi
69
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}"
72
73if [ ! -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
76fi
77
78sudo mkdir -p $new_vm_config_dir
79
80sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
81
82if 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
85fi
86
87if [[ ${os,,} == *"freebsd"* ]]; then
88 ostype="generic"
89fi
90
91# Define the disk options based on OS type
92if [ "$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"
95else
96 disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom"
97fi
98
99generate_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
104sudo 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
116sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
117
118sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
119
120sudo virsh start $vmname
121
122sudo rm "${workingdir}"/*
123