# How convert an LXC container to systemd-nspawn

1. Stop the container with `lxc-stop -n container_name`.
2. Create a config file called e.g. `/etc/systemd/nspawn/container_name.nspawn`.
   Look at the other files for inspiration. Here's a basic example:
   ```
   [Exec]
   Boot=yes
   Hostname=container_name
   PrivateUsers=no

   [Network]
   Bridge=br0
   ```
   **IMPORTANT**: make sure that `PrivateUsers` is set to `no`. Otherwise the
   UIDs and GIDs will get screwed up.
3. Determine where the container's rootfs was mounted on the host. This
   will be set in `/var/lib/lxc/container_name/config`. Usually it will be
   something like `/var/lib/lxc/container_name/rootfs`. It might also be
   `/var/lib/lxc/container_name` itself.
4. Create a symlink under `/var/lib/machines` pointing to this rootfs, e.g.
   ```sh
   ln -s /vm/container_name/rootfs /var/lib/machines/container_name
   ```
5. Remove/rename the LXC config file so that LXC does not try to start it anymore:
   ```sh
   mv /var/lib/lxc/container_name/config /var/lib/lxc/container_name/config.bak
   ```
6. Leave a kind note behind to future sysadmins so that they know the
   container is no longer managed by LXC:
   ```sh
   touch /var/lib/lxc/container_name/THIS_IS_A_SYSTEMD_NSPAWN_CONTAINER
   ```
7. If the container's rootfs is an LVM volume, double-check that it gets
   mounted at boot time in /etc/fstab.
8. Edit the /etc/network/interfaces file in the container and replace all
   instances of `eth0` by `host0`:
   ```sh
   vim /var/lib/lxc/container_name/rootfs/etc/network/interfaces
   ```
   If the container was using systemd-networkd instead, first mask all
   of the files under /lib/systemd/network:
   ```sh
   chroot /var/lib/lxc/container_name/rootfs
   cd /lib/systemd/network
   for file in 80-container-*.network; do ln -s /dev/null /etc/systemd/network/$file; done
   ```
   Then edit the network file:
   ```sh
   cd /etc/systemd/network
   mv eth0.network host0.network
   # Replace eth0 with host0 in the file
   vim host0.network
   exit
   ```
9. Start the nspawn container:
   ```sh
   machinectl start container_name
   ```
10. Check that the container is running, then attach to it and make sure that
    everything is OK:
    ```sh
    machinectl status container_name
    machinectl shell container_name
    # Make sure that systemd services are running, IP address got set, etc.
    ```
11. Once you are sure that everything is OK, enable the container's systemd
    service:
    ```sh
    machinectl enable container_name
    ```