Swap. Sure, it would be nice if all of your servers had an infinite amount of RAM, but they don’t. Sometimes a piece of software requires a certain amount of swap space in order to install cleanly. Other times, you need to ensure you are on a vendor’s support matrix so they don’t hang up the phone when you call in with an issue. Once in a while, you just have to buy enough time to make it to the next downtime window to deploy a hardware upgrade.

For a modern Linux kernel, there should be no performance difference between a swap partition and an unfragmented swap file. That’s the trick, though - how do you guarantee you aren’t creating a fragmented swap file? I’ve created a swap files in the past, but these days we can sidestep the fragmentation issue by just creating a swap partition - pretty easy on a system running LVM. Here are the steps:

Step 1. As with all changes, make sure you understand what is going on before you dive in. Check out the current swap situation, and verify that there is enough room in your logical volume:

$ swapon -s
Filename                   Type         Size            Used    Priority
/dev/mapper/rootvg-swap1   partition	16777208	359304	-1

$ free -m
             total       used       free     shared    buffers     cached
Mem:         96670      95261       1408          0        285      11295
-/+ buffers/cache:      83680      12989
Swap:        16383        350      16033

$ sudo vgdisplay
  --- Volume group ---
  VG Name               rootvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               132.09 GB
  PE Size               32.00 MB
  Total PE              4227
  Alloc PE / Size       1728 / 54.00 GB
  Free  PE / Size       2499 / 78.09 GB

Free PE says 78 GB - plenty of free space for swap.

Step 2. Create a new logical volume. In this case, the volume will be 16 GB in size, named swap2, and get carved out of the rootvg volume group: lvcreate -L 16G -n swap2 rootvg

Step 3. Format the logical volume to create a swap partition: mkswap /dev/rootvg/swap2

Step 4. Add the swap partition to /etc/fstab so that it will persist beyond a reboot:

/dev/rootvg/swap2       swap                    swap    defaults        0 0

Step 5. Activate the swap partition: swapon /dev/rootvg/swap2

Step 6. Verify:

$ swapon -s
Filename                   Type         Size            Used    Priority
/dev/mapper/rootvg-swap1   partition	16777208	359304	-1
/dev/mapper/rootvg-swap2   partition	16777208	0	-2

$ free -m
             total       used       free     shared    buffers     cached
Mem:         96670      95261       1408          0        285      11295
-/+ buffers/cache:      83680      12989
Swap:        32766        350      32416