criu是linux平臺在用戶空間實現checkpoint/restore功能的工具軟件。通過該工具,可以凍結正在運行的應用程序或者其中的一部分,并將應用程序的執行狀態以文件形式保存在磁盤上,然后通過這些快照文件,可以將應用程序從凍結的時間點恢復回來繼續運行。借助該軟件,可以實現應用的實時遷移、應用快照和遠程調試等功能。criu最顯著的特點是在用戶空間實現checkpoint/restore,不需要修改應用程序或者操作系統,并且也是內核中功能最豐富和最活躍的。
INSTALL
# centos7
yum install criu -y
cuir -V
# Version: 3.12
TEST
- 寫個簡單的程序:
vi example.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;
}
不斷循環,1s打印個不斷遞增的數字
- 編譯
gcc example.c -o example
- 運行
./example
0
1
2
3
...
- 查看程序的pid
ps -ef | grep example
# root 5525 5383 0 15:54 pts/1 00:00:00 ./example
- dump
criu dump -D dump_dir -j -t 5525
ll dump_dir
-rw-r--r-- 1 root root 3448 Apr 16 15:56 cgroup.img
-rw-r--r-- 1 root root 2180 Apr 16 15:56 core-5517.img
-rw-r--r-- 1 root root 44 Apr 16 15:56 fdinfo-2.img
-rw-r--r-- 1 root root 341 Apr 16 15:56 files.img
-rw-r--r-- 1 root root 18 Apr 16 15:56 fs-5517.img
-rw-r--r-- 1 root root 32 Apr 16 15:56 ids-5517.img
-rw-r--r-- 1 root root 40 Apr 16 15:56 inventory.img
-rw-r--r-- 1 root root 747 Apr 16 15:56 mm-5517.img
-rw-r--r-- 1 root root 216 Apr 16 15:56 pagemap-5517.img
-rw-r--r-- 1 root root 106496 Apr 16 15:56 pages-1.img
-rw-r--r-- 1 root root 26 Apr 16 15:56 pstree.img
-rw-r--r-- 1 root root 12 Apr 16 15:56 seccomp.img
-rw-r--r-- 1 root root 40 Apr 16 15:56 stats-dump
-rw-r--r-- 1 root root 177 Apr 16 15:56 tty-info.img
# 在dump_dir下生成dump文件
# 生成很多img文件,這些文件主要用于恢復應用。
通過criu的dump命令,-D選項指定應用的快照文件保存目錄,-j表示該應用是一個通過shell啟動的作業,通過-t指定需要checkpoint的應用pid。當對應用設置checkpoint后,應用會自動退出,如果希望應用繼續執行,需指定-R或--leave-running選項。
- 恢復應用
criu restore -D /dump_dir/ -j
44
45
46
....
# 查看進程pid,還是以原來的pid恢復
root 5525 5537 0 16:10 pts/0 00:00:00 ./example
root 5537 5454 0 16:10 pts/0 00:00:00 criu restore -D example_dump/ -j
通過criu的restore命令,-D選項指定應用的快照文件保存目錄,checkpoint時指定的應用程序是由shell啟動,所以restore時需要指定相應的-j選項。由示例中可以看到,恢復后的程序從設置checkpoint的時間點繼續運行,程序在輸出43時被kill掉,恢復后繼續輸出44,恢復后查找進程5525,發現進程使用原來的進程號繼續運行。