Skip to content

Commit b1ccf9d

Browse files
committed
add sanity checks to validate minimum qemu and libvirt versions
1 parent 6dfb0ea commit b1ccf9d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

scripts/vm/hypervisor/kvm/nasbackup.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,46 @@ MOUNT_OPTS=""
3232
BACKUP_DIR=""
3333
DISK_PATHS=""
3434

35+
vercomp() {
36+
local IFS=.
37+
local i ver1=($1) ver2=($3)
38+
39+
# Compare each segment of the version numbers
40+
for ((i=0; i<${#ver1[@]}; i++)); do
41+
if [[ -z ${ver2[i]} ]]; then
42+
ver2[i]=0
43+
fi
44+
45+
if ((10#${ver1[i]} > 10#${ver2[i]})); then
46+
return 1 # Version 1 is greater
47+
elif ((10#${ver1[i]} < 10#${ver2[i]})); then
48+
return 2 # Version 2 is greater
49+
fi
50+
done
51+
return 0 # Versions are equal
52+
}
53+
54+
sanity_checks() {
55+
hvVersion=$(virsh version | grep hypervisor | awk '{print $(NF)}')
56+
libvVersion=$(virsh version | grep libvirt | awk '{print $(NF)}' | tail -n 1)
57+
apiVersion=$(virsh version | grep API | awk '{print $(NF)}')
58+
59+
# Compare qemu version (hvVersion >= 4.2.0)
60+
vercomp "$hvVersion" ">=" "4.2.0"
61+
hvStatus=$?
62+
63+
# Compare libvirt version (libvVersion >= 7.2.0)
64+
vercomp "$libvVersion" ">=" "7.2.0"
65+
libvStatus=$?
66+
67+
if [[ ($hvStatus -eq 0 || $hvStatus -eq 1) && ($libvStatus -eq 0 || $libvStatus -eq 1) ]]; then
68+
echo "Success \t\t [ QEMU: $hvVersion Libvirt: $libvVersion apiVersion: $apiVersion ]"
69+
else
70+
echo "Failure... \tYour QEMU version $hvVersion or libvirt version $libvVersion is unsupported. Consider upgrading to the required minimum version of QEMU: 4.2.0 and Libvirt: 7.2.0"
71+
exit 1
72+
fi
73+
}
74+
3575
### Operation methods ###
3676

3777
backup_running_vm() {
@@ -163,6 +203,9 @@ while [[ $# -gt 0 ]]; do
163203
esac
164204
done
165205

206+
# Perform Initial sanity checks
207+
sanity_checks
208+
166209
if [ "$OP" = "backup" ]; then
167210
STATE=$(virsh -c qemu:///system list | grep $VM | awk '{print $3}')
168211
if [ "$STATE" = "running" ]; then

0 commit comments

Comments
 (0)