@@ -39,13 +39,76 @@ function verify_vars() {
39
39
40
40
# Function to create the libvirt image based on the type.
41
41
function create_libvirt_image() {
42
+
43
+ if ! check_pool_and_volume_existence; then
44
+ create_pool_and_volume
45
+ fi
46
+
42
47
# Based on the value of `IMAGE_TYPE` the image is either build from scratch or using the prebuilt artifact.
43
48
if [[ " ${IMAGE_TYPE} " == " operator-built" ]]; then
44
49
create_libvirt_image_from_scratch
45
50
elif [[ " ${IMAGE_TYPE} " == " pre-built" ]]; then
46
51
create_libvirt_image_from_prebuilt_artifact
47
52
fi
53
+ }
54
+
55
+ # Function to create Libvirt pool and configure libvirt volume to upload qcow2 image
56
+ function create_pool_and_volume() {
57
+ KVM_HOST_ADDRESS=$( echo " ${LIBVIRT_URI} " | sed -E ' s|^.*//([^/]+).*$|\1|' )
58
+
59
+ if [[ -z " $KVM_HOST_ADDRESS " ]]; then
60
+ echo " Failed to extract IP address from the URI."
61
+ exit 1
62
+ fi
63
+
64
+ ssh " $KVM_HOST_ADDRESS " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i " /root/.ssh/id_rsa" " mkdir -p ${LIBVIRT_DIR_NAME} "
65
+ if [[ $? -eq 0 ]]; then
66
+ echo " Directory '$LIBVIRT_DIR_NAME ' created successfully."
67
+ else
68
+ echo " Failed to create directory '$LIBVIRT_DIR_NAME ' on '$KVM_HOST_ADDRESS '."
69
+ exit 1
70
+ fi
71
+
72
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-info " ${LIBVIRT_POOL} " > /dev/null 2>&1
73
+ if [ $? -eq 0 ]; then
74
+ echo " Pool '${LIBVIRT_POOL} ' already exists."
75
+ else
76
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-define-as " ${LIBVIRT_POOL} " --type dir --target " ${LIBVIRT_DIR_NAME} " \
77
+ || error_exit " Failed to define libvirt pool '${LIBVIRT_POOL} '."
48
78
79
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-start " ${LIBVIRT_POOL} " \
80
+ || error_exit " Failed to start libvirt pool '${LIBVIRT_POOL} '."
81
+ fi
82
+
83
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-create-as --pool " ${LIBVIRT_POOL} " \
84
+ --name " ${LIBVIRT_VOL_NAME} " \
85
+ --capacity 20G \
86
+ --allocation 2G \
87
+ --prealloc-metadata \
88
+ --format qcow2 \
89
+ || error_exit " Failed to create libvirt volume '${LIBVIRT_VOL_NAME} ' in pool '${LIBVIRT_POOL} '."
90
+
91
+ echo " Created libvirt pool and volume successfully."
92
+ }
93
+
94
+ # Function that checks if defined volume and pool are already present
95
+ # Returns 0 if both pool and volume exist, 1 otherwise
96
+ function check_pool_and_volume_existence() {
97
+
98
+ echo " Checking existence of libvirt pool '${LIBVIRT_POOL} ' and volume '${LIBVIRT_VOL_NAME} '..."
99
+
100
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-info " ${LIBVIRT_POOL} " > /dev/null 2>&1
101
+ POOL_EXISTS=$?
102
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-info --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} " > /dev/null 2>&1
103
+ VOL_EXISTS=$?
104
+
105
+ if [ " $POOL_EXISTS " -eq 0 ] && [ " $VOL_EXISTS " -eq 0 ]; then
106
+ echo " Disclaimer: A Libvirt pool named '${LIBVIRT_POOL} ' with volume '${LIBVIRT_VOL_NAME} ' already exists on the KVM host. Image will be uploaded to the same volume."
107
+ return 0
108
+ else
109
+ echo " Libvirt pool '${LIBVIRT_POOL} ' or volume '${LIBVIRT_VOL_NAME} ' does not exist. Proceeding to create..."
110
+ return 1
111
+ fi
49
112
}
50
113
51
114
# Function to create the libvirt image from a prebuilt artifact.
@@ -204,14 +267,31 @@ function delete_libvirt_image() {
204
267
# LIBVIRT_POOL shouldn't be empty
205
268
[[ -z " ${LIBVIRT_POOL} " ]] && error_exit " LIBVIRT_POOL is empty"
206
269
207
- # Delete the libvirt pool
208
- echo " Deleting libvirt pool."
209
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " ||
270
+ # Delete the libvirt volume
271
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-delete --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} " ||
272
+ error_exit " Failed to delete volume '${LIBVIRT_VOL_NAME} ' from pool '${LIBVIRT_POOL} '"
273
+
274
+ echo " Volume '${LIBVIRT_VOL_NAME} ' deleted successfully."
275
+
276
+ # Check remaining volumes in the pool
277
+ VOLUMES=$( virsh -d 0 -c " ${LIBVIRT_URI} " vol-list " ${LIBVIRT_POOL} " | awk ' NR>2 {print $1}' | grep -v " ^$LIBVIRT_VOL_NAME \$ " )
278
+
279
+ # Deleting pool if there are other volumes in the pool
280
+ if [ -z " $VOLUMES " ]; then
281
+ echo " No other volumes found in the pool. Deleting the pool"
282
+
283
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " ||
210
284
error_exit " Failed to destroy the libvirt pool"
211
285
212
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " ||
286
+ echo " Pool '${LIBVIRT_POOL} ' destroyed successfully."
287
+
288
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " ||
213
289
error_exit " Failed to undefine the libvirt pool"
214
290
291
+ echo " Pool '${LIBVIRT_POOL} ' undefined successfully."
292
+ else
293
+ echo " Other volumes are present in the pool. Pool will not be deleted."
294
+ fi
215
295
echo " Deleted libvirt image successfully"
216
296
217
297
# Remove the libvirt_volume_name from peer-pods-cm configmap
0 commit comments