renaming / lxd managed lxc container

renaming / lxd managed lxc container

  • Written by
    Walter Doekes
  • Published on

Renaming an LXD managed LXC container is not straight forward. But if you want to rename the host from inside the container, you should do so on the outside as well. If you don’t, you may notice that for instance the DHCP manual IP address assignment doesn’t work as expected.

Creating a new LXC container

For example, we’ll create a new container called walter-old with a fresh Debian/Jessie on it.

lxd# lxc image list images: | grep debian/.*amd64
| debian/jessie/amd64 (1 more)    | 44d03be949e5 | yes    | Debian jessie (amd64) (20160320_22:42)    | x86_64  | 104.27MB | Mar 20, 2016 at 11:23pm (UTC) |
| debian/sid/amd64 (1 more)       | 83eaa940759c | yes    | Debian sid (amd64) (20160320_22:42)       | x86_64  | 109.47MB | Mar 20, 2016 at 11:43pm (UTC) |
| debian/stretch/amd64 (1 more)   | 692d8e094ec1 | yes    | Debian stretch (amd64) (20160320_22:42)   | x86_64  | 109.08MB | Mar 20, 2016 at 11:33pm (UTC) |
| debian/wheezy/amd64 (1 more)    | 427b19b85622 | yes    | Debian wheezy (amd64) (20160320_22:42)    | x86_64  | 98.47MB  | Mar 20, 2016 at 11:17pm (UTC) |
lxd# lxc launch images:debian/jessie/amd64 walter-old
Creating walter-old
Retrieving image: 100%
Starting walter-old
lxd# lxc exec walter-old /bin/bash
walter-old# hostname
walter-old

Very well. Let’s assign a fixed IP address to it.

lxd# echo 'dhcp-host=walter-old,10.11.12.13' >> /etc/lxc/dnsmasq.conf
lxd# systemctl restart lxc-net
lxd# lxc stop walter-old
lxd# lxc start walter-old
lxd# lxc list | grep walter-old
| walter-old | RUNNING | 10.11.12.13 (eth0) | PERSISTENT |

Awesome. But now, rename the container and watch it get the wrong IP.

Rename the container from inside

lxd# lxc exec walter-old /bin/bash
walter-old# hostname walter-new
walter-old# echo walter-new > /etc/hostname
walter-old# bash
walter-new# reboot
lxd# lxc list | grep walter-old
| walter-old | RUNNING | 10.11.12.99 (eth0) | PERSISTENT |

Drat! The wrong IP. And we don’t want to put walter-new in /etc/lxc/dnsmasq.conf without renaming the container.

Renaming the container from outside

Stop the container and rename it by moving it on localhost:

lxd# lxc stop walter-old
lxd# lxc list | grep walter-old
| walter-old | STOPPED |                    | PERSISTENT |
lxd# lxc move walter-old walter-new

Rename the host in the dnsmasq.conf:

lxd# sed -i -e 's/walter-old/walter-new/' /etc/lxc/dnsmasq.conf
lxd# systemctl restart lxc-net

That should do it. Start it.

lxd# lxc start walter-new
lxd# lxc list | grep walter-new
| walter-new | RUNNING | 10.11.12.13 (eth0) | PERSISTENT |

Behind the scenes

Behind the scenes, lxc move takes care of the details. Moving things on localhost could be done manually as well. It would look somewhat like this:

lxd# find /var/lib/lxd/ -name 'walter-old*'
/var/lib/lxd/containers/walter-old
/var/lib/lxd/containers/walter-old.zfs
/var/lib/lxd/shmounts/walter-old
/var/lib/lxd/devices/walter-old
/var/lib/lxd/security/seccomp/walter-old
lxd# ls -l /var/lib/lxd/containers | grep walter-old
lrwxrwxrwx 1 root   root      38 Mar 21 13:29 walter-old -> /var/lib/lxd/containers/walter-old.zfs
drwxr-xr-x 4 100000 100000     5 Mar 21 13:29 walter-old.zfs

Start by renaming and moving the ZFS filesystem.

lxd# zfs get mountpoint data/containers/walter-old
NAME                        PROPERTY    VALUE                                   SOURCE
data/containers/walter-old  mountpoint  /var/lib/lxd/containers/walter-old.zfs  local
lxd# umount /var/lib/lxd/containers/walter-old.zfs
lxd# mv /var/lib/lxd/containers/walter-{old,new}.zfs
lxd# zfs rename data/containers/walter-{old,new}
lxd# zfs set mountpoint=/var/lib/lxd/containers/walter-new.zfs data/containers/walter-new
lxd# zfs mount data/containers/walter-new
lxd# mount | grep walter-new
data/containers/walter-new on /var/lib/lxd/containers/walter-new.zfs type zfs (rw,relatime,xattr,noacl)
lxd# rm /var/lib/lxd/containers/walter-old
lxd# ln -s /var/lib/lxd/containers/walter-new{.zfs,}
lxd# ls -lda /var/lib/lxd/containers/walter-new{.zfs,}
lrwxrwxrwx 1 root   root   38 Mar 21 13:40 /var/lib/lxd/containers/walter-new -> /var/lib/lxd/containers/walter-new.zfs
drwxr-xr-x 4 100000 100000  5 Mar 21 13:40 /var/lib/lxd/containers/walter-new.zfs

Next, a few more references:

lxd# find /var/lib/lxd/ -name 'walter-old*'
/var/lib/lxd/shmounts/walter-old
/var/lib/lxd/devices/walter-old
/var/lib/lxd/security/seccomp/walter-old
lxd# mv /var/lib/lxd/shmounts/walter-{old,new}
lxd# mv /var/lib/lxd/devices/walter-{old,new}
lxd# mv /var/lib/lxd/security/seccomp/walter-{old,new}

Lastly, the LXD database:

lxd# sqlite3 /var/lib/lxd/lxd.db
sqlite> select * from containers where name = 'walter-old';
14|walter-old|2|0|0|0|1458563364
sqlite> update containers set name = 'walter-new' where name = 'walter-old';
sqlite> ^D

Back to overview Newer post: lxc - ubuntu xenial - reboot Older post: missing sofiles / linker / asterisk / pjsip