How to add multi-line raw.lxc configuration to LXD

In LXD, you can add multiple settings in a single command line. For example, to both limit the memory to 2GB and the CPUs to a single core, you would run the following in a single line. Obviously, you can set these separately as well.

lxc config set mycontainer limits.memory=2GB limits.cpu=1

See the LXD key/value configuration options for more settings. In most cases, the type of the value is either string or integer. However, there are cases where the type is blob. Currently, there are four cases of blob,

Blob data types in LXD

Blob is a special type, and it means that LXD takes the value verbatim and does not perform any processing by itself. This means that if you want to set a multi-line blob, the following will not work, because raw.lxc will keep one of the values.

$ lxc config set mycontainer raw.lxc="lxc.cgroup.devices.allow = c 10 237" raw.lxc="lxc.cgroup.devices.allow = b 7 *"
$ lxc config show mycontainer
raw.lxc: lxc.cgroup.devices.allow = b 7 *

In addition, the following will not work either, because of parsing errors (multiple = characters).

lxc config set mycontainer raw.lxc='lxc.cgroup.devices.allow = c 10 237\nlxc.cgroup.devices.allow = b 7 *'

Moreover, the following will not work either, because LXD will take the multi-line string as a single string.

$ echo 'lxc.cgroup.devices.allow=c 10 237\nlxc.cgroup.devices.allow=b 7 *' | lxc config set mycontainer raw.lxc -
$ lxc config show mycontainer
raw.lxc: |
     lxc.cgroup.devices.allow=c 10 237\nlxc.cgroup.devices.allow=b 7 *
$ # But why? Because
$ echo "lxc.cgroup.devices.allow=c 10 237\nlxc.cgroup.devices.allow=b 7 *"
lxc.cgroup.devices.allow=c 10 237\nlxc.cgroup.devices.allow=b 7 *

echo does not interpret the control characters by default, unless you do shopt -s xpg_echo first. Or, unless you add the -e flag in the echo command to enable the interpretation of the _backslash escapes_, as Uli reminds us in the comments.

How to add then multi-line blobs to LXD?

We can use either printf (or echo -e), as in the following.

$ printf 'lxc.cgroup.devices.allow = c 10 237\nlxc.cgroup.devices.allow = b 7 *' | lxc config set mycontainer raw.lxc -

The configuration now looks as the following.

$ lxc config show mycontainer
raw.lxc: |-
   lxc.cgroup.devices.allow = c 10 237
   lxc.cgroup.devices.allow = b 7 *

Alternatively, if you are comfortable in using the text mode editors, you can try with lxc config edit mycontainer. Make sure to obey the spaces and avoid adding tabs to the configuration. When you save and exit the text editor, the configuration is parsed and saved.

Permanent link to this article:


    • Uli Heller on November 11, 2019 at 05:34
    • Reply

    You could use “echo -e”:

    $ echo “a\nb”
    $ echo -e “a\nb”

    1. Thanks!

      I updated the post to reflect this.

    • steven prothero on August 14, 2020 at 04:38
    • Reply

    totalled solved another lxd mystery, thanks much!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.