Recover LXCs after apt autoremove of "lxc"

Hi Folks,

I accidently apt autoremove-d lxc (lxc-utils remained installed), and it seems my containers (as shown in “lxc list”) have been removed.

Is there a way to recover them?

Thanks, Tom

Hi!

When you create a LXC container, the container files are located under /var/lib/lxc/.
There are subfolders there for each container.
Do you find there your containers?

If the container files are there, then re-installing lxc should be able to show you your containers.

Nope, there’s nothing in /var/lib/lxc/ and /var/lib/lxd only has 992K of data, so I think that means they’re gone. Thanks.

So just confirming a few things:

  • Do you have a /var/lib/lxd ?
  • What’s the output of dpkg -l | grep lxd
  • What’s the output of snap list
  • Do you have a /var/snap/lxd/common/lxd ?

In the event where disaster recovery is needed:

  • What storage backend were you using?
  • Were you using a dedicated disk/partition/lv or using loop storage?

I do have /var/lib/lxd.
$ dpkg -l | grep lxd
ii lxd 3.0.3-0ubuntu1~18.04.1 amd64 Container hypervisor based on LXC - daemon
ii lxd-client 3.0.3-0ubuntu1~18.04.1 amd64 Container hypervisor based on LXC - client
ii python-pylxd 2.2.6-0ubuntu1.1 all Python library for interacting with LXD REST API
$ snap list

lxd 3.18 12631 stable canonical✓ -

And indeed I do see my containers in /var/snap/lxd/common/lxd/containers! I think I was using “dir” storage. So seems hopeful of recovery. I assume I should purge the lxd packages so I’m using the snap?

Have a look first at Comparing LXD vs. LXC and https://blog.simos.info/comparison-between-lxc-and-lxd/ (my post that summarizes the main points of this forum’s thread). These are about the differences between LXC and LXD.

You have installed LXD twice, as a deb package and as a snap package.
And most importantly, your containers are located in /var/snap/lxd/common/lxd/containers, which means that you have been using LXD from the snap package.

But it does not make sense though. Because if you have installed lxd-client (along with the snap package of lxd), then you get the utility /usr/bin/lxc from this deb package, and by default it masks the lxc from the snap package (/snap/bin/lxc).

It is likely that initially you only had the snap package of LXD, and it has been working great. For some reason, the deb package of LXD was installed, which brought in the deb package lxd-client that eventually masked the /snap/bin/lxc from the snap package (because it is later in the $PATH).

Therefore, to verify whether what I am saying is correct, run the following.

/snap/bin/lxc list

If you can see the list of your containers, then all is good.
Report back to get to the next step.

Yep, all there if I run /snap/bin/lxc list (and have verified I can drop into the containers with exec too).

That’s good!

You can

sudo apt remove lxd-client

and it should work for you. By doing this, when you run lxc (after you login again to your computer), you will get the usual working output.

You may also run sudo apt remove lxd to remove the deb package of LXD.

Great, worked a treat.

Thanks for the help!