日期:2014-05-16  浏览次数:20772 次

增加,修改 Linux 交换空间
It all happens some time: you run out of your main memory and the system turns to the hard disk to use as a temporary memory. What if you come to the limits of your Linux server’s swap space but the system demands more? There are basically four options here:

   1. Create a new swap file
   2. Create a swap file on an existing logical volume
   3. Extend a swap file on an existing logical volume
   4. Create a new swap partition

If you are in need of swap space now and do not think you will need it later, then taking the system down for creating a new swap partition or extending the swap file on the logical volume will be the longer way to take; creating a new swap file and making it available will be the easiest and the quickest solution. So, in our list, creating a new swap file is the easiest, extending swap file on an existing logical volume is a little harder but recommended, and creating a new swap partition is the hardest.

Create a New Swap File

We need to know the size of the additional swap space we will need. Let’s assume that we will need a 512 Megabytes of swap space. To determine the number of blocks, we need to multiply the size with 1024. In our example this gives 512 x 1024 = 524288. Now we issue

dd if=/dev/zero of=/home/swapfile bs=1024 count=524888

to open up our free space (you may argue that making a swap space in the /home directory is questionable in terms of security, but if you’re tight on hard disk space on the server, /home is the directory that you can find some free space). Then setup the file as swap with

mkswap /home/swapfile

The swap space will not be enabled at boot time. You need to add the space to /etc/fstab. Add the following line to your /etc/fstab:

/home/swapfile swap swap defaults 0 0

Creating a Swap File on an Existing Logical Volume

In this case, let’s assume we want to add the swap space on /dev/VolGroup00 and /LogVol02 is the swap space we want to add. Let’s also assume that the swap space we need is 2 Gigabytes. First, we create the logical volume of size 2 GB by

lvm lvcreate VolGroup00 -n LogVol02 -L 2 G

Then we format it by

mkswap /dev/VolGroup00/LogVol02

and add it to /etc/fstab by adding this line:

/dev/VolGroup00/LogVol02 swap swap defaults 0 0

Now we activate the swap volume by

swapon -va

To check if the swap is working properly, you can issue cat /proc/swaps or simply free.

Read on for extending a swap file on an existing logical volume and creating a new partition.
Extending Swap File on an Existing Logical Volume

For this case, let’s assume that the swap space we will extend is /dev/VolGroup00/LogVol01 and we want to extend it by 1 Gigabyte. First, we disable the active swap file by

swapoff -v /dev/VolgGroup00/LogVol01

Then we add the swap space by

lvm lvresize /dev/Volgroup00/LogVol01 -L 1G

format it

mkswap /dev/VolGroup00/LogVol01

and finally enable the extended volume by swapon -va. As always, you can check the new space by cat /proc/swaps or free commands.

Create a New Swap Partition

We can go with the parted program here since it is easier than fdisk. Before adding a swap partition, we have to check which hard disk has sufficient space to be used as swap. We can do that by issuing print at the parted prompt. With the information we receive, we determine how much space we will use and at what partition. Assume that we will add 1 GB of swap space to /dev/hdc1. The syntax that we will use in parted is

mkpartfs partition-type filesystem-type start end

wh