Linux Services Organization

Our goal, introduce Linux services to the enterprise world.
Contact us in contact@linuxsv.org

Linux Services Organization : Linux Swap Linux Server

Swap space is used when the amount of physical memory (RAM) is full. If the system needs more memory and no more RAM is available, inactive pages in memory are moved to the swap space. Swap should not be considered as a replacement of RAM memory because swap space is on hard drives and I/O access to hard drives is slower than I/O access memory.

The swap must be located on a dedicated swap partition and it is designed to help RAM memory not to replace it.

Swap partition

* Using parted a swap partition can be created in order to be added to the existing swap (or just to create it for the first time) :

$ parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition type? primary/extended? p
File system type? [ext2]? linux-swap
Start? 1
End? 512M
(parted) quit

Now /dev/sdb1 is a 512M swap partition

* Next step is create the swap filesystem on the swap partition :

$ mkswap /dev/sdb1
Setting up swapspace version 1, size = 498684 KiB
no label, UUID=81631aa8-8064-47fc-92ef-5eb3fa6e8b87



* Once the swap filesystem has been created the final step is activate the new swap space :

$ free | grep Swap
Swap: 1736696 0 1736696

The initial swap size is 1736696 Kb

$ swapon /dev/sdb1
It activates the new swap

$ free | grep Swap
Swap: 2235376 0 2235376

After the new swap (512 M) has been added the new swap size is 2.2G

* Adding the corresponding line to /etc/fstab the system will activate automatically the swap on boot :

$ echo "/dev/sdb1 swap swap defaults 0 0" >> /etc/fstab

LVM Swap

* As normal partition an LV partition can be used to store a filesystem. Just create a LV for swap, create the swap filesystem on it and activate it as a standard swap partition :

$ lvcreate -L512M -n VolGroup01Swap VolGroup01
Logical volume "VolGroup01Swap" created


* Create swap filesystem on LV VolGroupSwap :

$ mkswap -f /dev/VolGroup01/VolGroup01Swap
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=146df94a-4ddc-4e03-b7b7-879bb4db64ea


* Configure the swap on /etc/fstab and activate it :

$ echo "/dev/VolGroup01/VolGroup01Swap swap swap defaults 0 0" >> /etc/fstab
$ swapon -a

It activates all swap partitions configured on /etc/fstab

$ free | grep Swap
Swap: 2235376 0 2235376

After the new LV swap (512 M) has been added the new swap size is 2.2G

Extending Swap on LVM

As all LVM partitions, the swap LV partition can be extended/reduced. In this way the swap area can be modified via LVM :

1.- Disable swap on the Swap-LV

$ swapoff /dev/VolGroup01/VolGroup01Swap

2.- Resize the Swap-LV

$ lvextend -L+256M /dev/VolGroup01/VolGroup01Swap
Extending logical volume VolGroup01Swap to 768.00 MiB
Logical volume VolGroup01Swap successfully resized



2.- Recreate swap filesystem in the new extended Swap-LV :

$ mkswap -f /dev/VolGroup01/VolGroup01Swap
Setting up swapspace version 1, size = 786428 KiB
no label, UUID=675d6278-72cd-457a-96ad-b18c81d25b3f


3.- Activate the new swap on extended Swap-LV :

$ swapon -a


4.- Verify the new swap :


$ free | grep Swap
Swap: 2523120 0 2523120

After the Swap-LV extension there are 2.5G of swap available

Questions

1.- Swap can only be setup in 'Linux-Swap' partitions (true/false)

2.- The command 'swapon -a' activates all swap partition configured on /etc/fstab (true/false)

3.- Active Swap-LV must be deactivated first in order to resize it (true/false)

4.- Which command creates an swap filesystem in /dev/sdb1 ?

5.- Which of the following commands can be used in order to extend in 300M the LV /dev/VolGroup/VolGroupRoot ?
A - free
B - cat /proc/meminfo
C - Both of them
D - None of them

Labs

1.- Create 256M Swap partition and add it to the system. Make sure it will be enabled at boot.

2.- Create 512 Swap-LV partition and add it to the system. Make sure it will be enabled at boot.

-- This page is part of Linux Server online tutorial --