Checkpoint and Restore Emacs into Immortality

Gallagher PryorComputing Topics, Education, Open Source 1 Comment

TL;DR. An Emacs death is horrible to endure. Checkpoint Emacs’ state at regular intervals to bring it back to life with CRIU. Yup, CRIU can handle Emacs, now! The steps, nowadays, are simple and this post quickly outlines the constraints and the commands to make it work.

This post is short and to the point. The build choices, configurations, etc. have been stripped again and again to make things as simple as possible.

Also, these instructions are given for a recent Ubuntu in order to be more canonical 😄

Make a Place for bins at ~/.bin

mkdir -p $HOME/.bin/snaps
export PATH=$PATH:$HOME/.bin:$HOME/.bin/emacs/bin:$HOME/.bin/criu

Grab an Emacs without DBUS

cd ~
wget http://ftp.gnu.org/gnu/emacs/emacs-27.2.tar.gz
tar xzf emacs-27.2.tar.gz
mkdir -p $HOME/.bin/emacs      # I like keeping a custom toolchain here
cd emacs-27.2
sudo apt-get install autoconf make gcc texinfo libgtk-3-dev libxpm-dev libjpeg-dev libgif-dev libtiff5-dev libgnutls28-dev libncurses5-dev -y
./configure --without-dbus --prefix=$HOME/.bin/emacs
make && make install

Grab CRIU

cd ~
wget http://github.com/checkpoint-restore/criu/archive/v3.16.1/criu-3.16.1.tar.gz
tar xzf criu-3.16.1.tar.gz
cd criu-3.16.1
sudo apt-get install libprotobuf-dev libprotoc-dev protobuf-compiler protobuf-c-compiler libnftables-dev libprotobuf-c-dev libbsd-dev libgnutls28-dev libnetl-dev libnnl-3-dev libcap-dev
make
sudo ./criu/criu check    # Should say: Looks good.
cp -r ./criu ~/.bin/criu
cp ./scripts/criu-ns $HOME/.bin
mkdir -p $HOME/

Configure Emacs

Your immortal Emacs will be in daemon mode and will talk with emacsclients over a local TCP port (CRIU doesn’t like UNIX pipes). Put the following somewhere in your .emacs and the identical configuration in your .emacs.d/server file.

;; in your .emacs
(setq server-use-tcp t)
(setq server-host "0.0.0.0")
(setq server-port "4000")
(setq server-auth-key "(TpHfI,PBg~`s7bMwT,:\\e/gA\"TI]X')0X|FD9fWf5h~wl?bHlrF-j/;8oq)#\"wh")

In .emacs.d/server:

0.0.0.0:4000 1452
(TpHfI,PBg~`s7bMwT,:\e/gA"TI]X')0X|FD9fWf5h~wl?bHlrF-j/;8oq)#"wh

Start the Emacs daemon as usual:

emacs --daemon

Bring Emacs to Life (for the last time)

Start up emacs --daemon in some shell or wherever you desire. Take the first snapshot:

cd ~/.bin/snaps
sudo criu-ns dump --shell-job --leave-running --tcp-established -t $(pgrep -u $USER -x emacs)

Edit with the Emacsclient

emacsclient -f ~/emacs.d/server -nw ~/

Checkpoint the Emacs Daemon Periodically

cd ~/.bin/snaps
sudo criu-ns dump --shell-job --leave-running --tcp-established -t $(pgrep -u $USER -x emacs)

Bring Emacs Back to Life

If your Emacs fails, bring it back to life by recovering the snapshot. Reconnect with your emacsclient as described above.

cd ~/.bin/snaps
sudo criu-ns restore --shell-job --tcp-close

Note that in recovering a snapshot, CRIU tries to bring the process back with the same PID. If things go wrong, look out for a conflicting process.

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *