@@ -39,6 +39,7 @@ function verify_vars() {
39
39
40
40
# Function to create the libvirt image based on the type.
41
41
function create_libvirt_image() {
42
+ check_pool_and_volume_existence
42
43
# Based on the value of `IMAGE_TYPE` the image is either build from scratch or using the prebuilt artifact.
43
44
if [[ " ${IMAGE_TYPE} " == " operator-built" ]]; then
44
45
create_libvirt_image_from_scratch
@@ -48,6 +49,46 @@ function create_libvirt_image() {
48
49
49
50
}
50
51
52
+ # Function to create Libvirt pool and configure libvirt volume to upload qcow2 image
53
+ function create_pool_and_volume() {
54
+
55
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-define-as " ${LIBVIRT_POOL} " --type dir --target " ${LIBVIRT_DIR_NAME} " \
56
+ || error_exit " Failed to define libvirt pool '${LIBVIRT_POOL} '."
57
+
58
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-start " ${LIBVIRT_POOL} " \
59
+ || error_exit " Failed to start libvirt pool '${LIBVIRT_POOL} '."
60
+
61
+ # Create the libvirt volume
62
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-create-as --pool " ${LIBVIRT_POOL} " \
63
+ --name " ${LIBVIRT_VOL_NAME} " \
64
+ --capacity 20G \
65
+ --allocation 2G \
66
+ --prealloc-metadata \
67
+ --format qcow2 \
68
+ || error_exit " Failed to create libvirt volume '${LIBVIRT_VOL_NAME} ' in pool '${LIBVIRT_POOL} '."
69
+
70
+ echo " Created libvirt pool and volume successfully."
71
+
72
+ }
73
+
74
+ # Function that check if defined volume and pool are already present
75
+ function check_pool_and_volume_existence() {
76
+
77
+ echo " Checking existence of libvirt pool '${LIBVIRT_POOL} ' and volume '${LIBVIRT_VOL_NAME} '..."
78
+
79
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-info " ${LIBVIRT_POOL} " > /dev/null 2>&1
80
+ POOL_EXISTS=$?
81
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-info --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} " > /dev/null 2>&1
82
+ VOL_EXISTS=$?
83
+
84
+ if [ " $POOL_EXISTS " -eq 0 ] && [ " $VOL_EXISTS " -eq 0 ]; then
85
+ echo " Disclaimer: A Libvirt pool named '${LIBVIRT_POOL} ' with volume '${LIBVIRT_VOL_NAME} ' already exists on the KVM host. Image will be uploaded to same volume."
86
+ else
87
+ echo " Libvirt pool '${LIBVIRT_POOL} ' or volume '${LIBVIRT_VOL_NAME} ' does not exist. Proceeding to create..."
88
+ create_pool_and_volume
89
+ fi
90
+ }
91
+
51
92
# Function to create the libvirt image from a prebuilt artifact.
52
93
function create_libvirt_image_from_prebuilt_artifact() {
53
94
echo " Pulling the podvm image from the provided path"
@@ -204,14 +245,31 @@ function delete_libvirt_image() {
204
245
# LIBVIRT_POOL shouldn't be empty
205
246
[[ -z " ${LIBVIRT_POOL} " ]] && error_exit " LIBVIRT_POOL is empty"
206
247
207
- # Delete the libvirt pool
208
- echo " Deleting libvirt pool."
209
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " ||
248
+ # Delete the libvirt volume
249
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-delete --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} " ||
250
+ error_exit " Failed to delete volume '${LIBVIRT_VOL_NAME} ' from pool '${LIBVIRT_POOL} '"
251
+
252
+ echo " Volume '${LIBVIRT_VOL_NAME} ' deleted successfully."
253
+
254
+ # Check remaining volumes in the pool
255
+ VOLUMES=$( virsh -d 0 -c " ${LIBVIRT_URI} " vol-list " ${LIBVIRT_POOL} " | awk ' NR>2 {print $1}' | grep -v " ^$LIBVIRT_VOL_NAME \$ " )
256
+
257
+ # Deleting pool if there are other volumes in the pool
258
+ if [ -z " $VOLUMES " ]; then
259
+ echo " No other volumes found in the pool. Deleting the pool"
260
+
261
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " ||
210
262
error_exit " Failed to destroy the libvirt pool"
211
263
212
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " ||
264
+ echo " Pool '${LIBVIRT_POOL} ' destroyed successfully."
265
+
266
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " ||
213
267
error_exit " Failed to undefine the libvirt pool"
214
268
269
+ echo " Pool '${LIBVIRT_POOL} ' undefined successfully."
270
+ else
271
+ echo " Other volumes are present in the pool. Pool will not be deleted."
272
+ fi
215
273
echo " Deleted libvirt image successfully"
216
274
217
275
# Remove the libvirt_volume_name from peer-pods-cm configmap
0 commit comments