G4IT

Group For IT Discussion

Increase your available swap space with a swap file

You’ve finally configured every aspect of your Linux system. All of your devices function, and everything is configured just the way you like it. At least you think so, until you start running out of memory when you have OpenOffice.org and lots of browser tabs open simultaneously. You realize you should have specified a larger swap partition during your install. Does this mean you have to install again from scratch? Happily, no. Here’s how to set up some extra swap space on one of your existing partitions.
There’s always more than one way to maintain your Linux system. Instead of creating a swap file, you could instead resize and or reshuffle your partitions with parted or its graphical front end QtParted. However, resizing your partitions is a much more severe change than simply adding a file to one of them.

To start off, see how much swap space you already have. At a command line, type swapon -s (you might need to prepend /sbin/ if you’re not root). The command should produce a message that looks something like this:

Filename Type Size Used Priority
/dev/hda2 partition 128044 92472 -1
The numbers under “Size” and “Used” are in kilobytes. Uh oh. On this system we’ve used about 92MB of our measly 128MB swap partition — and we’re not even running OpenOffice.org yet. We need an auxiliary swap file.

Let’s figure out where to put it. Running df -m (short for “disk free”) from a command line should produce output something like this:

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/hda1 11443 6191 5252 55% /
The -m switch we used provided us with output in megabytes. Under the “Available” column we have approximately 5GB of free space on our root partition. Let’s steal 512MB of that for our auxiliary swap file. You might want more or less, depending on your memory needs, how much swap space you already have available, and how much free disk space you have. The general rule of thumb for swap size is that your total available swap space should be around double your RAM size. If you have additional partitions, and one of those is a better candidate than the / partition, feel free to use it instead.

You may have heard before that “everything’s a file in Unix and Linux.” Device files (like our /dev/hda1 partition) are a special type of file. What we’re about to do is create a plain-jane, non-special file, prepare it for swapification, and then tell Linux to use it when it runs out of space on its regular swap device (file).

A brief word of warning. Back up your important data before proceeding. If you carefully follow the steps below you should be fine, but it’s always better to be safe.

In order to create our supplementary swap file, we’re going to use the dd (data dump) command. You’ll need to become root to perform the next few steps. In a regular terminal type su – and enter your root password. When you’re ready, carefully type:

dd if=/dev/zero of=/extraswap bs=1M count=512
replacing 512 with the number of megabytes you want in your auxiliary swap file. if= and of= are short for infile and outfile. The /dev/zero device file will give us zeroes to be written to the output file. If you want this file on a different partition, say your /var partition, you would replace /extraswap with /var/extraswap.

Now we have a file the size we want on disk, and we can prepare it for use as a swap partition. We’ll use the mkswap command to make our file swap-consumable for the Linux kernel. Again as root, carefully type:

mkswap /extraswap
To turn on our swap file, we run swapon /extraswap. Now when we run swapon -s we should see our existing swap partition and our new swapfile. Also, the free command should show an increase in total swap space.

But we’re still not done yet. If we reboot our machine now, our new swapfile won’t be active, and we’ll have to run swapon /extraswap again. In order to make things more permanent, we’ll need to edit our /etc/fstab file.

First, make a copy of the file. (You’ll see why shortly.) Something like this should do the trick:

cp /etc/fstab /etc/fstab.mybackup
Now open /etc/fstab in your favorite text editor and find a line about your swapfile that looks something like this:

/dev/hda2 none swap sw 0 0
You’ll need another line like that underneath it pointing to your new swap file. Replace the first column with the location of your new swap file. For our example, the new line should look like this:

/extraswap none swap sw 0 0
Save the file. Mistaken changes to /etc/fstab could render your system unbootable, so just to make sure you didn’t accidently change anything else in /etc/fstab, run diff /etc/fstab.mybackup /etc/fstab to check for differences. That should output only the single line you added, with a “>” sign in front of it. If you see anything else in diff’s output, edit /etc/fstab again, fix it, and run the above diff command again.

Congratulations! You should now have more swap space than you did before.

Was this an ideal solution? Hardly. Linux nuts would call this a “dirty hack.” When swapping with your new auxiliary swap file your system will be working a little harder than when it’s swapping to a dedicated swap partition. In order to access the swapfile, the Linux kernel will have to first talk to the filesystem that the swapfile is on before it can get the information it wants from the swapfile itself. When swapping directly to a partition, the kernel doesn’t have to take this extra step.

