Vagrant Dev Environment

Vagrant 101: Basic Virtual Machine Customizations

Vagrant is a powerful too, and it’s power comes from the modifiable, programmable configuration file. The configuration file allows us to define how the guest virtual machine that will be launched by vagrant should look like. If you haven’t already read my very simple Vagrant 101: Getting Started guide, please read it now.

For our examples we will focus on Virtualbox, as I also use it for my day-to-day tasks and it is easily available for all platforms. There are many ways you can customize virtual machine, but imagine keeping your virtual machine customization with your project under version control. Can you see the power it will give you over your development? Can you imagine the possibilities?

Enough talk, let’s see how we can actually do it in Vagrant. Vagrant stores all of the vagrant configuration for a project in Vagrantfile which we already added to our version control. Let’s go it and modify different aspects of our Virtual machine. All the virtual machine customization is done under config.vm.provider section. As we are using virtual box our provider should be virtualbox, so our section will look like following:

config.vm.provider "virtualbox" do |vb|
    # VM Customizations go here
end

Search for the line config.vm.provider “virtualbox” and add the following customization options.

Modify CPU Configuration

You can modify number of CPUs available to your virtual machine by specifying the the number in this section. For example if you want to assign 2 CPUs to your VM, you can use the following example customization, or else specify your own number instead of 2.

vb.customize ["modifyvm", :id, "--cpus", "2"]

Modify Memory Configuration

Just like you modified your CPU configuration, you can also modify your virtual machine memory configuration, for example if your want to assign your virtual machine 2 GB memory, you can use the following customization option. Remember have to provide the memory size in MBs.

vb.customize ["modifyvm", :id, "--memory", "2048"]

Disable USB 2.0

As I mostly use Virtualbox to do development and don’t need USB access, I can disable it safely, this might be true for everyone at least in 90% of the cases. Disabling USB support on your guest virtual machine is very simple and it requires the following 2 customization options.

vb.customize ["modifyvm", :id, "--usb", "off"]
vb.customize ["modifyvm", :id, "--usbehci", "off"]

How to apply the changes?

Find the status of your virtual machine using status command.

vagrant status

If your virtual machine is already running, use the reload command.

vagrant reload

If your virtual machine is not running, just use the up command.

vagrant up

What else can be customized?

By now you might be wondering what aspects of your virtual machine you can customize. Almost all of them, to see the list of options, use the following command (tested on linux)

vboxmanage modifyvm

The above command will show you all the options available, you just need to use the option in Vagrantfile in the same way we have modified the CPU and memory above and provide the correct values depending on the feature you are customizing.

Complete section.

Just for your verification and to clear any confusion you might have, your config.vm.provider section should look like following:

customize.vm.provider "virtualbox" do |vb|
  # Assign 2 CPUs
  vb.customize ["modifyvm", :id, "--cpus", "2"]
 
  # Assign 2 GB Memory
  vb.customize ["modifyvm", :id, "--memory", "2048"]
 
  # Disable USB
  vb.customize ["modifyvm", :id, "--usb", "off"]
  vb.customize ["modifyvm", :id, "--usbehci", "off"]
end

What’s next?

In the next few articles I will write about shared folders, networking, sharing and provisioning which are other powerful options vagrant provides you. Let me know in comments if you have any other questions regarding these basic VM customziations?

Published by

Hameedullah Khan

VR enthusiast, Cloud Architect, Passionate Programmer and Opensource advocate.

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge