Find orphaned juju instances in LXD cloud

I’m looking for advice on how to find orphaned juju instances in a LXD cloud.

I am looking to free up storage in my zfs pool.

I discovered an orphaned instance juju-012954-0

zfs list | tr -s ' ' | cut -d" "  -f1,2 | grep G
default/containers/juju-012954-0 1.49G

I tried to track it down with

juju models --all --uuid | grep 012954

So, I couldn’t find a model matching that instance based on the uuid.

Now, since I have many, many containers - I would need some advice on how to track down all orphaned containers so I can manually purge them.

1 Like

So Juju uses the last 6 bytes of the model-uuid to give the containers a unique identifier. To get the list of Juju’s model uuid infixes you can do:

juju models --all --uuid --format=json | jq -r '.["models"]|.[]|.["model-uuid"]' | cut -b 31-

And then you want to compare that to the names of all the containers on the system:

$ lxc list --all-projects --format json | jq -r '.[].name'

And if you just want the model uuids known by lxd it could be

lxc list --all-projects --format json | jq -r '.[].name' | grep ^juju | cut -d - -f 2

If you put both of those through sort -u you could then just do a diff and see what uuids are known only to lxc, though you’d then need to go through and find the actual container names again.

eg:

$ juju models --all --uuid --format=json | jq -r '.["models"]|.[]|.["model-uuid"]' | cut -b 31- | sort -u > juju-uuids.txt
$ lxc list --all-projects --format json | jq -r '.[].name' | grep ^juju > lxc-juju-containers.txt 
$ cat lxc-juju-containers.txt | cut -d - -f 2 | sort -u > lxc-uuids.txt
$ diff -u juju-uuids.txt lxc-uuids.txt
$ for uuid in `comm -13 juju-uuids.txt lxc-uuids.txt` ; do # only lines in lxc-uuids.txt
  echo $uuid;
  grep $uuid lxc-juju-containers.txt;
done;

(this is the first I’ve used comm but it definitely seems like a useful tool)

1 Like

I’ll try this out and see how it goes! Thanx for this!

Hey @jameinel! Just wanted to share my script that is based on your awesome answer. Basically force to be logged in as admin to see all models. And specify lxc remote and juju cloud. Since we have one cloud per lxc host. Thanks!

#!/bin/bash

# Check if there are instances on the LXD host that does not exist in Juju.

LXC_REMOTE=$1
JUJU_CLOUD=$2

user=$(juju whoami --format json | jq -r '.user')
if [ "$user" != "admin" ]; then
  echo "You must be logged in as user 'admin'."
  exit 1
fi

if [ "$1" == "-h" ] | [ -z "$LXC_REMOTE" ] | [ -z "$JUJU_CLOUD" ]; then
  echo "Usage: ./orphaned-instances.sh <lxc-remote> <juju-cloud>"
	exit 1
fi

juju models --all --uuid --format=json | jq -r --arg JUJU_CLOUD "$JUJU_CLOUD" '.["models"]|.[]| select(.cloud==$JUJU_CLOUD)|.["model-uuid"]' | cut -b 31- | sort -u > juju-uuids.txt
lxc list --all-projects --format json $LXC_REMOTE: | jq -r '.[].name' | grep ^juju > lxc-juju-containers.txt 
cat lxc-juju-containers.txt | cut -d - -f 2 | sort -u > lxc-uuids.txt
for uuid in `comm -13 juju-uuids.txt lxc-uuids.txt` ; do # only lines in lxc-uuids.txt
  grep $uuid lxc-juju-containers.txt;
done;
1 Like

@hallback check this out.

1 Like

@joakimnyman might be good to also have some controller parameter in there since this would be returning models from a controller which might not be aware of the cloud+remote used in the call?