Saturday, March 7, 2009

Making SWAP file while running UBUNTU

OS: UBUNTU 8.10 (Intrepid Ibex)
Kernel: 2.6.27-3-rt
Date: March 7th, 2009

Hi guys ever happened to you : You have a preinstalled Windows with Media center by Laptop company - so basically they have one partition for Media center. Well its a trivial thing but when I got my DELL Inspiron I didn't wanna lose the media center even though I'm never actually gonna use it (It was that thing you feel about something new :P, pretty dumb huh?). And yeah I wanted UBUNTU on primamary partition so I can have seperate boot loaders - couldn't actually got it working though (otherwise you wouldn't be seeing post about Windows after UBUNTU hehe).
Anyway jist of the story somehow I couldn't manage to create SWAP partition at the time of installation and after you have data its too painful to resize/delete-create partition.
Well having 4 gigs of RAM didn't bother me until one day I actually started doing productive work - synthesizing verilog designs the software needs way too much RAM - if you don't know.
So now I needed a SWAP but :( we don't have one to start and I'm too lazy or maybe don't wanna lose data so not resizing/deleting the drive.
Here is the way how you can create SWAP on the fly - when UBUNTU is running:
Open the terminal and issue follwing set of commands:
(PLEASE READ TO THE END BEFORE ISSUING ANY COMMAND)
sudo su OR you may also want to use sudo for each of the command if required - your choice
dd if=/dev/zero of=/swapfile bs=1024k count=2048
mkswap /swapfile
swapon /swapfile

You will have to issue swapon everytime you boot the computer
However if you want it to be done automatically each time you reboot/power ON you can put following lines in fstab:
sudo vi /etc/fstab (vi, nano, gedit, kate whatever you like)
Add following line
/swapfile none swap sw 0 0
(Again /swapfile - path to your swapfile for me /home/protik/mySwap)

P.S. If dd if fails on your computer please issue
LANG=en dd if=/dev/zero of=/swapfile bs=1024k count=2048 (There is some known problems with dd - it gives segmentation fault, as it can work only with english language (my guess :P) - for me it worked out of the box)

This will create swapfile of name /swapfile with size 2GB and will switch it on
  • /swapfile -> path to swapfile you want, It can be /path to your file name/your swap file name in my case it was /home/protik/mySwap
  • bs=1024k -> base used for further calculation (I'm not sure if we can change this - put a comment if any of you know)
  • count=2048 -> size of swap = 2048 * 1024k = 2GB you can change 2048 to whatever value you like (256, 1024, 512 etc.)

With command
free (Or any partition manager, GUI based)
you can see amout of SWAP you have

  • And ya I find it pretty convenient as I can delete the swap file any time I want and free the space if required to :)

This solution was posted at:
http://ubuntuforums.org/showthread.php?t=265051&page=2 (last checked March 7th, 2009)

No comments:

Post a Comment