Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 16 down vote accepted

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

share|improve this answer
    
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 Aug 11 '09 at 8:53
    
See my update with a link to the documentation on resuming from swap files. –  Robert Munteanu Aug 11 '09 at 8:57
    
See also nik's answer below for a good Ubuntu tutorial that uses the information documented in the kernel.org link above. –  Hanno Fietz Aug 11 '09 at 10:31
4  
Plz, add the command chmod 0600 /swapfile. A world-readable swap file is a huge local vulnerability. –  user4035 May 12 '13 at 1:23
1  
I kwow this is old, but I would use 'truncate' instead of 'dd'. No need to actually write anything to the disk. –  Guido Aug 5 '13 at 23:58

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).

share|improve this answer
1  
Well, 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 Aug 14 '09 at 9:20
4  
What 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 Aug 24 '09 at 18:10
1  
Actually, 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 Jun 17 '11 at 11:17
    
@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 Jun 21 '11 at 12:34

You probably have two ways here,

  1. Consider a swap-file instead of a swap-partition
  2. 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.

share|improve this answer
    
Suspend to USB flash stick would be so slow. –  Tadeusz A. Kadłubowski Aug 11 '09 at 9:14
4  
I 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 Aug 11 '09 at 10:21
    
+1 for the link to the howto. –  Hanno Fietz Aug 11 '09 at 10:30
1  
@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 Aug 11 '09 at 10:43
1  
Firefox 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 Nov 27 '12 at 8:59

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.

share|improve this answer
2  
Well, 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 Aug 11 '09 at 10:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.