Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
Last active February 19, 2020 12:05
Show Gist options
  • Save stefanotorresi/ff2c8ed5f47ad8199e1bf856f9928dbf to your computer and use it in GitHub Desktop.
Save stefanotorresi/ff2c8ed5f47ad8199e1bf856f9928dbf to your computer and use it in GitHub Desktop.
Libvirt vacuum cleaner
#!/bin/bash
set -u
EXCLUDE_PATTERN="jenkins-swarm|hana_cockpit|nfs-|angi"
VOLUME_POOLS=(default terraform)
# most code borrowed from https://github.com/openshift/installer scripts
printf 'Warning: This will destroy effectively all libvirt resources\nContinue [yN]? '
read -r CONTINUE
if test "${CONTINUE}" != y -a "${CONTINUE}" != Y
then
echo 'Aborted' >&2
exit 1
fi
for DOMAIN in $(virsh list --all --name | egrep -vi "$EXCLUDE_PATTERN"); do
virsh destroy "$DOMAIN"; virsh undefine "$DOMAIN"
done
for POOL in "${VOLUME_POOLS[@]}"; do
virsh vol-list "$POOL" | egrep -vi "$EXCLUDE_PATTERN" | tail -n +3 | while read -r VOLUME _
do
if test -z "${VOLUME}"; then continue; fi
virsh vol-delete --pool "$POOL" "$VOLUME"
done
done
for NET in $(virsh net-list --all --name | egrep -vi "$EXCLUDE_PATTERN"); do
if test "${NET}" = default; then continue; fi
virsh net-destroy "${NET}"
virsh net-undefine "${NET}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment