Skip to content

Commit 95a4394

Browse files
Extending libvirt image handler to create pool and volume
Added code to check if Libvirt pool and volume exists if not creating them through handler Signed-off-by: Saripalli Lavanya <[email protected]>
1 parent 404d29f commit 95a4394

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

config/peerpods/podvm/libvirt-podvm-image-handler.sh

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function verify_vars() {
3939

4040
# Function to create the libvirt image based on the type.
4141
function create_libvirt_image() {
42+
check_pool_and_volume_existence
4243
# Based on the value of `IMAGE_TYPE` the image is either build from scratch or using the prebuilt artifact.
4344
if [[ "${IMAGE_TYPE}" == "operator-built" ]]; then
4445
create_libvirt_image_from_scratch
@@ -48,6 +49,46 @@ function create_libvirt_image() {
4849

4950
}
5051

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+
5192
# Function to create the libvirt image from a prebuilt artifact.
5293
function create_libvirt_image_from_prebuilt_artifact() {
5394
echo "Pulling the podvm image from the provided path"
@@ -204,14 +245,31 @@ function delete_libvirt_image() {
204245
# LIBVIRT_POOL shouldn't be empty
205246
[[ -z "${LIBVIRT_POOL}" ]] && error_exit "LIBVIRT_POOL is empty"
206247

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}" ||
210262
error_exit "Failed to destroy the libvirt pool"
211263

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}" ||
213267
error_exit "Failed to undefine the libvirt pool"
214268

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
215273
echo "Deleted libvirt image successfully"
216274

217275
# Remove the libvirt_volume_name from peer-pods-cm configmap

0 commit comments

Comments
 (0)