Commit 94716e54 authored by Paul John King's avatar Paul John King

Added an examine domain command, and tidied up the verify domain command --

parent d42f1a32
......@@ -178,6 +178,14 @@ print_domain_usage_message(){
<bridge>. If the '-m' option is used then the domain has MAC address
<mac_address>.
The command call
${0##*/} examine domain <name>
outputs the CPU count, RAM, file-system image, bridge device and MAC address of
the KVM domain with name <name> and returns 0 if the domain exists, and returns
1 otherwise.
The command call
${0##*/} verify domain [-m <mac_address>] <name> <cpu_count> <ram> <path> <bridge>
......@@ -284,16 +292,57 @@ create_domain(){
} ;
examine_domain(){
local _NAME="${1}" ;
shift 1 ;
local _domain_xml ;
local _disk_vda_xml ;
local _interface_xml ;
local _cpu_count ;
local _ram ;
local _image ;
local _mac_address ;
if _domain_xml=$(virsh -q dumpxml "${_NAME}" 2> /dev/null) ;
then
_disk_vda_xml=$(_get_elements_by_tag_and_sub_tag_and_attribute_from_xml \
"${_domain_xml}" "disk" "target" "dev" "vda"
) ;
_interface_xml=$(_get_elements_by_tag_and_attribute_from_xml \
"${_domain_xml}" "interface" "type" "bridge"
) ;
_cpu_count=$(_get_content_by_tag_from_xml "${_domain_xml}" "vcpu") ;
_ram=$(_get_content_by_tag_from_xml "${_domain_xml}" "memory") ;
_ram_units=$(_get_value_by_tag_and_attribute_from_xml "${_domain_xml}" "memory" "unit") ;
_image=$(_get_value_by_tag_and_attribute_from_xml "${_disk_vda_xml}" "source" "file") ;
_bridge=$(_get_value_by_tag_and_attribute_from_xml "${_interface_xml}" "source" "bridge") ;
_mac_address=$(_get_value_by_tag_and_attribute_from_xml "${_interface_xml}" "mac" "address") ;
echo "CPU count: ${_cpu_count}" ;
echo "RAM: ${_ram} ${_ram_units}" ;
echo "Image: ${_image}" ;
echo "Bridge: ${_bridge}" ;
echo "MAC address: ${_mac_address}" ;
return 0 ;
else
return 1 ;
fi ;
}
verify_domain(){
local OPTIND=1 ;
local _mac_address="" ;
while getopts ":m:" OPTION ;
do
case "${OPTION}" in
"m")
_mac_address="${OPTARG}" ;
local _MAC_ADDRESS="${OPTARG}" ;
;;
"?")
echo "The command-line option '-${OPTARG}' is invalid." >&2 ;
......@@ -316,157 +365,51 @@ verify_domain(){
local _BRIDGE="${5}" ;
shift 5 ;
local _RAM_KB=$((_RAM * 1024)) ;
local _domain_xml ;
local _disk_vda_xml ;
local _interface_xml ;
local _cpu_count ;
local _ram ;
local _cpu_count_matches ;
local _ram_matches ;
local _image_matches ;
local _bridge_matches ;
_domain_xml=$(virsh -q dumpxml "${_NAME}" 2> /dev/null) ;
_disk_vda_xml=$(_get_elements_by_tag_and_sub_tag_and_attribute_from_xml \
"${_domain_xml}" "disk" "target" "dev" "vda" ) ;
_interface_xml=$(_get_elements_by_tag_and_sub_tag_and_attribute_from_xml \
"${_domain_xml}" "interface" "source" "bridge" "${_BRIDGE}" ) ;
_cpu_count=$(_get_content_by_tag_from_xml "${_domain_xml}" "vcpu") ;
_ram=$(_get_content_by_tag_from_xml "${_domain_xml}" "memory") ;
_image=$(_get_value_by_tag_and_attribute_from_xml "${_disk_vda_xml}" "source" "file") ;
_mac_address=$(_get_value_by_tag_and_attribute_from_xml "${_interface_xml}" "mac" "address") ;
echo "CPU count: ${_cpu_count}" ;
echo "RAM: ${_ram}" ;
echo "Image: ${_image}" ;
echo "MAC address: ${_mac_address}" ;
exit 0 ;
local _image ;
local _mac_address ;
if _domain_xml=$(virsh -q dumpxml "${_NAME}" 2> /dev/null) ;
then
echo "${_domain_xml}" |
while IFS="" read -r _line ;
do
case "${_line}" in
*"<vcpu "*">${_CPU_COUNT}</vcpu>"*)
exit 1 ;
;;
esac ;
done &&
_cpu_count_matches=false || _cpu_count_matches=true ;
echo "${_domain_xml}" |
while IFS="" read -r _line ;
do
case "${_line}" in
*"<memory "*">${_RAM_KB}</memory>"*)
exit 1 ;
;;
esac ;
done &&
_ram_matches=false || _ram_matches=true ;
echo "${_domain_xml}" |
while IFS="" read -r _line ;
do
case "${_line}" in
*"<disk "*)
_disk=true ;
;;
*"<target "*"dev='vda'"*"/>")
if test "${_disk}" ;
then
_vda=true ;
fi ;
if \
test "${_disk}" &&
test "${_vda}" &&
test "${_image}" ;
then
exit 1 ;
fi ;
;;
*"<source "*"file='${_IMAGE}'"*"/>"*)
if test "${_disk}" ;
then
_image=true ;
fi ;
if \
test "${_disk}" &&
test "${_vda}" &&
test "${_image}" ;
then
exit 1 ;
fi ;
;;
*"</disk>"*)
unset _disk ;
unset _vda ;
unset _image ;
;;
esac ;
done &&
_image_matches=false || _image_matches=true ;
echo "${_domain_xml}" |
while IFS="" read -r _line ;
do
case "${_line}" in
*"<interface "*"type='bridge'"*">"*)
_bridge=true ;
;;
*"<source "*"bridge='${_BRIDGE}'"*"/>"*)
if test "${_bridge}" ;
then
_source=true ;
fi ;
if \
test "${_bridge}" &&
test "${_source}" &&
( test -z "${_mac_address}" || test "${_mac}" ) ;
then
exit 1 ;
fi ;
;;
*"<mac "*"address='${_mac_address}'"*"/>"*)
if test "${_bridge}" ;
then
_mac=true ;
fi ;
if \
test "${_bridge}" &&
test "${_source}" &&
( test -z "${_mac_address}" || test "${_mac}" ) ;
then
exit 1 ;
fi ;
;;
*"</interface>"*)
unset _bridge ;
unset _source ;
unset _mac ;
;;
esac ;
done &&
_bridge_matches=false || _bridge_matches=true ;
if ${_cpu_count_matches} && ${_ram_matches} && ${_image_matches} && ${_bridge_matches} ;
# The domain exists.
_disk_vda_xml=$(_get_elements_by_tag_and_sub_tag_and_attribute_from_xml \
"${_domain_xml}" "disk" "target" "dev" "vda"
) ;
_interface_xml=$(_get_elements_by_tag_and_attribute_from_xml \
"${_domain_xml}" "interface" "type" "bridge"
) ;
_cpu_count=$(_get_content_by_tag_from_xml "${_domain_xml}" "vcpu") ;
_ram=$(_get_content_by_tag_from_xml "${_domain_xml}" "memory") ;
_image=$(_get_value_by_tag_and_attribute_from_xml "${_disk_vda_xml}" "source" "file") ;
_bridge=$(_get_value_by_tag_and_attribute_from_xml "${_interface_xml}" "source" "bridge") ;
_mac_address=$(_get_value_by_tag_and_attribute_from_xml "${_interface_xml}" "mac" "address") ;
if \
test $((_cpu_count)) -eq $((_CPU_COUNT)) &&
test $((_ram)) -eq $((_RAM * 1024)) &&
test "${_image}" = "${_IMAGE}" &&
test "${_bridge}" = "${_BRIDGE}" &&
( test ! "${_MAC_ADDRESS}" || test "${_mac_address}" = "${_MAC_ADDRESS}" ) &&
true ;
then
exit 0 ;
# The domain exists and is verified.
return 0 ;
else
exit 1 ;
# The domain exists bit is not verified.
return 1 ;
fi ;
else
exit 2 ;
# The domain does not exist.
return 2 ;
fi ;
} ;
......@@ -1089,7 +1032,7 @@ case "${1} ${2} ${3}" in
exit 0 ;
;;
"create domain help"|"verify domain help"|"destroy domain help")
"create domain help"|"examine domain help"|"verify domain help"|"destroy domain help")
shift 3 ;
print_domain_usage_message ;
exit 0 ;
......@@ -1145,6 +1088,12 @@ case "${1} ${2}" in
exit $? ;
;;
"examine domain")
shift 2 ;
examine_domain "${@}" ;
exit $? ;
;;
"verify domain")
shift 2 ;
verify_domain "${@}" ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment