Query on creating containers using c api and not from commands

Hi,

I am using the below code to create the container.

#include <stdio.h>

#include <lxc/lxccontainer.h>

int main() {
struct lxc_container *c;
int ret = 1;

/* Setup container struct */
c = lxc_container_new("apicontainer", "/var/lib/lxc");
if (!c) {
    fprintf(stderr, "Failed to setup lxc_container struct");
    goto out;
}

if (c->is_defined(c)) {
    fprintf(stderr, "Container already exists");
    goto out;
}

/* Create the container */
if (!c->createl(c, "none", NULL, NULL, LXC_CREATE_QUIET, NULL)) {
    fprintf(stderr, "Failed to create container rootfs");
    goto out;
}

/* Start the container */
if (!c->start(c, 0, NULL)) {
    fprintf(stderr, "Failed to start the container");
    goto out;
}

/* Query some information */
printf("Container state: %s", c->state(c));
printf("Container PID: %d", c->init_pid(c));

/* Stop the container */
if (!c->shutdown(c, 30)) {
    printf("Failed to cleanly shutdown the container, forcing.");
    if (!c->stop(c)) {
        fprintf(stderr, "Failed to kill the container.");
        goto out;
    }
}

/* Destroy the container */
if (!c->destroy(c)) {
    fprintf(stderr, "Failed to destroy the container.");
    goto out;
}

ret = 0;

out:
lxc_container_put(c);
return ret;
}

Queries:

  1. Should i create “apicontainer” folder and place config file inside that before executing the program?
    If i do so program says container already exists.

If i remove the apicontainer folder, i get below error

strace -f ./a.out  ✘
execve("./a.out", ["./a.out"], 0x7ffe6b4b85e8 /* 92 vars /) = 0
brk(NULL) = 0x5634cc33b000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=108796, …}) = 0
mmap(NULL, 108796, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f9cafbd2000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/usr/local/lib/liblxc.so.1”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320S\1\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2980096, …}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9cafbd0000
mmap(NULL, 2841192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9caf710000
mprotect(0x7f9caf7c2000, 2093056, PROT_NONE) = 0
mmap(0x7f9caf9c1000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xb1000) = 0x7f9caf9c1000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/lib/x86_64-linux-gnu/libc.so.6”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, …}) = 0
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9caf31f000
mprotect(0x7f9caf506000, 2097152, PROT_NONE) = 0
mmap(0x7f9caf706000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f9caf706000
mmap(0x7f9caf70c000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f9caf70c000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/lib/x86_64-linux-gnu/libseccomp.so.2”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\27\0\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=129312, …}) = 0
mmap(NULL, 2224480, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9caf0ff000
mprotect(0x7f9caf11d000, 2097152, PROT_NONE) = 0
mmap(0x7f9caf31d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e000) = 0x7f9caf31d000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/lib/x86_64-linux-gnu/libutil.so.1”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\16\0\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=10592, …}) = 0
mmap(NULL, 2105616, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9caeefc000
mprotect(0x7f9caeefe000, 2093056, PROT_NONE) = 0
mmap(0x7f9caf0fd000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1000) = 0x7f9caf0fd000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/lib/x86_64-linux-gnu/libpthread.so.0”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000b\0\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=144976, …}) = 0
mmap(NULL, 2221184, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9caecdd000
mprotect(0x7f9caecf7000, 2093056, PROT_NONE) = 0
mmap(0x7f9caeef6000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19000) = 0x7f9caeef6000
mmap(0x7f9caeef8000, 13440, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f9caeef8000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9cafbce000
arch_prctl(ARCH_SET_FS, 0x7f9cafbcef40) = 0
mprotect(0x7f9caf706000, 16384, PROT_READ) = 0
mprotect(0x7f9caeef6000, 4096, PROT_READ) = 0
mprotect(0x7f9caf0fd000, 4096, PROT_READ) = 0
mprotect(0x7f9caf31d000, 4096, PROT_READ) = 0
mprotect(0x7f9caf9c1000, 8192, PROT_READ) = 0
mprotect(0x5634cb77e000, 4096, PROT_READ) = 0
mprotect(0x7f9cafbed000, 4096, PROT_READ) = 0
munmap(0x7f9cafbd2000, 108796) = 0
set_tid_address(0x7f9cafbcf210) = 6029
set_robust_list(0x7f9cafbcf220, 24) = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7f9caece2cb0, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f9caecef8a0}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7f9caece2d50, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f9caecef8a0}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192
1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL) = 0x5634cc33b000
brk(0x5634cc35c000) = 0x5634cc35c000
stat("/run", {st_mode=S_IFDIR|0777, st_size=1200, …}) = 0
geteuid() = 1001
getegid() = 1002
mkdir("/", 0755) = -1 EEXIST (File exists)
mkdir("/run/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/lxc/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/lxc/lock//", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/lxc/lock//var/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/lxc/lock//var/lib/", 0755) = -1 EEXIST (File exists)
mkdir("/run/user/1001/lxc/lock//var/lib/lxc", 0755) = -1 EEXIST (File exists)
stat("/var/lib/lxc/apicontainer/config", 0x7ffc690001b0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, “/var/lib/lxc/apicontainer/partial”, O_RDWR|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/var/lib/lxc/apicontainer/config", 0x7ffc690001f0) = -1 ENOENT (No such file or directory)
access("/usr/local/share/lxc/templates/lxc-none", X_OK) = -1 ENOENT (No such file or directory)
stat("/var/lib/lxc/apicontainer/config", 0x7ffc68fff7a0) = -1 ENOENT (No such file or directory)
write(2, “Failed to create container rootf”…, 33Failed to create container rootfs) = 33
exit_group(1) = ?
+++ exited with 1 +++

Prerequisites is not clear.

It’s complaining that there is no template called none I believe.