Skip to content

Commit b6927d4

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 c9dadc7 commit b6927d4

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

config/peerpods/peerpodscm.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ data:
88
#LIBVIRT_NET: "default
99
#LIBVIRT_POOL: "default"
1010
#LIBVIRT_VOL_NAME: "default"
11-
#LIBVIRT_DIR_NAME: "/var/lib/libvirt/images/"
11+
#LIBVIRT_DIR_NAME: "/var/lib/libvirt/images/default"

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

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,76 @@ function verify_vars() {
3939

4040
# Function to create the libvirt image based on the type.
4141
function create_libvirt_image() {
42+
43+
if ! check_pool_and_volume_existence; then
44+
create_pool_and_volume
45+
fi
46+
4247
# Based on the value of `IMAGE_TYPE` the image is either build from scratch or using the prebuilt artifact.
4348
if [[ "${IMAGE_TYPE}" == "operator-built" ]]; then
4449
create_libvirt_image_from_scratch
4550
elif [[ "${IMAGE_TYPE}" == "pre-built" ]]; then
4651
create_libvirt_image_from_prebuilt_artifact
4752
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}'."
4878

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
49112
}
50113

51114
# Function to create the libvirt image from a prebuilt artifact.
@@ -204,14 +267,31 @@ function delete_libvirt_image() {
204267
# LIBVIRT_POOL shouldn't be empty
205268
[[ -z "${LIBVIRT_POOL}" ]] && error_exit "LIBVIRT_POOL is empty"
206269

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

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

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
215295
echo "Deleted libvirt image successfully"
216296

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

0 commit comments

Comments
 (0)