Created
September 9, 2022 22:35
-
-
Save linuxmalaysia/3c061c33a05e923c46f17b3ec93eb40b to your computer and use it in GitHub Desktop.
Script to reset LXD LXC for Jammy Ubuntu 22.04 LTS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # https://gist.github.com/ppartarr/018a3858bba8655dc421cc4589598d97 | |
| ## sudo snap install lxd --classic | |
| ## this is only for jammy | |
| ## Harisfazillah Jamel 20221009 | |
| 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" == 'jammy' ]; 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" == 'jammy' ]; 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" == 'jammy' ]; then | |
| ###snap install lxd --channel=3.0/stable | |
| sudo snap install lxd --classic | |
| 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; | |
| exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment