Custom Swap on Linux Virtual Machines


Codero creates a 1 GB swap partition for new virtual machines by default. For most users, this is plenty of swap space. However, different users may have differing needs. If your needs require a different amount of swap space, this guide shows you how to set up a custom sized swap space.

First Steps

Codero configures your virtual machine with one small (512 MB) physical /boot partition and two LVM partitions. One LVM partition is your 1 GB swap partition and the other LVM partition is the / (filesystem root). You can see this layout by running lvdisplay:

root@localhost:~# lvdisplay
--- Logical volume ---
LV Path /dev/debian/swap
LV Name swap
VG Name debian
LV UUID 42A03A37-A563-4C4A-A921-25E2230E7D60
LV Write Access read/write
LV Creation host, time ,
LV Status available
# open 2
LV Size 976.00 MiB
Current LE 244
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1
--- Logical volume ---
LV Path /dev/debian/root
LV Name root
VG Name debian
LV UUID AF641D98-2E17-40E9-AFBD-876657EC636E
LV Write Access read/write
LV Creation host, time ,
LV Status available
# open 1
LV Size 76.69 GiB
Current LE 19632
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0

In this example, we have our swap on the LVM partition /dev/debian/swap. The path varies by distribution name but follows this pattern: /dev/distribution_name/swap

Next, decide where you want to keep your new swap space. Codero recommends /var/swap:

root@localhost:~# mkdir -p /var/swap

Now we need to create our new swap space. A good rule of thumb for swap space is main memory size plus 1 gigabyte. In this example, we are on a two gigabyte VM, so we will make our swap space three gigabytes in size. We'll use the fallocate command to do this:

root@localhost:~# fallocate -l 3G /var/swap/swapfile

Now, let's make the swap space only accessible by the root user:

root@localhost:~# chmod 600 /var/swap/swapfile

Before enabling your new swap, use free to see your existing swap:

root@localhost:~# free -h
total used free shared buffers cached
Mem: 2.0G 712M 1.3G 5.4M 36M 564M
-/+ buffers/cache: 111M 1.8G
Swap: 975M 0B 975M

Enabling the new swap space

We need to format our new swap space for use. We'll use the mkswap command to do this:

root@localhost:~# mkswap -L swap /var/swap/swapfile

Finally, we can enable the swap space. The swapon command does exactly that for us:

root@localhost:~# swapon /var/swap/swapfile

And if we run free again, we'll see our new swap is enabled:

root@localhost:~# free -h
total used free shared buffers cached
Mem: 2.0G 715M 1.3G 5.4M 37M 565M
-/+ buffers/cache: 112M 1.8G
Swap: 4.0G 0B 4.0G

Wait, weren't we going for three gigabytes? Yes, but we still have our original one gigabyte as configured by Codero. Let's git rid of that now.

Removing the old swap space

We need to edit our /etc/fstab file to enable the new swap by default and not to use the original swap. Use whatever text editor you are comfortable with to edit the /etc/fstab. You should see something like this:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/debian-root / ext4 errors=remount-ro 0 1
UUID=91765859-5B0E-46F5-ACE8-06CFEB04EFFD /boot ext3 defaults 0 2
/dev/mapper/debian-swap none swap sw 0 0

 

Obviously, we need to change the line with a filesystem of type swap. Change your file to look something like this (keep in mind the details will be different on your instance):

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/debian-root / ext4 errors=remount-ro 0 1
UUID=91765859-5B0E-46F5-ACE8-06CFEB04EFFD /boot ext3 defaults 0 2
/var/swap/swapfile none swap sw 0 0

Final steps

If you are using a Red Hat derived system (CentOS, Fedora, or Oracle), you have one more thing to do. If you are using a Debian or Ubuntu system, you don't need to do this. The kernel configuration will have an entry for the Codero default swap partition. We need to get rid of this before continuing. We need to edit the /etc/default/grub file to do this. It will look something like this:

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

 

Remove the part pointing to the swap in the GRUB_CMDLINE_LINUX. Your /etc/default/grub should look something like this when you're done:

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root crashkernel=auto rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

 

Now, run the grub2-mkconfig command (if you are not running a current release, you will use the grub-mkconfig command instead):

[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

Now we're back to instructions for all Linux systems. You can reboot your instance to see the new swap configuration.

root@localhost:~# free -h
total used free shared buffers cached
Mem: 2.0G 121M 1.8G 5.3M 9M 55M
-/+ buffers/cache: 55M 1.9G
Swap: 3.0G 0B 3.0G

 

One more thing!

We want to recover the one gigabyte used by the Codero default swap space. First, we need to delete the old swap space. We will use lvremove to do this:

root@localhost:~# lvremove /dev/debian/swap
Do you really want to remove active logical volume swap? [y/n]: y
Logical volume "swap" successfully removed

 

So, how much free space do we have? Use pvs to find out:

root@localhost:~# pvs
PV VG Fmt Attr PSize PFree
/dev/xvda2 debian lvm2 a-- 77.64g 976.00m

 

To make sure you're regaining the old swap space, run df´ to see how much free space you have before reclaiming the space:

root@localhost:~# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/dm-0 81G 4.6G 73G 6% /
udev 11M 0 11M 0% /dev
tmpfs 421M 5.6M 415M 2% /run
tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/xvda1 487M 35M 427M 8% /boot

 

Now, let's reclaim the space using lvresize (keep in mind the partition name to delete may be different on your virtual machine):

root@localhost:~# lvresize -l +100%FREE -r /dev/debian/root
Size of logical volume debian/root changed from 76.69 GiB (19632 extents) to 77.64 GiB (19876 extents).
Logical volume root successfully resized
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/mapper/debian-root is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 5
The filesystem on /dev/mapper/debian-root is now 20353024 (4k) blocks long.

 

Run df once more to see the new space:

root@localhost:~# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/dm-0 82G 4.6G 74G 6% /
udev 11M 0 11M 0% /dev
tmpfs 421M 5.6M 415M 2% /run
tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/xvda1 487M 35M 427M 8% /boot

 

Conclusion

You have now created a custom sized swap space for your virtual machine. Congratulations!

Posted in
Last update:
2016-02-29 10:36
Average rating:0 (0 Votes)