]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * Copyright © 2014-2015 Canonical Ltd. | |
5 | * | |
6 | * Authors: | |
7 | * Tycho Andersen <tycho.andersen@canonical.com> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | #define _GNU_SOURCE | |
24 | #include <assert.h> | |
25 | #include <inttypes.h> | |
26 | #include <linux/limits.h> | |
27 | #include <sched.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/mount.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/wait.h> | |
34 | #include <unistd.h> | |
35 | ||
36 | #include "config.h" | |
37 | ||
38 | #include "bdev.h" | |
39 | #include "cgroup.h" | |
40 | #include "conf.h" | |
41 | #include "commands.h" | |
42 | #include "criu.h" | |
43 | #include "log.h" | |
44 | #include "lxc.h" | |
45 | #include "lxclock.h" | |
46 | #include "network.h" | |
47 | #include "utils.h" | |
48 | ||
49 | #if IS_BIONIC | |
50 | #include <../include/lxcmntent.h> | |
51 | #else | |
52 | #include <mntent.h> | |
53 | #endif | |
54 | ||
55 | #define CRIU_VERSION "2.0" | |
56 | ||
57 | #define CRIU_GITID_VERSION "2.0" | |
58 | #define CRIU_GITID_PATCHLEVEL 0 | |
59 | ||
60 | #define CRIU_IN_FLIGHT_SUPPORT "2.4" | |
61 | ||
62 | lxc_log_define(lxc_criu, lxc); | |
63 | ||
64 | struct criu_opts { | |
65 | /* the thing to hook to stdout and stderr for logging */ | |
66 | int pipefd; | |
67 | ||
68 | /* The type of criu invocation, one of "dump" or "restore" */ | |
69 | char *action; | |
70 | ||
71 | /* the user-provided migrate options relevant to this action */ | |
72 | struct migrate_opts *user; | |
73 | ||
74 | /* The container to dump */ | |
75 | struct lxc_container *c; | |
76 | ||
77 | /* dump: stop the container or not after dumping? */ | |
78 | char tty_id[32]; /* the criu tty id for /dev/console, i.e. "tty[${rdev}:${dev}]" */ | |
79 | ||
80 | /* restore: the file to write the init process' pid into */ | |
81 | struct lxc_handler *handler; | |
82 | int console_fd; | |
83 | /* The path that is bind mounted from /dev/console, if any. We don't | |
84 | * want to use `--ext-mount-map auto`'s result here because the pts | |
85 | * device may have a different path (e.g. if the pty number is | |
86 | * different) on the target host. NULL if lxc.console = "none". | |
87 | */ | |
88 | char *console_name; | |
89 | ||
90 | /* The detected version of criu */ | |
91 | char *criu_version; | |
92 | }; | |
93 | ||
94 | static int load_tty_major_minor(char *directory, char *output, int len) | |
95 | { | |
96 | FILE *f; | |
97 | char path[PATH_MAX]; | |
98 | int ret; | |
99 | ||
100 | ret = snprintf(path, sizeof(path), "%s/tty.info", directory); | |
101 | if (ret < 0 || ret >= sizeof(path)) { | |
102 | ERROR("snprintf'd too many chacters: %d", ret); | |
103 | return -1; | |
104 | } | |
105 | ||
106 | f = fopen(path, "r"); | |
107 | if (!f) { | |
108 | /* This means we're coming from a liblxc which didn't export | |
109 | * the tty info. In this case they had to have lxc.console = | |
110 | * none, so there's no problem restoring. | |
111 | */ | |
112 | if (errno == ENOENT) | |
113 | return 0; | |
114 | ||
115 | SYSERROR("couldn't open %s", path); | |
116 | return -1; | |
117 | } | |
118 | ||
119 | if (!fgets(output, len, f)) { | |
120 | fclose(f); | |
121 | SYSERROR("couldn't read %s", path); | |
122 | return -1; | |
123 | } | |
124 | ||
125 | fclose(f); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | static void exec_criu(struct criu_opts *opts) | |
130 | { | |
131 | char **argv, log[PATH_MAX]; | |
132 | int static_args = 23, argc = 0, i, ret; | |
133 | int netnr = 0; | |
134 | struct lxc_list *it; | |
135 | FILE *mnts; | |
136 | struct mntent mntent; | |
137 | ||
138 | char buf[4096], tty_info[32]; | |
139 | size_t pos; | |
140 | ||
141 | /* If we are currently in a cgroup /foo/bar, and the container is in a | |
142 | * cgroup /lxc/foo, lxcfs will give us an ENOENT if some task in the | |
143 | * container has an open fd that points to one of the cgroup files | |
144 | * (systemd always opens its "root" cgroup). So, let's escape to the | |
145 | * /actual/ root cgroup so that lxcfs thinks criu has enough rights to | |
146 | * see all cgroups. | |
147 | */ | |
148 | if (!cgroup_escape()) { | |
149 | ERROR("failed to escape cgroups"); | |
150 | return; | |
151 | } | |
152 | ||
153 | /* The command line always looks like: | |
154 | * criu $(action) --tcp-established --file-locks --link-remap \ | |
155 | * --manage-cgroups=full --action-script foo.sh -D $(directory) \ | |
156 | * -o $(directory)/$(action).log --ext-mount-map auto | |
157 | * --enable-external-sharing --enable-external-masters | |
158 | * --enable-fs hugetlbfs --enable-fs tracefs --ext-mount-map console:/dev/pts/n | |
159 | * +1 for final NULL */ | |
160 | ||
161 | if (strcmp(opts->action, "dump") == 0 || strcmp(opts->action, "pre-dump") == 0) { | |
162 | /* -t pid --freeze-cgroup /lxc/ct */ | |
163 | static_args += 4; | |
164 | ||
165 | /* --prev-images-dir <path-to-directory-A-relative-to-B> */ | |
166 | if (opts->user->predump_dir) | |
167 | static_args += 2; | |
168 | ||
169 | /* --page-server --address <address> --port <port> */ | |
170 | if (opts->user->pageserver_address && opts->user->pageserver_port) | |
171 | static_args += 5; | |
172 | ||
173 | /* --leave-running (only for final dump) */ | |
174 | if (strcmp(opts->action, "dump") == 0 && !opts->user->stop) | |
175 | static_args++; | |
176 | ||
177 | /* --external tty[88,4] */ | |
178 | if (opts->tty_id[0]) | |
179 | static_args += 2; | |
180 | ||
181 | /* --force-irmap */ | |
182 | if (!opts->user->preserves_inodes) | |
183 | static_args++; | |
184 | ||
185 | /* --ghost-limit 1024 */ | |
186 | if (opts->user->ghost_limit) | |
187 | static_args += 2; | |
188 | } else if (strcmp(opts->action, "restore") == 0) { | |
189 | /* --root $(lxc_mount_point) --restore-detached | |
190 | * --restore-sibling | |
191 | * --lsm-profile apparmor:whatever | |
192 | */ | |
193 | static_args += 6; | |
194 | ||
195 | tty_info[0] = 0; | |
196 | if (load_tty_major_minor(opts->user->directory, tty_info, sizeof(tty_info))) | |
197 | return; | |
198 | ||
199 | /* --inherit-fd fd[%d]:tty[%s] */ | |
200 | if (tty_info[0]) | |
201 | static_args += 2; | |
202 | } else { | |
203 | return; | |
204 | } | |
205 | ||
206 | if (cgroup_num_hierarchies() > 0) | |
207 | static_args += 2 * cgroup_num_hierarchies(); | |
208 | ||
209 | if (opts->user->verbose) | |
210 | static_args++; | |
211 | ||
212 | if (opts->user->action_script) | |
213 | static_args += 2; | |
214 | ||
215 | static_args += 2 * lxc_list_len(&opts->c->lxc_conf->mount_list); | |
216 | ||
217 | ret = snprintf(log, PATH_MAX, "%s/%s.log", opts->user->directory, opts->action); | |
218 | if (ret < 0 || ret >= PATH_MAX) { | |
219 | ERROR("logfile name too long"); | |
220 | return; | |
221 | } | |
222 | ||
223 | argv = malloc(static_args * sizeof(*argv)); | |
224 | if (!argv) | |
225 | return; | |
226 | ||
227 | memset(argv, 0, static_args * sizeof(*argv)); | |
228 | ||
229 | #define DECLARE_ARG(arg) \ | |
230 | do { \ | |
231 | if (arg == NULL) { \ | |
232 | ERROR("Got NULL argument for criu"); \ | |
233 | goto err; \ | |
234 | } \ | |
235 | argv[argc++] = strdup(arg); \ | |
236 | if (!argv[argc-1]) \ | |
237 | goto err; \ | |
238 | } while (0) | |
239 | ||
240 | argv[argc++] = on_path("criu", NULL); | |
241 | if (!argv[argc-1]) { | |
242 | ERROR("Couldn't find criu binary"); | |
243 | goto err; | |
244 | } | |
245 | ||
246 | DECLARE_ARG(opts->action); | |
247 | DECLARE_ARG("--tcp-established"); | |
248 | DECLARE_ARG("--file-locks"); | |
249 | DECLARE_ARG("--link-remap"); | |
250 | DECLARE_ARG("--manage-cgroups=full"); | |
251 | DECLARE_ARG("--ext-mount-map"); | |
252 | DECLARE_ARG("auto"); | |
253 | DECLARE_ARG("--enable-external-sharing"); | |
254 | DECLARE_ARG("--enable-external-masters"); | |
255 | DECLARE_ARG("--enable-fs"); | |
256 | DECLARE_ARG("hugetlbfs"); | |
257 | DECLARE_ARG("--enable-fs"); | |
258 | DECLARE_ARG("tracefs"); | |
259 | DECLARE_ARG("-D"); | |
260 | DECLARE_ARG(opts->user->directory); | |
261 | DECLARE_ARG("-o"); | |
262 | DECLARE_ARG(log); | |
263 | ||
264 | for (i = 0; i < cgroup_num_hierarchies(); i++) { | |
265 | char **controllers = NULL, *fullname; | |
266 | char *path; | |
267 | ||
268 | if (!cgroup_get_hierarchies(i, &controllers)) { | |
269 | ERROR("failed to get hierarchy %d", i); | |
270 | goto err; | |
271 | } | |
272 | ||
273 | /* if we are in a dump, we have to ask the monitor process what | |
274 | * the right cgroup is. if this is a restore, we can just use | |
275 | * the handler the restore task created. | |
276 | */ | |
277 | if (!strcmp(opts->action, "dump") || !strcmp(opts->action, "pre-dump")) { | |
278 | path = lxc_cmd_get_cgroup_path(opts->c->name, opts->c->config_path, controllers[0]); | |
279 | if (!path) { | |
280 | ERROR("failed to get cgroup path for %s", controllers[0]); | |
281 | goto err; | |
282 | } | |
283 | } else { | |
284 | const char *p; | |
285 | ||
286 | p = cgroup_get_cgroup(opts->handler, controllers[0]); | |
287 | if (!p) { | |
288 | ERROR("failed to get cgroup path for %s", controllers[0]); | |
289 | goto err; | |
290 | } | |
291 | ||
292 | path = strdup(p); | |
293 | if (!path) { | |
294 | ERROR("strdup failed"); | |
295 | goto err; | |
296 | } | |
297 | } | |
298 | ||
299 | if (!lxc_deslashify(&path)) { | |
300 | ERROR("failed to deslashify %s", path); | |
301 | free(path); | |
302 | goto err; | |
303 | } | |
304 | ||
305 | fullname = lxc_string_join(",", (const char **) controllers, false); | |
306 | if (!fullname) { | |
307 | ERROR("failed to join controllers"); | |
308 | free(path); | |
309 | goto err; | |
310 | } | |
311 | ||
312 | ret = sprintf(buf, "%s:%s", fullname, path); | |
313 | free(path); | |
314 | free(fullname); | |
315 | if (ret < 0 || ret >= sizeof(buf)) { | |
316 | ERROR("sprintf of cgroup root arg failed"); | |
317 | goto err; | |
318 | } | |
319 | ||
320 | DECLARE_ARG("--cgroup-root"); | |
321 | DECLARE_ARG(buf); | |
322 | } | |
323 | ||
324 | if (opts->user->verbose) | |
325 | DECLARE_ARG("-vvvvvv"); | |
326 | ||
327 | if (opts->user->action_script) { | |
328 | DECLARE_ARG("--action-script"); | |
329 | DECLARE_ARG(opts->user->action_script); | |
330 | } | |
331 | ||
332 | mnts = write_mount_file(&opts->c->lxc_conf->mount_list); | |
333 | if (!mnts) | |
334 | goto err; | |
335 | ||
336 | while (getmntent_r(mnts, &mntent, buf, sizeof(buf))) { | |
337 | char *fmt, *key, *val; | |
338 | char arg[2 * PATH_MAX + 2]; | |
339 | ||
340 | if (strcmp(opts->action, "dump") == 0) { | |
341 | fmt = "/%s:%s"; | |
342 | key = mntent.mnt_dir; | |
343 | val = mntent.mnt_dir; | |
344 | } else { | |
345 | fmt = "%s:%s"; | |
346 | key = mntent.mnt_dir; | |
347 | val = mntent.mnt_fsname; | |
348 | } | |
349 | ||
350 | ret = snprintf(arg, sizeof(arg), fmt, key, val); | |
351 | if (ret < 0 || ret >= sizeof(arg)) { | |
352 | fclose(mnts); | |
353 | ERROR("snprintf failed"); | |
354 | goto err; | |
355 | } | |
356 | ||
357 | DECLARE_ARG("--ext-mount-map"); | |
358 | DECLARE_ARG(arg); | |
359 | } | |
360 | fclose(mnts); | |
361 | ||
362 | if (strcmp(opts->action, "dump") == 0 || strcmp(opts->action, "pre-dump") == 0) { | |
363 | char pid[32], *freezer_relative; | |
364 | ||
365 | if (sprintf(pid, "%d", opts->c->init_pid(opts->c)) < 0) | |
366 | goto err; | |
367 | ||
368 | DECLARE_ARG("-t"); | |
369 | DECLARE_ARG(pid); | |
370 | ||
371 | freezer_relative = lxc_cmd_get_cgroup_path(opts->c->name, | |
372 | opts->c->config_path, | |
373 | "freezer"); | |
374 | if (!freezer_relative) { | |
375 | ERROR("failed getting freezer path"); | |
376 | goto err; | |
377 | } | |
378 | ||
379 | ret = snprintf(log, sizeof(log), "/sys/fs/cgroup/freezer/%s", freezer_relative); | |
380 | if (ret < 0 || ret >= sizeof(log)) | |
381 | goto err; | |
382 | ||
383 | if (!opts->user->disable_skip_in_flight && | |
384 | strcmp(opts->criu_version, CRIU_IN_FLIGHT_SUPPORT) >= 0) | |
385 | DECLARE_ARG("--skip-in-flight"); | |
386 | ||
387 | DECLARE_ARG("--freeze-cgroup"); | |
388 | DECLARE_ARG(log); | |
389 | ||
390 | if (opts->tty_id[0]) { | |
391 | DECLARE_ARG("--ext-mount-map"); | |
392 | DECLARE_ARG("/dev/console:console"); | |
393 | ||
394 | DECLARE_ARG("--external"); | |
395 | DECLARE_ARG(opts->tty_id); | |
396 | } | |
397 | ||
398 | if (opts->user->predump_dir) { | |
399 | DECLARE_ARG("--prev-images-dir"); | |
400 | DECLARE_ARG(opts->user->predump_dir); | |
401 | } | |
402 | ||
403 | if (opts->user->pageserver_address && opts->user->pageserver_port) { | |
404 | DECLARE_ARG("--page-server"); | |
405 | DECLARE_ARG("--address"); | |
406 | DECLARE_ARG(opts->user->pageserver_address); | |
407 | DECLARE_ARG("--port"); | |
408 | DECLARE_ARG(opts->user->pageserver_port); | |
409 | } | |
410 | ||
411 | if (!opts->user->preserves_inodes) | |
412 | DECLARE_ARG("--force-irmap"); | |
413 | ||
414 | if (opts->user->ghost_limit) { | |
415 | char ghost_limit[32]; | |
416 | ||
417 | ret = sprintf(ghost_limit, "%"PRIu64, opts->user->ghost_limit); | |
418 | if (ret < 0 || ret >= sizeof(ghost_limit)) { | |
419 | ERROR("failed to print ghost limit %"PRIu64, opts->user->ghost_limit); | |
420 | goto err; | |
421 | } | |
422 | ||
423 | DECLARE_ARG("--ghost-limit"); | |
424 | DECLARE_ARG(ghost_limit); | |
425 | } | |
426 | ||
427 | /* only for final dump */ | |
428 | if (strcmp(opts->action, "dump") == 0 && !opts->user->stop) | |
429 | DECLARE_ARG("--leave-running"); | |
430 | } else if (strcmp(opts->action, "restore") == 0) { | |
431 | void *m; | |
432 | int additional; | |
433 | struct lxc_conf *lxc_conf = opts->c->lxc_conf; | |
434 | ||
435 | DECLARE_ARG("--root"); | |
436 | DECLARE_ARG(opts->c->lxc_conf->rootfs.mount); | |
437 | DECLARE_ARG("--restore-detached"); | |
438 | DECLARE_ARG("--restore-sibling"); | |
439 | ||
440 | if (tty_info[0]) { | |
441 | if (opts->console_fd < 0) { | |
442 | ERROR("lxc.console configured on source host but not target"); | |
443 | goto err; | |
444 | } | |
445 | ||
446 | ret = snprintf(buf, sizeof(buf), "fd[%d]:%s", opts->console_fd, tty_info); | |
447 | if (ret < 0 || ret >= sizeof(buf)) | |
448 | goto err; | |
449 | ||
450 | DECLARE_ARG("--inherit-fd"); | |
451 | DECLARE_ARG(buf); | |
452 | } | |
453 | if (opts->console_name) { | |
454 | if (snprintf(buf, sizeof(buf), "console:%s", opts->console_name) < 0) { | |
455 | SYSERROR("sprintf'd too many bytes"); | |
456 | } | |
457 | DECLARE_ARG("--ext-mount-map"); | |
458 | DECLARE_ARG(buf); | |
459 | } | |
460 | ||
461 | if (lxc_conf->lsm_aa_profile || lxc_conf->lsm_se_context) { | |
462 | ||
463 | if (lxc_conf->lsm_aa_profile) | |
464 | ret = snprintf(buf, sizeof(buf), "apparmor:%s", lxc_conf->lsm_aa_profile); | |
465 | else | |
466 | ret = snprintf(buf, sizeof(buf), "selinux:%s", lxc_conf->lsm_se_context); | |
467 | ||
468 | if (ret < 0 || ret >= sizeof(buf)) | |
469 | goto err; | |
470 | ||
471 | DECLARE_ARG("--lsm-profile"); | |
472 | DECLARE_ARG(buf); | |
473 | } | |
474 | ||
475 | additional = lxc_list_len(&opts->c->lxc_conf->network) * 2; | |
476 | ||
477 | m = realloc(argv, (argc + additional + 1) * sizeof(*argv)); | |
478 | if (!m) | |
479 | goto err; | |
480 | argv = m; | |
481 | ||
482 | lxc_list_for_each(it, &opts->c->lxc_conf->network) { | |
483 | char eth[128], *veth; | |
484 | struct lxc_netdev *n = it->elem; | |
485 | ||
486 | if (n->name) { | |
487 | if (strlen(n->name) >= sizeof(eth)) | |
488 | goto err; | |
489 | strncpy(eth, n->name, sizeof(eth)); | |
490 | } else { | |
491 | ret = snprintf(eth, sizeof(eth), "eth%d", netnr); | |
492 | if (ret < 0 || ret >= sizeof(eth)) | |
493 | goto err; | |
494 | } | |
495 | ||
496 | switch (n->type) { | |
497 | case LXC_NET_VETH: | |
498 | veth = n->priv.veth_attr.pair; | |
499 | ||
500 | if (n->link) | |
501 | ret = snprintf(buf, sizeof(buf), "veth[%s]:%s@%s", eth, veth, n->link); | |
502 | else | |
503 | ret = snprintf(buf, sizeof(buf), "veth[%s]:%s", eth, veth); | |
504 | if (ret < 0 || ret >= sizeof(buf)) | |
505 | goto err; | |
506 | break; | |
507 | case LXC_NET_MACVLAN: | |
508 | if (!n->link) { | |
509 | ERROR("no host interface for macvlan %s", n->name); | |
510 | goto err; | |
511 | } | |
512 | ||
513 | ret = snprintf(buf, sizeof(buf), "macvlan[%s]:%s", eth, n->link); | |
514 | if (ret < 0 || ret >= sizeof(buf)) | |
515 | goto err; | |
516 | break; | |
517 | case LXC_NET_NONE: | |
518 | case LXC_NET_EMPTY: | |
519 | break; | |
520 | default: | |
521 | /* we have screened for this earlier... */ | |
522 | ERROR("unexpected network type %d", n->type); | |
523 | goto err; | |
524 | } | |
525 | ||
526 | DECLARE_ARG("--external"); | |
527 | DECLARE_ARG(buf); | |
528 | netnr++; | |
529 | } | |
530 | ||
531 | } | |
532 | ||
533 | argv[argc] = NULL; | |
534 | ||
535 | buf[0] = 0; | |
536 | pos = 0; | |
537 | ||
538 | for (i = 0; argv[i]; i++) { | |
539 | ret = snprintf(buf + pos, sizeof(buf) - pos, "%s ", argv[i]); | |
540 | if (ret < 0 || ret >= sizeof(buf) - pos) | |
541 | goto err; | |
542 | else | |
543 | pos += ret; | |
544 | } | |
545 | ||
546 | INFO("execing: %s", buf); | |
547 | ||
548 | /* before criu inits its log, it sometimes prints things to stdout/err; | |
549 | * let's be sure we capture that. | |
550 | */ | |
551 | if (dup2(opts->pipefd, STDOUT_FILENO) < 0) { | |
552 | SYSERROR("dup2 stdout failed"); | |
553 | goto err; | |
554 | } | |
555 | ||
556 | if (dup2(opts->pipefd, STDERR_FILENO) < 0) { | |
557 | SYSERROR("dup2 stderr failed"); | |
558 | goto err; | |
559 | } | |
560 | ||
561 | close(opts->pipefd); | |
562 | ||
563 | #undef DECLARE_ARG | |
564 | execv(argv[0], argv); | |
565 | err: | |
566 | for (i = 0; argv[i]; i++) | |
567 | free(argv[i]); | |
568 | free(argv); | |
569 | } | |
570 | ||
571 | /* | |
572 | * Check to see if the criu version is recent enough for all the features we | |
573 | * use. This version allows either CRIU_VERSION or (CRIU_GITID_VERSION and | |
574 | * CRIU_GITID_PATCHLEVEL) to work, enabling users building from git to c/r | |
575 | * things potentially before a version is released with a particular feature. | |
576 | * | |
577 | * The intent is that when criu development slows down, we can drop this, but | |
578 | * for now we shouldn't attempt to c/r with versions that we know won't work. | |
579 | * | |
580 | * Note: If version != NULL criu_version() stores the detected criu version in | |
581 | * version. Allocates memory for version which must be freed by caller. | |
582 | */ | |
583 | static bool criu_version_ok(char **version) | |
584 | { | |
585 | int pipes[2]; | |
586 | pid_t pid; | |
587 | ||
588 | if (pipe(pipes) < 0) { | |
589 | SYSERROR("pipe() failed"); | |
590 | return false; | |
591 | } | |
592 | ||
593 | pid = fork(); | |
594 | if (pid < 0) { | |
595 | SYSERROR("fork() failed"); | |
596 | return false; | |
597 | } | |
598 | ||
599 | if (pid == 0) { | |
600 | char *args[] = { "criu", "--version", NULL }; | |
601 | char *path; | |
602 | close(pipes[0]); | |
603 | ||
604 | close(STDERR_FILENO); | |
605 | if (dup2(pipes[1], STDOUT_FILENO) < 0) | |
606 | exit(1); | |
607 | ||
608 | path = on_path("criu", NULL); | |
609 | if (!path) | |
610 | exit(1); | |
611 | ||
612 | execv(path, args); | |
613 | exit(1); | |
614 | } else { | |
615 | FILE *f; | |
616 | char *tmp; | |
617 | int patch; | |
618 | ||
619 | close(pipes[1]); | |
620 | if (wait_for_pid(pid) < 0) { | |
621 | close(pipes[0]); | |
622 | SYSERROR("execing criu failed, is it installed?"); | |
623 | return false; | |
624 | } | |
625 | ||
626 | f = fdopen(pipes[0], "r"); | |
627 | if (!f) { | |
628 | close(pipes[0]); | |
629 | return false; | |
630 | } | |
631 | ||
632 | tmp = malloc(1024); | |
633 | if (!tmp) { | |
634 | fclose(f); | |
635 | return false; | |
636 | } | |
637 | ||
638 | if (fscanf(f, "Version: %1023[^\n]s", tmp) != 1) | |
639 | goto version_error; | |
640 | ||
641 | if (fgetc(f) != '\n') | |
642 | goto version_error; | |
643 | ||
644 | if (strcmp(tmp, CRIU_VERSION) >= 0) | |
645 | goto version_match; | |
646 | ||
647 | if (fscanf(f, "GitID: v%1023[^-]s", tmp) != 1) | |
648 | goto version_error; | |
649 | ||
650 | if (fgetc(f) != '-') | |
651 | goto version_error; | |
652 | ||
653 | if (fscanf(f, "%d", &patch) != 1) | |
654 | goto version_error; | |
655 | ||
656 | if (strcmp(tmp, CRIU_GITID_VERSION) < 0) | |
657 | goto version_error; | |
658 | ||
659 | if (patch < CRIU_GITID_PATCHLEVEL) | |
660 | goto version_error; | |
661 | ||
662 | version_match: | |
663 | fclose(f); | |
664 | if (!version) | |
665 | free(tmp); | |
666 | else | |
667 | *version = tmp; | |
668 | return true; | |
669 | ||
670 | version_error: | |
671 | fclose(f); | |
672 | free(tmp); | |
673 | ERROR("must have criu " CRIU_VERSION " or greater to checkpoint/restore"); | |
674 | return false; | |
675 | } | |
676 | } | |
677 | ||
678 | /* Check and make sure the container has a configuration that we know CRIU can | |
679 | * dump. */ | |
680 | static bool criu_ok(struct lxc_container *c, char **criu_version) | |
681 | { | |
682 | struct lxc_list *it; | |
683 | ||
684 | if (!criu_version_ok(criu_version)) | |
685 | return false; | |
686 | ||
687 | if (geteuid()) { | |
688 | ERROR("Must be root to checkpoint"); | |
689 | return false; | |
690 | } | |
691 | ||
692 | /* We only know how to restore containers with veth networks. */ | |
693 | lxc_list_for_each(it, &c->lxc_conf->network) { | |
694 | struct lxc_netdev *n = it->elem; | |
695 | switch(n->type) { | |
696 | case LXC_NET_VETH: | |
697 | case LXC_NET_NONE: | |
698 | case LXC_NET_EMPTY: | |
699 | case LXC_NET_MACVLAN: | |
700 | break; | |
701 | default: | |
702 | ERROR("Found un-dumpable network: %s (%s)", lxc_net_type_to_str(n->type), n->name); | |
703 | return false; | |
704 | } | |
705 | } | |
706 | ||
707 | return true; | |
708 | } | |
709 | ||
710 | static bool restore_net_info(struct lxc_container *c) | |
711 | { | |
712 | struct lxc_list *it; | |
713 | bool has_error = true; | |
714 | ||
715 | if (container_mem_lock(c)) | |
716 | return false; | |
717 | ||
718 | lxc_list_for_each(it, &c->lxc_conf->network) { | |
719 | struct lxc_netdev *netdev = it->elem; | |
720 | char template[IFNAMSIZ]; | |
721 | ||
722 | if (netdev->type != LXC_NET_VETH) | |
723 | continue; | |
724 | ||
725 | snprintf(template, sizeof(template), "vethXXXXXX"); | |
726 | ||
727 | if (!netdev->priv.veth_attr.pair) | |
728 | netdev->priv.veth_attr.pair = lxc_mkifname(template); | |
729 | ||
730 | if (!netdev->priv.veth_attr.pair) | |
731 | goto out_unlock; | |
732 | } | |
733 | ||
734 | has_error = false; | |
735 | ||
736 | out_unlock: | |
737 | container_mem_unlock(c); | |
738 | return !has_error; | |
739 | } | |
740 | ||
741 | // do_restore never returns, the calling process is used as the | |
742 | // monitor process. do_restore calls exit() if it fails. | |
743 | static void do_restore(struct lxc_container *c, int status_pipe, struct migrate_opts *opts, char *criu_version) | |
744 | { | |
745 | pid_t pid; | |
746 | struct lxc_handler *handler; | |
747 | int status, fd; | |
748 | int pipes[2] = {-1, -1}; | |
749 | ||
750 | /* Try to detach from the current controlling tty if it exists. | |
751 | * Othwerise, lxc_init (via lxc_console) will attach the container's | |
752 | * console output to the current tty, which is probably not what any | |
753 | * library user wants, and if they do, they can just manually configure | |
754 | * it :) | |
755 | */ | |
756 | fd = open("/dev/tty", O_RDWR); | |
757 | if (fd >= 0) { | |
758 | if (ioctl(fd, TIOCNOTTY, NULL) < 0) | |
759 | SYSERROR("couldn't detach from tty"); | |
760 | close(fd); | |
761 | } | |
762 | ||
763 | handler = lxc_init(c->name, c->lxc_conf, c->config_path); | |
764 | if (!handler) | |
765 | goto out; | |
766 | ||
767 | if (!cgroup_init(handler)) { | |
768 | ERROR("failed initing cgroups"); | |
769 | goto out_fini_handler; | |
770 | } | |
771 | ||
772 | if (!cgroup_create(handler)) { | |
773 | ERROR("failed creating groups"); | |
774 | goto out_fini_handler; | |
775 | } | |
776 | ||
777 | if (!restore_net_info(c)) { | |
778 | ERROR("failed restoring network info"); | |
779 | goto out_fini_handler; | |
780 | } | |
781 | ||
782 | resolve_clone_flags(handler); | |
783 | ||
784 | if (pipe(pipes) < 0) { | |
785 | SYSERROR("pipe() failed"); | |
786 | goto out_fini_handler; | |
787 | } | |
788 | ||
789 | pid = fork(); | |
790 | if (pid < 0) | |
791 | goto out_fini_handler; | |
792 | ||
793 | if (pid == 0) { | |
794 | struct criu_opts os; | |
795 | struct lxc_rootfs *rootfs; | |
796 | int flags; | |
797 | ||
798 | close(status_pipe); | |
799 | status_pipe = -1; | |
800 | ||
801 | close(pipes[0]); | |
802 | pipes[0] = -1; | |
803 | ||
804 | if (unshare(CLONE_NEWNS)) | |
805 | goto out_fini_handler; | |
806 | ||
807 | /* CRIU needs the lxc root bind mounted so that it is the root of some | |
808 | * mount. */ | |
809 | rootfs = &c->lxc_conf->rootfs; | |
810 | ||
811 | if (rootfs_is_blockdev(c->lxc_conf)) { | |
812 | if (do_rootfs_setup(c->lxc_conf, c->name, c->config_path) < 0) | |
813 | goto out_fini_handler; | |
814 | } else { | |
815 | if (mkdir(rootfs->mount, 0755) < 0 && errno != EEXIST) | |
816 | goto out_fini_handler; | |
817 | ||
818 | if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0) { | |
819 | SYSERROR("remount / to private failed"); | |
820 | goto out_fini_handler; | |
821 | } | |
822 | ||
823 | if (mount(rootfs->path, rootfs->mount, NULL, MS_BIND, NULL) < 0) { | |
824 | rmdir(rootfs->mount); | |
825 | goto out_fini_handler; | |
826 | } | |
827 | } | |
828 | ||
829 | os.pipefd = pipes[1]; | |
830 | os.action = "restore"; | |
831 | os.user = opts; | |
832 | os.c = c; | |
833 | os.console_fd = c->lxc_conf->console.slave; | |
834 | os.criu_version = criu_version; | |
835 | os.handler = handler; | |
836 | ||
837 | if (os.console_fd >= 0) { | |
838 | /* Twiddle the FD_CLOEXEC bit. We want to pass this FD to criu | |
839 | * via --inherit-fd, so we don't want it to close. | |
840 | */ | |
841 | flags = fcntl(os.console_fd, F_GETFD); | |
842 | if (flags < 0) { | |
843 | SYSERROR("F_GETFD failed: %d", os.console_fd); | |
844 | goto out_fini_handler; | |
845 | } | |
846 | ||
847 | flags &= ~FD_CLOEXEC; | |
848 | ||
849 | if (fcntl(os.console_fd, F_SETFD, flags) < 0) { | |
850 | SYSERROR("F_SETFD failed"); | |
851 | goto out_fini_handler; | |
852 | } | |
853 | } | |
854 | os.console_name = c->lxc_conf->console.name; | |
855 | ||
856 | /* exec_criu() returning is an error */ | |
857 | exec_criu(&os); | |
858 | umount(rootfs->mount); | |
859 | rmdir(rootfs->mount); | |
860 | goto out_fini_handler; | |
861 | } else { | |
862 | int ret; | |
863 | char title[2048]; | |
864 | ||
865 | close(pipes[1]); | |
866 | pipes[1] = -1; | |
867 | ||
868 | pid_t w = waitpid(pid, &status, 0); | |
869 | if (w == -1) { | |
870 | SYSERROR("waitpid"); | |
871 | goto out_fini_handler; | |
872 | } | |
873 | ||
874 | if (WIFEXITED(status)) { | |
875 | char buf[4096]; | |
876 | ||
877 | if (WEXITSTATUS(status)) { | |
878 | int n; | |
879 | ||
880 | n = read(pipes[0], buf, sizeof(buf)); | |
881 | if (n < 0) { | |
882 | SYSERROR("failed reading from criu stderr"); | |
883 | goto out_fini_handler; | |
884 | } | |
885 | ||
886 | buf[n] = 0; | |
887 | ||
888 | ERROR("criu process exited %d, output:\n%s", WEXITSTATUS(status), buf); | |
889 | goto out_fini_handler; | |
890 | } else { | |
891 | ret = snprintf(buf, sizeof(buf), "/proc/self/task/%lu/children", (unsigned long)syscall(__NR_gettid)); | |
892 | if (ret < 0 || ret >= sizeof(buf)) { | |
893 | ERROR("snprintf'd too many characters: %d", ret); | |
894 | goto out_fini_handler; | |
895 | } | |
896 | ||
897 | FILE *f = fopen(buf, "r"); | |
898 | if (!f) { | |
899 | SYSERROR("couldn't read restore's children file %s", buf); | |
900 | goto out_fini_handler; | |
901 | } | |
902 | ||
903 | ret = fscanf(f, "%d", (int*) &handler->pid); | |
904 | fclose(f); | |
905 | if (ret != 1) { | |
906 | ERROR("reading restore pid failed"); | |
907 | goto out_fini_handler; | |
908 | } | |
909 | ||
910 | if (lxc_set_state(c->name, handler, RUNNING)) { | |
911 | ERROR("error setting running state after restore"); | |
912 | goto out_fini_handler; | |
913 | } | |
914 | } | |
915 | } else { | |
916 | ERROR("CRIU was killed with signal %d", WTERMSIG(status)); | |
917 | goto out_fini_handler; | |
918 | } | |
919 | ||
920 | close(pipes[0]); | |
921 | ||
922 | ret = write(status_pipe, &status, sizeof(status)); | |
923 | close(status_pipe); | |
924 | status_pipe = -1; | |
925 | ||
926 | if (sizeof(status) != ret) { | |
927 | SYSERROR("failed to write all of status"); | |
928 | goto out_fini_handler; | |
929 | } | |
930 | ||
931 | /* | |
932 | * See comment in lxcapi_start; we don't care if these | |
933 | * fail because it's just a beauty thing. We just | |
934 | * assign the return here to silence potential. | |
935 | */ | |
936 | ret = snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name); | |
937 | ret = setproctitle(title); | |
938 | ||
939 | ret = lxc_poll(c->name, handler); | |
940 | if (ret) | |
941 | lxc_abort(c->name, handler); | |
942 | lxc_fini(c->name, handler); | |
943 | exit(ret); | |
944 | } | |
945 | ||
946 | out_fini_handler: | |
947 | if (pipes[0] >= 0) | |
948 | close(pipes[0]); | |
949 | if (pipes[1] >= 0) | |
950 | close(pipes[1]); | |
951 | ||
952 | lxc_fini(c->name, handler); | |
953 | ||
954 | out: | |
955 | if (status_pipe >= 0) { | |
956 | /* ensure getting here was a failure, e.g. if we failed to | |
957 | * parse the child pid or something, even after a successful | |
958 | * restore | |
959 | */ | |
960 | if (!status) | |
961 | status = 1; | |
962 | if (write(status_pipe, &status, sizeof(status)) != sizeof(status)) { | |
963 | SYSERROR("writing status failed"); | |
964 | } | |
965 | close(status_pipe); | |
966 | } | |
967 | ||
968 | exit(1); | |
969 | } | |
970 | ||
971 | static int save_tty_major_minor(char *directory, struct lxc_container *c, char *tty_id, int len) | |
972 | { | |
973 | FILE *f; | |
974 | char path[PATH_MAX]; | |
975 | int ret; | |
976 | struct stat sb; | |
977 | ||
978 | if (c->lxc_conf->console.path && !strcmp(c->lxc_conf->console.path, "none")) { | |
979 | tty_id[0] = 0; | |
980 | return 0; | |
981 | } | |
982 | ||
983 | ret = snprintf(path, sizeof(path), "/proc/%d/root/dev/console", c->init_pid(c)); | |
984 | if (ret < 0 || ret >= sizeof(path)) { | |
985 | ERROR("snprintf'd too many chacters: %d", ret); | |
986 | return -1; | |
987 | } | |
988 | ||
989 | ret = stat(path, &sb); | |
990 | if (ret < 0) { | |
991 | SYSERROR("stat of %s failed", path); | |
992 | return -1; | |
993 | } | |
994 | ||
995 | ret = snprintf(path, sizeof(path), "%s/tty.info", directory); | |
996 | if (ret < 0 || ret >= sizeof(path)) { | |
997 | ERROR("snprintf'd too many characters: %d", ret); | |
998 | return -1; | |
999 | } | |
1000 | ||
1001 | ret = snprintf(tty_id, len, "tty[%llx:%llx]", | |
1002 | (long long unsigned) sb.st_rdev, | |
1003 | (long long unsigned) sb.st_dev); | |
1004 | if (ret < 0 || ret >= sizeof(path)) { | |
1005 | ERROR("snprintf'd too many characters: %d", ret); | |
1006 | return -1; | |
1007 | } | |
1008 | ||
1009 | f = fopen(path, "w"); | |
1010 | if (!f) { | |
1011 | SYSERROR("failed to open %s", path); | |
1012 | return -1; | |
1013 | } | |
1014 | ||
1015 | ret = fprintf(f, "%s", tty_id); | |
1016 | fclose(f); | |
1017 | if (ret < 0) | |
1018 | SYSERROR("failed to write to %s", path); | |
1019 | return ret; | |
1020 | } | |
1021 | ||
1022 | /* do one of either predump or a regular dump */ | |
1023 | static bool do_dump(struct lxc_container *c, char *mode, struct migrate_opts *opts) | |
1024 | { | |
1025 | pid_t pid; | |
1026 | char *criu_version = NULL; | |
1027 | int criuout[2]; | |
1028 | ||
1029 | if (!criu_ok(c, &criu_version)) | |
1030 | return false; | |
1031 | ||
1032 | if (pipe(criuout) < 0) { | |
1033 | SYSERROR("pipe() failed"); | |
1034 | return false; | |
1035 | } | |
1036 | ||
1037 | if (mkdir_p(opts->directory, 0700) < 0) | |
1038 | goto fail; | |
1039 | ||
1040 | pid = fork(); | |
1041 | if (pid < 0) { | |
1042 | SYSERROR("fork failed"); | |
1043 | goto fail; | |
1044 | } | |
1045 | ||
1046 | if (pid == 0) { | |
1047 | struct criu_opts os; | |
1048 | struct lxc_handler h; | |
1049 | ||
1050 | close(criuout[0]); | |
1051 | ||
1052 | h.name = c->name; | |
1053 | if (!cgroup_init(&h)) { | |
1054 | ERROR("failed to cgroup_init()"); | |
1055 | exit(1); | |
1056 | } | |
1057 | ||
1058 | os.pipefd = criuout[1]; | |
1059 | os.action = mode; | |
1060 | os.user = opts; | |
1061 | os.c = c; | |
1062 | os.console_name = c->lxc_conf->console.path; | |
1063 | os.criu_version = criu_version; | |
1064 | ||
1065 | if (save_tty_major_minor(opts->directory, c, os.tty_id, sizeof(os.tty_id)) < 0) | |
1066 | exit(1); | |
1067 | ||
1068 | /* exec_criu() returning is an error */ | |
1069 | exec_criu(&os); | |
1070 | exit(1); | |
1071 | } else { | |
1072 | int status; | |
1073 | ssize_t n; | |
1074 | char buf[4096]; | |
1075 | bool ret; | |
1076 | ||
1077 | close(criuout[1]); | |
1078 | ||
1079 | pid_t w = waitpid(pid, &status, 0); | |
1080 | if (w == -1) { | |
1081 | SYSERROR("waitpid"); | |
1082 | close(criuout[0]); | |
1083 | return false; | |
1084 | } | |
1085 | ||
1086 | n = read(criuout[0], buf, sizeof(buf)); | |
1087 | close(criuout[0]); | |
1088 | if (n < 0) { | |
1089 | SYSERROR("read"); | |
1090 | n = 0; | |
1091 | } | |
1092 | buf[n] = 0; | |
1093 | ||
1094 | if (WIFEXITED(status)) { | |
1095 | if (WEXITSTATUS(status)) { | |
1096 | ERROR("dump failed with %d", WEXITSTATUS(status)); | |
1097 | ret = false; | |
1098 | } else { | |
1099 | ret = true; | |
1100 | } | |
1101 | } else if (WIFSIGNALED(status)) { | |
1102 | ERROR("dump signaled with %d", WTERMSIG(status)); | |
1103 | ret = false; | |
1104 | } else { | |
1105 | ERROR("unknown dump exit %d", status); | |
1106 | ret = false; | |
1107 | } | |
1108 | ||
1109 | if (!ret) | |
1110 | ERROR("criu output: %s", buf); | |
1111 | return ret; | |
1112 | } | |
1113 | fail: | |
1114 | close(criuout[0]); | |
1115 | close(criuout[1]); | |
1116 | rmdir(opts->directory); | |
1117 | return false; | |
1118 | } | |
1119 | ||
1120 | bool __criu_pre_dump(struct lxc_container *c, struct migrate_opts *opts) | |
1121 | { | |
1122 | return do_dump(c, "pre-dump", opts); | |
1123 | } | |
1124 | ||
1125 | bool __criu_dump(struct lxc_container *c, struct migrate_opts *opts) | |
1126 | { | |
1127 | char path[PATH_MAX]; | |
1128 | int ret; | |
1129 | ||
1130 | ret = snprintf(path, sizeof(path), "%s/inventory.img", opts->directory); | |
1131 | if (ret < 0 || ret >= sizeof(path)) | |
1132 | return false; | |
1133 | ||
1134 | if (access(path, F_OK) == 0) { | |
1135 | ERROR("please use a fresh directory for the dump directory"); | |
1136 | return false; | |
1137 | } | |
1138 | ||
1139 | return do_dump(c, "dump", opts); | |
1140 | } | |
1141 | ||
1142 | bool __criu_restore(struct lxc_container *c, struct migrate_opts *opts) | |
1143 | { | |
1144 | pid_t pid; | |
1145 | int status, nread; | |
1146 | int pipefd[2]; | |
1147 | char *criu_version = NULL; | |
1148 | ||
1149 | if (!criu_ok(c, &criu_version)) | |
1150 | return false; | |
1151 | ||
1152 | if (geteuid()) { | |
1153 | ERROR("Must be root to restore"); | |
1154 | return false; | |
1155 | } | |
1156 | ||
1157 | if (pipe(pipefd)) { | |
1158 | ERROR("failed to create pipe"); | |
1159 | return false; | |
1160 | } | |
1161 | ||
1162 | pid = fork(); | |
1163 | if (pid < 0) { | |
1164 | close(pipefd[0]); | |
1165 | close(pipefd[1]); | |
1166 | return false; | |
1167 | } | |
1168 | ||
1169 | if (pid == 0) { | |
1170 | close(pipefd[0]); | |
1171 | // this never returns | |
1172 | do_restore(c, pipefd[1], opts, criu_version); | |
1173 | } | |
1174 | ||
1175 | close(pipefd[1]); | |
1176 | ||
1177 | nread = read(pipefd[0], &status, sizeof(status)); | |
1178 | close(pipefd[0]); | |
1179 | if (sizeof(status) != nread) { | |
1180 | ERROR("reading status from pipe failed"); | |
1181 | goto err_wait; | |
1182 | } | |
1183 | ||
1184 | // If the criu process was killed or exited nonzero, wait() for the | |
1185 | // handler, since the restore process died. Otherwise, we don't need to | |
1186 | // wait, since the child becomes the monitor process. | |
1187 | if (!WIFEXITED(status) || WEXITSTATUS(status)) | |
1188 | goto err_wait; | |
1189 | return true; | |
1190 | ||
1191 | err_wait: | |
1192 | if (wait_for_pid(pid)) | |
1193 | ERROR("restore process died"); | |
1194 | return false; | |
1195 | } |