How to reinstall LXD host OS without losing the containers?

I ended up using “snap save lxd”, as described here: https://snapcraft.io/docs/snapshots

Same steps as above, except instead of backup/restore /var/snap/lxd, use this:

  • snap save lxd
  • copy the snapshot file from /var/lib/snapd/snapshots to the new machine (create the directory first, as it may not exist)
  • snap restore {id}

In both cases, when I tried “lxc list” after restore, I got the error:
Error: Get http://unix.socket/1.0: dial unix /var/snap/lxd/common/lxd/unix.socket: connect: no such file or directory

It got fixed by rebooting.