Sean's i/o Stream

Musings-as-a-Service

Enabling more than one cpu in OS X vagrant machine on VirtualBox

  • June 04, 2014
  •   category: virtualization
  • tags: vagrant, os-x, virtualization, ruby

Help if you find that your images are not getting the expected number of cpus. You have something like this in your Vagrant file:

   config.vm.provider :virtualbox do |vb|
     vb.customize ["modifyvm", :id, "--cpus"  , "2"   ]
     vb.customize ["modifyvm", :id, "--memory", "6144"] 
   end  
 end

But checking lscpu or cat /proc/cpuinfo show only one processor, what gives ?

Take a look at your dmesg output for something like this:

CPU: Unsupported number of siblings 2

Means you need IOAPIC enabled to pick up the other cores.

So you will end up with a vm provider section like this:

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--ioapic", "on"  ]
    vb.customize ["modifyvm", :id, "--cpus"  , "2"   ]
    vb.customize ["modifyvm", :id, "--memory", "6144"]
  end

As an added tip, found this Gist on github.com that I really like, for setting your Vagrant file to match the host system’s cpu/core count:

if not RUBY_PLATFORM.downcase.include?("mswin")
  config.vm.customize ["modifyvm", :id, "--cpus",
    `awk "/^processor/ {++n} END {print n}" /proc/cpuinfo 2> /dev/null || sh -c 'sysctl hw.logicalcpu 2> /dev/null || echo ": 2"' | awk \'{print \$2}\' `.chomp ]
end

Or another gem from a blog by Stefan Wrobel aptly named How to make Vagrant performance not suck, which shows since the Vagrant script is in fact Ruby you are not limited to just using Shell commands to figure out and manipulating the values if you are not an awk hero.

config.vm.provider "virtualbox" do |v|
  host = RbConfig::CONFIG['host_os']
  
  # Give VM 1/4 system memory & access to all cpu cores on the host
  if host =~ /darwin/
    cpus = `sysctl -n hw.ncpu`.to_i
    # sysctl returns Bytes and we need to convert to MB
    mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
  elsif host =~ /linux/
    cpus = `nproc`.to_i
    # meminfo shows KB and we need to convert to MB
    mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4
  else # sorry Windows folks, I can't help you
    cpus = 2
    mem = 1024
  end
  
  v.customize ["modifyvm", :id, "--memory", mem]
  v.customize ["modifyvm", :id, "--cpus", cpus]
end

Should save you a few minutes if you stumble across this one.

Reference:
Back to Top