8

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.

4

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.

  • Thanks, I kind of figured that the base image had to have its swap modified after I looked further into it. As it turns out I can get around my issues by using different operations on the program that requires a lot of memory. It doesn't seem like there's another solution other than modifying the base image. – Sean Quinn Aug 2 '16 at 20:14
  • The linked script (first one) worked nicely for me. In case the link ever gets stale: create_swap.sh and remove_swap.sh as raw gists – panepeter Feb 11 '18 at 8:44
11

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.

0

If you are using Chef to provision your Vagrant box, the easiest solution is probably to use the swap_file resource. This resource is new in Chef 14.0.

For example, add the following to your recipe:

swap_file '/var/swap.1' do
  size 1024
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.