使用可变数量的物理屏幕配置Vagrant

时间:2016-12-08 14:59:12

标签: vagrant

我们创建了一个基于Vagrant的开发环境,用于在VM中创建其本地工作环境。这包括带有UI的代码编辑和调试工具,我们希望使用所有屏幕专门在VM中工作。但是,我们的开发人员并不都拥有相同数量的屏幕,而且我经常只使用一个屏幕在其他位置工作。是否有可能让VagrantFile获取附加屏幕的数量并创建那么多VM屏幕?

我们的VagrantFile配置(摘录)目前有2个屏幕:

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

理想情况下,我想将监视器数量2替换为自动获取屏幕数量或允许开发人员将额外参数传递给vagrant up以设置屏幕数量。

1 个答案:

答案 0 :(得分:1)

这个并不简单,基本上你需要知道当前连接了多少个显示器,Vagrantfile是一个ruby脚本但不幸的是ruby不知道这个(或者至少我不知道)你需要在系统中获得更多因此不容易检查所有系统。

在Mac上,您可以通过运行system_profiler SPDisplaysDataType命令找到您拥有的监视器数量,例如

$ 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

所以要计算显示器的数量,你可以检查你有多少分辨率:

$ system_profiler SPDisplaysDataType | grep Resolution | wc -l
   2

这将有效,所以你可以把它放在你的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

我很确定Linux世界有相同的命令,可能是针对windows。