]>
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 user-provided migrate options relevant to this action */
60 struct migrate_opts
*user
;
62 /* The container to dump */
63 struct lxc_container
*c
;
65 /* dump: stop the container or not after dumping? */
66 char tty_id
[32]; /* the criu tty id for /dev/console, i.e. "tty[${rdev}:${dev}]" */
68 /* restore: the file to write the init process' pid into */
70 const char *cgroup_path
;
72 /* The path that is bind mounted from /dev/console, if any. We don't
73 * want to use `--ext-mount-map auto`'s result here because the pts
74 * device may have a different path (e.g. if the pty number is
75 * different) on the target host. NULL if lxc.console = "none".
80 static int load_tty_major_minor(char *directory
, char *output
, int len
)
86 ret
= snprintf(path
, sizeof(path
), "%s/tty.info", directory
);
87 if (ret
< 0 || ret
>= sizeof(path
)) {
88 ERROR("snprintf'd too many chacters: %d", ret
);
94 /* This means we're coming from a liblxc which didn't export
95 * the tty info. In this case they had to have lxc.console =
96 * none, so there's no problem restoring.
101 SYSERROR("couldn't open %s", path
);
105 if (!fgets(output
, len
, f
)) {
107 SYSERROR("couldn't read %s", path
);
115 static void exec_criu(struct criu_opts
*opts
)
117 char **argv
, log
[PATH_MAX
];
118 int static_args
= 23, argc
= 0, i
, ret
;
122 char buf
[4096], tty_info
[32];
124 /* If we are currently in a cgroup /foo/bar, and the container is in a
125 * cgroup /lxc/foo, lxcfs will give us an ENOENT if some task in the
126 * container has an open fd that points to one of the cgroup files
127 * (systemd always opens its "root" cgroup). So, let's escape to the
128 * /actual/ root cgroup so that lxcfs thinks criu has enough rights to
131 if (!cgroup_escape()) {
132 ERROR("failed to escape cgroups");
136 /* The command line always looks like:
137 * criu $(action) --tcp-established --file-locks --link-remap \
138 * --manage-cgroups=full action-script foo.sh -D $(directory) \
139 * -o $(directory)/$(action).log --ext-mount-map auto
140 * --enable-external-sharing --enable-external-masters
141 * --enable-fs hugetlbfs --enable-fs tracefs --ext-mount-map console:/dev/pts/n
142 * +1 for final NULL */
144 if (strcmp(opts
->action
, "dump") == 0 || strcmp(opts
->action
, "pre-dump") == 0) {
145 /* -t pid --freeze-cgroup /lxc/ct */
148 /* --prev-images-dir <path-to-directory-A-relative-to-B> */
149 if (opts
->user
->predump_dir
)
152 /* --page-server --address <address> --port <port> */
153 if (opts
->user
->pageserver_address
&& opts
->user
->pageserver_port
)
156 /* --leave-running (only for final dump) */
157 if (strcmp(opts
->action
, "dump") == 0 && !opts
->user
->stop
)
160 /* --external tty[88,4] */
165 if (!opts
->user
->preserves_inodes
)
167 } else if (strcmp(opts
->action
, "restore") == 0) {
168 /* --root $(lxc_mount_point) --restore-detached
169 * --restore-sibling --pidfile $foo --cgroup-root $foo
170 * --lsm-profile apparmor:whatever
175 if (load_tty_major_minor(opts
->user
->directory
, tty_info
, sizeof(tty_info
)))
178 /* --inherit-fd fd[%d]:tty[%s] */
185 if (opts
->user
->verbose
)
188 if (opts
->user
->action_script
)
191 ret
= snprintf(log
, PATH_MAX
, "%s/%s.log", opts
->user
->directory
, opts
->action
);
192 if (ret
< 0 || ret
>= PATH_MAX
) {
193 ERROR("logfile name too long\n");
197 argv
= malloc(static_args
* sizeof(*argv
));
201 memset(argv
, 0, static_args
* sizeof(*argv
));
203 #define DECLARE_ARG(arg) \
206 ERROR("Got NULL argument for criu"); \
209 argv[argc++] = strdup(arg); \
214 argv
[argc
++] = on_path("criu", NULL
);
216 ERROR("Couldn't find criu binary\n");
220 DECLARE_ARG(opts
->action
);
221 DECLARE_ARG("--tcp-established");
222 DECLARE_ARG("--file-locks");
223 DECLARE_ARG("--link-remap");
224 DECLARE_ARG("--manage-cgroups=full");
225 DECLARE_ARG("--ext-mount-map");
227 DECLARE_ARG("--enable-external-sharing");
228 DECLARE_ARG("--enable-external-masters");
229 DECLARE_ARG("--enable-fs");
230 DECLARE_ARG("hugetlbfs");
231 DECLARE_ARG("--enable-fs");
232 DECLARE_ARG("tracefs");
234 DECLARE_ARG(opts
->user
->directory
);
238 if (opts
->user
->verbose
)
239 DECLARE_ARG("-vvvvvv");
241 if (opts
->user
->action_script
) {
242 DECLARE_ARG("--action-script");
243 DECLARE_ARG(opts
->user
->action_script
);
246 if (strcmp(opts
->action
, "dump") == 0 || strcmp(opts
->action
, "pre-dump") == 0) {
247 char pid
[32], *freezer_relative
;
249 if (sprintf(pid
, "%d", opts
->c
->init_pid(opts
->c
)) < 0)
255 freezer_relative
= lxc_cmd_get_cgroup_path(opts
->c
->name
,
256 opts
->c
->config_path
,
258 if (!freezer_relative
) {
259 ERROR("failed getting freezer path");
263 ret
= snprintf(log
, sizeof(log
), "/sys/fs/cgroup/freezer/%s", freezer_relative
);
264 if (ret
< 0 || ret
>= sizeof(log
))
267 DECLARE_ARG("--freeze-cgroup");
270 if (opts
->tty_id
[0]) {
271 DECLARE_ARG("--ext-mount-map");
272 DECLARE_ARG("/dev/console:console");
274 DECLARE_ARG("--external");
275 DECLARE_ARG(opts
->tty_id
);
278 if (opts
->user
->predump_dir
) {
279 DECLARE_ARG("--prev-images-dir");
280 DECLARE_ARG(opts
->user
->predump_dir
);
283 if (opts
->user
->pageserver_address
&& opts
->user
->pageserver_port
) {
284 DECLARE_ARG("--page-server");
285 DECLARE_ARG("--address");
286 DECLARE_ARG(opts
->user
->pageserver_address
);
287 DECLARE_ARG("--port");
288 DECLARE_ARG(opts
->user
->pageserver_port
);
291 if (!opts
->user
->preserves_inodes
)
292 DECLARE_ARG("--force-irmap");
294 /* only for final dump */
295 if (strcmp(opts
->action
, "dump") == 0 && !opts
->user
->stop
)
296 DECLARE_ARG("--leave-running");
297 } else if (strcmp(opts
->action
, "restore") == 0) {
300 struct lxc_conf
*lxc_conf
= opts
->c
->lxc_conf
;
302 DECLARE_ARG("--root");
303 DECLARE_ARG(opts
->c
->lxc_conf
->rootfs
.mount
);
304 DECLARE_ARG("--restore-detached");
305 DECLARE_ARG("--restore-sibling");
306 DECLARE_ARG("--pidfile");
307 DECLARE_ARG(opts
->pidfile
);
308 DECLARE_ARG("--cgroup-root");
309 DECLARE_ARG(opts
->cgroup_path
);
312 if (opts
->console_fd
< 0) {
313 ERROR("lxc.console configured on source host but not target");
317 ret
= snprintf(buf
, sizeof(buf
), "fd[%d]:%s", opts
->console_fd
, tty_info
);
318 if (ret
< 0 || ret
>= sizeof(buf
))
321 DECLARE_ARG("--inherit-fd");
324 if (opts
->console_name
) {
325 if (snprintf(buf
, sizeof(buf
), "console:%s", opts
->console_name
) < 0) {
326 SYSERROR("sprintf'd too many bytes");
328 DECLARE_ARG("--ext-mount-map");
332 if (lxc_conf
->lsm_aa_profile
|| lxc_conf
->lsm_se_context
) {
334 if (lxc_conf
->lsm_aa_profile
)
335 ret
= snprintf(buf
, sizeof(buf
), "apparmor:%s", lxc_conf
->lsm_aa_profile
);
337 ret
= snprintf(buf
, sizeof(buf
), "selinux:%s", lxc_conf
->lsm_se_context
);
339 if (ret
< 0 || ret
>= sizeof(buf
))
342 DECLARE_ARG("--lsm-profile");
346 additional
= lxc_list_len(&opts
->c
->lxc_conf
->network
) * 2;
348 m
= realloc(argv
, (argc
+ additional
+ 1) * sizeof(*argv
));
353 lxc_list_for_each(it
, &opts
->c
->lxc_conf
->network
) {
354 char eth
[128], *veth
;
355 struct lxc_netdev
*n
= it
->elem
;
357 if (n
->type
!= LXC_NET_VETH
)
361 if (strlen(n
->name
) >= sizeof(eth
))
363 strncpy(eth
, n
->name
, sizeof(eth
));
365 sprintf(eth
, "eth%d", netnr
);
367 veth
= n
->priv
.veth_attr
.pair
;
370 ret
= snprintf(buf
, sizeof(buf
), "%s=%s@%s", eth
, veth
, n
->link
);
372 ret
= snprintf(buf
, sizeof(buf
), "%s=%s", eth
, veth
);
373 if (ret
< 0 || ret
>= sizeof(buf
))
376 DECLARE_ARG("--veth-pair");
387 for (i
= 0; argv
[i
]; i
++) {
388 ret
= snprintf(buf
+ pos
, sizeof(buf
) - pos
, "%s ", argv
[i
]);
389 if (ret
< 0 || ret
>= sizeof(buf
) - pos
)
395 INFO("execing: %s", buf
);
398 execv(argv
[0], argv
);
400 for (i
= 0; argv
[i
]; i
++)
406 * Check to see if the criu version is recent enough for all the features we
407 * use. This version allows either CRIU_VERSION or (CRIU_GITID_VERSION and
408 * CRIU_GITID_PATCHLEVEL) to work, enabling users building from git to c/r
409 * things potentially before a version is released with a particular feature.
411 * The intent is that when criu development slows down, we can drop this, but
412 * for now we shouldn't attempt to c/r with versions that we know won't work.
414 static bool criu_version_ok()
419 if (pipe(pipes
) < 0) {
420 SYSERROR("pipe() failed");
426 SYSERROR("fork() failed");
431 char *args
[] = { "criu", "--version", NULL
};
435 close(STDERR_FILENO
);
436 if (dup2(pipes
[1], STDOUT_FILENO
) < 0)
439 path
= on_path("criu", NULL
);
451 if (wait_for_pid(pid
) < 0) {
453 SYSERROR("execing criu failed, is it installed?");
457 f
= fdopen(pipes
[0], "r");
463 if (fscanf(f
, "Version: %1023[^\n]s", version
) != 1)
466 if (fgetc(f
) != '\n')
469 if (strcmp(version
, CRIU_VERSION
) >= 0)
472 if (fscanf(f
, "GitID: v%1023[^-]s", version
) != 1)
478 if (fscanf(f
, "%d", &patch
) != 1)
481 if (strcmp(version
, CRIU_GITID_VERSION
) < 0)
484 if (patch
< CRIU_GITID_PATCHLEVEL
)
493 ERROR("must have criu " CRIU_VERSION
" or greater to checkpoint/restore\n");
498 /* Check and make sure the container has a configuration that we know CRIU can
500 static bool criu_ok(struct lxc_container
*c
)
504 if (!criu_version_ok())
508 ERROR("Must be root to checkpoint\n");
512 /* We only know how to restore containers with veth networks. */
513 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
514 struct lxc_netdev
*n
= it
->elem
;
521 ERROR("Found network that is not VETH or NONE\n");
529 static bool restore_net_info(struct lxc_container
*c
)
532 bool has_error
= true;
534 if (container_mem_lock(c
))
537 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
538 struct lxc_netdev
*netdev
= it
->elem
;
539 char template[IFNAMSIZ
];
541 if (netdev
->type
!= LXC_NET_VETH
)
544 snprintf(template, sizeof(template), "vethXXXXXX");
546 if (!netdev
->priv
.veth_attr
.pair
)
547 netdev
->priv
.veth_attr
.pair
= lxc_mkifname(template);
549 if (!netdev
->priv
.veth_attr
.pair
)
556 container_mem_unlock(c
);
560 // do_restore never returns, the calling process is used as the
561 // monitor process. do_restore calls exit() if it fails.
562 void do_restore(struct lxc_container
*c
, int status_pipe
, struct migrate_opts
*opts
)
565 char pidfile
[L_tmpnam
];
566 struct lxc_handler
*handler
;
567 int status
, pipes
[2] = {-1, -1};
569 if (!tmpnam(pidfile
))
572 handler
= lxc_init(c
->name
, c
->lxc_conf
, c
->config_path
);
576 if (!cgroup_init(handler
)) {
577 ERROR("failed initing cgroups");
578 goto out_fini_handler
;
581 if (!cgroup_create(handler
)) {
582 ERROR("failed creating groups");
583 goto out_fini_handler
;
586 if (!restore_net_info(c
)) {
587 ERROR("failed restoring network info");
588 goto out_fini_handler
;
591 resolve_clone_flags(handler
);
593 if (pipe(pipes
) < 0) {
594 SYSERROR("pipe() failed");
595 goto out_fini_handler
;
600 goto out_fini_handler
;
604 struct lxc_rootfs
*rootfs
;
612 if (dup2(pipes
[1], STDERR_FILENO
) < 0) {
613 SYSERROR("dup2 failed");
614 goto out_fini_handler
;
617 if (dup2(pipes
[1], STDOUT_FILENO
) < 0) {
618 SYSERROR("dup2 failed");
619 goto out_fini_handler
;
622 if (unshare(CLONE_NEWNS
))
623 goto out_fini_handler
;
625 /* CRIU needs the lxc root bind mounted so that it is the root of some
627 rootfs
= &c
->lxc_conf
->rootfs
;
629 if (rootfs_is_blockdev(c
->lxc_conf
)) {
630 if (do_rootfs_setup(c
->lxc_conf
, c
->name
, c
->config_path
) < 0)
631 goto out_fini_handler
;
633 if (mkdir(rootfs
->mount
, 0755) < 0 && errno
!= EEXIST
)
634 goto out_fini_handler
;
636 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
) < 0) {
637 SYSERROR("remount / to private failed");
638 goto out_fini_handler
;
641 if (mount(rootfs
->path
, rootfs
->mount
, NULL
, MS_BIND
, NULL
) < 0) {
642 rmdir(rootfs
->mount
);
643 goto out_fini_handler
;
647 os
.action
= "restore";
650 os
.pidfile
= pidfile
;
651 os
.cgroup_path
= cgroup_canonical_path(handler
);
652 os
.console_fd
= c
->lxc_conf
->console
.slave
;
654 if (os
.console_fd
>= 0) {
655 /* Twiddle the FD_CLOEXEC bit. We want to pass this FD to criu
656 * via --inherit-fd, so we don't want it to close.
658 flags
= fcntl(os
.console_fd
, F_GETFD
);
660 SYSERROR("F_GETFD failed: %d", os
.console_fd
);
661 goto out_fini_handler
;
664 flags
&= ~FD_CLOEXEC
;
666 if (fcntl(os
.console_fd
, F_SETFD
, flags
) < 0) {
667 SYSERROR("F_SETFD failed");
668 goto out_fini_handler
;
671 os
.console_name
= c
->lxc_conf
->console
.name
;
673 /* exec_criu() returning is an error */
675 umount(rootfs
->mount
);
676 rmdir(rootfs
->mount
);
677 goto out_fini_handler
;
685 pid_t w
= waitpid(pid
, &status
, 0);
688 goto out_fini_handler
;
691 ret
= write(status_pipe
, &status
, sizeof(status
));
695 if (sizeof(status
) != ret
) {
696 SYSERROR("failed to write all of status");
697 goto out_fini_handler
;
700 if (WIFEXITED(status
)) {
701 if (WEXITSTATUS(status
)) {
705 n
= read(pipes
[0], buf
, sizeof(buf
));
707 SYSERROR("failed reading from criu stderr");
708 goto out_fini_handler
;
713 ERROR("criu process exited %d, output:\n%s\n", WEXITSTATUS(status
), buf
);
714 goto out_fini_handler
;
717 FILE *f
= fopen(pidfile
, "r");
719 SYSERROR("couldn't read restore's init pidfile %s\n", pidfile
);
720 goto out_fini_handler
;
723 ret
= fscanf(f
, "%d", (int*) &handler
->pid
);
725 if (unlink(pidfile
) < 0 && errno
!= ENOENT
)
726 SYSERROR("unlinking pidfile failed");
729 ERROR("reading restore pid failed");
730 goto out_fini_handler
;
733 if (lxc_set_state(c
->name
, handler
, RUNNING
)) {
734 ERROR("error setting running state after restore");
735 goto out_fini_handler
;
739 ERROR("CRIU was killed with signal %d\n", WTERMSIG(status
));
740 goto out_fini_handler
;
746 * See comment in lxcapi_start; we don't care if these
747 * fail because it's just a beauty thing. We just
748 * assign the return here to silence potential.
750 ret
= snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
751 ret
= setproctitle(title
);
753 ret
= lxc_poll(c
->name
, handler
);
755 lxc_abort(c
->name
, handler
);
756 lxc_fini(c
->name
, handler
);
766 lxc_fini(c
->name
, handler
);
767 if (unlink(pidfile
) < 0 && errno
!= ENOENT
)
768 SYSERROR("unlinking pidfile failed");
771 if (status_pipe
>= 0) {
773 if (write(status_pipe
, &status
, sizeof(status
)) != sizeof(status
)) {
774 SYSERROR("writing status failed");
782 static int save_tty_major_minor(char *directory
, struct lxc_container
*c
, char *tty_id
, int len
)
789 if (c
->lxc_conf
->console
.path
&& !strcmp(c
->lxc_conf
->console
.path
, "none")) {
794 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/console", c
->init_pid(c
));
795 if (ret
< 0 || ret
>= sizeof(path
)) {
796 ERROR("snprintf'd too many chacters: %d", ret
);
800 ret
= stat(path
, &sb
);
802 SYSERROR("stat of %s failed", path
);
806 ret
= snprintf(path
, sizeof(path
), "%s/tty.info", directory
);
807 if (ret
< 0 || ret
>= sizeof(path
)) {
808 ERROR("snprintf'd too many characters: %d", ret
);
812 ret
= snprintf(tty_id
, len
, "tty[%llx:%llx]",
813 (long long unsigned) sb
.st_rdev
,
814 (long long unsigned) sb
.st_dev
);
815 if (ret
< 0 || ret
>= sizeof(path
)) {
816 ERROR("snprintf'd too many characters: %d", ret
);
820 f
= fopen(path
, "w");
822 SYSERROR("failed to open %s", path
);
826 ret
= fprintf(f
, "%s", tty_id
);
829 SYSERROR("failed to write to %s", path
);
833 /* do one of either predump or a regular dump */
834 static bool do_dump(struct lxc_container
*c
, char *mode
, struct migrate_opts
*opts
)
841 if (mkdir_p(opts
->directory
, 0700) < 0)
846 SYSERROR("fork failed");
856 os
.console_name
= c
->lxc_conf
->console
.path
;
858 if (save_tty_major_minor(opts
->directory
, c
, os
.tty_id
, sizeof(os
.tty_id
)) < 0)
861 /* exec_criu() returning is an error */
866 pid_t w
= waitpid(pid
, &status
, 0);
872 if (WIFEXITED(status
)) {
873 if (WEXITSTATUS(status
)) {
874 ERROR("dump failed with %d\n", WEXITSTATUS(status
));
879 } else if (WIFSIGNALED(status
)) {
880 ERROR("dump signaled with %d\n", WTERMSIG(status
));
883 ERROR("unknown dump exit %d\n", status
);
889 bool __criu_pre_dump(struct lxc_container
*c
, struct migrate_opts
*opts
)
891 return do_dump(c
, "pre-dump", opts
);
894 bool __criu_dump(struct lxc_container
*c
, struct migrate_opts
*opts
)
899 ret
= snprintf(path
, sizeof(path
), "%s/inventory.img", opts
->directory
);
900 if (ret
< 0 || ret
>= sizeof(path
))
903 if (access(path
, F_OK
) == 0) {
904 ERROR("please use a fresh directory for the dump directory\n");
908 return do_dump(c
, "dump", opts
);
911 bool __criu_restore(struct lxc_container
*c
, struct migrate_opts
*opts
)
921 ERROR("Must be root to restore\n");
926 ERROR("failed to create pipe");
939 // this never returns
940 do_restore(c
, pipefd
[1], opts
);
945 nread
= read(pipefd
[0], &status
, sizeof(status
));
947 if (sizeof(status
) != nread
) {
948 ERROR("reading status from pipe failed");
952 // If the criu process was killed or exited nonzero, wait() for the
953 // handler, since the restore process died. Otherwise, we don't need to
954 // wait, since the child becomes the monitor process.
955 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
960 if (wait_for_pid(pid
))
961 ERROR("restore process died");