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
- Check Ubuntu version
- Command: lsb_release -a
- Output
No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.1 LTS Release: 18.04 Codename: bionic
- Command: lsb_release -a
- Check swap configuration
- Command: sudo swapon --show --bytes
- Output
NAME TYPE SIZE USED PRIO /swapfile file 2147479552 0 -2
- Comment
- 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.
- 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.
- Command: sudo swapon --show --bytes
- Examine the metadata of the swap file
- Command: ls -lh /swapfile
- Output
-rw------- 1 root root 2.0G Oct 18 18:55 /swapfile
- Comments
- Root should be the only user that can read/write to the swapfile. The above output confirms that this is happening.
- Notice that the size of the swapfile is displayed as "2.0G".
- Root should be the only user that can read/write to the swapfile. The above output confirms that this is happening.
- Command: ls -lh /swapfile
- Disable swap file
- Command: sudo swapoff /swapfile
- Output: None
- Command: sudo swapoff /swapfile
- Check swap configuration
- Command: sudo swapon --show --bytes
- Output: None
- Command: sudo swapon --show --bytes
- Add 14 GB to the existing swap file
- Command: sudo dd if=/dev/zero of=/swapfile bs=1M count=14336 oflag=append conv=nocreat,notrunc,fsync status=progress
- 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
- Comments
- Since the number of input bytes is 1M, need the count to be 14,336 (14 * 1,024).
- Used fsync to make sure that physically wrote output file as well as its associated metadata before finishing.
- Since the number of input bytes is 1M, need the count to be 14,336 (14 * 1,024).
- Examine the metadata of the swap file
- Command: ls -lh /swapfile
- Output
-rw------- 1 root root 16G Dec 22 15:44 /swapfile
- Comment
- Confirmed that the final swapfile size is 16 GB.
- Confirmed that the final swapfile size is 16 GB.
- Command: ls -lh /swapfile
- Make swapfile usable
- Command: sudo mkswap --check /swapfile
- 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
- Comments
- 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.
- 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.
- 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.
- Command: sudo mkswap --check /swapfile
- Enable swaping
- Command: sudo swapon --verbose /swapfile
- Output
swapon: /swapfile: found signature [pagesize=4096, signature=swap] swapon: /swapfile: pagesize=4096, swapsize=17179869184, devsize=17179869184
- Comment
- The size of 17,179,869,184 bytes corresponds exactly to to 16 GB.
- The size of 17,179,869,184 bytes corresponds exactly to to 16 GB.
- Command: sudo swapon --verbose /swapfile
- Check swap configuration
- Command: sudo swapon --show --bytes
- Output
NAME TYPE SIZE USED PRIO /swapfile file 17179865088 0 -2
- Command: sudo swapon --show --bytes
- Reboot
- Check swap configuration
References
- Articles
- Unix Man Pages
Todd Kaufmann via the Code & Supply #chat slack channel stated the following
ReplyDeletehere'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"
Via a private exchange, I recevied the following
ReplyDeleteMy 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