summaryrefslogtreecommitdiff
path: root/others/vm3/compute
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-12-21 15:11:19 +0800
committerYour Name <you@example.com>2024-12-21 15:11:19 +0800
commitbe3dde6ce14698c5818f21f65037a4beef1199cf (patch)
tree79dbd14ec4a07d6b41561931d15a4c03406d5836 /others/vm3/compute
parent47303626554d42356dfd1b61ad3f9de97929cc87 (diff)
betavm3
Diffstat (limited to 'others/vm3/compute')
-rwxr-xr-xothers/vm3/compute/create.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
new file mode 100755
index 0000000..6d83a03
--- /dev/null
+++ b/others/vm3/compute/create.sh
@@ -0,0 +1,60 @@
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
5vmname=$1
6os=$2
7vcpu=$3
8ram_gb=$4
9disk_gb=$5
10
11#echo "$vmname $os $vcpu $ram_gb $disk_gb"
12
13
14if [ $# -ne 5 ]; then
15 echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>"
16 echo "seed.iso and image file have to be present!"
17 exit 1
18fi
19
20scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
21workingdir="/var/lib/libvirt/images/.temp"
22seed_iso="${workingdir}/seed.iso"
23xml="${workingdir}/xml"
24
25image_dir_path="/var/lib/libvirt/images/.image_store"
26src_file="${image_dir_path}/${os}.qcow2"
27new_vm="/var/lib/libvirt/images/${vmname}.qcow2"
28
29if [ ! -f "${src_file}" ]; then
30 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
31 exit 1
32fi
33
34sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
35
36if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
37 echo -e "\n$vmname already exist. Delete it before using the same name."
38 exit 1
39fi
40
41sudo virt-install --name $vmname \
42 --vcpus $vcpu \
43 --memory "$((ram_gb * 1024))"\
44 --disk path=$new_vm_path,size=$disk_gb,format=qcow2 \
45 --disk path=$seed_iso,device=cdrom \
46 --os-type linux \
47 --os-variant $os \
48 --virt-type kvm \
49 --graphics none \
50 --network bridge=virbr0,model=virtio \
51 --print-xml > $xml || { echo "Failed to print XML."; exit 1; }
52
53sudo virsh define $xml &> /dev/null || { echo "Failed to define the new VM."; exit 1; }
54
55sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
56
57sudo virsh start $vmname
58
59sudo rm "${workingdir}"/*
60