tools/deployment/libvirt/functions.sh
Michel Thebeau 0a924bc76a deployment: libvirt: make use of rm command safer
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>
2018-09-11 11:49:50 -04:00

39 lines
795 B
Bash

#!/usr/bin/env bash
# delete a node's disk file in a safe way
delete_disk() {
local fpath="$1"
if [ ! -f "$fpath" ]; then
echo "file to delete is not a regular file: $fpath" >&2
return 1
fi
file -b "$fpath" | grep -q "^QEMU QCOW Image (v3),"
if [ $? -ne 0 ]; then
echo "file to delete is not QEMU QCOW Image (v3): $fpath" >&2
return 1
fi
sudo rm "$fpath"
}
# delete an xml file in a safe way
delete_xml() {
local fpath="$1"
if [ ! -f "$fpath" ]; then
echo "file to delete is not a regular file: $fpath" >&2
return 1
fi
file -b "$fpath" | grep -q "^ASCII text$"
if [ $? -ne 0 ]; then
echo "file to delete is not ASCII text: $fpath" >&2
return 1
fi
sudo rm "$fpath"
}