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.sh68
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 @@
2 2
3# takes in vm name, os type, vcpu, ram, disk as argument 3# takes in vm name, os type, vcpu, ram, disk as argument
4# takes in already generated seed iso and downloaded vm.iso file 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 5
11#echo "$vmname $os $vcpu $ram_gb $disk_gb" 6# default values
7vcpu=8
8ram_gb=4
9disk_gb=32
10os="fedora40.qcow2"
12 11
12# parse arguments
13while [[ $# -gt 0 ]]; do
14 case $1 in
15 --vcpu)
16 vcpu="$2"
17 shift 2
18 ;;
19 --ram)
20 ram_gb="$2"
21 shift 2
22 ;;
23 --disk-size)
24 disk_gb="$2"
25 shift 2
26 ;;
27 --image)
28 os="$2"
29 shift 2
30 ;;
31 *)
32 # Handle positional arguments (vmname and os)
33 if [ -z "$vmname" ]; then
34 vmname="$1"
35 else
36 echo "Unknown argument: $1"
37 exit 1
38 fi
39 shift
40 ;;
41 esac
42done
13 43
14if [ $# -ne 5 ]; then 44# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
15 echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>" 45# image is os!!
16 echo "seed.iso and image file have to be present!" 46if [ -z "$vmname" ] ; then
47 echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]"
48 echo "seed.iso and image file have to be present! Default os is fedora"
17 echo "" 49 echo ""
18 echo "Available images" 50 echo "Available images:"
19 sudo ls /var/lib/libvirt/images/.image_store 51 sudo ls /var/lib/libvirt/images/.image_store
20 exit 1 52 exit 1
21fi 53fi
@@ -26,14 +58,20 @@ seed_iso="${workingdir}/seed.iso"
26xml="${workingdir}/xml" 58xml="${workingdir}/xml"
27 59
28image_dir_path="/var/lib/libvirt/images/.image_store" 60image_dir_path="/var/lib/libvirt/images/.image_store"
29src_file="${image_dir_path}/${os}.qcow2" 61src_file="${image_dir_path}/${os}"
30new_vm="/var/lib/libvirt/images/${vmname}.qcow2" 62new_vm_dir="/var/lib/libvirt/images/${vmname}"
63new_vm="${new_vm_dir}/${vmname}.qcow2"
31 64
32if [ ! -f "${src_file}" ]; then 65if [ ! -f "${src_file}" ]; then
33 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" 66 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
34 exit 1 67 exit 1
35fi 68fi
36 69
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}"
72
73sudo mkdir -p $new_vm_dir
74
37sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } 75sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
38 76
39if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then 77if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
@@ -47,11 +85,11 @@ sudo virt-install --name $vmname \
47 --disk path=$new_vm,format=qcow2 \ 85 --disk path=$new_vm,format=qcow2 \
48 --disk path=$seed_iso,device=cdrom \ 86 --disk path=$seed_iso,device=cdrom \
49 --os-type linux \ 87 --os-type linux \
50 --os-variant $os \ 88 --os-variant $(basename "$os" .qcow2) \
51 --virt-type kvm \ 89 --virt-type kvm \
52 --graphics none \ 90 --graphics none \
53 --network bridge=virbr0,model=virtio \ 91 --network bridge=virbr0,model=virtio \
54 --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; } 92 --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; }
55 93
56sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } 94sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
57 95
@@ -59,5 +97,5 @@ sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
59 97
60sudo virsh start $vmname 98sudo virsh start $vmname
61 99
62#sudo rm "${workingdir}"/* 100sudo rm "${workingdir}"/*
63 101