/
checkpoint.c
186 lines (154 loc) · 5.21 KB
/
checkpoint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2020 Adrian Reber <areber@redhat.com>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <regex.h>
#if HAVE_CRIU && HAVE_DLOPEN
# include <criu/criu.h>
#endif
#include "crun.h"
#include "libcrun/container.h"
#include "libcrun/status.h"
#include "libcrun/utils.h"
enum
{
OPTION_IMAGE_PATH = 1000,
OPTION_WORK_PATH,
OPTION_LEAVE_RUNNING,
OPTION_TCP_ESTABLISHED,
OPTION_SHELL_JOB,
OPTION_EXT_UNIX_SK,
OPTION_FILE_LOCKS,
OPTION_PARENT_PATH,
OPTION_PRE_DUMP,
OPTION_MANAGE_CGROUPS_MODE,
};
static char doc[] = "OCI runtime";
static libcrun_checkpoint_restore_t cr_options;
static struct argp_option options[]
= { { "image-path", OPTION_IMAGE_PATH, "DIR", 0, "path for saving criu image files", 0 },
{ "work-path", OPTION_WORK_PATH, "DIR", 0, "path for saving work files and logs", 0 },
{ "leave-running", OPTION_LEAVE_RUNNING, 0, 0, "leave the process running after checkpointing", 0 },
{ "tcp-established", OPTION_TCP_ESTABLISHED, 0, 0, "allow open tcp connections", 0 },
{ "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 },
{ "file-locks", OPTION_FILE_LOCKS, 0, 0, "allow file locks", 0 },
#ifdef CRIU_PRE_DUMP_SUPPORT
{ "parent-path", OPTION_PARENT_PATH, "DIR", 0, "path for previous criu image files in pre-dump", 0 },
{ "pre-dump", OPTION_PRE_DUMP, 0, 0, "dump container's memory information only, leave the container running after this", 0 },
#endif
{ "manage-cgroups-mode", OPTION_MANAGE_CGROUPS_MODE, "MODE", 0, "cgroups mode: 'soft' (default), 'ignore', 'full' and 'strict'", 0 },
{
0,
} };
static char args_doc[] = "checkpoint CONTAINER";
int
crun_parse_manage_cgroups_mode (char *param arg_unused)
{
#if HAVE_CRIU && HAVE_DLOPEN
if (strcmp (param, "soft") == 0)
return CRIU_CG_MODE_SOFT;
else if (strcmp (param, "ignore") == 0)
return CRIU_CG_MODE_IGNORE;
else if (strcmp (param, "full") == 0)
return CRIU_CG_MODE_FULL;
else if (strcmp (param, "strict") == 0)
return CRIU_CG_MODE_STRICT;
else
libcrun_fail_with_error (0, "unknown cgroup mode specified");
#else
return 0;
#endif
}
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ARGP_KEY_NO_ARGS:
libcrun_fail_with_error (0, "please specify a ID for the container");
case OPTION_IMAGE_PATH:
cr_options.image_path = argp_mandatory_argument (arg, state);
break;
case OPTION_WORK_PATH:
cr_options.work_path = argp_mandatory_argument (arg, state);
break;
case OPTION_PARENT_PATH:
cr_options.parent_path = argp_mandatory_argument (arg, state);
break;
case OPTION_PRE_DUMP:
cr_options.pre_dump = true;
break;
case OPTION_LEAVE_RUNNING:
cr_options.leave_running = true;
break;
case OPTION_TCP_ESTABLISHED:
cr_options.tcp_established = true;
break;
case OPTION_EXT_UNIX_SK:
cr_options.ext_unix_sk = true;
break;
case OPTION_SHELL_JOB:
cr_options.shell_job = true;
break;
case OPTION_FILE_LOCKS:
cr_options.file_locks = true;
break;
case OPTION_MANAGE_CGROUPS_MODE:
cr_options.manage_cgroups_mode = crun_parse_manage_cgroups_mode (argp_mandatory_argument (arg, state));
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, NULL };
int
crun_command_checkpoint (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
{
cleanup_free char *cr_path = NULL;
int first_arg;
int ret;
libcrun_context_t crun_context = {
0,
};
cr_options.manage_cgroups_mode = -1;
argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &cr_options);
crun_assert_n_args (argc - first_arg, 1, 2);
ret = init_libcrun_context (&crun_context, argv[first_arg], global_args, err);
if (UNLIKELY (ret < 0))
return ret;
if (cr_options.image_path == NULL)
{
cleanup_free char *path = NULL;
path = getcwd (NULL, 0);
if (UNLIKELY (path == NULL))
libcrun_fail_with_error (0, "realloc failed");
ret = asprintf (&cr_path, "%s/checkpoint", path);
if (UNLIKELY (ret < 0))
OOM ();
cr_options.image_path = cr_path;
}
return libcrun_container_checkpoint (&crun_context, argv[first_arg], &cr_options, err);
}