Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
vms:debian [2019/01/17 05:00]
admin [Login using an rsa key]
vms:debian [2019/01/17 05:44]
admin [Swap File]
Line 273: Line 273:
  
 As stated above, using a swap partition isn't always the best option when using a VM since it might become necessary to resize the swap space when the VM's memory configuration is changed based on its utilization. When a swap partition is used, it becomes necessary to "​play"​ with the partitions sizes, which means changing the partitions scheme and oftentimes even the virtual disk's size, which is tedious... As stated above, using a swap partition isn't always the best option when using a VM since it might become necessary to resize the swap space when the VM's memory configuration is changed based on its utilization. When a swap partition is used, it becomes necessary to "​play"​ with the partitions sizes, which means changing the partitions scheme and oftentimes even the virtual disk's size, which is tedious...
 +
 +Therefore, in the case of VMs, it is often much easier to create the swap space using a system file, you can learn more about this in [[https://​www.digitalocean.com/​community/​tutorials/​how-to-configure-virtual-memory-swap-file-on-a-vps|this Digital Ocean'​s article]]. Here are the steps to create and use a swap file.
 +
 +First to make sure swap is not already activated on the system use the ''​free''​ command:
 +<​Code:​bash>​
 +> free
 +              total        used        free      shared ​ buff/​cache ​  ​available
 +Mem:        2058304 ​      ​37680 ​    ​1759884 ​       2968      260740 ​    ​1872108
 +Swap:             ​0 ​          ​0 ​          0
 +</​Code>​
 +
 +The ''​free : 0''​ value on the ''​Swap:''​ line means swap is indeed **not activated**
 +
 +We'll create the partition file under ''/​var''​ and name it ''​swap.img'',​ then change its permissions to ''​600''​ so no user will be able to access it:
 +<​Code:​bash>​
 +> touch /​var/​swap.img
 +> chmod 600 /​var/​swap.img
 +</​Code>​
 +
 +=== Sizing ===
 +
 +Deciding on the swap size is really case dependent. In general, it is recommended recommend to set it to 1-2x the available system RAM. So, if you have a 512mb RAM VM, use 512mb-1gb swap. If you have a 1gb RAM VM use 1gb-2gb swap, etc. This is not a hard and fast rule, for example if you have a 4gb RAM VM it may be best to use little (512mb) or no swap at all.\\
 +We use the ''​dd''​ command to stretch our swap file size, filling it with zeroes to the size we need (here 2Gb):
 +<​Code:​bash>​
 +> dd if=/​dev/​zero of=/​var/​swap.img bs=1024k count=2048
 +2048+0 records in
 +2048+0 records out
 +2147483648 bytes (2.1 GB, 2.0 GiB) copied, 1.57258 s, 1.4 GB/s
 +</​Code>​
 +
 +Next, we prepare the file to be usable as the swap file:
 +<​Code:​bash>​
 +> mkswap /​var/​swap.img
 +Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
 +no label, UUID=f8d9bdfe-4090-4bc6-8f7a-fc74b64946ad
 +</​Code>​
 +
 +Then we turn on swapping:
 +<​Code:​bash>​
 +> swapon /​var/​swap.img
 +> free
 +              total        used        free      shared ​ buff/​cache ​  ​available
 +Mem:        2058304 ​      ​38784 ​      ​67972 ​       2968     ​1951548 ​    ​1847628
 +Swap:       ​2097148 ​          ​0 ​    ​2097148
 +</​Code>​
 +
 +We now see that the swap is active. We could turn off swapping with the ''​swapoff /​var/​swap.img''​ command.\\
 +Turning the swap on this way will **not activate swapping on the next boot**, so we need to modify ''/​etc/​fstab''​ to have the swap file activated at boot time:
 +
 +<​Code>​
 +> nano /etc/fstab
 +ADD THOSE LINES
 +# swap file
 +/​var/​swap.img ​   none    swap    sw    0    0
 +</​Code>​
 +
 +Finally, one could define the system'​s //​swappiness//,​ which tells the Linux kernel/VM handler how likely it should be to use VM. It is a percent value, between 0 & 100. A usual recommendation for VMs is 30:
 +<​Code:​bash>​
 +> sysctl -w vm.swappiness=30
 +vm.swappiness = 30
 +</​Code>​
 +
 +That's about it !\\
 +If you want to know what your current system'​s swappiness is, use ''​sysctl -a | grep swappiness''​.
 ===== Shell Customization & Utilities ===== ===== Shell Customization & Utilities =====