Howto add swap space without repartitiong disk

2 minute read

To add swap space online without repartitioning, you can create a swap file instead of resizing the swap partition. Here are the steps to follow:

  1. Open a terminal or shell on your system.

  2. Create a swap file of the desired size. In this example, we will create a 1GB swap file named “/tmp/swap_file” filled with zeros using the dd command:

    dd if=/dev/zero of=/tmp/swap_file bs=1M count=1000
    
  3. “Format” the swap file using the mkswap command:

    mkswap /tmp/swap_file
    
  4. Activate the swap file using the swapon command:

    swapon /tmp/swap_file
    
  5. You can use the free or top command to verify that the available swap space has indeed increased.

Instead of using the dd command to create a swap file, you can also use the fallocate command, which can be faster and more efficient. Here’s an example using fallocate to create a 1GB swap file:

fallocate -l 1G /tmp/swap_file

This command creates a 1GB swap file named “/tmp/swap_file” using the -l option to specify the file size.

After creating the swap file, you can proceed with formatting and activating it as mentioned before:

mkswap /tmp/swap_file
swapon /tmp/swap_file

To check the activated swap space on your system and include example outputs, you can follow these steps:

  1. Open a terminal or shell on your system.

  2. To display the currently activated swap devices/files and their details, use the swapon -s command:

    swapon -s
    

    Example output:

    Filename                Type        Size    Used    Priority
    /dev/sda2               partition   2047996 0       -2
    /tmp/swap_file          file        1048572 0       -3
    

    In this example output, you can see two activated swap spaces: one is a partition /dev/sda2 with a size of 2047996 kilobytes, and the other is a swap file /tmp/swap_file with a size of 1048572 kilobytes.

  3. To display information about both physical memory (RAM) and swap space, you can use the free -h command:

    free -h
    

    Example output:

                 total        used        free      shared  buff/cache   available
    Mem:           7.7G        3.4G        1.1G        346M        3.2G        3.8G
    Swap:          2.0G          0B        2.0G
    

    In this example output, you can see that the total swap space is 2.0GB, and currently, none of it is being used (0B used). The available swap space is also 2.0GB.

These commands allow you to check the activated swap space and provide you with information about its size, usage, and availability. The example outputs illustrate how the information may appear on your system.

Remember to adjust the file path and name according to your preference or system requirements.

Keep in mind that this method is temporary, and the swap file will not persist after a reboot. If you want to make the swap file persistent, you will need to add the swapon command to an appropriate startup script or configuration file. The specific location may vary depending on your Linux distribution.

For example, on Gentoo Linux, you can add the following line to the /etc/conf.d/local.start file:

swapon /tmp/swap_file

This will ensure that the swap file is activated automatically during system startup.

By following these steps, you can add swap space online without the need to repartition your system.