Saturday, December 22, 2018

Increasing Swap Size on Ubuntu

Some of the blogs on increasing swap size on Ubuntu provide incorrect information. For example, some blogs suggest using fallocate which will not work for certain file systems. For the details on which file systems fallocate does and does not work on, refer to the swapon/swapoff man page. We avoid this complexity by using dd.

There are many potential scenarios. In this particular case, swap is initially configured to 2 GB and we want to increase it to 16 GB.


All the commands below were executed on Ubuntu 18.04 LTS


  1. Check Ubuntu version
    1. Command: lsb_release -a
      1. Output
        No LSB modules are available.
        Distributor ID: Ubuntu
        Description:    Ubuntu 18.04.1 LTS
        Release:        18.04
        Codename:       bionic
        
      2. Check swap configuration
        1. Command: sudo swapon --show --bytes
          1. Output
            NAME      TYPE       SIZE USED PRIO
            /swapfile file 2147479552    0   -2
            
            
            1. Comment
              1. The size of 2,147,479,552 bytes is close to but does not exactly correspond to 2 GB (2,147,483,648). The option "--bytes" was used to illustrate that there will be differences and that should not be surprised by this. The discrepancy is so small that you should not worry about it. An explanation for the discrepancy is beyond the scope of this blog.
            2. Examine the metadata of the swap file
              1. Command: ls -lh /swapfile
                1. Output
                  -rw------- 1 root root 2.0G Oct 18 18:55 /swapfile
                  
                  1. Comments
                    1. Root should be the only user that can read/write to the swapfile. The above output confirms that this is happening.
                      1. Notice that the size of the swapfile is displayed as "2.0G".
                    2. Disable swap file
                      1. Command: sudo swapoff /swapfile
                        1. Output: None
                        2. Check swap configuration
                          1. Command: sudo swapon --show --bytes
                            1. Output: None
                            2. Add 14 GB to the existing swap file
                              1. Command: sudo dd if=/dev/zero of=/swapfile bs=1M count=14336 oflag=append conv=nocreat,notrunc,fsync status=progress
                              2. Output
                                14579400704 bytes (15 GB, 14 GiB) copied, 5 s, 2.9 GB/s 
                                14336+0 records in
                                14336+0 records out
                                15032385536 bytes (15 GB, 14 GiB) copied, 8.94244 s, 1.7 GB/s
                                
                                1. Comments
                                  1. Since the number of input bytes is 1M, need the count to be 14,336 (14 * 1,024).
                                    1. Used fsync to make sure that physically wrote output file as well as its associated metadata before finishing.
                                  2. Examine the metadata of the swap file
                                    1. Command: ls -lh /swapfile
                                      1. Output
                                        -rw------- 1 root root 16G Dec 22 15:44 /swapfile
                                        
                                        1. Comment
                                          1. Confirmed that the final swapfile size is 16 GB.
                                        2. Make swapfile usable
                                          1. Command: sudo mkswap --check /swapfile
                                            1. Output
                                              mkswap: warning: checking bad blocks from swap file is not supported: /swapfile
                                              mkswap: /swapfile: warning: wiping old swap signature.
                                              Setting up swapspace version 1, size = 16 GiB (17179865088 bytes)
                                              no label, UUID=9ec7f376-65ab-44f0-b40b-31b8e725f3b6
                                              
                                              1. Comments
                                                1. Don't worry about the warning "checking bad blocks from swap file is not supported". This is because the check can only be performed on block devices. In this particular case, the swapfile is not a block device.
                                                  1. The size of 17,179,865,088 bytes is close to but does not exactly corresponds to 16 GB (17,179,869,184). As stated previously, the discrepancy is so small that you should not worry about it.
                                                2. Enable swaping
                                                  1. Command: sudo swapon --verbose /swapfile
                                                    1. Output
                                                      swapon: /swapfile: found signature [pagesize=4096, signature=swap]
                                                      swapon: /swapfile: pagesize=4096, swapsize=17179869184, devsize=17179869184
                                                      
                                                      1. Comment
                                                        1. The size of 17,179,869,184 bytes corresponds exactly to to 16 GB.
                                                      2. Check swap configuration
                                                        1. Command: sudo swapon --show --bytes
                                                          1. Output
                                                            NAME      TYPE        SIZE USED PRIO
                                                            /swapfile file 17179865088    0   -2
                                                        2. Reboot
                                                          1. Check swap configuration
                                                            1. Command: sudo swapon --show --bytes
                                                              1. Output
                                                                NAME      TYPE        SIZE USED PRIO
                                                                /swapfile file 17179865088    0   -2
                                                                
                                                                1. Comment
                                                                  1. The reboot verified that fstab is properly configured for the swapfile.

                                                              References


                                                              2 comments:

                                                              1. Todd Kaufmann via the Code & Supply #chat slack channel stated the following

                                                                here's a script I've used to quickly add some swap space ... mostly for vagrant VMs that usually don't need any, but might for a specific task.

                                                                You can run multiple times to add 2G increments (or, edit the size and/or device)

                                                                https://github.com/toddkaufmann/bin-scripts/blob/master/add-swap.sh"

                                                                ReplyDelete
                                                              2. Via a private exchange, I recevied the following

                                                                My thoughts on this are:

                                                                1) with a new file, I set the permissions, so I know they are correct

                                                                2) there is only a calculation on the total size I want to create, I don't have to take into account the size of the current swap file

                                                                3) If this is on a spinning disk, I've now moved where the swap file is located, so you may end up evening out the wear on the drive. Solid state drives wear level, so that does not matter with that storage technology.

                                                                Either way, it's a very minor quibble, much in the same vain of what text editor to use

                                                                ReplyDelete