saveconfig.c revision bb9702b539188bcc0c37f351633b087d1c22d2a6
45d862b9c655542aa44c02958011065f6994f512Trond Norbye/* liblxcapi
45d862b9c655542aa44c02958011065f6994f512Trond Norbye *
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * Copyright © 2012 Canonical Ltd.
45d862b9c655542aa44c02958011065f6994f512Trond Norbye *
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * This program is free software; you can redistribute it and/or modify
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * it under the terms of the GNU General Public License version 2, as
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * published by the Free Software Foundation.
45d862b9c655542aa44c02958011065f6994f512Trond Norbye *
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * This program is distributed in the hope that it will be useful,
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * but WITHOUT ANY WARRANTY; without even the implied warranty of
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * GNU General Public License for more details.
45d862b9c655542aa44c02958011065f6994f512Trond Norbye *
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * You should have received a copy of the GNU General Public License along
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * with this program; if not, write to the Free Software Foundation, Inc.,
45d862b9c655542aa44c02958011065f6994f512Trond Norbye * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45d862b9c655542aa44c02958011065f6994f512Trond Norbye */
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include "../lxc/lxccontainer.h"
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <unistd.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <signal.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <stdio.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <sys/types.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <sys/wait.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <stdlib.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#include <errno.h>
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye#define MYNAME "lxctest1"
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbyestatic int create_ubuntu(void)
45d862b9c655542aa44c02958011065f6994f512Trond Norbye{
45d862b9c655542aa44c02958011065f6994f512Trond Norbye int status, ret;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye pid_t pid = fork();
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (pid < 0) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye perror("fork");
45d862b9c655542aa44c02958011065f6994f512Trond Norbye return -1;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (pid == 0) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye ret = execlp("lxc-create", "lxc-create", "-t", "ubuntu", "-f", "/etc/lxc/default.conf", "-n", MYNAME, NULL);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye // Should not return
45d862b9c655542aa44c02958011065f6994f512Trond Norbye perror("execl");
45d862b9c655542aa44c02958011065f6994f512Trond Norbye exit(1);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbyeagain:
45d862b9c655542aa44c02958011065f6994f512Trond Norbye ret = waitpid(pid, &status, 0);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (ret == -1) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (errno == -EINTR)
45d862b9c655542aa44c02958011065f6994f512Trond Norbye goto again;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye perror("waitpid");
45d862b9c655542aa44c02958011065f6994f512Trond Norbye return -1;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (ret != pid)
45d862b9c655542aa44c02958011065f6994f512Trond Norbye goto again;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (!WIFEXITED(status)) { // did not exit normally
45d862b9c655542aa44c02958011065f6994f512Trond Norbye fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye return -1;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye return WEXITSTATUS(status);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye}
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbyeint main(int argc, char *argv[])
45d862b9c655542aa44c02958011065f6994f512Trond Norbye{
a937449043f1e649320ccb609d9d1de16f6a9208Knut Anders Hatlen struct lxc_container *c;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye int ret = 1;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if ((c = lxc_container_new(MYNAME)) == NULL) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye ret = 1;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye goto out;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (c->is_defined(c)) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye goto out;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (create_ubuntu()) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye fprintf(stderr, "%d: failed to create a ubuntu container\n", __LINE__);
45d862b9c655542aa44c02958011065f6994f512Trond Norbye goto out;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
45d862b9c655542aa44c02958011065f6994f512Trond Norbye if (!c->is_defined(c)) {
45d862b9c655542aa44c02958011065f6994f512Trond Norbye fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
a937449043f1e649320ccb609d9d1de16f6a9208Knut Anders Hatlen goto out;
45d862b9c655542aa44c02958011065f6994f512Trond Norbye }
45d862b9c655542aa44c02958011065f6994f512Trond Norbye
c->load_config(c, NULL);
unlink("/tmp/lxctest1");
if (!c->save_config(c, "/tmp/lxctest1")) {
fprintf(stderr, "%d: failed writing config file /tmp/lxctest1\n", __LINE__);
goto out;
}
rename(LXCPATH "/" MYNAME "/config", LXCPATH "/" MYNAME "/config.bak");
if (!c->save_config(c, NULL)) {
fprintf(stderr, "%d: failed writing config file\n", __LINE__);
goto out;
}
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
lxc_container_put(c);
exit(ret);
}