Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt crun to work with Podman's checkpoint/restore expectations #321

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/libcrun/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,8 +2522,15 @@ libcrun_container_checkpoint (libcrun_context_t *context, const char *id,
ret = read_container_config_from_state (&container, state_root, id, err);
if (UNLIKELY (ret < 0))
return ret;
return libcrun_container_checkpoint_linux (&status, container, cr_options,
err);
ret = libcrun_container_checkpoint_linux (&status, container, cr_options,
err);
if (UNLIKELY (ret < 0))
return ret;

if (!cr_options->leave_running)
return container_delete_internal (context, NULL, id, true, true, err);

return 0;
}

int
Expand All @@ -2532,8 +2539,7 @@ libcrun_container_restore (libcrun_context_t *context, const char *id,
libcrun_error_t *err)
{
cleanup_free libcrun_container_t *container = NULL;
runtime_spec_schema_config_schema *def = NULL;
const char *state_root = context->state_root;
runtime_spec_schema_config_schema *def;
cleanup_free char *cgroup_path = NULL;
libcrun_container_status_t status;
int cgroup_mode, cgroup_manager;
Expand All @@ -2542,23 +2548,33 @@ libcrun_container_restore (libcrun_context_t *context, const char *id,
gid_t root_gid = -1;
int ret;

memset (&status, 0, sizeof (status));
ret = libcrun_read_container_status (&status, state_root, id, err);
container = libcrun_container_load_from_file ("config.json", err);
if (container == NULL)
return -1;

container->context = context;
def = container->container_def;

if (def->oci_version && strstr (def->oci_version, "1.0") == NULL)
return crun_make_error (err, 0, "unknown version specified");

ret = check_config_file (def, err);
if (UNLIKELY (ret < 0))
return ret;

ret = libcrun_is_container_running (&status, err);
ret = libcrun_status_check_directories (context->state_root, context->id, err);
if (UNLIKELY (ret < 0))
return ret;
if (ret == 1)
return crun_make_error (err, 0,
"the container `%s` is not in 'stopped' state",
id);

ret = read_container_config_from_state (&container, state_root, id, err);
ret = libcrun_copy_config_file (context->id, context->state_root, context->bundle, err);
if (UNLIKELY (ret < 0))
return ret;

/* The CRIU restore code uses bundle and rootfs of status. */
memset (&status, 0, sizeof (status));
status.bundle = xstrdup(context->bundle);
status.rootfs = xstrdup (def->root->path);

ret = libcrun_container_restore_linux (&status, container, cr_options, err);
if (UNLIKELY (ret < 0))
return ret;
Expand Down Expand Up @@ -2620,6 +2636,15 @@ libcrun_container_restore (libcrun_context_t *context, const char *id,
if (UNLIKELY (ret < 0))
return ret;

if (context->pid_file)
{
char buf[12];
size_t buf_len = sprintf (buf, "%d", status.pid);
ret = write_file (context->pid_file, buf, buf_len, err);
if (UNLIKELY (ret < 0))
return ret;
}

if (!cr_options->detach)
{
int wait_status;
Expand Down
12 changes: 6 additions & 6 deletions src/libcrun/criu.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status,

runtime_spec_schema_config_schema *def = container->container_def;
cleanup_close int image_fd = -1;
cleanup_free char *path = NULL;
cleanup_free char *root = NULL;
cleanup_close int work_fd = -1;
int ret_out;
size_t i;
int ret;

Expand Down Expand Up @@ -237,8 +237,6 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status,
criu_add_ext_mount (def->linux->masked_paths[i], "/dev/null");
}

xasprintf (&path, "%s/%s", status->bundle, status->rootfs);

/* Mount the container rootfs for CRIU. */
xasprintf (&root, "%s/criu-root", status->bundle);

Expand All @@ -247,7 +245,7 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status,
return crun_make_error (err, errno,
"error creating restore directory %s\n", root);
/* do realpath on root */
ret = mount (path, root, NULL, MS_BIND | MS_REC, NULL);
ret = mount (status->rootfs, root, NULL, MS_BIND | MS_REC, NULL);
if (UNLIKELY (ret == -1))
{
ret = crun_make_error (err, errno,
Expand All @@ -258,7 +256,7 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status,
ret = criu_set_root (root);
if (UNLIKELY (ret != 0))
{
ret = crun_make_error (err, 0, "error setting CRIU root to %s\n", path);
ret = crun_make_error (err, 0, "error setting CRIU root to %s\n", root);
goto out_umount;
}

Expand Down Expand Up @@ -294,8 +292,10 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status,
return crun_make_error (err, errno,
"error unmounting restore directory %s\n", root);
out:
ret = rmdir (root);
ret_out = rmdir (root);
if (UNLIKELY (ret == -1))
return ret;
if (UNLIKELY (ret_out == -1))
return crun_make_error (err, errno,
"error removing restore directory %s\n", root);
return ret;
Expand Down
37 changes: 34 additions & 3 deletions src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ enum
OPTION_WORK_PATH,
OPTION_TCP_ESTABLISHED,
OPTION_SHELL_JOB,
OPTION_EXT_UNIX_SK
OPTION_EXT_UNIX_SK,
OPTION_PID_FILE
};

static char doc[] = "OCI runtime";

static const char *bundle = NULL;

static libcrun_context_t crun_context;

static libcrun_checkpoint_restore_t cr_options;

static struct argp_option options[] = {
{"bundle", 'b', "DIR", 0, "container bundle (default \".\")", 0},
{"image-path", OPTION_IMAGE_PATH, "DIR", 0,
"path for saving criu image files", 0},
{"work-path", OPTION_WORK_PATH, "DIR", 0,
Expand All @@ -55,6 +61,8 @@ static struct argp_option options[] = {
{"ext-unix-sk", OPTION_EXT_UNIX_SK, 0, 0, "allow external unix sockets", 0},
{"shell-job", OPTION_SHELL_JOB, 0, 0, "allow shell jobs", 0},
{"detach", 'd', 0, 0, "detach from the container's process", 0},
{"pid-file", OPTION_PID_FILE, "FILE", 0,
"where to write the PID of the container", 0},
{0,}
};

Expand All @@ -76,6 +84,10 @@ parse_opt (int key, char *arg arg_unused, struct argp_state *state arg_unused)
cr_options.work_path = argp_mandatory_argument (arg, state);
break;

case 'b':
bundle = argp_mandatory_argument (arg, state);
break;

case OPTION_TCP_ESTABLISHED:
cr_options.tcp_established = true;
break;
Expand All @@ -92,6 +104,10 @@ parse_opt (int key, char *arg arg_unused, struct argp_state *state arg_unused)
cr_options.detach = true;
break;

case OPTION_PID_FILE:
crun_context.pid_file = argp_mandatory_argument (arg, state);
break;

default:
return ARGP_ERR_UNKNOWN;
}
Expand All @@ -106,15 +122,29 @@ int
crun_command_restore (struct crun_global_arguments *global_args, int argc,
char **argv, libcrun_error_t *err)
{
cleanup_free char *bundle_cleanup = NULL;
cleanup_free char *cr_path = NULL;
int first_arg;
int ret;

libcrun_context_t crun_context = { 0, };

argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &cr_options);
crun_assert_n_args (argc - first_arg, 1, 2);

/* Make sure the bundle is an absolute path. */
if (bundle)
{
if (bundle[0] != '/')
{
bundle_cleanup = realpath (bundle, NULL);
if (bundle_cleanup == NULL)
libcrun_fail_with_error (errno, "realpath `%s` failed", bundle);
bundle = bundle_cleanup;
}

if (chdir (bundle) < 0)
libcrun_fail_with_error (errno, "chdir `%s` failed", bundle);
}

ret =
init_libcrun_context (&crun_context, argv[first_arg], global_args, err);
if (UNLIKELY (ret < 0))
Expand All @@ -133,6 +163,7 @@ crun_command_restore (struct crun_global_arguments *global_args, int argc,
cr_options.image_path = cr_path;
}

crun_context.bundle = bundle ? bundle : ".";
return libcrun_container_restore (&crun_context, argv[first_arg],
&cr_options, err);
}
16 changes: 12 additions & 4 deletions tests/test_checkpoint_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ def test_cr1():
cmdline_fd.close()

run_crun_command(["_checkpoint", "--image-path=%s" % cr_dir, cid])
s = json.loads(run_crun_command(["state", cid]))
if s['status'] != "stopped":
return -1

run_crun_command(["_restore", "-d", "--image-path=%s" % cr_dir, cid])
bundle = os.path.join(
get_tests_root(),
cid.split('-')[1]
)

run_crun_command([
"_restore",
"-d",
"--image-path=%s" % cr_dir,
"--bundle=%s" % bundle,
cid
])

s = json.loads(run_crun_command(["state", cid]))
if s['status'] != "running":
Expand Down