]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/startone.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <lxc/lxccontainer.h>
24 #include <sys/types.h>
31 #define MYNAME "lxctest1"
33 static int destroy_container(void)
44 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
49 ret
= waitpid(pid
, &status
, 0);
61 if (!WIFEXITED(status
)) { // did not exit normally
62 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
66 return WEXITSTATUS(status
);
69 static int create_container(void)
80 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
85 ret
= waitpid(pid
, &status
, 0);
97 if (!WIFEXITED(status
)) { // did not exit normally
98 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
102 return WEXITSTATUS(status
);
105 int main(int argc
, char *argv
[])
107 struct lxc_container
*c
;
116 /* test a real container */
117 c
= lxc_container_new(MYNAME
, NULL
);
119 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
124 if (c
->is_defined(c
)) {
125 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
129 ret
= create_container();
131 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
135 b
= c
->is_defined(c
);
137 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
141 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 200);
143 fprintf(stderr
, "%d: %s not running but had cgroup settings\n", __LINE__
, MYNAME
);
148 b
= c
->set_cgroup_item(c
, "cpuset.cpus", buf
);
150 fprintf(stderr
, "%d: %s not running but coudl set cgroup settings\n", __LINE__
, MYNAME
);
155 if (!s
|| strcmp(s
, "STOPPED")) {
156 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
160 b
= c
->load_config(c
, NULL
);
162 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
166 if (!c
->set_config_item(c
, "lxc.uts.name", "bobo")) {
167 fprintf(stderr
, "%d: failed setting lxc.uts.name\n", __LINE__
);
171 if (!lxc_container_get(c
)) {
172 fprintf(stderr
, "%d: failed to get extra ref to container\n", __LINE__
);
176 c
->want_daemonize(c
, true);
177 if (!c
->startl(c
, 0, NULL
)) {
178 fprintf(stderr
, "%d: %s failed to start\n", __LINE__
, c
->name
);
185 if (!s
|| strcmp(s
, "RUNNING")) {
186 fprintf(stderr
, "%d: %s is in state %s, not in RUNNING.\n", __LINE__
, c
->name
, s
? s
: "undefined");
190 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 0);
192 fprintf(stderr
, "%d: not able to get length of cpuset.cpus (ret %d)\n", __LINE__
, len
);
196 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 200);
197 if (len
<= 0 || strncmp(buf
, "0", 1)) {
198 fprintf(stderr
, "%d: not able to get cpuset.cpus (len %d buf %s)\n", __LINE__
, len
, buf
);
202 sprintf(buf
, "FROZEN");
203 b
= c
->set_cgroup_item(c
, "freezer.state", buf
);
205 fprintf(stderr
, "%d: not able to set freezer.state.\n", __LINE__
);
210 len
= c
->get_cgroup_item(c
, "freezer.state", buf
, 200);
211 if (len
<= 0 || (strcmp(buf
, "FREEZING\n") && strcmp(buf
, "FROZEN\n"))) {
212 fprintf(stderr
, "%d: not able to get freezer.state (len %d buf %s)\n", __LINE__
, len
, buf
);
216 c
->set_cgroup_item(c
, "freezer.state", "THAWED");
220 /* feh - multilib has moved the lxc-init crap */
224 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs//usr/local/libexec/lxc");
226 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc/");
228 ret
= system("cp src/lxc/lxc-init " LXCPATH
"/lxctest1/rootfs//usr/local/libexec/lxc");
230 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc");
232 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc/liblxc.so.0");
234 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib");
236 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs/dev/shm");
238 ret
= system("chroot " LXCPATH
"/lxctest1/rootfs apt-get install --no-install-recommends lxc");
240 fprintf(stderr
, "%d: failed to installing lxc-init in container\n", __LINE__
);
243 // next write out the config file; does it match?
244 if (!c
->startl(c
, 1, "/bin/hostname", NULL
)) {
245 fprintf(stderr
, "%d: failed to lxc-execute /bin/hostname\n", __LINE__
);
248 // auto-check result? ('bobo' is printed on stdout)
252 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
260 lxc_container_put(c
);