Skip to content

Commit ba5e6d1

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 ba5e6d1

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
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: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,92 @@ 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+
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
4887

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
49128
}
50129

51130
# Function to create the libvirt image from a prebuilt artifact.
@@ -183,12 +262,14 @@ function add_libvirt_vol_to_peer_pods_cm() {
183262
echo "peer-pods-cm configmap does not exist. Skipping adding the libvirt volume name"
184263
return
185264
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+
189269
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+
192273
fi
193274
}
194275

@@ -204,14 +285,30 @@ function delete_libvirt_image() {
204285
# LIBVIRT_POOL shouldn't be empty
205286
[[ -z "${LIBVIRT_POOL}" ]] && error_exit "LIBVIRT_POOL is empty"
206287

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}')
211298

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}" || \
213305
error_exit "Failed to undefine the libvirt pool"
214306

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+
215312
echo "Deleted libvirt image successfully"
216313

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

0 commit comments

Comments
 (0)