[SOLVED] Best way to repackage a running lxc container?

Hi Stephane,

First, Happy New Year 2020 to you all the whole Linux Containers team! It looks like your will be very busy with lots of projects on this front :slight_smile:

Thanks for your answer concerning the configuration part, I have tried updating my yaml file and indeed the error about the configuration part has disappeared.

Here is the updated YAML file (quickpath.yml below), updated as per your suggestions:

image: 
  description: Shortcut for Packer development
  distribution: ubuntu 
  release: 0.1

source:
  downloader: ubuntu-http

packages:
  manager: apt

targets:
  lxc:
    create-message: |-
        This "shortcut" container was created out of a Packer build by distrobuilder.

    config:
      - type: all
        content: |-
          lxc.include = LXC_TEMPLATE_CONFIG/common.conf

          # For Ubuntu 14.04
          lxc.mount.entry = /sys/kernel/debug sys/kernel/debug none bind,optional 0 0
          lxc.mount.entry = /sys/kernel/security sys/kernel/security none bind,optional 0 0
          lxc.mount.entry = /sys/fs/pstore sys/fs/pstore none bind,optional 0 0
          lxc.mount.entry = mqueue dev/mqueue mqueue rw,relatime,create=dir,optional 0 0

      - type: user
        content: |-
          lxc.include = LXC_TEMPLATE_CONFIG/userns.conf

          # For Ubuntu 14.04
          lxc.mount.entry = /sys/firmware/efi/efivars sys/firmware/efi/efivars none bind,optional 0 0
          lxc.mount.entry = /proc/sys/fs/binfmt_misc proc/sys/fs/binfmt_misc none bind,optional 0 0

      - type: all
        content: |-
          lxc.arch = {{ image.architecture_personality }}

The container creation does not output any error anymore:

$ sudo distrobuilder pack-lxc quickpath.yml /var/lib/lxc/lxc_app-backend_ubuntu/ /tmp/vincent/
$ sudo lxc-create -n backend-respawn -t local -- -m /tmp/vincent/meta.tar.xz -f /tmp/vincent/rootfs.tar.xz

The created container does not start though. With logging enabled, I can see this is because /sbin/init cannot be found:

$ sudo lxc-start -n backend-respawn --logfile=/tmp/lxc-start.txt
lxc-start: backend-respawn: lxccontainer.c: wait_on_daemonized_start: 872 Received container state "ABORTING" instead of "RUNNING"
lxc-start: backend-respawn: tools/lxc_start.c: main: 329 The container failed to start
lxc-start: backend-respawn: tools/lxc_start.c: main: 332 To get more details, run the container in foreground mode                                                                                                                            
lxc-start: backend-respawn: tools/lxc_start.c: main: 334 Additional information can be obtained by setting the --logfile and --logpriority options
$ cat /tmp/lxc-start.txt
lxc-start backend-respawn 20200107124444.562 ERROR    start - start.c:start:2121 - No such file or directory - Failed to exec "/sbin/init"
lxc-start backend-respawn 20200107124444.562 ERROR    sync - sync.c:__sync_wait:61 - An error occurred in another process (expected sequence number 7)
lxc-start backend-respawn 20200107124444.582 ERROR    lxccontainer - lxccontainer.c:wait_on_daemonized_start:872 - Received container state "ABORTING" instead of "RUNNING"
lxc-start backend-respawn 20200107124444.583 ERROR    start - start.c:__lxc_start:2036 - Failed to spawn container "backend-respawn"
lxc-start backend-respawn 20200107124444.583 ERROR    lxc_start - tools/lxc_start.c:main:329 - The container failed to start
lxc-start backend-respawn 20200107124444.583 ERROR    lxc_start - tools/lxc_start.c:main:332 - To get more details, run the container in foreground mode
lxc-start backend-respawn 20200107124444.583 ERROR    lxc_start - tools/lxc_start.c:main:334 - Additional information can be obtained by setting the --logfile and --logpriority options

Looking at the new container root FS, I can see some of its rootfs got expanded outside the rootfs directory, which appear to contain the original rootfs:

$ sudo ls /var/lib/lxc/backend-respawn/rootfs -ls
total 32
4 -rw-r--r--.  1 root root  363 Jan  7 12:03 config
4 drwxr-xr-x.  3 root root 4096 Jan  7 13:43 dev
4 drwxr-xr-x.  2 root root 4096 Jan  7 12:09 etc
4 drwxr-xr-x.  2 root root 4096 Jan  7 12:09 proc
4 drwxr-xr-x. 22 root root 4096 Jan  7 12:03 rootfs
4 drwxr-xr-x.  2 root root 4096 Jan  7 12:09 run
4 drwxr-xr-x.  2 root root 4096 Jan  7 12:09 sys
4 drwxr-xr-x.  2 root root 4096 Jan  7 12:09 tmp

