Skip to content

Instantly share code, notes, and snippets.

@ppartarr
Created December 1, 2020 12:11
Show Gist options
  • Save ppartarr/018a3858bba8655dc421cc4589598d97 to your computer and use it in GitHub Desktop.
Save ppartarr/018a3858bba8655dc421cc4589598d97 to your computer and use it in GitHub Desktop.
Reset LXD
#!/usr/bin/env bash
set -euo pipefail # -> http://redsymbol.net/articles/unofficial-bash-strict-mode/
# Reverse what lxd init does
function check_distro() {
local distro=$(grep 'DISTRIB_CODENAME' /etc/lsb-release | cut -d '=' -f 2)
echo "$distro"
}
DIST=$(check_distro)
function remove_containers() {
# Hardcore: delete all containers
for name in `lxc list -c n --format csv`; do
lxc delete $name
done
# Delete all OS images
for name in `lxc image list -c f --format csv`; do
lxc image delete $name
done
}
function remove_storage() {
if [ "$DIST" == 'focal' ]; then
snap remove --purge lxd
else
#these don't work... Error: can't delete default profile.
#lxc profile delete default
#lxc storage delete default
#unmount storage pool image before deleting storage pool folder
umount /var/lib/lxd/storage-pools/default
#remove lxc and bunch before removing storage pools
apt remove --purge -y lxd lxc lxc-utils ebtables liblxc-common liblxc1 libpam-cgfs libuv1 lxcfs lxd-client uidmap xdelta3
#remove storage pools...
/bin/rm -rf /var/lib/lxd
/bin/rm -rf /var/lib/lxc
/bin/rm -rf /var/etc/lxc
/bin/rm -rf /var/lib/lxd
/bin/rm -rf /var/lib/lxdlx
fi
}
function reinstall() {
if [ "$DIST" == 'focal' ]; then
snap remove --purge lxd
else
#these don't work... Error: can't delete default profile.
#lxc profile delete default
#lxc storage delete default
#unmount storage pool image before deleting storage pool folder
umount /var/lib/lxd/storage-pools/default
#remove lxc and bunch before removing storage pools
apt remove --purge -y lxd lxc lxc-utils ebtables liblxc-common liblxc1 libpam-cgfs libuv1 lxcfs lxd-client uidmap xdelta3
#remove storage pools...
/bin/rm -rf /var/lib/lxd
/bin/rm -rf /var/lib/lxc
/bin/rm -rf /var/etc/lxc
/bin/rm -rf /var/lib/lxd
/bin/rm -rf /var/lib/lxdlx
fi
}
function reinstall() {
if [ "$DIST" == 'focal' ]; then
snap install lxd --channel=3.0/stable
else
#finally reinstall lxc, lxd
apt install -y lxd lxc
fi
}
echo "removing containers..."
remove_containers;
echo "removing storage..."
remove_storage;
echo "reinstalling lxd / lxc"
reinstall;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment