![Michel Thebeau](/assets/img/avatar_default.png)
Add sanity around the use of 'rm' in script. Make sure the file to be deleted is the correct type, and that its content also meets expectation. An example of dangerous outcome in this code from using "sudo rm -rf" is that if the CONTROLLER or COMPUTE variables are preceded by a space character then the directory /var/lib/libvirt/images/ will be deleted. Refrain from using recursive and force options, and in this case because they are not needed. Related-Bug: #1790716 Change-Id: I76797133589d993dca2b2aac3c97184bac0457ca Signed-off-by: Michel Thebeau <michel.thebeau@windriver.com>
43 lines
1.5 KiB
Bash
Executable File
43 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
MY_WORKING_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
|
|
|
|
source ${MY_WORKING_DIR}/functions.sh
|
|
|
|
BRIDGE_INTERFACE=${BRIDGE_INTERFACE:-stxbr}
|
|
CONTROLLER=${CONTROLLER:-controller}
|
|
COMPUTE=${COMPUTE:-compute}
|
|
DOMAIN_DIRECTORY=vms
|
|
|
|
for i in {0..1}; do
|
|
CONTROLLER_NODE=${CONTROLLER}-${i}
|
|
DOMAIN_FILE=$DOMAIN_DIRECTORY/$CONTROLLER_NODE.xml
|
|
if virsh list --all --name | grep ${CONTROLLER_NODE}; then
|
|
STATUS=$(virsh list --all | grep ${CONTROLLER_NODE} | awk '{ print $3}')
|
|
if ([ "$STATUS" == "running" ])
|
|
then
|
|
sudo virsh destroy ${CONTROLLER_NODE}
|
|
fi
|
|
sudo virsh undefine ${CONTROLLER_NODE}
|
|
delete_disk /var/lib/libvirt/images/${CONTROLLER_NODE}-0.img
|
|
delete_disk /var/lib/libvirt/images/${CONTROLLER_NODE}-1.img
|
|
[ -e ${DOMAIN_FILE} ] && delete_xml ${DOMAIN_FILE}
|
|
fi
|
|
done
|
|
|
|
for i in {0..1}; do
|
|
COMPUTE_NODE=${COMPUTE}-${i}
|
|
DOMAIN_FILE=$DOMAIN_DIRECTORY/$COMPUTE_NODE.xml
|
|
if virsh list --all --name | grep ${COMPUTE_NODE}; then
|
|
STATUS=$(virsh list --all | grep ${COMPUTE_NODE} | awk '{ print $3}')
|
|
if ([ "$STATUS" == "running" ])
|
|
then
|
|
sudo virsh destroy ${COMPUTE_NODE}
|
|
fi
|
|
sudo virsh undefine ${COMPUTE_NODE}
|
|
delete_disk /var/lib/libvirt/images/${COMPUTE_NODE}-0.img
|
|
delete_disk /var/lib/libvirt/images/${COMPUTE_NODE}-1.img
|
|
[ -e ${DOMAIN_FILE} ] && delete_xml ${DOMAIN_FILE}
|
|
fi
|
|
done
|