blob: d85560f110daaf7913292e8f288fbdad365daf46 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#!/usr/bin/env bash
# Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "linux-home" project.
# For details, see https://github.com/egor-tensin/linux-home.
# Distributed under the MIT License.
# This script destroys all libvirt resources.
set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit
CONNECT="${CONNECT:=qemu:///system}"
POOL="${POOL:=default}"
dump() {
local prefix="${FUNCNAME[0]}"
[ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}"
local msg
for msg; do
echo "$prefix: $msg"
done
}
run_virsh() {
virsh -c "$CONNECT" "$@"
}
list_domains() {
run_virsh list --all --name
}
list_networks() {
local output
output="$( run_virsh net-list --all --name )"
local name
while IFS= read -r name; do
[ "$name" = default ] && continue
echo "$name"
done <<< "$output"
}
list_volumes() {
local output
output="$( run_virsh vol-list "$POOL" | tail -n +3 | tr -s ' ' )"
local name
while IFS=' ' read -r name _; do
echo "$name"
done <<< "$output"
}
remove_domain() {
local domain
for domain; do
dump "$domain"
run_virsh destroy "$domain"
run_virsh undefine "$domain"
done
}
remove_network() {
local network
for network; do
dump "$network"
run_virsh net-destroy "$network"
run_virsh net-undefine "$network"
done
}
remove_volume() {
local volume
for volume; do
dump "$volume"
run_virsh vol-delete --pool "$POOL" "$volume"
done
}
main() {
local output item
output="$( list_domains )"
if [ -n "$output" ]; then
while IFS= read -r item; do remove_domain "$item"; done <<< "$output"
fi
output="$( list_volumes )"
if [ -n "$output" ]; then
while IFS= read -r item; do remove_volume "$item"; done <<< "$output"
fi
output="$( list_networks )"
if [ -n "$output" ]; then
while IFS= read -r item; do remove_network "$item"; done <<< "$output"
fi
}
main
|