criu是linux平台在用户空间实现checkpoint/restore功能的工具软件。通过该工具,可以冻结正在运行的应用程序或者其中的一部分,并将应用程序的执行状态以文件形式保存在磁盘上,然后通过这些快照文件,可以将应用程序从冻结的时间点恢复回来继续运行。借助该软件,可以实现应用的实时迁移、应用快照和远程调试等功能。criu最显著的特点是在用户空间实现checkpoint/restore,不需要修改应用程序或者操作系统,并且也是内核中功能最丰富和最活跃的。
本文主要介绍如何在centos7安装criu,并通过criu checkpoint冻结应用,然后restore恢复应用。
1 安装criu
$ yum install -y criu
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* epel: mirrors.tongji.edu.cn
* extras: mirrors.cqu.edu.cn
* ius: mirrors.tongji.edu.cn
* updates: mirrors.shu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package criu.x86_64 0:3.5-4.el7 will be installed
--> Finished Dependency Resolution
运行yum install -y criu命令即可安装criu。
2 设置应用的checkpoint
示例程序是一个不断打印数字的c程序,checkpoint_demo.c代码如下:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int i = 0;
for (i = 0; i < 100; ++i)
{
sleep(1);
printf("%d\n", i);
}
return 0;
}
编译代码后,示例程序运行效果如下:
$ ./checkpoint_demo
0
1
2
3
4
5
6
将示例程序复制到/root/chkpnt_dir目录。
$ pwd
/root/chkp