Results 1 to 10 of 45

Thread: HOWTO: Use swapfile instead of partition and hibernate

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Location
    Silicon Valley
    Beans
    19

    Post HOWTO: Use swapfile instead of swap partition and have working hibernation

    This HOWTO explains how to use swapfile and still have hibernation working.

    (For advanced console users / cmdline junkies - you may find compressed list of commands at the end of this HOWTO)

    Why would you want a swapfile? Isn't the traditional swap partition working fine?
    Swap partition does work fine, but there are 3 cases when you may want a swapfile instead:
    1. You forgot to make a swap partition
    2. You need to have no swap partition, for instance to have Mac trippleboot Ubuntu/MacOSX/Win* and comply with all of the OS's limitations (which in my case allowed only 4 primary MBR partitions, one taken by GUID/GPT/EFI which makes MacOSX comfortable)
    3. Have flexibility of resizing swap at any time. Partition may be impossible to change/resize later.


    This HOWTO was tested only on Intrepid, but may work on other versions.
    UPDATE 7 Sept 2011: I tested it with Ubuntu 10.04 LTS (Lucid Lynx) and made appropriate edits.
    To reverse back to swap partition: http://ubuntuforums.org/showpost.php...0&postcount=13

    DISCLAIMER: Messing up with disk partitions may make your system unusable, cause loss of data, loss of hair and/or sleep. Knowledge of terminal and command line is required. You're warned - proceed at your own risk.

    PREREQUISITES
    • Ubuntu installed on disk drive and running
    • IMPORTANT! Hibernation and resume should already work OK before this change
    • Enough free space on "/" partition
    • You should have your sudo password and be on the sudoers list


    PART 1 - SWAPFILE

    Attention: If you have swap partition and hibernation does not work, STOP HERE. First fix hibernation (seek help elsewhere, try "sudo apt-get install hibernation") before proceeding.

    Note: Using swapfile breaks hibernation. We will fix it in PART 2 below.

    Boot into your system (reboot is recommended to close all extra applications). Open terminal. Copy & paste one command at a time (making necessary edits as denoted by "<...>").

    Code:
    cat /proc/meminfo
    Note memory size (first line saying "MemTotal"). Graphics card may eat a chunk out of the main RAM, so it is not necessarily a multiple of GiB.

    For RAM of size N Bytes we will need a swapfile size of N to 2*N Bytes for both virtual memory and hibernation to work properly. Having swap larger than 2*N Bytes is usually unnecessary and just wastes space, unless you are planning to upgrade RAM in the near future. You can rerun this HOWTO prior to RAM upgrade, or allocate enough swap space now.

    Code:
    sudo dd if=/dev/zero of=/swapfile bs=1024 count=XX
    The swapfile size is equal to [count*bs] (in Bytes), So XX = desired swap size in Bytes / 1024 (bs parameter implies Bytes).

    dd can take standard multiplier letters, e.g. count=2M or count=2048K will both result in 1024*2M=2G (note that count parameter has special decoding of multipliers - adding letter B changes from computer 1024 to metric 1000, i.e 1M = 1024*1024, while 1MB=1000*1000). You can dry-run dd with of=/dev/null to see if your numbers will get the desired swapfile size. See "man dd" if you need more information.

    You can go ahead and check stock quotes while dd creates the new file for a few minutes.

    Code:
    sudo chmod 600 /swapfile && sudo mkswap /swapfile
    Important Note: The UUID that mkswap returns is absolutely useless.

    Code:
    sudo swapoff -a
    sudo swapon /swapfile
    sudo -b gedit /etc/fstab
    Edit /etc/fstab and :
    1. Remove or comment out ALL old swap partitions
    2. Add new line:

    Code:
    /swapfile   none   swap   sw   0   0
    Verify that everything is done OK:
    Code:
    free -m
    You will see your swap reported.
    Code:
    swapon -s
    - will show you that you are indeed using swapfile


    PART 2 - HIBERNATION

    Now we will make the hibernation work again. Intrepid kernel has all the provisions (thanks to Rafael J. Wysocki), but surrounding files do not tell it how to use the swapfile. Even more:
    1. Reinstall or update of initramf-tools won't make the right setup, though it should (maybe it will someday)
    2. Hibernate does not work with swapfiles out of the box and will exhibit behavior similar to this bug: [Ubuntu bug 313724, Red Hat bug 466408] "PM: Swap header not found!

    So we do it by hand:

    Code:
    mount | grep " / "
    Note your /dev/... for "/" disk.
    Code:
    sudo blkid -g
    sudo blkid
    Note UUID of /dev/... that corresponds to "/", we will use it in resume=UUID= below.

    Code:
    sudo filefrag -v /swapfile | grep "First block:"
    Note first block, we will use it for resume_offset below.
    If there is no output from this command, you have a new filefrag which changed its output. Do this instead:
    Code:
    sudo filefrag -v /swapfile
    ...and note the number in the first line under "physical" (3rd column)
    Here is an example of the output, the number for resume_offset is 154484:
    Code:
    Filesystem type is: 58465342
    File size of /swapfile is 8261935104 (2017074 blocks, blocksize 4096)
     ext logical physical expected length flags
       0       0   154484          1241648 
       1 1241648  6233816  1396131 775426 eof
    /swapfile: 2 extents found
    Replace UUID and resume_offset:
    Code:
    echo "resume=UUID=<your UUID> resume_offset=<youroffset>" | sudo tee /etc/initramfs-tools/conf.d/resume
    Now edit GRUB configuration:
    (for old grub, before grub2)
    Code:
    sudo -b gedit /boot/grub/menu.lst
    Find the line "# kopt=root=UUID=..."
    and add to it "resume=UUID=<your UUID> resume_offset=<youroffset>" (without quotes).
    Note that your UUID will match root=UUID, unless you have placed swapfile on different partition than "/".

    It should look like this (with your UUID and offset):
    Code:
    # kopt=root=UUID=... ro resume=UUID=cd71de6f-907a-40d7-bf26-c17f201e5718 resume_offset=66050
    Make sure you have no carriage returns in this line.

    (for new grub2)
    Code:
    sudo -b gedit /etc/default/grub
    Find the line "GRUB_CMDLINE_LINUX_DEFAULT=..."
    and add to it "resume=UUID=<your UUID> resume_offset=<youroffset>" (so it stays inside quotes with whatether text was there).
    Note that your UUID will match root=UUID in /boot/grub/grub.cfg, unless you have placed swapfile on different partition than "/".

    Now let's run the tools to make them work:

    For pre-grub2:
    Code:
    sudo update-grub -y
    For grub2:
    Code:
    sudo update-grub
    Answer "instal the package maintainer's version" if it asks.

    Code:
    sudo update-initramfs -u
    Verify that all your kernel stanzas (menu entries) got updated in /boot/grub/menu.lst (or in /boot/grub/grub.cfg for grub2) with resume=... and resume_offset=..., if not - verify your # kopt= line - it should have # and space (GRUB_CMDLINE_LINUX_DEFAULT= line for grub2 should have quotes). Rerun update-grub if necessary. It may keep your old file if you chose wrong answer above.

    Now reboot before trying to hibernate (important), so kernel gets the right resume_offset parameter.

    After reboot, try to hibernate. It should get right back to this window after you power your computer back up.

    Now you can get rid of the old swap partition and use it for other stuff.


    CONCLUSION

    This is a quick command summary:
    Part 1 - make swap file
    Code:
    cat /proc/meminfo
    ## Note memory size. Graphics card may eat a chunk of main RAM
    ## We will need swapfile the size of N to 2*N of RAM
    sudo swapoff -a
    sudo dd if=/dev/zero of=/swapfile bs=1024 count=8M	;## 2*N of RAM, swap size=count*bs
    sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
    sudo -b gedit /etc/fstab
    # Remove ALL old swap partitions
    # Add: /swapfile   none   swap   sw   0   0
    free -m
    swapon -s
    Part 2 - use swap file for hibernation
    Code:
    mount | grep " / "    ;# Note your /dev/... on /
    sudo blkid -g
    sudo blkid    ;## get / -> /dev/... -> partition UUID -> resume=UUID=
    sudo filefrag -v /swapfile | grep "First block:"	;## get First block -> resume_offset
    # for grub2:
    sudo filefrag -v /swapfile	;## get first "physical" number -> resume_offset
    echo "resume=UUID=cdXX--X18 resume_offset=66050" | sudo tee /etc/initramfs-tools/conf.d/resume
    sudo -b gedit /boot/grub/menu.lst
    # kopt=root=UUID=... ro resume=UUID=cdXX--X18 resume_offset=66050 
    # for grub2:
    sudo -b gedit /etc/default/grub
    GRUB_CMDLINE_LINUX_DEFAULT="... resume=UUID=cdXX--X18 resume_offset=66050"
    sudo update-grub -y
    # Answer "use maintainer version" if it asks
    sudo update-initramfs -u

    APPENDIX - SAME OR SEPARATE FILES FOR SWAP AND HIBERNATION?

    There is some controversy going on in regards to using the same file or partition for both swap and hibernation. If swapfile is not big enough, hibernation may fail, depending on how many applications and documents are open. Some argue rightfully for 2 separate files - one for swap, one for hibernation.

    Swapfile approach should make it very easy to have 2 separate files and avoid problems. Given RAM size of N Bytes the above instructions could be used as follows to create two files:
    1. /swapfile of size N Bytes for "swapon /swapfile" only.
    2. /hiberfile of size N Bytes for PART2 - HIBERNATION (use instructions in PART1 to create it, but don't add it to fstab or swapon)

    Unfortunately, when done this way, hibernation fails with kernel message "PM: Cannot find swap device, try swapon -a". It looks like a deficiency of PM code in current kernel. So the answer is no, not yet. For now use single file that is big enough and hope you never run out of space in it and get failing hibernation.
    Last edited by iva2k; September 11th, 2011 at 06:20 AM. Reason: Added notes for grub2 and latest filefrag

  2. #2
    Join Date
    Jul 2005
    Location
    Hughenden, Australia
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: HOWTO: Use swapfile instead of partition and hibernate

    We will need swapfile the size of N to 2*N of RAM size for virtual memory and hibernation to work properly.
    What units are you using for N?
    In the first command we looked up our Memtotal in kB.
    My EeePC has 512 Mb or RAM, and Memtotal is 505744 kB, with 6145 kB MemFree.

    So I presume you mean if I want a 512 Mb swap file, I would do 512000/1024=500 kB ?

    So for me, N=500 ?

    Would that be right?

    No, wait ... the dd command 'bs' is in bytes, so 1024 x 500 is only 512 000 bytes, or 500 kB.
    I want 500 Mb, so maybe 512 000 would be more like it.
    N=512000 ?
    Code:
    herman@eeepc-ibex:~$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=512000
    [sudo] password for herman: 
    512000+0 records in
    512000+0 records out
    524288000 bytes (524 MB) copied, 54.9668 s, 9.5 MB/s
    Nevermind,got it! Sorry if I bothered anyone.

    Thanks very much for sharing your knowledge with that useful post, iva2k, it's just what I needed.
    I completed part 1, but I don't think I'll use hibernation, so I'll leave part 2 for later maybe.
    Last edited by Herman; January 18th, 2009 at 01:14 PM.
    Ubuntu user since 2004 (Warty Warthog)

  3. #3
    Join Date
    Nov 2008
    Location
    Enschede, the Netherlands
    Beans
    16
    Distro
    Ubuntu

    Talking Re: HOWTO: Use swapfile instead of partition and hibernate

    Wow, many thanks iva2k!

    My hibernation didn't work at all, even though I use a swap partition. I followed part 2 of your guide substituting /swapfile for my swap partition and leaving out the resume_offset=<youroffset> parts.

    Works like a charm!

  4. #4
    Join Date
    Apr 2006
    Location
    Silicon Valley
    Beans
    19

    Re: HOWTO: Use swapfile instead of partition and hibernate

    Herman - Thanks! I edited HOWTO to explain dd count in more detail.

    q41 - you are right, it should work in principle for swap partitions as well. Just keep using corresponding /dev/sdXX instead of /swapfile. Then that "absolutely useless" UUID from mkswap will be your UUID for resume=UUID=, skip or replace dd step with partitioning step and also remove resume_offset from both /boot/grub/menu.lst and /etc/initramfs-tools/conf.d/resume.
    Last edited by iva2k; January 18th, 2009 at 11:25 PM.

  5. #5
    Join Date
    Jul 2005
    Location
    Hughenden, Australia
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: HOWTO: Use swapfile instead of partition and hibernate

    I have now completed stage 2 of your how-to and hiberated my EeePC to try it out.
    It hibernated and resumed beautifully, thanks once again, iva2k !
    Ubuntu user since 2004 (Warty Warthog)

  6. #6
    Join Date
    Jan 2009
    Beans
    35

    Re: HOWTO: Use swapfile instead of partition and hibernate

    If I have 1 gig of ram, do I REALLY need 2 gig of ram for my swap space? I have Ubuntu installed on a usb stick that's partitioned to 7 gig. I really don't want to waste space. What will happen if I use a have a swap space of 512meg or 1 gig instead of the recommended 2 gig. will I still be able to hibernate? It's hard to imagine that linux will dump the whole ram onto the disk instead of only dumping what's useful in memory.

    Thanks.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •