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.
To install criu on Ubuntu or Debian, install the following prerequisites first.
To build criu on Ubuntu or Debian:
$ tar xvfvj crtools-0.5.tar.bz2
$ cd crtools-0.5
Once build is successful, it will generate a command line utility called crtools which is used to checkpoint and restore a Linux 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.
To restore a dumped process to its original running state:
Note that if the running process is launched directly from shell, you must use "--shell-job" option in crtools command as follows.
$ 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.
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):
To restore a process and its previously active TCP connection(s):
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!