LXD recovery after HDD failure

For anyone who encounters the same situation, here is my successful solution. I needed a few days to find out the right steps and their ordering, fighting various error messages.

Recovery containers from a damaged drive, assuming you managed to backup/dump the zfs partition on a second HDD that is now in a new machine with Ubuntu 20.04, goes as follows:

  1. sudo snap install lxd && sudo apt install zfsutils-linux
    installs the necessary stuff
  2. sudo adduser $(whoami) lxd && newgrp lxd
    adds you to the lxd group granting you permissions to manage LXD
  3. sudo zpool import
    prints a list of available volumes to import, remember a long number at the top, e.g. 1234567890123456789
  4. sudo zpool import -d /dev -f -R / 1234567890123456789 default
    imports the volume, note that long number copied from the listing in the previous step
    – if you just want to read the files, you can also add -o readonly=on but that will not allow you to import any container
    – i chose -R / since the mountpoints for all containers were already correctly specified as /var/snap/lxd/common/lxd/storage-pools/default/containers/container_name (after zpool import just check by zfs list otherwise export and import again with a new path (or just reboot)
    – the last argument is the pool name, use the same as you had on the old machine
  5. Maybe you need to call lxc info once, not sure but it will do no harm anyway.
  6. create any profiles you used in the old setup, e.g. X11
  7. sudo zfs mount default/containers/container_name
  8. sudo lxd import --force container_name
  9. sudo zfs unmount default/containers/container_name
  10. repeat 7. - 9. for all containers you need to recover

lxc list should now show all recovered containers, note that after starting their IP address will not be assigned. Now you may either export the containers, re-install lxd (now finally calling lxd init as well) and import the images again or you may follow the suggestions over here to initialize network and storage manually:

Fingers crossed for your recovery!