How to checkpoint and restore a Linux process

Last updated on September 21, 2020 by Dan Nanni

Suppose you have a Linux process running, and want to capture its current snapshot after freezing it. The frozen process is then loaded from the snapshot to resume. Such concept of process checkpointing can be very useful under a variety of circumstances. It can be used for process backup, live migration, faster boot-up service, etc. There is actually a user-space tool that can perform process checkpointing on Linux.

A Linux command-line utility called criu can checkpoint and restore processes in user space. criu can freeze a running process or process tree, dump its state as a set of files, and then restore the frozen process from the snapshot. For criu to work, there must be kernel support that exposes additional information about a Linux process. Fortunately, upstream Linux kernel 3.9 already has most of the required kernel support built into it. So you can easily run criu on kernel 3.9 and higher.

In this tutorial, I will show you how to checkpoint and restore a Linux process by using criu. I assume that you already have the Linux kernel 3.9 on your system.

Install criu on Linux

For Ubuntu or Debian:

To install criu on Ubuntu or Debian, install the following prerequisites first.

$ sudo apt-get install libprotobuf-dev libprotoc-dev protobuf-c-compiler libprotobuf-c0 libprotobuf-c0-dev

Then go ahead and build criu as follows.

$ wget http://download.openvz.org/criu/criu-0.5.tar.bz2
$ tar xvfvj  crtools-0.5.tar.bz2
$ cd crtools-0.5
$ make

Once build is successful, it will generate a command line utility called crtools which is used to checkpoint and restore a Linux process.

Checkpoint and Restore a Running Process

To checkpoint a running process with crtools, run the following command. <PID> is the process ID of a running process. Once process checkpointing is successfully completed, the process will be terminated, and its state will be stored as a set of files in the current directory.

$ sudo ./crtools dump -t <PID>

To restore a dumped process to its original running state:

$ sudo ./crtools restore -t <PID>

Note that if the running process is launched directly from shell, you must use --shell-job option in crtools command as follows.

$ sudo ./crtools dump -t <PID> --shell-job
$ sudo ./crtools restore -t <PID> --shell-job

When restoring a process, the crtools command waits for the restored process to finish, and then exits. That is because when crtools restores a process, crtools becomes a parent process of the process.

If you want to make crtools return right after a process has been restored, use --restore-detached option as follows. A restored process will then be re-parented to init, so crtools can exit right away.

$ sudo ./crtools restore -t <PID> --shell-job --restore-detached

The criu also supports checkpoint/restart for a process which opens an active TCP connection. This feature takes advantage of sockets in repair mode, which is supported by the Linux kernel 3.5 and higher.

To checkpoint a process which has active TCP connection(s):

$ sudo ./crtools dump -t <PID> --tcp-established

To restore a process and its previously active TCP connection(s):

$ sudo ./crtools restore -t <PID> --tcp-established

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean