How to save your Linux state (suspend to disk) periodically to recover from crashes?

2

2

One in a while my laptop crashes/dies because of a bad/empty battery, crappy wifi driver or whatever other reason.

For a while I've wondered if it's possible to force Linux to periodically save the state (like vmware snapshots) to disk so you can restore from that with possibly slightly outdated work but at least with all of your apps open in the same state you've left them.

I don't really see the point in having to boot everything from cratch constantly, although KDE saves your state on logout, that doesn't happen periodically (by default) either. It would make it much nicer to recover from your crashes if your ram was written to disk periodically.

Anyone know if there's a system call to do this without also shutting down the machine? Even a manual button to save the entire state would be nice.

Wolph

Posted 2012-09-05T20:56:51.277

Reputation: 595

Answers

3

The hibernation interface is accessible via /dev/snapshot, which is described in Documentation/power/userland-swsusp.txt. You could write a program based on uswsusp's s2disk, for example.

But I am not sure if this can work reliably, since hibernation only stores information kept in RAM, but not the current disk/filesystem state. For example, if you saved a hibernation snapshot, modified a few files and reloaded the snapshot, your disk would contain the new data and metadata, but the snapshot might still have the old version in the snapshotted kernel's memory...

To quote Documentation/power/swsusp.txt:

 * BIG FAT WARNING *********************************************************
 *
 * If you touch anything on disk between suspend and resume...
 *              ...kiss your data goodbye.
 *

If your laptop dies due to an empty battery, configure it to automatically hibernate when reaching 1% or so. (If it happens while the laptop is suspended – try out "suspend to both" mode in Linux 3.6-rc1 and newer.)

If your laptop dies due to a driver bug, REPORT THAT BUG so that it could get fixed.

user1686

Posted 2012-09-05T20:56:51.277

Reputation: 283 655

Thank's for the reply, I'll see if I can write a little tool to do this. I'm mostly worried about write buffering occuring in some locations, having a filesystem that's out of sync with the RAM should be manageable. As for the laptop dying due to an empty battery, I usually put my laptop in standby but that still drains the battery within 48 hours with a full charge. So once in a while I am too late with charging it again. And the driver bug is a rare thing and not reliably reproducible unfortunately, otherwise I would have debugged it myself :( – Wolph – 2012-09-06T09:21:49.880

@WoLpH: As long as "should be manageable" doesn't descend to stealthy filesystem corruption... Documentation/power/swsusp.txt actually has a "BIG FAT WARNING" about this.

– user1686 – 2012-09-06T11:01:55.973

@WoLpH: Also, regarding the battery thing, Linux 3.6 will have "hybrid suspend", which writes a snapshot but still uses suspend mode -- this sounds like it would be useful in your case.

– user1686 – 2012-09-06T11:04:10.057

That indeed would fix half of the problems I have, the crashes are rare so I'm not that worried about that. The big fat warning should be acceptable if it would somehow be possible to freeze the state of the disk while writing this file (perhaps btrfs snapshots?). Cause even with suspend to disk you're not guaranteed that the filesystem never changed (i.e. booting another OS in the meantime). – Wolph – 2012-09-07T13:05:57.380