[How to] Increase instance RAM using a swap device


#1

Increase instance RAM using a swap device (Ubuntu/Debian)

Follow the steps below using “root”:

  1. Create a new L_SSD volume (preferably) and attach it to your instance
    Look @ https://doc.cloud.online.net/howto/create_volume.html

  2. Create a System V init-script with the following content:

    #!/bin/bash

    BEGIN INIT INFO

    Provides: mountswap

    Required-Start: $network $local_fs

    Required-Stop: $local_fs

    Default-Start: 2 3 4 5

    Default-Stop: 0 1 6

    X-Start-After: nbd-client

    Short-Description: Workaround to mount an NBD device as swap OS device. IMPORTANT: add ‘_netdev’ mount option to the swap device in /etc/fstab

    END INIT INFO

    . /lib/lsb/init-functions

    case “${1:-’’}” in
    ‘start’)
    log_daemon_msg “Activating swap devices”
    /sbin/swapon -a
    [ $? -eq 0 ] && log_end_msg 0 || log_end_msg 1
    ;;
    esac

  3. Add the appropriate entry into /etc/fstab

    /dev/nbd1 none swap defaults,_netdev,nobootwait 0 0

_netdev’ is the important mount options, so this device won’t be used by /etc/init.d/nbd-client script during startup (no bad errors will be generated).

  1. Add init-script to runlevels using the specific order (e.g. 80 is ok 'cause after ndb-client script):

    update-rc.d mountswap defaults 80

  2. Reboot and wait server restarts

  3. Verify memory of the instance:

    free -m

              total       used       free     shared    buffers     cached
    

    Mem: 2020 418 1602 5 49 217
    -/+ buffers/cache: 152 1868
    Swap: 3814 0 3814

  4. Verify that vm.swappiness kernel parameter is not ‘0’:

    sysctl vm.swappiness

    vm.swappiness = 60

You can tune this value according to your needs. You can check @ http://en.wikipedia.org/wiki/Swappiness

ROOT CAUSE
At the moment this is the best way to add a swap device. Online.net custom procedures are not fully compatible with NBD_TYPE=“s” (swap) so the file /etc/nbd-client is always re-generated at boot by OC scripts using NBD_TYPE[x]=f on all additional NBD volumes.


#2

–Adding another disk device is overkill unless you need another ~50GB of disk space. If your swap needs are minimal, creating a swapfile in the root directory suffices (do all as root):

cd /

time (dd if=/dev/zero of=SWAP1 bs=1M count=256;sync) # make a 256MB swapfile

mkswap SWAP1 # format the swap

chmod 600 SWAP1 # set file permissions

echo ‘/SWAP1 none swap sw,pri=2 0 0’ >> /etc/fstab

swapon -a;swapon -s


#3

Thanks !
Very usefull but with a 2 GB ram server, according to swap design rules to set up 1 to 2 times the ram size, I think a 2 GB size is a good value


#5

cd /
time (dd if=/dev/zero of=SWAP1 bs=1M count=2048;sync) # make a 2GB swapfile
mkswap SWAP1 # format the swap
chmod 600 SWAP1 # set file permissions
echo ‘/SWAP1 none swap sw,pri=2 0 0’ >> /etc/fstab
swapon -a;swapon -s


#6

Did anyone try to use zram module, aka (Compressed RAM based block devices)?
https://www.kernel.org/doc/Documentation/blockdev/zram.txt

I tried to enable it using 4.2.5 kernel, but it seems that it’s not present in Docker 1.7 image I am using