@@ -39,13 +39,92 @@ 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
+ error_exit " Failed to extract IP address from the URI."
61
+ fi
62
+
63
+ ssh " $KVM_HOST_ADDRESS " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i " /root/.ssh/id_rsa" " mkdir -p ${LIBVIRT_DIR_NAME} "
64
+ if [[ $? -eq 0 ]]; then
65
+ echo " Directory '$LIBVIRT_DIR_NAME ' created successfully."
66
+ else
67
+ error_exit " Failed to create directory '$LIBVIRT_DIR_NAME ' on '$KVM_HOST_ADDRESS '."
68
+ fi
69
+
70
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-info " ${LIBVIRT_POOL} " > /dev/null 2>&1
71
+ if [ $? -eq 0 ]; then
72
+ echo " Pool '${LIBVIRT_POOL} ' already exists."
73
+ else
74
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-define-as " ${LIBVIRT_POOL} " --type dir --target " ${LIBVIRT_DIR_NAME} " \
75
+ || error_exit " Failed to define libvirt pool '${LIBVIRT_POOL} '."
76
+
77
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-start " ${LIBVIRT_POOL} " \
78
+ || error_exit " Failed to start libvirt pool '${LIBVIRT_POOL} '."
79
+ if [[ $? -ne 0 ]]; then
80
+ echo " Failed to start libvirt pool '${LIBVIRT_POOL} '. Attempting to undefine the pool."
81
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} "
82
+ error_exit " Failed to start libvirt pool '${LIBVIRT_POOL} ', pool has been undefine."
83
+ else
84
+ echo " Pool '${LIBVIRT_POOL} ' started successfully."
85
+ fi
86
+ fi
48
87
88
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-create-as --pool " ${LIBVIRT_POOL} " \
89
+ --name " ${LIBVIRT_VOL_NAME} " \
90
+ --capacity 20G \
91
+ --allocation 2G \
92
+ --prealloc-metadata \
93
+ --format qcow2 \
94
+ || virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " && \
95
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " && \
96
+ error_exit " Failed to create libvirt volume '${LIBVIRT_VOL_NAME} ' in pool '${LIBVIRT_POOL} ' Cleaning up used pool."
97
+
98
+ echo " Created libvirt pool and volume successfully."
99
+ }
100
+
101
+ # Function that checks if defined volume and pool are already present
102
+ # Returns 0 if both pool and volume exist, 1 otherwise
103
+ function check_pool_and_volume_existence() {
104
+
105
+ if [[ -z " ${LIBVIRT_POOL} " || -z " ${LIBVIRT_VOL_NAME} " || -z " ${LIBVIRT_DIR_NAME} " ]]; then
106
+ echo " LIBVIRT_POOL or LIBVIRT_VOL_NAME values are not provided in the configmap. Skipping the existence check."
107
+ [ -z " $LIBVIRT_POOL " ] && LIBVIRT_POOL=" ${CLOUD_PROVIDER} _pool_$( date +' %Y%m%d%S' ) "
108
+ [ -z " $LIBVIRT_VOL_NAME " ] && LIBVIRT_VOL_NAME=" ${CLOUD_PROVIDER} _vol_$( date +' %Y%m%d%S' ) "
109
+ [ -z " $LIBVIRT_DIR_NAME " ] && LIBVIRT_DIR_NAME=" /var/lib/libvirt/images/${CLOUD_PROVIDER} _$( date +' %Y%m%d%S' ) "
110
+ echo " Proceeding with defaults LIBVIRT_POOL as $LIBVIRT_POOL , LIBVIRT_VOL_NAME as $LIBVIRT_VOL_NAME "
111
+ return 1
112
+ fi
113
+
114
+ echo " Checking existence of libvirt pool '${LIBVIRT_POOL} ' and volume '${LIBVIRT_VOL_NAME} '..."
115
+
116
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-info " ${LIBVIRT_POOL} " > /dev/null 2>&1
117
+ POOL_EXISTS=$?
118
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-info --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} " > /dev/null 2>&1
119
+ VOL_EXISTS=$?
120
+
121
+ if [ " $POOL_EXISTS " -eq 0 ] && [ " $VOL_EXISTS " -eq 0 ]; then
122
+ 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."
123
+ return 0
124
+ else
125
+ echo " Libvirt pool '${LIBVIRT_POOL} ' or volume '${LIBVIRT_VOL_NAME} ' does not exist. Proceeding to create..."
126
+ return 1
127
+ fi
49
128
}
50
129
51
130
# Function to create the libvirt image from a prebuilt artifact.
@@ -183,12 +262,14 @@ function add_libvirt_vol_to_peer_pods_cm() {
183
262
echo " peer-pods-cm configmap does not exist. Skipping adding the libvirt volume name"
184
263
return
185
264
fi
186
-
187
- # Add the libvirt image id to peer-pods-cm configmap
188
- echo " Updating peer-pods-cm configmap with LIBVIRT_IMAGE_ID=${LIBVIRT_VOL_NAME} "
265
+ # Updating per-pods-cm
266
+
267
+ echo " Updating peer-pods-cm configmap with LIBVIRT_IMAGE_ID=${LIBVIRT_VOL_NAME} , LIBVIRT_POOL=${LIBVIRT_POOL} , and LIBVIRT_VOL_NAME=${LIBVIRT_VOL_NAME} "
268
+
189
269
kubectl patch configmap peer-pods-cm -n openshift-sandboxed-containers-operator \
190
- --type merge -p " {\" data\" :{\" LIBVIRT_IMAGE_ID\" :\" ${LIBVIRT_VOL_NAME} \" }}" ||
191
- error_exit " Failed to add the libvirt image id to peer-pods-cm configmap"
270
+ --type merge -p " {\" data\" :{\" LIBVIRT_IMAGE_ID\" :\" ${LIBVIRT_VOL_NAME} \" , \" LIBVIRT_POOL\" :\" ${LIBVIRT_POOL} \" , \" LIBVIRT_VOL_NAME\" :\" ${LIBVIRT_VOL_NAME} \" }}" ||
271
+ error_exit " Failed to add the libvirt image id, pool, and volume name to peer-pods-cm configmap"
272
+
192
273
fi
193
274
}
194
275
@@ -204,14 +285,30 @@ function delete_libvirt_image() {
204
285
# LIBVIRT_POOL shouldn't be empty
205
286
[[ -z " ${LIBVIRT_POOL} " ]] && error_exit " LIBVIRT_POOL is empty"
206
287
207
- # Delete the libvirt pool
208
- echo " Deleting libvirt pool."
209
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " ||
210
- error_exit " Failed to destroy the libvirt pool"
288
+ # Attempt to delete the libvirt volume
289
+ virsh -d 0 -c " ${LIBVIRT_URI} " vol-delete --pool " ${LIBVIRT_POOL} " " ${LIBVIRT_VOL_NAME} "
290
+ if [ $? -eq 0 ]; then
291
+ echo " Volume '${LIBVIRT_VOL_NAME} ' deleted successfully."
292
+ else
293
+ echo " Failed to delete volume '${LIBVIRT_VOL_NAME} ' from pool '${LIBVIRT_POOL} ', continuing to check remaining volumes."
294
+ fi
295
+
296
+ # Check remaining volumes in the pool
297
+ VOLUMES=$( virsh -d 0 -c " ${LIBVIRT_URI} " vol-list " ${LIBVIRT_POOL} " | awk ' NR>2 {print $1}' )
211
298
212
- virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " ||
299
+ # Deleting pool if there are other volumes in the pool
300
+ if [ " $VOLUMES " == " $LIBVIRT_VOL_NAME " ]; then
301
+ echo " No other volumes found in the pool except '${LIBVIRT_VOL_NAME} '. Deleting the pool."
302
+
303
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-destroy " ${LIBVIRT_POOL} " && \
304
+ virsh -d 0 -c " ${LIBVIRT_URI} " pool-undefine " ${LIBVIRT_POOL} " || \
213
305
error_exit " Failed to undefine the libvirt pool"
214
306
307
+ echo " Pool '${LIBVIRT_POOL} ' destroyed successfully."
308
+ else
309
+ echo " Other volumes are present in the pool. Pool will not be deleted."
310
+ fi
311
+
215
312
echo " Deleted libvirt image successfully"
216
313
217
314
# Remove the libvirt_volume_name from peer-pods-cm configmap
0 commit comments