From: Michel Normand Date: Fri, 2 Apr 2010 16:45:47 +0000 (+0200) Subject: lxc: add --statefile opt to lxc-checkpoint/restart X-Git-Tag: lxc-0.7.0~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b8e796c37e4fa9291de31f94dbc9e06216b58ff;p=thirdparty%2Flxc.git lxc: add --statefile opt to lxc-checkpoint/restart based on patch from: Sukadev Bhattiprolu but also: * remove the deprecated --directory one. * change liblxc api of checkpoint/restart to use fd and not string. * explicitely report error messages for the checkpoint/restart stub functions. Signed-off-by: Michel Normand Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/checkpoint.c b/src/lxc/checkpoint.c index 7e8a93e41..a2d0d8a07 100644 --- a/src/lxc/checkpoint.c +++ b/src/lxc/checkpoint.c @@ -25,7 +25,8 @@ lxc_log_define(lxc_checkpoint, lxc); -int lxc_checkpoint(const char *name, const char *statefile, int flags) +int lxc_checkpoint(const char *name, int sfd, int flags) { - return 0; + ERROR("'checkpoint' function not implemented"); + return -1; } diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index b0b9f4e61..d89102fe3 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -142,23 +142,23 @@ extern const char *lxc_strerror(int error); /* * Checkpoint a container * @name : the name of the container being checkpointed - * @statefile: string object on which the container is checkpointed + * @sfd: fd on which the container is checkpointed * @flags : checkpoint flags (an ORed value) * Returns 0 on success, < 0 otherwise */ -extern int lxc_checkpoint(const char *name, const char *statefile, int flags); +extern int lxc_checkpoint(const char *name, int sfd, int flags); #define LXC_FLAG_PAUSE 1 #define LXC_FLAG_HALT 2 /* * Restart a container * @name : the name of the container being restarted - * @statefile: string object from which the container is restarted + * @sfd: fd from which the container is restarted * @conf: lxc_conf structure. * @flags : restart flags (an ORed value) * Returns 0 on success, < 0 otherwise */ -extern int lxc_restart(const char *, const char *, struct lxc_conf *, int); +extern int lxc_restart(const char *, int, struct lxc_conf *, int); /* * Returns the version number of the library diff --git a/src/lxc/lxc_checkpoint.c b/src/lxc/lxc_checkpoint.c index a8c74a9a3..b67a53d07 100644 --- a/src/lxc/lxc_checkpoint.c +++ b/src/lxc/lxc_checkpoint.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -52,7 +53,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) switch (c) { case 'k': args->flags = LXC_FLAG_HALT; break; case 'p': args->flags = LXC_FLAG_PAUSE; break; - case 'd': args->statefile = arg; break; + case 'S': args->statefile = arg; break; } return 0; } @@ -60,14 +61,14 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) static const struct option my_longopts[] = { {"kill", no_argument, 0, 'k'}, {"pause", no_argument, 0, 'p'}, - {"directory", required_argument, 0, 'd'}, + {"statefile", required_argument, 0, 'S'}, LXC_COMMON_OPTIONS }; static struct lxc_arguments my_args = { .progname = "lxc-checkpoint", .help = "\ ---name=NAME --directory STATEFILE\n\ +--name=NAME --statefile STATEFILE\n\ \n\ lxc-checkpoint checkpoints in STATEFILE the NAME container\n\ \n\ @@ -75,7 +76,7 @@ Options :\n\ -n, --name=NAME NAME for name of the container\n\ -k, --kill stop the container after checkpoint\n\ -p, --pause don't unfreeze the container after the checkpoint\n\ - -d, --directory=STATEFILE where to store the statefile\n", + -S, --statefile=STATEFILE file in which to store the statefile\n", .options = my_longopts, .parser = my_parser, @@ -84,19 +85,10 @@ Options :\n\ .rcfile = NULL, }; -static int create_statefile(const char *dir) -{ - if (mkdir(dir, 0700) == -1 && errno != EEXIST) { - ERROR("'%s' creation error : %m", dir); - return -1; - } - - return 0; -} - int main(int argc, char *argv[]) { int ret; + int sfd = -1; ret = lxc_arguments_parse(&my_args, argc, argv); if (ret) @@ -107,11 +99,14 @@ int main(int argc, char *argv[]) if (ret) return ret; - ret = create_statefile(my_args.statefile); - if (ret) - return ret; +#define OPEN_WRITE_MODE O_CREAT | O_RDWR | O_EXCL | O_CLOEXEC | O_LARGEFILE + sfd = open(my_args.statefile, OPEN_WRITE_MODE, 0600); + if (sfd < 0) { + ERROR("'%s' open failure : %m", my_args.statefile); + return sfd; + } - ret = lxc_checkpoint(my_args.name, my_args.statefile, my_args.flags); + ret = lxc_checkpoint(my_args.name, sfd, my_args.flags); if (ret) ERROR("failed to checkpoint '%s'", my_args.name); else diff --git a/src/lxc/lxc_restart.c b/src/lxc/lxc_restart.c index 7db1d85da..6ce5cafd5 100644 --- a/src/lxc/lxc_restart.c +++ b/src/lxc/lxc_restart.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "log.h" #include "lxc.h" @@ -52,7 +53,7 @@ static int my_checker(const struct lxc_arguments* args) static int my_parser(struct lxc_arguments* args, int c, char* arg) { switch (c) { - case 'd': args->statefile = arg; break; + case 'S': args->statefile = arg; break; case 'f': args->rcfile = arg; break; case 'p': args->flags = LXC_FLAG_PAUSE; break; case 's': return lxc_config_define_add(&defines, arg); @@ -62,7 +63,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) } static const struct option my_longopts[] = { - {"directory", required_argument, 0, 'd'}, + {"statefile", required_argument, 0, 'S'}, {"rcfile", required_argument, 0, 'f'}, {"pause", no_argument, 0, 'p'}, {"define", required_argument, 0, 's'}, @@ -72,14 +73,14 @@ static const struct option my_longopts[] = { static struct lxc_arguments my_args = { .progname = "lxc-restart", .help = "\ ---name=NAME --directory STATEFILE\n\ +--name=NAME --statefile STATEFILE\n\ \n\ lxc-restart restarts from STATEFILE the NAME container\n\ \n\ Options :\n\ -n, --name=NAME NAME for name of the container\n\ -p, --pause do not release the container after the restart\n\ - -d, --directory=STATEFILE for name of statefile\n\ + -S, --statefile=STATEFILE file from which to read data\n\ -f, --rcfile=FILE Load configuration file FILE\n\ -s, --define KEY=VAL Assign VAL to configuration variable KEY\n", .options = my_longopts, @@ -89,6 +90,7 @@ Options :\n\ int main(int argc, char *argv[]) { + int sfd = -1; char *rcfile = NULL; struct lxc_conf *conf; @@ -131,6 +133,12 @@ int main(int argc, char *argv[]) if (lxc_config_define_load(&defines, conf)) return -1; - return lxc_restart(my_args.name, my_args.statefile, conf, - my_args.flags); +#define OPEN_READ_MODE O_RDONLY | O_CLOEXEC | O_LARGEFILE + sfd = open(my_args.statefile, OPEN_READ_MODE, 0); + if (sfd < 0) { + ERROR("'%s' open failure : %m", my_args.statefile); + return sfd; + } + + return lxc_restart(my_args.name, sfd, conf, my_args.flags); } diff --git a/src/lxc/restart.c b/src/lxc/restart.c index 467489ebc..2fbdabf30 100644 --- a/src/lxc/restart.c +++ b/src/lxc/restart.c @@ -25,8 +25,8 @@ lxc_log_define(lxc_restart, lxc); -int lxc_restart(const char *name, const char *statefile, struct lxc_conf *conf, - int flags) +int lxc_restart(const char *name, int sfd, struct lxc_conf *conf, int flags) { - return 0; + ERROR("'restart' function not implemented"); + return -1; }