Commit 9b8e796c by Michel Normand Committed by Daniel Lezcano

lxc: add --statefile opt to lxc-checkpoint/restart

based on patch from: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> 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: 's avatarMichel Normand <normand@fr.ibm.com> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 9ea8066a
......@@ -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;
}
......@@ -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
......
......@@ -27,6 +27,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <lxc/lxc.h>
#include <lxc/log.h>
......@@ -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
......
......@@ -27,6 +27,7 @@
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#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);
}
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment