3 quick tips for lxd


Turn on completion in zsh

Include the following lines in your .zshrc

autoload bashcompinit
export -f _have() { which $@ >/dev/null }
source /usr/share/bash-completion/completions/lxc

This turns on ability to use bash completions within zsh.

Then we need to create our own function for the _have bashism that is typically defined in /etc/bash_completion.

Finally just source the already awesome tab-completion for zsh.

launch tmux in a lxc

A slightly more involved version of this.

function lmux() {
    lxc exec $1 -- sh -ic 'WHO=$(awk  -F":" "\$3>999&&\$1!=\"nobody\" {print \$1; exit 1}" /etc/passwd || getent passwd 0 | sed "s_:.*__"); su -c "cd ~; script -qfc \"tmux attach\" /dev/null" $WHO'
function lmux-autocomplete {
    reply=( $(lxc list -c "n" | grep -v '^+' | tr -d '| ' | tail -n +2) )
compctl -K lmux-autocomplete lmux

It gets the first user from /etc/passwd or falls back to root, changes to the home directory of that user and runs “tmux attach” via script which creates a tty for us.

Since we obviously want autocomplete, write a small function to do that and enable it.

Use dns names for your lxcs

If you are using NetworkManager on your machine where lxd is running and want dns names for your containers. Simply run the following:

echo server=/lxd/$(lxc network get lxdbr0 ipv4.address | sed 's/\/.*//') | sudo tee /etc/NetworkManager/dnsmasq.d/lxd.conf
sudo service network-manager restart

This assumes that you are using managed networking so that there is a line like server=/lxd/ (the dnsmasq config line to add a dns server authoritative for the “lxd” TLD from that ip — where the lxd daemon has its own dnsmasq for the managed network).

So now if you can reach your lxcs like so:

$ lxc launch ubuntu cooltest
$ ping cooltest.lxd
PING cooltest.lxd ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from ( icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from ( icmp_seq=3 ttl=64 time=0.043 ms

And use that in your browser if you are doing something. Never have to remember ips again!

3 quick tips for lxd