The Starting Point

Annotating LXC containers with a custom comment field allows you to add meaningful descriptions or additional information to your containers. Unfortunately this isn’t a standard feature of LXC, but it is quite easy to retrofit.

This guide refers to bash-based terminal shells. Other shells like zsh or fish can be adapted to be compatible however. Let’s go!

The Solution

To be able to write comments and retrieve them later via lxc list, just open up your ~/.bashrc file and add the following custom function to the end of the file:

# Custom LXC Functions
lxc() {
    if [[ $1 == "list" ]]; then
        command lxc list -c ns46tS,user.comment:comment
    elif [[ $1 == "comment" ]]; then
        command lxc config set "$2" user.comment "$3"
        command lxc "$@"

After saving and closing the file, log out of the terminal session and log back in.


The inserted function is now active and ready for use.

lxc comment infra-front01 "Frontend"

lxc list
|                NAME                |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |    COMMENT     |
| infra-front01                      | RUNNING | (eth0)  | fd42:67c7:4bf3:1813:216:3eff:fe54:97fd (eth0) | CONTAINER | 0         | Frontend       |

The Explanation

Basically, LXC already has the functionality to add custom fields built in. However, the commands for writing and retrieving these custom fields – and thus a proposed comment field – are quite lengthy and hard to remember. The provided lxc() function enables the use of aliases to simplify the commands, making them more user-friendly.

This way, the modified lxc list breaks down to the native command of lxc list -c ns46tS,user.comment:comment, whereas the command for lxc comment is completely fictional. It boils down to lxc config set "$2" user.comment "$3".

If you want to compose your own lxc list table with custom columns and sort orders, you actually can. Just refer to the official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like