Advertisements

Adding swap space for my Ubuntu Server 15.10.3 image running on my Raspberry Pi 3

A swap space. also known as virtual memory, is a dedicated area on a writable medium that acts like the RAM for Linux processes to remember things while they are running. For laptop and desktop computers, that writable medium is usually a hard disk. For my Raspberry Pi 3, it is the microSD card which I had written my Ubuntu 15.10.3 image on.

While running the Let's Encrypt application to set up a free CA signed SSL certificate for my LEMP server on my Raspberry Pi 3 to secure connections to my WordPress site, the Let's Encrypt application hanged while it was trying to install the Python dependencies that it needed. This prompted me to turn to swap space for increasing the total memory that processes on my Ubuntu 15.10.3 image can utilize.

This post documents the steps that I took to add some swap space on my Ubuntu 15.10.3 image running on my Raspberry Pi 3 so that processes can use more than 1GB of memory.

Getting the file system of my Ubuntu 15.10.3 image to use the entire microSD card space that I am slotted into my Raspberry Pi 3

Since the Ubuntu Server Standard 15.10.3 image that I had downloaded from Ubuntu Pi Flavour Maker only allocated around 3.5 GB of space to my root directory, I was not able to create more than 1.5 GB of swap space.

Hence, before continuing the creation of swap space for my Ubuntu Server 15.10.3 image, I had to first resize the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space on my Raspberry Pi 3.

Checking whether there were any swap space created for my Ubuntu Server 15.10.3 image

To confirm that there was no swap space configured for my Ubuntu Server 15.10.3, I first ran the following command:

sudo swapon -s

which returned me nothing. This meant that there was no swap space configured for my Ubuntu Server 15.10.3 image; in another words, there was no additional virtual memory for processes to use if the 1GB ram is fully occupied.

Running the following command also gave me some useful statistic of the total amount of memory, RAM plus virtual memory, that was available for my Ubuntu Server 15.10.3 image to use:

free -m

which gave me the following output:

             total       used       free     shared    buffers     cached
Mem:           925        417        508         22         22        134
-/+ buffers/cache:        260        665
Swap:            0          0          0

From the table, I could see that there was no swap space configured on my Ubuntu Server 15.10.3 image.

Creating a swap file for my Ubuntu Server 15.10.3 image to use for swap space

The first step that I took to add swap space for my Ubuntu Server 15.10.3 was to create a physical file on my root directory that will be used for storing volatile data in the event that the 1GB ram is fully occupied. To do so, I ran the following command:

sudo fallocate -l 4G /swapfile

After the command completed, I verified the swap file creation with a run of the ls command:

ls -lh /swapfile

which gave me the following output:

-rw-r--r-- 1 root root 4.0G May  2 15:54 /swapfile

Which that, I was certain that my swap file was created successfully.

I also ran the chmod command to ensure that the swap file can only be read and written by the root user:

sudo chmod 600 /swapfile

Pointing my Ubuntu Server 15.10.3 image to use /swapfile as the swap space

Once I had my swap file ready, the next step would be to get my Ubuntu Server 15.10.3 image to use that swap file as virtual memory.

I first declare /swapfile to be used as swap space:

sudo mkswap /swapfile

which gave me the following output:

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=35c25aa6-d335-4a78-beb6-a00447aee877

With that, I could be sure that my Ubuntu Server 15.10.3 will treat /swapfile as virtual memory.

After declaring /swapfile as virtual memory, I continued on to enable it for use by my Ubuntu Server 15.10.3 when it ran out of memory space in the 1GB ram on my Raspberry Pi 3:

sudo swapon /swapfile

This command adhered to the Unix philosophy: silent means ok.

To verify that my Ubuntu Server 15.10.3 image is now able to use /swapfile as virtual memory, I ran the swapon command from earlier again:

sudo swapon -s

which returned me the following output:

Filename				Type		Size	Used	Priority
/swapfile                              	file    	4194300	0	-1

I also used the free command to see my swap space in action alongside the 1GB ram:

free -m

Doing so gave me the following output:

             total       used       free     shared    buffers     cached
Mem:           925        477        448         22         24        136
-/+ buffers/cache:        317        608
Swap:         4095          0       4095

In this output, I could see that there were around 4GB of available swap space for my Ubuntu Server 15.10.3 processes to utilize. No swap space was utilized as there were still enough memory on the 1GB RAM.

Persisting the swap space configurations

At this moment in time, the swap space that I had created was temporary: a restart of my Ubuntu Server would cause it to forgot all my virtual memory settings.

To ensure that the swap space that I had created will always be treated as virtual memory: I added the following line in /etc/fstab:

/swapfile   none    swap    sw    0   0

I then saved my changes and closed the file.

Setting the frequency of memory swapping

Memory swapping is the process where contents on the RAM is copied to the swap space. This process can cause slowness and this is not desirable for a web server. Hence, in order to tell my Ubuntu Server not to perform memory swapping so frequency, I opened up /etc/sysctl.conf and added the following line at the bottom of the file:

vm.swappiness=10

I then saved my changes and closed the file.

Setting the cache pressure controls the tendency of the kernel to reclaim
the memory which is used for caching of directory and inode objects

The cache pressure of my Ubuntu Server was set at a default value of 100, which had caused it to reclaim memory that is used for caching directory and inode objects too quickly. This is another factor that could cause slowness as a web server often access the same directory and file information frequently, especially when serving popular web pages.

Hence, I opened up /etc/sysctl.conf again and added the following line at the bottom of the file:

vm.vfs_cache_pressure = 50

I saved my changes and closed the file. This setting would make my Ubuntu Server retain caches of directory and inode objects for longer period of time, hence improving performance in serving web pages.

Restarting my Ubuntu Server 15.10.3 image for the virtual memory settings to take effect

Once, I was satisfied with my virtual memory settings, I ran the reboot command to restart my Ubuntu Server:

sudo reboot

After the reboot, my Let's Encrypt application was able to proceed beyond the installation of my Python dependencies.

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people.

Advertisements