Is it possible to create a swap file for a Linux guest VM managed by Vagrant?
In my Vagrantfile
I have the following provider defined:
Excerpt from Vagrantfile:
config.vm.provider :virtualbox do |vb| vb.gui = false vb.customize ["modifyvm", :id, "--memory", "4096"] end
When I runn free -m
within the created VM I see the following report:
total used free shared buffers cached
Mem: 3953 337 3615 0 18 119
-/+ buffers/cache: 199 3753
Swap: 0 0 0
Is it possible to assign swappiness when the VM is being built and provisioned?
I have tried setting vm.swappiness
in /etc/sysctl.conf
and restarting the VM, but I do not see any affect of adding swap space to the VM.
Why do I need swap space? I would rather not deal with swap space at all and run everything in memory. However, for some applications (the composer project for PHP, for example) are notorious for using a lot of memory under certain conditions. Without swap space I have to be very liberal with the amount of memory I throw at a VM, if I had swap space--although it might not be as efficient--I would theoretically run into fewer issues.
The memory setting you see is only used to configure the VM's RAM. However, the swap space definition is part of the disk image. This image is provided to you as part of the config.vm.box
definition in Vagrantfile
. In my specific case I noticed that the swap space cannot easily be reconfigured (I only have 1 GByte of swap).
In your case I recommend to change the base image (config.vm.box
), or add a swap-file to your root file system by integrating for example this script into your Vagrantfile
. This is another link which seems worth trying out.
Simply add this line to your vagrant file
Vagrantfile
# Enable Dynamic Swap Space to prevent Out of Memory crashes
config.vm.provision "shell", inline: "sudo apt install swapspace -y"
And then reprovision
vagrant up --provision
How it works
This is a dynamic swap space daemon. There are several, but here is a popular one.
sudo apt install swapspace
To verify it's running
sudo service swapspace status
Excerpt from http://pqxx.org/development/swapspace/
This system daemon for the Linux kernel aims to do away with the need for large, fixed swap partitions or swap files.
When installing a Linux-based system (invariably GNU/Linux) with Swapspace, the usual swap partition can be omitted, or it can be kept quite small. Whenever Swapspace finds during normal system usage that more virtual memory is needed, it will automatically claim space from the hard disk. Conversely, swap space that is no longer needed is freed up again for regular use by the filesystem.
This means that with Swapspace installed, sizing the system's available swap space during installation is no longer a life-or-death choice. It now becomes practical to run GNU/Linux off just a single, big partition--with no disk space lost to regrettable installation choices. The system should also be able to handle the occasional memory-intensive task that takes much more swap space than was originally foreseen, without leaving the same swap space unused and unusable during normal operation as is normally the case.