1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#!/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=32
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"
else
disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom"
fi
# 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 \
--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}"/*
|