6

I have Ubuntu 16.04 installed, xfce desktop for what it's worth. I have LXD installed, with my images and containers on my large second drive, with symbolic links to said images and containers configured in /var/lib/lxd/. Everything works as expected.

I have now re-installed Ubuntu 16.04 on a new drive, Mate desktop for what it's worth. I have installed LXD and created the symbolic links (in /var/lib/lxd) to my images and containers folders on my large drive.

Neither my images nor containers are listed when using lxc list (so can't start them and use them).

Any pointers on what I need to do to get my newly installed LXD to use my pre-existing images and containers ?

hatterman
  • 2,260
  • 2
  • 21
  • 33

2 Answers2

3

For anybody interested, the missing piece of the puzzle was that I had to re-use the existing lxd database, lxd.db.

So the steps for me to use my pre-existing LXD containers on a fresh install are:-

Install LXD

sudo apt-get install lxd

Delete the containers and images directories

sudo rm -rf /var/lib/lxd/containers
sudo rm -rf /var/lib/lxd/images

Create symbolic links to my pre-existing containers and images directories.

sudo ln -s /media/sda10/lxd/containers /var/lib/lxd/containers
sudo ln -s /media/sda10/lxd/images /var/lib/lxd/images

Mount my original install partition and copy the lxd.db file from my original install onto my new install

sudo cp /mnt/var/lib/lxd/lxd.db /var/lib/lxd/lxd.db

My containers now show up when I list them and run as expected.

NOTE : I have multiple installations on multiple partitions, and I leap frog between new versions of Ubuntu as they become available, and don't erase an older version until I am happy. It's really easy to do and has just become second nature.

I know that's not common practice, but it has (again) saved my bacon on this occasion. For those who don't do this, making a backup of the lxd.db file would be advised.

EDIT : Further reading at https://insights.ubuntu.com/2016/04/13/lxd-2-0-remote-hosts-and-container-migration-612/ shows that it is also very easy to use a remote LXD host and simply move conatiners between hosts.

This isn't the solution for me, in this particular instance, but it is a very good read showing a very easy way to migrate containers.

hatterman
  • 2,260
  • 2
  • 21
  • 33
  • You could probably also `lxd image import` the images you want to re-use into your separate db. – Jonathan Y. May 09 '17 at 13:04
  • That wouldn't work really, as I want to use one of my containers, not one of the images the container was built from. I did find out I can export a container as an image, re-import that image, then make a new container from it but that just seemed a bit of a faf. I wonder if there is a way to simply export/import containers ? – hatterman May 09 '17 at 15:14
  • There is, if your LXD version is higher than 2.7. If so, the container's storage contains a .ymal which allows importing it to a new db. – Jonathan Y. May 09 '17 at 16:09
  • Great. Could you tell me how it works or give me a link to a how to ? – hatterman May 09 '17 at 19:51
  • As I am using the same PC, with the same large storage drive, just with a newly installed OS, I think my own answer is still correct (for me, in this instance). However, reading the answer from Jonathan Y prompted me to check out the LXD website (again) and I did see that remote hosts can be copied and moved very easily, so I have added an edit to my answer. – hatterman May 10 '17 at 15:29
  • Has anyone figured out how lxd import works? The instructions are extremely unclear and I can't find out how to do it. I want to use lxd import, and NOT one of the other ways. Thanks. – alexk Jul 02 '17 at 13:03
1

Since my comments contain some typos, I'll recap a couple of suggestions for importing an existing container (whose storage is available) into a new DB.

First, there are (arguably) easier alternatives if one wants to copy an entire instance of LXD (by basically taking the storage of all containers, plus the DB from /var/lib/lxd). Also, if the old instance is still running and reachable by network, a more native approach is to define it as a remote as described in the Remote hosts and container migration chapter in the wonderful Ubuntu Insights LXD 2.0: Blog post series.


Now, one approach described in another chapter of that series (again, only feasible if the old instance is still running) is to make an image of an existing container using

lxc publish <my-container>@<snapshot> <my-image-name>

then export it to tarballs with

lxc image export <my-image-name> /path/to/directory

The resulting tarballs can be imported with

lxc image import <metadata-tarball> <rootfs-tarball> --alias <my-image-name>

As OP said in comments, that's quite a bit of work. What's more serious is that it fails if for some reason you can't run the old instance.

As of version 2.7 LXD stores a backup.yaml file inside each container's storage (at /var/lib/lxd/containers/<my-container>) which allows importing a container from a copy, using lxd import (see LXD Backup Strategies and this GitHub issue). Note the lxd; this is different from lxc image import.

Based on the LXD News page I presume one does that by simply moving a container's storage into the /var/lib/lxd/containers directory, and running

lxd import <my-container>

However, as Xenial official repositories currently offer version 2.0.9 of LXD, I haven't tested that last part.

Jonathan Y.
  • 1,034
  • 1
  • 16
  • 35
  • That's not how you're supposed to use lxd import. I tried it and it doesn't work. Have you figured a way to use it? – alexk Jul 02 '17 at 13:05
  • @alexk I'm not sure, although: (a) this will only work on LXD 2.7 or higher, and (b) see the provided link. If your version is higher than 2.7, then perhaps `lxd import --help` might steer you in the right direction? Please edit this answer if you find the way it's meant to be run. (Or post the error message in a comment.) – Jonathan Y. Jul 02 '17 at 13:55
  • 1
    There's progress in the discussion here, about this topic: https://github.com/lxc/lxd/issues/3479 – alexk Jul 02 '17 at 21:24