]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/criu.c
2 * lxc: linux Container library
4 * Copyright © 2014-2015 Canonical Ltd.
7 * Tycho Andersen <tycho.andersen@canonical.com>
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.
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.
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
25 #include <linux/limits.h>
30 #include <sys/mount.h>
31 #include <sys/types.h>
37 #include "bdev/bdev.h"
48 #define CRIU_VERSION "2.0"
50 #define CRIU_GITID_VERSION "2.0"
51 #define CRIU_GITID_PATCHLEVEL 0
53 lxc_log_define(lxc_criu
, lxc
);
56 /* The type of criu invocation, one of "dump" or "restore" */
59 /* The directory to pass to criu */
62 /* The container to dump */
63 struct lxc_container
*c
;
65 /* Enable criu verbose mode? */
68 /* (pre-)dump: a directory for the previous dump's images */
71 /* dump: stop the container or not after dumping? */
73 char tty_id
[32]; /* the criu tty id for /dev/console, i.e. "tty[${rdev}:${dev}]" */
75 /* restore: the file to write the init process' pid into */
77 const char *cgroup_path
;
79 /* The path that is bind mounted from /dev/console, if any. We don't
80 * want to use `--ext-mount-map auto`'s result here because the pts
81 * device may have a different path (e.g. if the pty number is
82 * different) on the target host. NULL if lxc.console = "none".
87 static int load_tty_major_minor(char *directory
, char *output
, int len
)
93 ret
= snprintf(path
, sizeof(path
), "%s/tty.info", directory
);
94 if (ret
< 0 || ret
>= sizeof(path
)) {
95 ERROR("snprintf'd too many chacters: %d", ret
);
101 /* This means we're coming from a liblxc which didn't export
102 * the tty info. In this case they had to have lxc.console =
103 * none, so there's no problem restoring.
108 SYSERROR("couldn't open %s", path
);
112 if (!fgets(output
, len
, f
)) {
114 SYSERROR("couldn't read %s", path
);
122 static void exec_criu(struct criu_opts
*opts
)
124 char **argv
, log
[PATH_MAX
];
125 int static_args
= 24, argc
= 0, i
, ret
;
129 char buf
[4096], tty_info
[32];
131 /* If we are currently in a cgroup /foo/bar, and the container is in a
132 * cgroup /lxc/foo, lxcfs will give us an ENOENT if some task in the
133 * container has an open fd that points to one of the cgroup files
134 * (systemd always opens its "root" cgroup). So, let's escape to the
135 * /actual/ root cgroup so that lxcfs thinks criu has enough rights to
138 if (!cgroup_escape()) {
139 ERROR("failed to escape cgroups");
143 /* The command line always looks like:
144 * criu $(action) --tcp-established --file-locks --link-remap --force-irmap \
145 * --manage-cgroups action-script foo.sh -D $(directory) \
146 * -o $(directory)/$(action).log --ext-mount-map auto
147 * --enable-external-sharing --enable-external-masters
148 * --enable-fs hugetlbfs --enable-fs tracefs --ext-mount-map console:/dev/pts/n
149 * +1 for final NULL */
151 if (strcmp(opts
->action
, "dump") == 0 || strcmp(opts
->action
, "pre-dump") == 0) {
152 /* -t pid --freeze-cgroup /lxc/ct */
155 /* --prev-images-dir <path-to-directory-A-relative-to-B> */
156 if (opts
->predump_dir
)
159 /* --leave-running (only for final dump) */
160 if (strcmp(opts
->action
, "dump") == 0 && !opts
->stop
)
163 /* --external tty[88,4] */
166 } else if (strcmp(opts
->action
, "restore") == 0) {
167 /* --root $(lxc_mount_point) --restore-detached
168 * --restore-sibling --pidfile $foo --cgroup-root $foo
169 * --lsm-profile apparmor:whatever
174 if (load_tty_major_minor(opts
->directory
, tty_info
, sizeof(tty_info
)))
177 /* --inherit-fd fd[%d]:tty[%s] */
187 ret
= snprintf(log
, PATH_MAX
, "%s/%s.log", opts
->directory
, opts
->action
);
188 if (ret
< 0 || ret
>= PATH_MAX
) {
189 ERROR("logfile name too long\n");
193 argv
= malloc(static_args
* sizeof(*argv
));
197 memset(argv
, 0, static_args
* sizeof(*argv
));
199 #define DECLARE_ARG(arg) \
202 ERROR("Got NULL argument for criu"); \
205 argv[argc++] = strdup(arg); \
210 argv
[argc
++] = on_path("criu", NULL
);
212 ERROR("Couldn't find criu binary\n");
216 DECLARE_ARG(opts
->action
);
217 DECLARE_ARG("--tcp-established");
218 DECLARE_ARG("--file-locks");
219 DECLARE_ARG("--link-remap");
220 DECLARE_ARG("--force-irmap");
221 DECLARE_ARG("--manage-cgroups");
222 DECLARE_ARG("--ext-mount-map");
224 DECLARE_ARG("--enable-external-sharing");
225 DECLARE_ARG("--enable-external-masters");
226 DECLARE_ARG("--enable-fs");
227 DECLARE_ARG("hugetlbfs");
228 DECLARE_ARG("--enable-fs");
229 DECLARE_ARG("tracefs");
231 DECLARE_ARG(opts
->directory
);
236 DECLARE_ARG("-vvvvvv");
238 if (strcmp(opts
->action
, "dump") == 0 || strcmp(opts
->action
, "pre-dump") == 0) {
239 char pid
[32], *freezer_relative
;
241 if (sprintf(pid
, "%d", opts
->c
->init_pid(opts
->c
)) < 0)
247 freezer_relative
= lxc_cmd_get_cgroup_path(opts
->c
->name
,
248 opts
->c
->config_path
,
250 if (!freezer_relative
) {
251 ERROR("failed getting freezer path");
255 ret
= snprintf(log
, sizeof(log
), "/sys/fs/cgroup/freezer/%s", freezer_relative
);
256 if (ret
< 0 || ret
>= sizeof(log
))
259 DECLARE_ARG("--freeze-cgroup");
262 DECLARE_ARG("--ext-mount-map");
263 DECLARE_ARG("/dev/console:console");
264 if (opts
->tty_id
[0]) {
265 DECLARE_ARG("--external");
266 DECLARE_ARG(opts
->tty_id
);
269 if (opts
->predump_dir
) {
270 DECLARE_ARG("--prev-images-dir");
271 DECLARE_ARG(opts
->predump_dir
);
274 /* only for final dump */
275 if (strcmp(opts
->action
, "dump") == 0 && !opts
->stop
)
276 DECLARE_ARG("--leave-running");
277 } else if (strcmp(opts
->action
, "restore") == 0) {
280 struct lxc_conf
*lxc_conf
= opts
->c
->lxc_conf
;
282 DECLARE_ARG("--root");
283 DECLARE_ARG(opts
->c
->lxc_conf
->rootfs
.mount
);
284 DECLARE_ARG("--restore-detached");
285 DECLARE_ARG("--restore-sibling");
286 DECLARE_ARG("--pidfile");
287 DECLARE_ARG(opts
->pidfile
);
288 DECLARE_ARG("--cgroup-root");
289 DECLARE_ARG(opts
->cgroup_path
);
292 ret
= snprintf(buf
, sizeof(buf
), "fd[%d]:%s", opts
->console_fd
, tty_info
);
293 if (ret
< 0 || ret
>= sizeof(buf
))
296 DECLARE_ARG("--inherit-fd");
299 if (opts
->console_name
) {
300 if (snprintf(buf
, sizeof(buf
), "console:%s", opts
->console_name
) < 0) {
301 SYSERROR("sprintf'd too many bytes");
303 DECLARE_ARG("--ext-mount-map");
307 if (lxc_conf
->lsm_aa_profile
|| lxc_conf
->lsm_se_context
) {
309 if (lxc_conf
->lsm_aa_profile
)
310 ret
= snprintf(buf
, sizeof(buf
), "apparmor:%s", lxc_conf
->lsm_aa_profile
);
312 ret
= snprintf(buf
, sizeof(buf
), "selinux:%s", lxc_conf
->lsm_se_context
);
314 if (ret
< 0 || ret
>= sizeof(buf
))
317 DECLARE_ARG("--lsm-profile");
321 additional
= lxc_list_len(&opts
->c
->lxc_conf
->network
) * 2;
323 m
= realloc(argv
, (argc
+ additional
+ 1) * sizeof(*argv
));
328 lxc_list_for_each(it
, &opts
->c
->lxc_conf
->network
) {
329 char eth
[128], *veth
;
330 struct lxc_netdev
*n
= it
->elem
;
332 if (n
->type
!= LXC_NET_VETH
)
336 if (strlen(n
->name
) >= sizeof(eth
))
338 strncpy(eth
, n
->name
, sizeof(eth
));
340 sprintf(eth
, "eth%d", netnr
);
342 veth
= n
->priv
.veth_attr
.pair
;
345 ret
= snprintf(buf
, sizeof(buf
), "%s=%s@%s", eth
, veth
, n
->link
);
347 ret
= snprintf(buf
, sizeof(buf
), "%s=%s", eth
, veth
);
348 if (ret
< 0 || ret
>= sizeof(buf
))
351 DECLARE_ARG("--veth-pair");
360 execv(argv
[0], argv
);
362 for (i
= 0; argv
[i
]; i
++)
368 * Check to see if the criu version is recent enough for all the features we
369 * use. This version allows either CRIU_VERSION or (CRIU_GITID_VERSION and
370 * CRIU_GITID_PATCHLEVEL) to work, enabling users building from git to c/r
371 * things potentially before a version is released with a particular feature.
373 * The intent is that when criu development slows down, we can drop this, but
374 * for now we shouldn't attempt to c/r with versions that we know won't work.
376 static bool criu_version_ok()
381 if (pipe(pipes
) < 0) {
382 SYSERROR("pipe() failed");
388 SYSERROR("fork() failed");
393 char *args
[] = { "criu", "--version", NULL
};
397 close(STDERR_FILENO
);
398 if (dup2(pipes
[1], STDOUT_FILENO
) < 0)
401 path
= on_path("criu", NULL
);
413 if (wait_for_pid(pid
) < 0) {
415 SYSERROR("execing criu failed, is it installed?");
419 f
= fdopen(pipes
[0], "r");
425 if (fscanf(f
, "Version: %1023[^\n]s", version
) != 1)
428 if (fgetc(f
) != '\n')
431 if (strcmp(version
, CRIU_VERSION
) >= 0)
434 if (fscanf(f
, "GitID: v%1023[^-]s", version
) != 1)
440 if (fscanf(f
, "%d", &patch
) != 1)
443 if (strcmp(version
, CRIU_GITID_VERSION
) < 0)
446 if (patch
< CRIU_GITID_PATCHLEVEL
)
455 ERROR("must have criu " CRIU_VERSION
" or greater to checkpoint/restore\n");
460 /* Check and make sure the container has a configuration that we know CRIU can
462 static bool criu_ok(struct lxc_container
*c
)
466 if (!criu_version_ok())
470 ERROR("Must be root to checkpoint\n");
474 /* We only know how to restore containers with veth networks. */
475 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
476 struct lxc_netdev
*n
= it
->elem
;
483 ERROR("Found network that is not VETH or NONE\n");
491 static bool restore_net_info(struct lxc_container
*c
)
494 bool has_error
= true;
496 if (container_mem_lock(c
))
499 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
500 struct lxc_netdev
*netdev
= it
->elem
;
501 char template[IFNAMSIZ
];
503 if (netdev
->type
!= LXC_NET_VETH
)
506 snprintf(template, sizeof(template), "vethXXXXXX");
508 if (!netdev
->priv
.veth_attr
.pair
)
509 netdev
->priv
.veth_attr
.pair
= lxc_mkifname(template);
511 if (!netdev
->priv
.veth_attr
.pair
)
518 container_mem_unlock(c
);
522 // do_restore never returns, the calling process is used as the
523 // monitor process. do_restore calls exit() if it fails.
524 void do_restore(struct lxc_container
*c
, int pipe
, char *directory
, bool verbose
)
527 char pidfile
[L_tmpnam
];
528 struct lxc_handler
*handler
;
531 if (!tmpnam(pidfile
))
534 handler
= lxc_init(c
->name
, c
->lxc_conf
, c
->config_path
);
538 if (!cgroup_init(handler
)) {
539 ERROR("failed initing cgroups");
540 goto out_fini_handler
;
543 if (!cgroup_create(handler
)) {
544 ERROR("failed creating groups");
545 goto out_fini_handler
;
548 if (!restore_net_info(c
)) {
549 ERROR("failed restoring network info");
550 goto out_fini_handler
;
553 resolve_clone_flags(handler
);
557 goto out_fini_handler
;
561 struct lxc_rootfs
*rootfs
;
567 if (unshare(CLONE_NEWNS
))
568 goto out_fini_handler
;
570 /* CRIU needs the lxc root bind mounted so that it is the root of some
572 rootfs
= &c
->lxc_conf
->rootfs
;
574 if (rootfs_is_blockdev(c
->lxc_conf
)) {
575 if (do_rootfs_setup(c
->lxc_conf
, c
->name
, c
->config_path
) < 0)
576 goto out_fini_handler
;
578 if (mkdir(rootfs
->mount
, 0755) < 0 && errno
!= EEXIST
)
579 goto out_fini_handler
;
581 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
) < 0) {
582 SYSERROR("remount / to private failed");
583 goto out_fini_handler
;
586 if (mount(rootfs
->path
, rootfs
->mount
, NULL
, MS_BIND
, NULL
) < 0) {
587 rmdir(rootfs
->mount
);
588 goto out_fini_handler
;
592 os
.action
= "restore";
593 os
.directory
= directory
;
595 os
.pidfile
= pidfile
;
596 os
.verbose
= verbose
;
597 os
.cgroup_path
= cgroup_canonical_path(handler
);
598 os
.console_fd
= c
->lxc_conf
->console
.slave
;
600 /* Twiddle the FD_CLOEXEC bit. We want to pass this FD to criu
601 * via --inherit-fd, so we don't want it to close.
603 flags
= fcntl(os
.console_fd
, F_GETFD
);
605 SYSERROR("F_GETFD failed");
606 goto out_fini_handler
;
609 flags
&= ~FD_CLOEXEC
;
611 if (fcntl(os
.console_fd
, F_SETFD
, flags
) < 0) {
612 SYSERROR("F_SETFD failed");
613 goto out_fini_handler
;
615 os
.console_name
= c
->lxc_conf
->console
.name
;
617 /* exec_criu() returning is an error */
619 umount(rootfs
->mount
);
620 rmdir(rootfs
->mount
);
621 goto out_fini_handler
;
626 pid_t w
= waitpid(pid
, &status
, 0);
629 goto out_fini_handler
;
632 ret
= write(pipe
, &status
, sizeof(status
));
636 if (sizeof(status
) != ret
) {
637 SYSERROR("failed to write all of status");
638 goto out_fini_handler
;
641 if (WIFEXITED(status
)) {
642 if (WEXITSTATUS(status
)) {
643 ERROR("criu process exited %d\n", WEXITSTATUS(status
));
644 goto out_fini_handler
;
647 FILE *f
= fopen(pidfile
, "r");
649 SYSERROR("couldn't read restore's init pidfile %s\n", pidfile
);
650 goto out_fini_handler
;
653 ret
= fscanf(f
, "%d", (int*) &handler
->pid
);
655 if (unlink(pidfile
) < 0 && errno
!= ENOENT
)
656 SYSERROR("unlinking pidfile failed");
659 ERROR("reading restore pid failed");
660 goto out_fini_handler
;
663 if (lxc_set_state(c
->name
, handler
, RUNNING
)) {
664 ERROR("error setting running state after restore");
665 goto out_fini_handler
;
669 ERROR("CRIU was killed with signal %d\n", WTERMSIG(status
));
670 goto out_fini_handler
;
674 * See comment in lxcapi_start; we don't care if these
675 * fail because it's just a beauty thing. We just
676 * assign the return here to silence potential.
678 ret
= snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
679 ret
= setproctitle(title
);
681 ret
= lxc_poll(c
->name
, handler
);
683 lxc_abort(c
->name
, handler
);
684 lxc_fini(c
->name
, handler
);
689 lxc_fini(c
->name
, handler
);
690 if (unlink(pidfile
) < 0 && errno
!= ENOENT
)
691 SYSERROR("unlinking pidfile failed");
696 if (write(pipe
, &status
, sizeof(status
)) != sizeof(status
)) {
697 SYSERROR("writing status failed");
705 static int save_tty_major_minor(char *directory
, struct lxc_container
*c
, char *tty_id
, int len
)
712 if (c
->lxc_conf
->console
.path
&& !strcmp(c
->lxc_conf
->console
.path
, "none")) {
717 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/console", c
->init_pid(c
));
718 if (ret
< 0 || ret
>= sizeof(path
)) {
719 ERROR("snprintf'd too many chacters: %d", ret
);
723 ret
= stat(path
, &sb
);
725 SYSERROR("stat of %s failed", path
);
729 ret
= snprintf(path
, sizeof(path
), "%s/tty.info", directory
);
730 if (ret
< 0 || ret
>= sizeof(path
)) {
731 ERROR("snprintf'd too many characters: %d", ret
);
735 ret
= snprintf(tty_id
, len
, "tty[%llx:%llx]",
736 (long long unsigned) sb
.st_rdev
,
737 (long long unsigned) sb
.st_dev
);
738 if (ret
< 0 || ret
>= sizeof(path
)) {
739 ERROR("snprintf'd too many characters: %d", ret
);
743 f
= fopen(path
, "w");
745 SYSERROR("failed to open %s", path
);
749 ret
= fprintf(f
, "%s", tty_id
);
752 SYSERROR("failed to write to %s", path
);
756 /* do one of either predump or a regular dump */
757 static bool do_dump(struct lxc_container
*c
, char *mode
, char *directory
,
758 bool stop
, bool verbose
, char *predump_dir
)
765 if (mkdir_p(directory
, 0700) < 0)
770 SYSERROR("fork failed");
778 os
.directory
= directory
;
781 os
.verbose
= verbose
;
782 os
.predump_dir
= predump_dir
;
783 os
.console_name
= c
->lxc_conf
->console
.path
;
785 if (save_tty_major_minor(directory
, c
, os
.tty_id
, sizeof(os
.tty_id
)) < 0)
788 /* exec_criu() returning is an error */
793 pid_t w
= waitpid(pid
, &status
, 0);
799 if (WIFEXITED(status
)) {
800 if (WEXITSTATUS(status
)) {
801 ERROR("dump failed with %d\n", WEXITSTATUS(status
));
806 } else if (WIFSIGNALED(status
)) {
807 ERROR("dump signaled with %d\n", WTERMSIG(status
));
810 ERROR("unknown dump exit %d\n", status
);
816 bool pre_dump(struct lxc_container
*c
, char *directory
, bool verbose
, char *predump_dir
)
818 return do_dump(c
, "pre-dump", directory
, false, verbose
, predump_dir
);
821 bool dump(struct lxc_container
*c
, char *directory
, bool stop
, bool verbose
, char *predump_dir
)
826 ret
= snprintf(path
, sizeof(path
), "%s/inventory.img", directory
);
827 if (ret
< 0 || ret
>= sizeof(path
))
830 if (access(path
, F_OK
) == 0) {
831 ERROR("please use a fresh directory for the dump directory\n");
835 return do_dump(c
, "dump", directory
, stop
, verbose
, predump_dir
);
838 bool restore(struct lxc_container
*c
, char *directory
, bool verbose
)
848 ERROR("Must be root to restore\n");
853 ERROR("failed to create pipe");
866 // this never returns
867 do_restore(c
, pipefd
[1], directory
, verbose
);
872 nread
= read(pipefd
[0], &status
, sizeof(status
));
874 if (sizeof(status
) != nread
) {
875 ERROR("reading status from pipe failed");
879 // If the criu process was killed or exited nonzero, wait() for the
880 // handler, since the restore process died. Otherwise, we don't need to
881 // wait, since the child becomes the monitor process.
882 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
887 if (wait_for_pid(pid
))
888 ERROR("restore process died");