Jan 23, 2011

Creating Swap File/ Partition with out re-installing the system

Creating Swap File/ Partition with out re-installing the system
You can add swap space without re-installing. Before we look at that option, let's explore some other possibilities. For instance, if you're right on the border of having enough memory you might look at reducing the amount of physical RAM you need. If you're using one of the heavier desktop environments (GNOME or KDE) you can save quite a bit of memory by switching to Xfce or LXDE. Depending on the machine you have and your resources, you might also look at adding more RAM to your computer. In an ideal world we want to keep our data in RAM, rather than rely on swap space.
However, if you do need to create some swap space it's not required that you give swap its own partition. Modern versions of the Linux kernel have greatly improved performance when it comes to accessing swap files. This makes creating a swap file far more appealing than re-installing or resizing your existing partitions to make room for a swap partition. Let's walk through the process of creating a 1 GB swap file.

First we need to create an empty file, 1 GB in size. We do this with the dd command. You will need to have root permissions (or sudo access) for the following commands to work:

     dd if=/dev/zero of=/mnt/my_swap bs=1M count=1024

In this case the "1024" means we're creating a 1GB file, "512" would result in a 512MB (half a GB) file, "2048" would create a 2GB file, etc. Next we have to format our file so it can be used for swap space.

     mkswap /mnt/my_swap

We now have a swap file and we need to tell the system to start using it.

     swapon /mnt/my_swap

The next step is to make sure your operating system remembers to use this swap file again after you reboot the machine. We do this by opening the file /etc/fstab in a text editor and adding this line at the bottom:

     /mnt/my_swap none swap sw 0 0

Save your changes to /etc/fstab and you're done. The next time you boot the machine you can run

     swapon -s

to make sure your swap file is in use. The "swapon -s" command displays all swap spaces currently in use and you should see /mnt/my_swap listed in the command's output.
[Via Distrowatch.com link]

No comments:

Post a Comment