Can I hibernate Linux without a swap partition?

30

20

I hadn't set up a swap partition on my PC, because a) I have plenty of RAM (8 GB) and b) I have large harddrives that I didn't want to chop into tiny pieces, so my smallest partitions are 50 GiB, and I'm already using those for the OS's.

Now, to hibernate, my Linux (Ubuntu Jaunty) required swap. I had a spare 100 GB partition so I set that up as swap, but I'm not satisfied with that. It's only ever used for hibernation (there's always plenty of RAM free and the system never goes to swap) and it's also a little large for swap. Also, I'm eventually going to need it for something else.

How can I hibernate (suspend to disk) without a swap partition in Ubuntu Jaunty?

Hanno Fietz

Posted 2009-08-11T08:38:22.607

Reputation: 1 045

3

Here an official guide from debian about hibernate without swap: https://wiki.debian.org/Hibernation/Hibernate_Without_Swap_Partition

– Francesco – 2015-03-17T03:31:42.387

Even though you have a lot of RAM, swapping can still help you also for increasing performance, when the space of unused (and therefore swapped out) application code is used for caching data. – Michael Schmid – 2017-06-11T09:53:58.177

Answers

23

Untested idea: why don't you create a wrapper for s2disk or whichever utility handles suspend to disk which manages a swap file ( as opposed to a swap partition ) and deletes it on resume?

  • Create the swap file : dd if=/dev/zero of=/swapfile bs=1024 count=8388608 ( 8GB )
  • Setup the swap file: mkswap /swapfile
  • Only when you need to set suspend you can activate it: swapon /swapfile
  • When you resume you can deactivate it: swapoff /swapfile

Resuming from swap files is possible, and is documented on kernel.org

Robert Munteanu

Posted 2009-08-11T08:38:22.607

Reputation: 4 240

Ihad also thought of a swap file, but I don't know how to pass that to the kernel on startup. Can I just do "resume=/path/to/file" as I would with the block device? – Hanno Fietz – 2009-08-11T08:53:25.213

See my update with a link to the documentation on resuming from swap files. – Robert Munteanu – 2009-08-11T08:57:00.900

See also nik's answer below for a good Ubuntu tutorial that uses the information documented in the kernel.org link above. – Hanno Fietz – 2009-08-11T10:31:31.100

8Plz, add the command chmod 0600 /swapfile. A world-readable swap file is a huge local vulnerability. – user4035 – 2013-05-12T01:23:13.320

2I kwow this is old, but I would use 'truncate' instead of 'dd'. No need to actually write anything to the disk. – Guido – 2013-08-05T23:58:03.747

4Actually, you do need to write a sequence of something to disk, creating the file with truncate results in swapon: /swapfile: skipping - it appears to have holes.. – hlovdal – 2014-01-18T23:58:11.913

9

You probably have two ways here,

  1. Consider a swap-file instead of a swap-partition
    • Using a small (compared to your partitions sizes, but large enough for memory) USB stick for swap-partition

Whatever you do, I think a swapoff and swapon after the resume would be useful.
And, since you don't really require the swap, you could leave it swapoff after resume.

Update: The comment makes a good point about slow USB hibernation.
So, check in the numbered order -- the first scheme has notes for hibernation using swap-files.


Out of curiosity,
I'd like to know why you want to hibernate when linuxes like Ubuntu can shutdown and start so fast.
I am guessing,

  • you load up your 8GB ram with some applications and leave them there
  • or, you Wake-up-on-LAN

But, is that so really? or, do you have some other reason to hibernate?

I use a USB booting Ubuntu and always shutdown.

nik

Posted 2009-08-11T08:38:22.607

Reputation: 50 788

2Suspend to USB flash stick would be so slow. – Tadeusz A. Kadłubowski – 2009-08-11T09:14:49.473

6I tend to open loads of stuff during work and I like to have it all back as it was the next day. Ubuntu is fast to start up (actually, to me it seems faster then resume), but Eclipse, Firefox etc. might not be, and other apps might not even save their current state when closing. – Hanno Fietz – 2009-08-11T10:21:00.463

2@Hanno, Firefox also allows you to save sessions right up to your scroll-position. So, Save-and-Quite is great. Not sure what Eclipse can do. – nik – 2009-08-11T10:43:50.420

3Firefox doesn't remember what workspace you had each of its windows in, so there's the minor PITA of having to redistribute them back into place, after waiting for them all to load. There's also no way to recover the state of things like terminal windows. – intuited – 2012-11-27T08:59:30.017

Hibernating the full 8GB of memory would take less than a minute with this USB flash stick.

– intuited – 2012-11-27T09:00:30.347

4

Yes, but not without some effort. There are 2 different ways to hibernate (suspend-to-disk) on linux:

  1. swswap, which is included in the kernel
  2. tuxonice (formerly suspend2), which is not.

Tuxonice is available as a patch to the kernel, and will let you write the suspend image to an ordinary file.

From Wikipedia:

TuxOnIce (formerly known as Suspend2) is an implementation of the suspend-to-disk (or hibernate) feature which is available as patches for the 2.6 Linux kernel. It was formerly known as 'swsusp'. During the 2.5 kernel era, Pavel Machek forked the original out-of-tree version of swsusp (then at approximately beta 10) and got it merged into the vanilla kernel, while development continued in the swsusp/Suspend2/TuxOnIce line. TuxOnIce includes support for SMP, highmem and preemption. Its major advantages over swsusp are:

    * It has an extensible architecture that allows for arbitrary transformations on the image and arbitrary backends for writing the image;
    * It prepares the image and allocates storage prior to doing any storage and accounts for memory and storage usage very carefully, thereby becoming more reliable;
    * Its current modules for writing the image have been designed for speed, combining asynchronous I/O, multithreading and readahead with LZF compression in its default configuration to read and write the image as fast as hardware is able;
    * It has an active community supporting it via a wiki, mailing lists and irc channel (see the TuxOnIce website);
    * It is more flexible and configurable (via a /sys/power/tuxonice interface);
    * Whereas the current swsusp (and uswsusp) implementations support writing the image to one swap device only, TuxOnIce supports multiple devices in any combination of swap files and swap partitions. It can also write the image to an ordinary file, thereby avoiding potential race issues in freeing memory when preparing to suspend.
    * It supports encryption by various methods;
    * It can store a full image of memory (resulting in a more responsive system post-resume), while uswsusp and swsusp write at most half the amount of RAM.

Since it's not included in the default kernel, you'd unfortunately have to pick up the kernel patches available for Jaunty and compile the kernel yourself.

There are some extended instructions here, but you might want to try out Robert's suggestion before wandering down this road, unless you are an old hand at rolling your own kernel images.

user4358

Posted 2009-08-11T08:38:22.607

Reputation:

2Well, I have handrolled a number of kernels (I've been a Gentoo enthusiast for some years), but there was a reason why I switched to Ubuntu... ;) – Hanno Fietz – 2009-08-11T10:17:42.547

3

As Joel and Jeff have discussed on the podcast, turning off swap is generally not a good idea - even if it would be worth it (which it isn't) - disk space is so incredibly cheap nowadays that leaving it on does not cost you anything. You can get a gig for less than ten cent (Euro cent, that is)!

to quote:

Atwood: I think that every geek at some point goes through this thought process: I got tons and tons of memory, maybe I can turn of my page file. I actually have a blog entry about this. ... The punch line is: it is never worth it.

Listen to the podcast for more of the conversation (it starts roughly at minute 59), or read the question at serverfault linked to on the podcast page (question 23621). They talk about the windows page file, but the argument is equally valid for UNIX (although they may manage memory in a different way).

0x89

Posted 2009-08-11T08:38:22.607

Reputation: 969

1Well, I don't mind having swap on, in fact, my current setup just uses that spare 100 gig partition as swap. What I do mind, though, is having a swap partition. 100 gig swap is just total nonsense, and I don't want to have a tiny partition just for swap. Swapfile is great for me, thanks to Robert's and nik's answers, I now also know how to use them for hibernation, so I'll use that. – Hanno Fietz – 2009-08-14T09:20:52.007

4What is the problem with having a "tiny" partition? Btw. I would not consider an 8GB partition tiny at all - I have used linux installs for years that lived on a / partiton that was smaller than that. And the first PC I have used had a 20MB hard drive.. – 0x89 – 2009-08-24T18:10:56.597

1Actually, Linux manages memory in completely different fashion to Windows. Even on my old laptop with 2GB of ram swap is barely used, 200MB offloaded there is the maximum value I saw – vava – 2011-06-17T11:17:59.730

@vava: You may be right, I adapted my answer a bit. But the fact that you saw 200MB offloaded to your swap actually supports my point.. – 0x89 – 2011-06-21T12:34:44.087