Increasing swap space on Oracle Linux

Environment: Oracle Linux 6.2 64-bit, Oracle VM VirtualBox 4.1

Whenever you increase the physical memory of a Linux server, you probably also need to increase the swap space. There are several ways you can do this. In most cases, the swap space is present as a separate swap partition, which can be a stand-alone partition or part of a logical volume inside of a volume group. The swap space can also be present as a swap file, or even a combination of swap partitions and swap files with priorities.

1) Investigating the system

You can use the commands swapon and free to display information about your currently configured swap space. You can also have a look at the system files /etc/fstab and /proc/swap.

Let’s have a look at my system:

$ swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                               partition	3571704	59144	-1

$ cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/dm-1                               partition	3571704	59144	-1

$ cat /etc/fstab|grep swap
/dev/mapper/vg_ol6ora11g02-lv_swap swap                    swap    defaults        0 0

So, in my case, I have one swap partition of 3,5GB which is part of a logical volume (lv_swap) inside a logical volume group.

2) First option: adding a swap partition

To create a new swap area on a disk partition, you need the command mkswap. After the swap area is created, you have to activate it using the command swapon. Finally, to enable the swap area after a server reboot, you have to add it to the system file /etc/fstab.

In my example, I first added a 2GB virtual disk to my VirtualBox installation. This new disk was visible under Oracle Linux as disk device /dev/sdb. I used this disk device to create my new swap area on. Note: please make sure you use the correct disk device!

$ mkswap -c /dev/sdb
mkswap: /dev/sdb: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=8e6f61c3-1070-450f-b89a-3af8d646e985

$ swapon /dev/sdb
$ swapon -s

Filename				Type		Size	Used	Priority
/dev/dm-1                               partition	3571704	133296	-1
/dev/sdb                                partition	2097144	0	-2

$ cat /etc/fstab|grep /dev/sdb

/dev/sdb                swap                    swap    defaults        0 0

3) Second option: adding a swap file

When you have no free disk or disk partitions available, it’s also possible to increase your swap area by creating a swap file. First, you need to create an empty file with the required size using the dd command. Then you need to activate the swap file using mkswap and finally add it to the /etc/fstab file (same as with swap partitions).

Let’s check my example where I add a 2GB swap file under /root:

$ dd if=/dev/zero of=/root/swapfile count=1024 bs=2097152

1024+0 records in
1024+0 records out
2147483648 bytes (2.1 GB) copied, 245.797 s, 8.7 MB/s

$ mkswap -c /root/swapfile

mkswap: /root/swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=99076d49-9893-425f-b0b1-1f21c7e9f8be
$ swapon /root/swapfile
$ swapon -s

Filename				Type		Size	Used	Priority
/dev/dm-1                               partition	3571704	298532	-1
/root/swapfile                          file		2097144	0	-2

$ cat /etc/fstab|grep swapfile

/root/swapfile 		swap                    swap    defaults        0 0

4) Third option: increasing the size of a logical volume

If your swap area is on a logical volume, you can increase it by extending the logical volume using the command lvextend. However, things are a bit more complicated. First, if you don’t have any free extents available, you need to add a physical volume to the volume group where the logical volume is located in. Second, to increase the size of the swap area, you need to temporarily disable it first (swapoff) followed by the recreation of the swap area using mkswap.

In my example, I first add the device /dev/sdb to my volume group, next I increase the logical volume to 4.5GB using lvextend, and then I disable and recreate the swap area:

lvm> vgextend vg_ol6ora11g02 /dev/sdb
  No physical volume label read from /dev/sdb
WARNING: swap signature detected on /dev/sdb. Wipe it? [y/n] y
  Wiping swap signature on /dev/sdb.
  Writing physical volume data to disk "/dev/sdb"
  Physical volume "/dev/sdb" successfully created
  Volume group "vg_ol6ora11g02" successfully extended
lvm> vgs
  VG             #PV #LV #SN Attr   VSize  VFree
  vg_ol6ora11g02   2   2   0 wz--n- 21.50g 2.00g

$ swapoff /dev/mapper/vg_ol6ora11g02-lv_swap
$ lvextend -L 4.5G /dev/mapper/vg_ol6ora11g02-lv_swap
  Extending logical volume lv_swap to 4.50 GiB
  Logical volume lv_swap successfully resized

$ mkswap -c /dev/mapper/vg_ol6ora11g02-lv_swap
mkswap: /dev/mapper/vg_ol6ora11g02-lv_swap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4718588 KiB
no label, UUID=04fa98f4-c72e-4bb6-94b4-57f9384f779f

$ swapon /dev/mapper/vg_ol6ora11g02-lv_swap
$ swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                               partition	4718584	0	-1

HTH,

Matthias

13 thoughts on “Increasing swap space on Oracle Linux

  1. Not sure if it makes a difference. but in your dd command for making a swapfile, you specify a blocksize of 2MB and then specify a count of 1024 to create a 2GB swap file, on other postings i found, they specify a block size as small and a large count e.g. http://www.askwebhosting.com/article/206/Add_swap_file_under_linux.html specifies for 1GB to create a block size of 1024 and a count of 1048576 – just wondered about the performance depending on which way round this is set as the total size will be the same which ever way it is set.

    • Good question! I haven’t done any performance testing with this, but I would think that the underlying disk subsystem (stripe size and stuff) could play a role in this. Maybe something to test if I find the time!

  2. Pingback: Installing Oracle 12cR1 Client on OEL 6 | Business Analytics & Data Management

  3. Pingback: Managing Swap Space on a Linux Machine | I just don't remember stuff...

  4. Great blog, but unfortunately this doesn’t seem to work on Oracle Linux 6.5.

    >>[root@oelwls ~]# swapon /root/swapfile
    >>swapon: /root/swapfile: swapon failed: Invalid argument

    It seems this has to do with the fact that I have a btrfs file system that doesn’t support this :(

  5. Pingback: Increasing swap space on Oracle Linux | Mike Desouza's Blog

  6. Pingback: Increasing swap space on Oracle Linux - I.T. HOW TOO

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.