How to increase swap size with a swap file and Partition

来源:互联网 发布:安卓app 知乎 编辑:程序博客网 时间:2024/05/21 20:24
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 withparted or its graphical front endQtParted. 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, typeswapon -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 typesu - 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= andof= 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 themkswap 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 runswapon -s we should see our existing swap partition and our new swapfile. Also, thefree 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 runswapon /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, rundiff /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 indiff's output, edit /etc/fstab again, fix it, and run the abovediff 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'realready 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.


-------------------------------------------------------

Swap memory is required when system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current process that needs the memory immediately.So a page of memory is copied to the preconfigured space on the hard disk. Disk speed are much slower compared to memory speed. Swaping pages gives more space for current application in the memory (RAM) and make application run faster.

In some scenarios system admin need to increase swap space. Below command helps you to expand the existing swap space

Step 1

Check if you have enough space on disk to create new partition for swap

Step 2

Create new partition using tool like fdisk

#fdisk /dev/cciss/c0d0

Make the new partition as swap. Change toggle id to 82 (for swap).

Step 3

Run mkswap command on the newly created swap partition

#mkswap /dev/cciss/c0d0p9
Setting up swapspace version 1, size = 6004416 kB

Step 4

Run swapon commad to enable swap space

#swapon /dev/cciss/c0d0p9

Step 5

Verify the newly added swap space using below commands

#cat /proc/swap

or

#swapon -s

Step 6

Add newly created swap partition to fstab file. It should look as below

/dev/cciss/c0d0p9 swap swap defaults 0 0

Somebody was asking me how to create / remove swap partiton for lvm. Below command helps for that. Assuming swap partiton was created on lvm partition.

Follow the below steps to increase the Swap for LVM

# swapoff -v /dev/rootvg/swapvol
# lvm lvresize /dev/rootvg/swapvol -L +8G (increase from 8 GB to 16 GB)
# mkswap /dev/rootvg/swapvol
# swapon -va

Follow the below steps to delete/ remove the Swap partition for LVM

Step 1

swapoff -v /dev/VolGroup00/LogVol01

Step 2

Then you need to delete the swap partition entirely

#lvremove /dev/VolGroup00/LogVol01

Step 3

Remove the following entry from your /etc/fstab file.

/dev/VolGroup00/LogVol01 swap swap defaults 0 0


原创粉丝点击