Martin Wilkerson
Martin Wilkerson

Reputation: 336

Configure Vagrant with a variable number of physical screens

We have created a Vagrant based development environment that devs use to create their local work environment in a VM. This includes code editing and debugging tools with UI, and we want to work in the VM exclusively, with it using all our screens. However, our devs don't all have the same amount of screens, and I often work from alternate locations with just a single screen. Is it possible to get the VagrantFile to pick up the number of attached screens and create that many VM screens?

Our VagrantFile configuration (extract) we have at the moment for 2 screens:

config.vm.provider "virtualbox" do |vb| vb.gui = true vb.customize ["modifyvm", :id, "--vram", "128"] vb.customize ["modifyvm", :id, "--monitorcount", "2"] end

Ideally I'd like to replace the monitor count of 2 with either automatically picking up the number of screens or allowing the devs to pass an extra parameter to vagrant up to set the screen count.

Upvotes: 0

Views: 1058

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53773

This one is not simple, basically you would need to know how many monitors are currently connected, Vagrantfile is a ruby script but unfortunately ruby does not know about this (or least I dont know) and you need to get more at system level so its not easy to check for all systems.

on mac, you can find how many monitors you have by running the system_profiler SPDisplaysDataType command, for example

$ system_profiler SPDisplaysDataType
Graphics/Displays:

Intel HD Graphics 4000:

  Chipset Model: Intel HD Graphics 4000
  Type: GPU
  Bus: Built-In
  VRAM (Dynamic, Max): 1536 MB
  Vendor: Intel (0x8086)
  Device ID: 0x0166
  Revision ID: 0x0009
  Automatic Graphics Switching: Supported
  gMux Version: 3.2.19 [3.2.8]
  Metal: Supported

NVIDIA GeForce GT 650M:

  Chipset Model: NVIDIA GeForce GT 650M
  Type: GPU
  Bus: PCIe
  PCIe Lane Width: x8
  VRAM (Total): 1024 MB
  Vendor: NVIDIA (0x10de)
  Device ID: 0x0fd5
  Revision ID: 0x00a2
  ROM Revision: 3688
  Automatic Graphics Switching: Supported
  gMux Version: 3.2.19 [3.2.8]
  Metal: Supported
  Displays:
    Color LCD:
      Display Type: Retina LCD
      Resolution: 2880 x 1800 Retina
      Retina: Yes
      Pixel Depth: 32-Bit Color (ARGB8888)
      Main Display: Yes
      Mirror: Off
      Online: Yes
      Automatically Adjust Brightness: Yes
      Built-In: Yes
    PA279:
      Resolution: 1920 x 1080 @ 60Hz (1080p)
      Pixel Depth: 32-Bit Color (ARGB8888)
      Display Serial Number: E2LMQS044803
      Mirror: Off
      Online: Yes
      Rotation: Supported
      Automatically Adjust Brightness: No
      Connection Type: DisplayPort
      Television: Yes

so to count the number of monitors you can check how many resolution you have :

$ system_profiler SPDisplaysDataType | grep Resolution | wc -l
   2

this will work so you can put that in your Vagrantfile :

monitor = 1
host = RbConfig::CONFIG['host_os']
if host =~ /darwin/
  monitor = `system_profiler SPDisplaysDataType | grep Resolution | wc -l`.to_i
#elseif host =~ /linux/
#maybe there's a command for linux
#elseif host =~ /mswin|mingw|cygwin/
#maybe there's a command for windows
end

config.vm.provider "virtualbox" do |vb|
  vb.gui = true
  vb.customize ["modifyvm", :id, "--vram", "128"]
  vb.customize ["modifyvm", :id, "--monitorcount", "#{monitor}"]
end

I am pretty sure there are equivalent command for linux world, probably for windows.

Upvotes: 1

Related Questions