In practical terms, there’s a minimal performance hit from this extra step. By the time you’ve run out of RAM and are beginning to swap, you’re already suffering a massive performance hit. After your original swap partition is full and you’re spilling into your auxiliary swap file, your system should be suffering badly enough that the added performance hit will be completely imperceptible.

In order to avoid this sort of problem entirely with your next install, using Linux’s Logical Volume Manger is probably a good idea, and there are other Linux memory management techniques. Of course the ideal solution is to just install additional RAM.

Filed under: Linux

Add a swap file or expand existing swap space in Ubuntu

It’s a myth to say that Ubuntu (or any Linux) needs a swap partition. This is certainly the preferred way of working, and is most efficient, but Linux can also use a single swap file located in the root partition, just like Windows or Mac OS X.

There are times when this is advantageous, such as if you’re only able to create one partition for Ubuntu (for example, Apple’s BootCamp software only allows the creation of a single non-Mac partition when dual-booting).

To create a swap file, you need to first create a dummy file of sufficient size, then format it as a swap file, and finally ensure that Ubuntu uses it at boot-up. The following steps do just that (be extremely careful entering these commands):

1) Open a terminal window and create an empty file in the root of the file system using the dd command, as follows (this creates a 1GB file—you should ideally adjust the count= figure to at least match the size of your memory, bearing in mind that there is 1,024MB in a 1GB):

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

2) Now we need to format it as a swap file:

$ sudo mkswap /swapfile

3) The final step is to make Ubuntu mount it at boot, which is done by editing /etc/fstab:

$ gksu gedit /etc/fstab

Then make a new line at the bottom of the file and add the following:

/swapfile none swap sw 0 0

You can align the entries on the line under the column headings in fstab, like the other entries in the file, but it doesn’t matter so long as there is at least one space between each entry on the line. Once done, save the file and reboot your computer.

Once the computer has rebooted, you can test to see if the swapfile is being utilized by typing cat /proc/meminfogrep Swap.

The steps above can also be used to add more swap space to a system that has an existing swap partition. You might want to do this if you’re editing extremely high-resolution photographs, for example, or working with large video files.

Filed under: Linux

Red Hat Enterprise Linux 5.2 Beta

Red Hat Enterprise Linux 5.2 Beta
by RHEL5 Team
On Tuesday, March 11, Red Hat released the Beta version of Red Hat Enterprise Linux 5.2. The Beta is expected to last approximately two months, with the final, supported update appearing soon after. During the Beta, Red Hat’s QA group, partners and customers will put the update through extensive testing for hardware support, feature enhancements and correct application operation to ensure that the final update is as solid as possible.
It is worth noting that although the Linux kernel is still 2.6-based, the number of enhancements between 2.6.0 and 2.6.24 (the latest version) are probably as extensive as between previous major releases, such as 2.2 to 2.4 or 2.4 to 2.6. This is because the 2.6 incremental development model is working very well, so that enhancements get merged upstream in manageable amounts. Who knows, perhaps the days of the “big new kernel release” are behind us, something that is a credit to the quality of the Linux kernel and the open source development model.Meanwhile, Red Hat Enterprise Linux 5 releases, which are designed to maintain stable user and core kernel APIs/ABIs, are consistent in their use of the 2.6.24 kernel. However, under the Red Hat Enterprise Linux subscription model, Red Hat engineers backport many of the new features from later kernels to the Red Hat Enterprise Linux 5 kernel, such as support for new hardware and virtualization enhancements. This provides Red Hat Enterprise Linux subscribers with important new capabilities while maintaining stable application interfaces — so that applications continue to run after new updates are installed. And, of course, it’s always worth repeating that updates, which are released about twice a year, are included with every Red Hat Enterprise Linux subscription.
This combination of application stability and new capabilities is one of the primary benefits of the Red Hat subscription model, and is valued by customers and ISVs alike.
This entry was posted by RHEL5 Team on Thursday, March 13th, 2008 at 2:48 pm and is filed under Red Hat Enterprise Linux 5, Open Source. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.
Comments are closed.

Filed under: Linux

Follow

Get every new post delivered to your Inbox.