在Debian shell中創建一個sh腳本來自動創建基於不同操作系統的Vagrantfile,以管理存儲控制器爲例(Debian的Sata Controller或CentOS的IDE控制器)。試着管理磁盤大小,我分析了盒子的默認磁盤(vmdk),轉換爲VirtualBox本機管理的vdi。Vagrant Ruby代碼從shell中管理Virtualbox
問題在於使用Ruby代碼來分析VM數據。
storagecontroller = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollertype = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollertype0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollerportcount = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollerportcount0 | cut -d '=' -f 2 | sed 's/\"//g']
當機器已經創建此代碼的工作,我使用:
vagrant reload
但是當我嘗試做
vagrant up
問題是,機犯規在機目錄中存在。我在shell中遇到錯誤。 錯誤:
Message: Errno::ENOENT: No such file or directory @ dir_chdir - /root/VirtualBox VMs/projectname/machinename-master
的事情是,我想analize計算機磁盤,但是這個Ruby代碼EXEC機創建之前,我需要一種方法來做到這一點,創建後的機器,但是當無業遊民了一句話叫在殼裏。
這是我生成的代碼的一部分。
config.vm.define "machinename" do |machinename|
machinename.vm.box ="debian/contrib-jessie64"
machinename.vm.hostname = "machinename"
machinename.vm.provider "virtualbox" do |virtualbox|
virtualbox.memory = "2048"
virtualbox.cpus = "8"
virtualbox.name = "machinename-master"
virtualbox.customize [
"modifyvm", :id, "--groups", "/projectname"
]
storagecontroller = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
path = File.join [ENV["HOME"], 'VirtualBox VMs/','projectname','machinename-master']
Dir.chdir("#{path}")
storagecontroller = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontroller.delete!("\n")
storagecontrollertype = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollertype0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollertype.delete!("\n")
storagecontrollerportcount = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollerportcount0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollerportcount.delete!("\n")
require "find"
file=Dir['*.vmdk'].first
if !File.exist?('/root/VirtualBox VMs/projectname/machinename-master/machinename-box.vdi')
virtualbox.customize [
"storagectl", :id,
"--name", "#{storagecontroller}",
"--controller", "#{storagecontrollertype}",
"--portcount", "#{storagecontrollerportcount}",
"--hostiocache", "on"
]
virtualbox.customize [
"clonehd", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/box-disk1.vmdk",
"#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/ojocuidao-box.vdi",
"--format", "VDI"
]
virtualbox.customize [
"modifyhd", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/machinename-box.vdi",
"--resize", 55 * 1024
]
virtualbox.customize [
"storageattach", :id,
"--storagectl", "#{storagecontroller}",
"--port", "0",
"--device", "0",
"--type", "hdd",
"--nonrotational", "on",
"--medium", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/machinename-box.vdi"
]
紅寶石刑期調用shell創建機器前正在執行的,所以我需要找到這個句子和vagrantfile盤定製右後箱被下載並創建VM的句子。
有人可以幫助我嗎?