Can I hibernate Linux without a swap partition?
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?
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
You probably have two ways here,
- 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.
Yes, but not without some effort. There are 2 different ways to hibernate (suspend-to-disk) on linux:
- swswap, which is included in the kernel
- 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.
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).