专栏
天翼云开发者社区

CRIU-进程C/R

2024-04-18 17:11:28 0阅读

criu是linux平台在用户空间实现checkpoint/restore功能的工具软件。通过该工具,可以冻结正在运行的应用程序或者其中的一部分,并将应用程序的执行状态以文件形式保存在磁盘上,然后通过这些快照文件,可以将应用程序从冻结的时间点恢复回来继续运行。借助该软件,可以实现应用的实时迁移、应用快照和远程调试等功能。criu最显著的特点是在用户空间实现checkpoint/restore,不需要修改应用程序或者操作系统,并且也是内核中功能最丰富和最活跃的。

INSTALL

# centos7
yum install criu -y
cuir -V
# Version: 3.12

TEST

  1. 写个简单的程序:
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打印个不断递增的数字

  1. 编译
gcc example.c -o example
  1. 运行
./example
0
1
2
3
...
  1. 查看程序的pid
ps -ef | grep example
# root      5525  5383  0 15:54 pts/1    00:00:00 ./example
  1. 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选项。

  1. 恢复应用
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,发现进程使用原来的进程号继续运行。

 

  • 0
  • 0
  • 0
0 评论
0/1000
评论(0) 发表评论
cactusii

cactusii

14 篇文章 0 粉丝
关注

CRIU-进程C/R

2024-04-18 17:11:28 0阅读

criu是linux平台在用户空间实现checkpoint/restore功能的工具软件。通过该工具,可以冻结正在运行的应用程序或者其中的一部分,并将应用程序的执行状态以文件形式保存在磁盘上,然后通过这些快照文件,可以将应用程序从冻结的时间点恢复回来继续运行。借助该软件,可以实现应用的实时迁移、应用快照和远程调试等功能。criu最显著的特点是在用户空间实现checkpoint/restore,不需要修改应用程序或者操作系统,并且也是内核中功能最丰富和最活跃的。

INSTALL

# centos7
yum install criu -y
cuir -V
# Version: 3.12

TEST

  1. 写个简单的程序:
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打印个不断递增的数字

  1. 编译
gcc example.c -o example
  1. 运行
./example
0
1
2
3
...
  1. 查看程序的pid
ps -ef | grep example
# root      5525  5383  0 15:54 pts/1    00:00:00 ./example
  1. 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选项。

  1. 恢复应用
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,发现进程使用原来的进程号继续运行。

 

文章来自专栏

灾备平台

14 篇文章 1 订阅
0 评论
0/1000
评论(0) 发表评论
  • 0
    点赞
  • 0
    收藏
  • 0
    评论