LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices



Reply
 
Search this Thread
Old 09-24-2012, 09:53 AM   #1
rabbit2345
Member
 
Registered: Apr 2007
Location: 中国上海
Distribution: openSUSE 11.3
Posts: 369

Rep: Reputation: 40
on-demand swap space?


Hi,

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:

Code:
#!/bin/sh

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.


thanks,
rabbit2345
 
Old 09-24-2012, 11:02 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,652

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
Do you have a small hard drive?

To hibernate, your swap should be the size of your RAM maximum.
 
Old 09-24-2012, 11:35 AM   #3
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
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).
 
Old 09-24-2012, 03:47 PM   #4
jefro
Guru
 
Registered: Mar 2008
Posts: 12,087

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Is it waking up to do a full shutdown?
 
Old 09-25-2012, 03:27 PM   #5
rabbit2345
Member
 
Registered: Apr 2007
Location: 中国上海
Distribution: openSUSE 11.3
Posts: 369

Original Poster
Rep: Reputation: 40
Hi,

After working on this a bit, I came up with this script and placed it into /usr/lib/pm-utils/sleep.d/00amkswap:
Code:
#!/bin/bash
SWAP='/home/swap'
MEM=$(free | sed -n 's/Mem: *\([[:digit:]]*\) *.*/\1/p')

case ${1} in
     hibernate)
          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}
     ;;

     thaw)
          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."
     ;;
esac
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?


thanks,
rabbit2345
 
Old 09-25-2012, 04:02 PM   #6
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Have a look at this thread in the Ubuntu forums;
http://ubuntuforums.org/showthread.php?t=1042946

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

Maybe you can adjust your approach.

Last edited by JaseP; 09-25-2012 at 04:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to reduce swap space and reclaim the space grantm Linux - Newbie 7 08-16-2012 08:05 AM
[SOLVED] what the difference swap space and swap file dundu Linux - Newbie 4 07-29-2012 01:03 PM
how much swap space is needed; how to increase swap space? johnpaulodonnell Linux - Newbie 5 03-23-2007 04:20 AM
How to unmount actual swap and mount a new(bigger) swap space? isaac Linux - Newbie 1 06-06-2004 02:23 AM
Writing swap cache to HD on demand MasterC Linux - General 2 02-27-2004 03:19 PM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration