[lxc-devel] [lxd/master] scripts: Add script to completely reset LXD

stgraber on Github lxc-bot at linuxcontainers.org
Wed May 22 19:21:11 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190522/b31504d3/attachment.bin>
-------------- next part --------------
From 1d8ce974d3da18ac1c2a04eebd5fe7f0ac4cbc7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 22 May 2019 14:41:56 -0400
Subject: [PATCH] scripts: Add script to completely reset LXD
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #5782

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 scripts/empty-lxd.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100755 scripts/empty-lxd.sh

diff --git a/scripts/empty-lxd.sh b/scripts/empty-lxd.sh
new file mode 100755
index 0000000000..a979db8f52
--- /dev/null
+++ b/scripts/empty-lxd.sh
@@ -0,0 +1,52 @@
+#!/bin/sh -eu
+if ! which jq >/dev/null 2>&1; then
+    echo "This tool requires: jq"
+    exit 1
+fi
+
+## Delete anything that's tied to a project
+for project in $(lxc query "/1.0/projects?recursion=1" | jq .[].name -r); do
+    echo "==> Deleting all containers for project: ${project}"
+    for container in $(lxc query "/1.0/containers?recursion=1&project=${project}" | jq .[].name -r); do
+        lxc delete --project "${project}" -f "${container}"
+    done
+
+    echo "==> Deleting all images for project: ${project}"
+    for image in $(lxc query "/1.0/images?recursion=1&project=${project}" | jq .[].fingerprint -r); do
+        lxc image delete --project "${project}" "${image}"
+    done
+done
+
+for project in $(lxc query "/1.0/projects?recursion=1" | jq .[].name -r); do
+    echo "==> Deleting all profiles for project: ${project}"
+    for profile in $(lxc query "/1.0/profiles?recursion=1&project=${project}" | jq .[].name -r); do
+        if [ "${profile}" = "default" ]; then
+            printf 'config: {}\ndevices: {}' | lxc profile edit --project "${project}" default
+            continue
+        fi
+        lxc profile delete --project "${project}" "${profile}"
+    done
+
+    if [ "${project}" != "default" ]; then
+        echo "==> Deleting project: ${project}"
+        lxc project delete "${project}"
+    fi
+done
+
+## Delete the networks
+echo "==> Deleting all networks"
+for network in $(lxc query "/1.0/networks?recursion=1" | jq '.[] | select(.managed) | .name' -r); do
+    lxc network delete "${network}"
+done
+
+## Delete the storage pools
+echo "==> Deleting all storage pools"
+for storage_pool in $(lxc query "/1.0/storage-pools?recursion=1" | jq .[].name -r); do
+    for volume in $(lxc query "/1.0/storage-pools/${storage_pool}/volumes/custom?recursion=1" | jq .[].name -r); do
+        echo "==> Deleting storage volume ${volume} on ${storage_pool}"
+        lxc storage volume delete "${storage_pool}" "${volume}"
+    done
+
+    ## Delete the custom storage volumes
+    lxc storage delete "${storage_pool}"
+done


More information about the lxc-devel mailing list