-   Linux - Desktop (
-   -   on-demand swap space? (

rabbit2345 09-24-2012 09:53 AM

on-demand swap space?

So my laptop has a funny "feature" that is quite annoying. It likes to wake itself up whenever the battery gets low, the time it really needs to stay asleep. I checked the BIOS for some setting, but it seems that ASUS made this a hardwired feature. Present in windows as well.

Anyway, under normal conditions, I don't want to waste disk space for swap that never gets used, but it seems the only way to stop my computer from running itself to the ground is to hibernate it. So my question is this: how can I make swap space on demand? I saw some code in someone's signature here, but I could not find it again. I was thinking about adding something like this to my sleep scripts:



dd if=/dev/zero of=/home/swap bs=1M count=1000

mkswap /home/swap

swapon /home/swap

Also, how much swap do I actually need to make? I only need enough to hibernate the computer.


AlucardZero 09-24-2012 11:02 AM

Do you have a small hard drive?

To hibernate, your swap should be the size of your RAM maximum.

JaseP 09-24-2012 11:35 AM

Actually, his question is pertinent to SSDs as well, as having a persistent swap space is counter-productive on SSD drives (dedicating a place to wear out as opposed to allowing it to be more dynamic, to take better advantage of wear-leveling), as is frequent logging, ... I'm actually looking into this for an older-ish EEE PC I have (1st gen., 8GB SSD). It's a low priority issue for me,... But, if I come up with a practical solution before anyone else does, I'll let you know,...

Oh,... and to safeguard against frequent swapping, set your swappiness to something low, like 5, or even 1... Default is typically 60 (which translates roughly as 60% likely to push something to swap).

jefro 09-24-2012 03:47 PM

Is it waking up to do a full shutdown?

rabbit2345 09-25-2012 03:27 PM


After working on this a bit, I came up with this script and placed it into /usr/lib/pm-utils/sleep.d/00amkswap:

MEM=$(free | sed -n 's/Mem: *\([[:digit:]]*\) *.*/\1/p')

case ${1} in
          fallocate -l ${MEM} ${SWAP}
          mkswap ${SWAP}
          OFFSET=$(filefrag -v ${SWAP}| sed -n '4 s/^ *0 *0 *\([[:digit:]]*\) *.*$/\1/p')
          sed "s/.*vmlinuz.*splash=.*$/& resume_offset=${OFFSET}/" /boot/grub2/grub.cfg > tmp
          mv /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bck
          mv tmp /boot/grub2/grub.cfg
          cp /etc/fstab /etc/fstab.bck
          echo "/home/swap              swap            swap    defaults      0 0" >> /etc/fstab
          swapon ${SWAP}

          rm /boot/grub2/grub.cfg
          mv /boot/grub2/grub.cfg.bck /boot/grub2/grub.cfg
          swapoff ${SWAP}
          rm ${SWAP}
          mv /etc/fstab.bck /etc/fstab

          echo "No action taken."

I know there are some inefficiencies, but overall the script works, I just have one last problem.
When pm-hibernate is called, everything is good, but the system is unable to find a swap device, despite there being swap space present, and recorded in /etc/fstab. PM complains "cannot find swap device, try swapon -a"

However, if I boot up with resume=/dev/sda4 resume_offset=12345, then the first suspend works fine, since my script replaces the offset with the real one. But any hibernates after that fail. Any ideas?


JaseP 09-25-2012 04:02 PM

Have a look at this thread in the Ubuntu forums;

Similar, but do not appear to be deleting the swap file...

Maybe you can adjust your approach.

All times are GMT -5. The time now is 12:38 PM.