]>
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.
22 #include <lxc/lxccontainer.h>
27 #include <sys/types.h>
34 #define MYNAME "lxctest1"
36 static int destroy_container(void)
47 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
52 ret
= waitpid(pid
, &status
, 0);
64 if (!WIFEXITED(status
)) { // did not exit normally
65 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
69 return WEXITSTATUS(status
);
72 static int create_container(void)
83 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
88 ret
= waitpid(pid
, &status
, 0);
100 if (!WIFEXITED(status
)) { // did not exit normally
101 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
105 return WEXITSTATUS(status
);
108 int main(int argc
, char *argv
[])
110 struct lxc_container
*c
;
119 /* test a real container */
120 c
= lxc_container_new(MYNAME
, NULL
);
122 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
127 if (c
->is_defined(c
)) {
128 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
132 ret
= create_container();
134 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
138 b
= c
->is_defined(c
);
140 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
144 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 200);
146 fprintf(stderr
, "%d: %s not running but had cgroup settings\n", __LINE__
, MYNAME
);
151 b
= c
->set_cgroup_item(c
, "cpuset.cpus", buf
);
153 fprintf(stderr
, "%d: %s not running but could set cgroup settings\n", __LINE__
, MYNAME
);
158 if (!s
|| strcmp(s
, "STOPPED")) {
159 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
163 b
= c
->load_config(c
, NULL
);
165 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
169 if (!c
->set_config_item(c
, "lxc.uts.name", "bobo")) {
170 fprintf(stderr
, "%d: failed setting lxc.uts.name\n", __LINE__
);
174 if (!lxc_container_get(c
)) {
175 fprintf(stderr
, "%d: failed to get extra ref to container\n", __LINE__
);
179 c
->want_daemonize(c
, true);
180 if (!c
->startl(c
, 0, NULL
)) {
181 fprintf(stderr
, "%d: %s failed to start\n", __LINE__
, c
->name
);
188 if (!s
|| strcmp(s
, "RUNNING")) {
189 fprintf(stderr
, "%d: %s is in state %s, not in RUNNING.\n", __LINE__
, c
->name
, s
? s
: "undefined");
193 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 0);
195 fprintf(stderr
, "%d: not able to get length of cpuset.cpus (ret %d)\n", __LINE__
, len
);
199 len
= c
->get_cgroup_item(c
, "cpuset.cpus", buf
, 200);
200 if (len
<= 0 || strncmp(buf
, "0", 1)) {
201 fprintf(stderr
, "%d: not able to get cpuset.cpus (len %d buf %s)\n", __LINE__
, len
, buf
);
205 sprintf(buf
, "FROZEN");
206 b
= c
->set_cgroup_item(c
, "freezer.state", buf
);
208 fprintf(stderr
, "%d: not able to set freezer.state.\n", __LINE__
);
213 len
= c
->get_cgroup_item(c
, "freezer.state", buf
, 200);
214 if (len
<= 0 || (strcmp(buf
, "FREEZING\n") && strcmp(buf
, "FROZEN\n"))) {
215 fprintf(stderr
, "%d: not able to get freezer.state (len %d buf %s)\n", __LINE__
, len
, buf
);
219 c
->set_cgroup_item(c
, "freezer.state", "THAWED");
223 /* feh - multilib has moved the lxc-init crap */
227 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs//usr/local/libexec/lxc");
229 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc/");
231 ret
= system("cp src/lxc/lxc-init " LXCPATH
"/lxctest1/rootfs//usr/local/libexec/lxc");
233 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc");
235 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib/lxc/liblxc.so.0");
237 ret
= system("cp src/lxc/liblxc.so " LXCPATH
"/lxctest1/rootfs/usr/lib");
239 ret
= system("mkdir -p " LXCPATH
"/lxctest1/rootfs/dev/shm");
241 ret
= system("chroot " LXCPATH
"/lxctest1/rootfs apt-get install --no-install-recommends lxc");
243 fprintf(stderr
, "%d: failed to installing lxc-init in container\n", __LINE__
);
246 // next write out the config file; does it match?
247 if (!c
->startl(c
, 1, "/bin/hostname", NULL
)) {
248 fprintf(stderr
, "%d: failed to lxc-execute /bin/hostname\n", __LINE__
);
251 // auto-check result? ('bobo' is printed on stdout)
255 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
263 lxc_container_put(c
);