
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Save and Restore a Linux Process
Sometimes, you get in a situation where you need to take a snapshot of the process and use it later in the same state. We call this operation of saving a process state to use it later in Linux Checkpointing. This operation is done by taking a running process and saving it into a file, and then later, you can quickly launch the process again without losing its state.
To make it easier to understand, let's say you have a process doing some calculations. To prevent data loss if the system crashes or when you reboot the machine, you save its state while it's running into a file. Then, when you need it, you just restore the file. This operation helps save memory and time.
To achieve this, we have a powerful tool in Linux called CRIU that does all this for us.
Note ? This checkpoint / restore feature needs some configuration at the kernel level, so first, you need to check if checkpoint/restore support is enabled in your kernel. You can check this by reading and looking for CONFIG_CHECKPOINT_RESTORE in the configuration file located in /boot/config:"
grep CONFIG_CHECKPOINT_RESTORE /boot/config-$(uname -r)
You should get ?
CONFIG_CHECKPOINT_RESTORE=y
What is CRIU?
CRIU stands for "Checkpoint / Restore in Userspace". What this tool does is read the state of a process in Linux and save it into a set of files called image files.
CRIU tries to capture the full state of the process to recreate the same process later on, either on the same machine or another one.
This image explains the process of checkpointing and restoring a process in Linux using CRIU.
It takes a dump of the process state, puts it into files, and then restores them. This is what checkpoint and restore mean.
CRIU uses standard kernel APIs (such as dump, prctl, netlink, and syscalls) to dump the process, and the same approach is used to restore the process.
Install CRIU
By default, CRIU doesn't come installed in Linux, but it is available in the package repository of almost all Linux distributions.
For Debian / Ubuntu / Mint ?
apt install criu
Note ? If you are using Ubuntu 24.04, due to some dependency problems, CRIU is not in the official repository. You will need to add it using this PPA from the CRIU team ?
sudo add-apt-repository ppa:criu/ppa
Update the system and install CRIU ?
sudo apt update sudo install criu
For Arch / Arch-based distributions ?
pacman -S criu
If you are using a distribution that doesn't have CRIU in the official repository or you want to build it from source, you can check the CRIU website.
After the installation is completed, you can check if the tool is successfully installed by typing criu in the terminal. You should get an output like this ?
Example of Using CRIU
We have successfully installed CRIU. The steps to dump a process are the same for any process.
- First, you need to get the PID for the process you want to dump.
- Second, the CRIU tool will be used to dump the process. The command for dumping the process using CRIU is like this:
criu dump -t <pid> --images-dir /path/to/save/images --leave-running
Here,
- <pid> ? Specify the process PID.
- <--images-dir> ? Specify the directory name where the dump will be saved.
- /path/to/save/images ? the directory that will contain the dumped file make sure you created this directory before you start the dumping otherwise; you will get an error.
- --leave-running ? This option keeps the process running after the checkpoint; otherwise, the process will be stopped after the dump is performed.
Let's take an example to apply those steps in a real example. To demonstrate this, let's run a process that will take some time to finish. For example ?
Open the terminal and run the following command ?
sleep 3600 &
This process will run for a quiet time, in this example, 3600 seconds (1 hr); the & symbol at the end means we need to run this in the background. Now, we do have the process running.
The second thing, we should do is get the process PID for this. We can use pregrp ?
pgrep sleep
The output for this is ?
8521
Third thing, create the directory to store the files ?
mkdir sleepdump
Here I call it sleepdump, you can name it as you like.
After that we get the process id and created the directory to store files now we should be ready to use CRIU ?
sudo criu dump -t 8521 --images-dir ~/sleepdump --shell-job
Here we add -shell-job to tell CRIU that this process is running from shell. Without this option, CRIU will not capture all the necessary tach related to the shell context, and because of this, the dump will fail.
We didn't add a leave-running option so that the process will be dead after the dump.
After the execution of this command, we should get image files in the directory that we created (in this example sleepdump) ?
At this point, we just dumped our process successfully. It's time to learn how we can restore it.
To restore a process using CRIU, we use the following command ?
sudo criu restore --images-dir ~/path/to/save/images
The path in our example here is sleepdump ?
sudo criu restore --images-dir ~/sleepdump
This should restore the process and run it again. The process we did here is applicable for any process you want to dump.
Conclusion
In this tutorial, we demonstrated how to perform checkpointing in Linux using the powerful tool CRIU. We just did a simple example, but CRIU is more powerful and has a lot of features that are waiting to be explored.