Setting up Additional Swap Space for Oracle on Openstack VMs

One of the Openstack flavors I administer is a “database-sized” flavor suitable for running an Oracle RDBM installation. It carries 16GB of RAM with it, but the base OS image only has 2GB of swap space. Oracle recommends 16GB swap for 16GB of RAM. If you’re reading this, you probably already know that, so on with how to create the new swapfile area…

You’ll need ephemeral disk as part of your VM instance. If you don’t have it, I’m not sure if this will work because I’ve not tried it, but it seems unlikely. As a side note, you won’t likely have good success trying to use NFS-layer storage as a physical device; that’s not what it’s designed for and, if you do somehow make it work, the context switching involved with 4kB pagesizes might be a nightmare on your VM host’s NAT gateway. Ymmv; I don’t recommend it.

1. Install LVM if you don’t already have it.
sudo apt-get install lvm2

2. Create a physical volume from the ephemeral disk
In the instances I typically run, the root HDD is /dev/vda and the ephemeral disk is /dev/vdb. We’ll make a physical volume of /dev/vdb.

sudo pvcreate /dev/vdb
If you want to limit the volume size, use the –setphysicalVolumeSize flag. There are other options via the –help flag if desired. You can check your work after the fact by using pvscan.

3. Create a volume group using LVM.
LVM is a powerful shell for managing logical devices. If you’ve never used it, enter the LVM shell with the lvm command and look at the help options. To create a volume group, this simple syntax does the trick:
vgcreate vg0 /dev/vdb

This creates a volume group named vg0 from physical device /dev/vdb. You can apply other options here if desired; if you’re unsure on executing the command, you can always use the --test flag the first time through to see what would happen.

4. Create a swap volume in the volume group.
Using the volume group that was just created — in this case vg0 — we now need to create a swap volume that brings our total swap space up to 16GB. In my example, I already had 2GB of swap space, so I need a 14GB volume. This command creates a volume named swapvol from /dev/vdb inside volume group vg0
lvm> lvcreate --size 14G --name swapvol vg0 /dev/vdb

5. Format the swap volume as swap.
Now that there’s a logical container dedicated for usage as swap space, it needs to be formatted as such.
The –check flag in this command will scan for bad blocks before creating the space. You can omit it if desired but I generally recommend using it.
mkswap --check /dev/vg0/swapvol

The result of this command should also add an entry into /etc/fstab for the new swap space. You should verify this by looking for the entry. cat /etc/fstab

If there’s not an entry, you need to add one before proceeding to the next step. I’m assuming that you know how to do that; google is your friend if not, sorry.

6. Turn the swap space on.
swapon is the general command for managing page/swap files. Its counterpart is swapoff. In the command below, the -v flag provides verbose output; the -a flag marks all swap devices in /etc/fstab as available (no effect on currently-available devices). The -a flag won’t work if, for some reason, your /etc/fstab entry includes the noauto property in the mount options.
swapon -va

Verify the amount of swap space available has risen to 16GB and you are home free:
free -g