Skip to content

Latest commit

 

History

History
130 lines (105 loc) · 4.8 KB

SWAP.md

File metadata and controls

130 lines (105 loc) · 4.8 KB

Add swap space to your cloud server

The cloud servers used in this course do not have enough memory (RAM) to run/compile many things at once. But you can easily add swap space to solve this issue.

Swap space in Linux is used when there is no more available physical memory (RAM). If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space (on disk).

Adding 2 gigabytes of swap space should be enough for our purposes.

Run the following commands to make sure you disable any previous swap file you might have created during the exercises:

# (It's okay if this command produces an error.)
$> sudo swapoff /swapfile
$> sudo rm -f /swapfile

Use the following commands to create and mount a 2-gigabyte swap file:

$> sudo fallocate -l 2G /swapfile
$> sudo chmod 600 /swapfile
$> sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=3c263053-41cc-4757-0000-13de0644cf97
$> sudo swapon /swapfile

You can verify that the swap space is correctly mounted by displaying available memory with the free -h command. You should see the Swap line indicating the amount of swap space you have added:

$> free -h
              total        used        free      shared  buff/cache   available
Mem:          914Mi       404Mi       316Mi        31Mi       193Mi       331Mi
Swap:         2.0Gi       200Mi       1.8Gi

This swap space is temporary by default and will only last until your reboot your server. To make it permanent, you must tell your server to mount it on boot.

You can see the currently configured mounts with this command (the output may not be exactly the same):

$> cat /etc/fstab
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
UUID=b1983cef-43a3-46ac-0000-b5e06a61c9fd       /        ext4   defaults,discard        0 1
UUID=0BC7-0000  /boot/efi       vfat    umask=0077      0 1
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig       0       2

⚠️ ⚠️ ⚠️ WARNING: BE VERY CAREFUL TO EXECUTE THE FOLLOWING COMMAND EXACTLY AS IS. (Corrupting your /etc/fstab file can prevent your server from rebooting.) ⚠️ ⚠️ ⚠️

To make the swap space permanent, execute the following command to add the appropriate line to your server's /etc/fstab file:

$> echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

This line tells your server to mount the swap file you have created as swap space on boot. You should see the new line at the end of the /etc/fstab file if you display its contents again:

$> cat /etc/fstab
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
UUID=b1983cef-43a3-46ac-0000-b5e06a61c9fd       /        ext4   defaults,discard        0 1
UUID=0BC7-08EF  /boot/efi       vfat    umask=0077      0 1
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig       0       2
/swapfile none swap sw 0 0

You can run the following command to check that you did not make any mistakes. It's okay if you have a couple of warnings about the swap file. These are expected since you've just added it and have not rebooted yet.

$> sudo findmnt --verify --verbose
/
   [ ] target exists
   [ ] FS options: discard,commit=30,errors=remount-ro
   [ ] UUID=bf171e20-4158-4861-0000-1443ece8c413 translated to /dev/sda1
   [ ] source /dev/sda1 exists
   [ ] FS type is ext4
...
none
   [W] non-bind mount source /swapfile is a directory or regular file
   [ ] FS type is swap
   [W] your fstab has been modified, but systemd still uses the old version;
       use 'systemctl daemon-reload' to reload

0 parse errors, 0 errors, 2 warnings

IF everything looks ok, reboot your server:

$> sudo reboot

Reconnect to your server over SSH and run the free -h command again. The swap space should still be enabled after reboot:

$> free -h
              total        used        free      shared  buff/cache   available
Mem:          914Mi       404Mi       316Mi        31Mi       193Mi       331Mi
Swap:         2.0Gi       200Mi       1.8Gi

You can also see the currently available swap space and how much is used with the htop command which shows it as the Swp bar at the top (you can quit it with q once it is open).

For more information, see the fstab Linux man page and How to Add Swap Space on Ubuntu 20.04.