Can't add device using REST API

I use the REST API and I’m trying to add a device to a container. If I understand the docs correctly, I have to fetch all current devices, add my new device to the list and issue a PUT request. This is the JSON I came up with (I don’t have any other devices):

{
  "devices": {
    "nginx": {
      "listen": "tcp:0.0.0.0:46729",
      "connect": "tcp:10.219.66.52:80",
      "bind": "host",
      "type": "proxy"
    }
  }
}

This results in this error:

Invalid expanded devices: Failed detecting root disk device: No root device could be found

When I try to add the device using the CLI, it works perfectly:

lxc config device add containerName nginx proxy listen=tcp:0.0.0.0:46729 connect=tcp:10.219.66.52:80 bind=host

What am I doing wrong here?

A PUT query replaces the current state with the new state.
So your PUT is clearing all config and existing devices from the container and replacing that with just that one device.

What you should do is do a GET to get the current state, then add your device to that and PUT the result.

Thanks for the quick reply! :smiley:

In several tries, I did GET the current state before PUTting it, but I was still getting the “Invalid expanded devices” error. It seems like after GETting the current state, I have to copy the contents of expanded_devices over to devices so that it contains the root disk. After that I’m able to add and remove devices with PUT. Is it worth mentioning this in the docs?

No, you don’t need to and shouldn’t copy expanded_devices to devices.
However you need to make sure that you do not alter profiles otherwise the devices coming from the profiles would be missing, causing the error.

Note that you can run whatever lxc config device command you want with --debug and it should show you what it fetches and pushes to LXD.

1 Like

Thanks for the tip using --debug. That helped a lot.

I was copying expanded_devices to devices because, in earlier tries, the devices array was empty, which caused the error. I think the trial & error fried my brain, because when I GET the current state now, LXD does give me a filled devices array. So it appears there wasn’t a problem at all, so thanks a lot for sticking with me!