From e2cc0cbebb52e30863ca23d69e8024c15870408b Mon Sep 17 00:00:00 2001 From: Borja Aranda Date: Wed, 8 Jun 2022 12:36:12 +0200 Subject: [PATCH] clean(libvirt-pool): control unbound --- hack/clean/clean-libvirt-pool.sh | 100 ++++++++++++++++--------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/hack/clean/clean-libvirt-pool.sh b/hack/clean/clean-libvirt-pool.sh index a6656edc6..a07f8672c 100755 --- a/hack/clean/clean-libvirt-pool.sh +++ b/hack/clean/clean-libvirt-pool.sh @@ -1,64 +1,68 @@ #!/usr/bin/env bash set -o pipefail -set -o nounset set -m -resourcesUsed="" +if [ -n "${1}" ]; then -# get all disk and isos in use -getUsedResources() { - for vm in $(kcli list vm -o json | jq -r .[].name); do - # preserve ISO/iso used by existing vm's - resourcesUsed=$(printf "%s|${resourcesUsed}" "${vm}.ISO") + resourcesUsed="" - # some vm's have a boot-ID iso - depending on how they were created - resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep iso: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')") + # get all disk and isos in use + getUsedResources() { + for vm in $(kcli list vm -o json | jq -r .[].name); do + # preserve ISO/iso used by existing vm's + resourcesUsed=$(printf "%s|${resourcesUsed}" "${vm}.ISO") - # preserve images used by existing vm's - resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep image: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')") + # some vm's have a boot-ID iso - depending on how they were created + resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep iso: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')") - # preserve all disks used by existing vm's - for disk in $(kcli show vm $vm | grep -E 'diskname:' | awk '{print $10}' | sed 's#\/var\/lib\/libvirt\/images\/##'); do - resourcesUsed=$(printf "%s|${resourcesUsed}" "${disk}") + # preserve images used by existing vm's + resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep image: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')") + + # preserve all disks used by existing vm's + for disk in $(kcli show vm $vm | grep -E 'diskname:' | awk '{print $10}' | sed 's#\/var\/lib\/libvirt\/images\/##'); do + resourcesUsed=$(printf "%s|${resourcesUsed}" "${disk}") + done done - done -} + } -# get the resources -getUsedResources + # get the resources + getUsedResources -# build a cmd that greps out the *used* resources -# cmd has to guarantee resources in toDelete are *unused* -# sanitize output by removing any trailing character -# and also double || introduced by getUsedResources\ -# failing to do this will make cmd unusable and unreliable -resourcesGrep=$(echo $resourcesUsed | sed 's/.$//' | sed 's/||/|/'g) -cmd=$(printf "ls /var/lib/libvirt/images | grep -Ev '%s'" ${resourcesGrep}) -toDelete=$(eval $cmd) + # build a cmd that greps out the *used* resources + # cmd has to guarantee resources in toDelete are *unused* + # sanitize output by removing any trailing character + # and also double || introduced by getUsedResources\ + # failing to do this will make cmd unusable and unreliable + resourcesGrep=$(echo $resourcesUsed | sed 's/.$//' | sed 's/||/|/'g) + cmd=$(printf "ls /var/lib/libvirt/images | grep -Ev '%s'" ${resourcesGrep}) + toDelete=$(eval $cmd) -# dry-run will prompt the images to be deleted -# now will wipe the pool -case "${1}" in - 'dry-run') - echo "############### ${0} dry-run" - echo "############### resources in use - won't be deleted" - echo $cmd - echo "" + # dry-run will prompt the images to be deleted + # now will wipe the pool + case $1 in + 'dry-run') + echo "############### ${0} dry-run" + echo "############### resources in use - won't be deleted" + echo $cmd + echo "" - echo "############### resources unused - will be delete" - for resource in $(echo $toDelete); do - echo $resource - done - ;; + echo "############### resources unused - will be delete" + for resource in $(echo $toDelete); do + echo $resource + done + ;; - 'now') - for resource in $(echo $toDelete); do - kcli delete disk --novm --yes ${resource} - done - ;; + 'now') + for resource in $(echo $toDelete); do + kcli delete disk --novm --yes ${resource} + done + ;; - *) - echo "Usage: $0 [dry-run|now]" - ;; -esac \ No newline at end of file + *) + echo "Usage: [dry-run|now]" + ;; + esac +else + echo "Usage: [dry-run|now]" +fi