Lxc Bash Completion For Fedora

LXC is great, I’m really loving it.

However I also love Fedora and work almost exclusively in the Red Hat, Centos, Fedora ecosphere - so the Ubuntu centric nature of LXC is sometimes a bit of a hindrance.

One thing is that the LXC implementation on Ubuntu and Fedora differs in how the commands are run, eg Ubuntu: lxc start -n containerName and Fedora: lxc-start -n containerName

This means that I can’t easily use the LXC bash completion I found here

I love bash completion, it’s awesome and really quite easy to get working

Here is something quick I threw together for working with LXC on Fedora:


#!/usr/bin/env bash

_lxc-containers(){
    local cur
    local -a toks
    cur="${COMP_WORDS[COMP_CWORD]}"
    toks=( $(  ls /var/lib/lxc | grep "$cur" ))
    COMPREPLY=( "${toks[@]}" )
    return 0
}
complete -F _lxc-containers -o nospace lxc-start
complete -F _lxc-containers -o nospace lxc-stop
complete -F _lxc-containers -o nospace lxc-attach

I also use the following aliases to make life a bit easier:

alias lxc-start="sudo lxc-start -n "
alias lxc-stop="sudo lxc-stop -n "
alias lxc-attach="sudo lxc-attach -n "

And now if I add both of the above to my ~/.bashrc file, I get completion on start, stop and attach. I’ve deliberately excluded the destroy command from this, just in case.

Note the use of sudo meaning that you avoid issues around not being root (haven’t got around unpriviledged containers yet) and also avoid the temptation of keeping a root terminal open


Tags: lxcfedorabashcompletion