$ sudo ls /var/lib/lxc/backend-respawn/rootfs/rootfs -al
total 88
drwxr-xr-x. 22 root    root   4096 Jan  7 12:03 .
drwxr-xr-x.  9 root    root   4096 Jan  7 13:44 ..
drwxr-xr-x.  2 root    root   4096 Jan  7 10:35 bin
drwxr-xr-x.  2 root    root   4096 Apr 24  2018 boot
drwxr-xr-x.  3 root    root   4096 Jan  7 10:35 dev
drwxr-xr-x. 74 root    root   4096 Jan  7 10:37 etc
drwxr-xr-x.  4 root    root   4096 Jan  7 10:36 home
drwxr-xr-x. 12 root    root   4096 Jan  7 10:37 lib
drwxr-xr-x.  2 root    root   4096 Dec 30 08:43 lib64
drwxr-xr-x.  2 root    root   4096 Dec 30 08:42 media
drwxr-xr-x.  2 root    root   4096 Dec 30 08:42 mnt
drwxr-xr-x.  2 root    root   4096 Dec 30 08:42 opt
drwxr-xr-x.  2 root    root   4096 Apr 24  2018 proc
drwx------.  7 root    root   4096 Jan  7 12:03 root
drwxr-xr-x.  2 root    root   4096 Dec 30 08:45 run
drwxr-xr-x.  2 root    root   4096 Jan  7 10:35 sbin
drwxr-xr-x.  2 root    root   4096 Dec 30 08:42 srv
drwxr-xr-x.  2 root    root   4096 Apr 24  2018 sys
drwxrwxrwt.  2 root    root   4096 Jan  7 10:44 tmp
drwxr-xr-x. 10 root    root   4096 Dec 30 08:42 usr
drwxr-xr-x. 12 root    root   4096 Dec 30 08:44 var
drwxr-xr-x. 10 vincent docker 4096 Jan  7 10:36 xdt

I also note that the original container got polluted in the same vein with those additional directories (that did not exist before distrobuilder was called):

$ ls -l /var/lib/lxc/lxc_redpesk-backend_ubuntu/
total 32K
-rw-r--r--.  1 root root  363 Jan  7 11:37 config
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 dev/
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 etc/
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 proc/
drwxr-xr-x. 22 root root 4.0K Jan  7 11:37 rootfs/
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 run/
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 sys/
drwxr-xr-x.  2 root root 4.0K Jan  7 11:40 tmp/

I have checked the new container config file and the rootfs path appears correct:

$ sudo cat /var/lib/lxc/backend-respawn/config
# Template used to create this container: /usr/share/lxc/templates/lxc-local
# Parameters passed to the template: -m /tmp/vincent/meta.tar.xz -f /tmp/vincent/rootfs.tar.xz
# Template script checksum (SHA-1): 381a40a454bdaf18de159d6d50bba0cf7425af4b
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)


# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf

# For Ubuntu 14.04
lxc.mount.entry = /sys/kernel/debug sys/kernel/debug none bind,optional 0 0
lxc.mount.entry = /sys/kernel/security sys/kernel/security none bind,optional 0 0
lxc.mount.entry = /sys/fs/pstore sys/fs/pstore none bind,optional 0 0
lxc.mount.entry = mqueue dev/mqueue mqueue rw,relatime,create=dir,optional 0 0
lxc.arch = linux64

# Container specific configuration
lxc.rootfs.path = dir:/var/lib/lxc/backend-respawn/rootfs
lxc.uts.name = backend-respawn

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:a3:74:f9

Do you know what could be causing this directory messup? Is there a particular layout the container directory should abide by when distrobuilder is called to pack it into config/rootfs?

Thanks much for your help!

Edit: I tried to use distrobuilder build-dir as per the doc at https://distrobuilder.readthedocs.io/en/latest/building/ to inspect the input state of pack-lxc but this fails:

$ sudo distrobuilder build-dir ~/Documents/Dev/repos/community/lxc-ci/images/ubuntu.yaml /tmp/vincent-lxc-ci
I: usage: [OPTION]... <suite> <target> [<mirror> [<script>]]
I: Try `debootstrap --help' for more information.
E: You must specify a suite and a target.
Error: Error while downloading source: exit status 1

According to the Jenkins logs at https://jenkins.linuxcontainers.org/job/image-ubuntu/architecture=amd64,release=bionic,restrict=lxc-priv,variant=default/2/console, it looks like the doc might be missing some parameters. I can indeed see the Jenkins invocation as being:

distrobuilder --timeout 3600 build-dir image.yaml rootfs -o image.serial=20200107_07:42 -o image.architecture=amd64 -o image.release=bionic -o image.variant=default -o source.url=http://us.archive.ubuntu.com/ubuntu