Increasing swap space on Oracle Linux
June 19, 2012 12 Comments
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