Modify swap space in Linux (with LVM)

I posted a while back how to modify your swap space in Solaris 10, now I will show you how to do it in Linux. This example is from a Red Hat Enterprise (RHEL5) clone and is using Logical Volume Management (LVM), but should be usable in other Linux distributions and you could use fdisk to resize the partition instead of using LVM. I will specifically show how to reduce swap space here, put is applicable to enlarging it as well.

To see what file system is being used for your current swap space, use proc:

# cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 4194296 0 -1

And then to see how much swap is in use, run the free command:

# free
total used free shared buffers cached
Mem: 385560 76388 309172 0 11328 33788
-/+ buffers/cache: 31272 354288
Swap: 4194296 0 4194296

If your swap space is in use, you will need to reboot into single user mode, shut some applications down until it is free, or add a separate swap drive or file for swap use temporarily. Once your swap space is free to be modified, turn off the swap space that you want to modify:

# swapoff -v /dev/VolGroup00/LogVol01
swapoff on /dev/VolGroup00/LogVol01

You can now verify that the swap space is no longer in use:

# free
total used free shared buffers cached
Mem: 385560 74404 311156 0 11340 33780
-/+ buffers/cache: 29284 356276
Swap: 0 0 0
# cat /proc/swaps

Now that the file system is not is use, we are free to modify it. In this case, I am reducing the 4 GB set aside for swap to 1 GB so that I can reuse it on my root partition. Here’s the layout of my current LVM partitions:

# lvdisplay
— Logical volume —
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID jzjdTT-Md9K-iP52-3kv4-OqSL-2Y0c-yxUq7o
LV Write Access read/write
LV Status available
# open 1
LV Size 7.88 GB
Current LE 252
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0

— Logical volume —
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID ixEabw-7lMg-ho6h-GcVq-pEOE-rHPb-X3HY3e
LV Write Access read/write
LV Status available
# open 1
LV Size 4.00 GB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

Now to shrink the swap space partition. The fact that shrinking the partition is destructive doesn’t matter since it is just swap space anyway:

# lvm lvreduce /dev/VolGroup00/LogVol01 -L -3G
WARNING: Reducing active logical volume to 1.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 1.00 GB
Logical volume LogVol01 successfully resized

And now to extend the root partition. (You can increase the size while the partition is in use and it is non-destructive.):

# lvm lvextend -l +100%FREE /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 10.88 GB
Logical volume LogVol00 successfully resized

Let’s take a look at the new partition sizes:

# lvdisplay
— Logical volume —
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID jzjdTT-Md9K-iP52-3kv4-OqSL-2Y0c-yxUq7o
LV Write Access read/write
LV Status available
# open 1
LV Size 10.88 GB
Current LE 348
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

— Logical volume —
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID ixEabw-7lMg-ho6h-GcVq-pEOE-rHPb-X3HY3e
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

Now to remake the smaller partition into usable swap space:

# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 1073737 kB

And then re-add it back to the OS as usable swap space:

# swapon -va
swapon on /dev/VolGroup00/LogVol01

Verify that the swap space is back:

# cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 1048568 0 -2

# free
total used free shared buffers cached
Mem: 385560 76156 309404 0 11668 35036
-/+ buffers/cache: 29452 356108
Swap: 1048568 0 1048568

Let’s go ahead and resize the actual file system on the root partition to take into account the newly available space. First, a before snapshot:

# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 764M 6.5G 11% /

Now to increase it. Note, that if you do not specify a new size, it will automatically fill up to the maximum size of the underlying partition:

# resize2fs -p /dev/mapper/VolGroup00-LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup00-LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-LogVol00 to 2850816 (4k) blocks.
The filesystem on /dev/mapper/VolGroup00-LogVol00 is now 2850816 blocks long.

Verify the new size:

# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
11G 766M 9.3G 8% /

Next post I’ll explain how to add a second drive to this system.





Please VOTE for this page at: ADD TO DEL.ICIO.US | ADD TO DIGG | ADD TO FURL | ADD TO NEWSVINE | ADD TO NETSCAPE | ADD TO REDDIT | ADD TO STUMBLEUPON | ADD TO TECHNORATI FAVORITES | ADD TO SQUIDOO | ADD TO WINDOWS LIVE | ADD TO YAHOO MYWEB | ADD TO ASK | ADD TO GOOGLE


5 Comments


  1. […] « Modify swap space in Linux (with LVM) […]

    Posted October 10, 2008, 9:38 pm

  2. Thanks for the very helpful information. I had extended my /usr partition and it was not reflecting the new size using df -h.

    Your article helped me see the missing part which was resize2fs.

    Posted March 19, 2009, 12:23 am

  3. How to make this changes permanent with the reboot.

    Thanks
    RD

    Posted August 25, 2011, 3:00 pm

  4. RD – What is not surviving the reboot? All these changes should be permanent.

    Posted August 25, 2011, 9:28 pm

  5. Excellent, this worked for me. I was able to increase swap space from 4Gb to 12Gb for my Oracle 11R2 installation flawlessly. Thank you very much!

    Posted March 28, 2012, 3:58 pm

Leave a reply