Setting up 1/4 of memory of virtualbox on vagrantfile - vagrant

I'm new on vagrant and I'm trying to put my machine to 1/4 of system memory like this:
config.vm.define "UbuntuServer2" do |server2|
server2.vm.box = "ubuntu/trusty64"
server2.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
vb.customize ["modifyvm", :id, "--memory", 'echo -n $($(awk '/MemTotal/' {print $2} /proc/meminfo / 1024 / 4))']
end
end
But i'm getting this error:
Path:
Line number: 38
Message: NameError: uninitialized constant MemTotal
How I can init this variable?
I'm using MacOs 10.11.5
Thanks in advance.

Here's what I am using in such case if your host OS is mac
config.vm.define "UbuntuServer2" do |server2|
server2.vm.box = "ubuntu/trusty64"
server2.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
vb.customize ["modifyvm", :id, "--memory", mem]
end
end
I got a more complete script that can help anyone if needed
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
cpus = `wmic cpu get NumberOfCores`.split("\n")[2].to_i
mem = `wmic OS get TotalVisibleMemorySize`.split("\n")[2].to_i / 1024 /4
end
v.customize ["modifyvm", :id, "--memory", mem]
v.customize ["modifyvm", :id, "--cpus", cpus]
end

Related

Vagrant not looping through directories

Is there a way to get vagrant to loop through directories and take action on files?
I've just added this line to my Vagrantfile.
config.ssh.private_key_path = ".vagrant/machines/demo#{i}/virtualbox/private_key', '~/.ssh/id_rsa"
These are the other bits of the same Vagrantfile
(1..3).each do |i|
config.vm.define "demo#{i}" do |config|
config.vm.box = "bento/ubuntu-16.04"
config.vm.box_version = "202002.04.0"
config.vm.hostname = "demo#{i}"
config.vm.network :private_network, ip: "10.0.0.#{i + 10}"
config.vm.network :public_network, bridge: "en0: Wi-Fi (Wireless)"
config.ssh.forward_agent = true
config.vm.synced_folder '.', '/vagrant', disabled: true
config.vm.synced_folder "demo/shared", "/vagrant/shared"
config.vm.synced_folder "demo/dual", "/vagrant/dual"
config.ssh.private_key_path = ".vagrant/machines/demo#{i}/virtualbox/private_key, ~/.ssh/id_rsa"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
#vb.gui = true
vb.memory = "2096"
vb.cpus = "2"
end
end
end
end
But running vagrant reload errors with
* `private_key_path` file must exist: .vagrant/machines/demo1/virtualbox/private_key', '~/.ssh/id_rsa
Which is interesting as the path and file definitely exist.
pwd ; ls -R
/Users/xxx/.vagrant/machines
demo1 demo2 demo3
./demo1/virtualbox:
action_provision action_set_name box_meta creator_uid private_key id synced_folders
./demo2/virtualbox:
action_provision action_set_name box_meta creator_uid private_key id synced_folders
./demo3/virtualbox:
action_provision action_set_name box_meta creator_uid private_key id synced_folders
What I expect is for vagrant to just loop through each directory and read the files
Is there some way to get vagrant to do this?
So I found what was wrong with it.
I just needed to add extra quotes.
config.ssh.private_key_path = ".vagrant/machines/demo#{i}/virtualbox/private_key, ~/.ssh/id_rsa"
Becomes
config.ssh.private_key_path = ".vagrant/machines/demo#{i}/virtualbox/private_key", "~/.ssh/id_rsa"
And it works a charm.

Removing default serial port from vagrant

I am using vagrant with machine.vm.box = "ubuntu/bionic64". No additional configuration.
My VM starts with open serial port COM1 with hardcoded path pointing to <HOMEFOLDER>/ubuntu-bionic-18.04-cloudimg-console.log. If I understand correctly it is used by vagrant for logs.
After some research, I found that it is not Vagrant, but ubuntu official images create this Serial port for logging.
Is there a way to disable this port?
Using VBoxManage Customizations that relies in the modify command:
config.vm.provider "virtualbox" do |vb|
vb.customize [ "modifyvm", :id, "--uart1", "off" ]
vb.customize [ "modifyvm", :id, "--uart2", "off" ]
vb.customize [ "modifyvm", :id, "--uart3", "off" ]
vb.customize [ "modifyvm", :id, "--uart4", "off" ]
end

Error setting VM Storage size in Vagrant

I'm following this link https://github.com/acfreitas/oraclebox
I tried to include the code for setting up VM storage size on my Vagrant.
But i'm getting error like below.
==> vagrant: Running provisioner: shell...
vagrant: Running: C:/Users/skywork/AppData/Local/Temp/vagrant-shell20151111-11660-1l3hhe.sh
==> vagrant: ++ '[' grep -ic 64GB /sizeDisk ']'
==> vagrant: /tmp/vagrant-shell: line 4: [: too many arguments
==> vagrant: ++ sudo fdisk -u /dev/sdb
==> vagrant: Welcome to fdisk (util-linux 2.23.2).
==> vagrant:
==> vagrant: Changes will remain in memory only, until you decide to write them.
==> vagrant: Be careful before using the write command.
==> vagrant:
==> vagrant:
==> vagrant: Command (m for help): Partition type:
==> vagrant: p primary (0 primary, 0 extended, 4 free)
==> vagrant: e extended
==> vagrant: Select (default p): Partition number (1-4, default 1): First sector (2048-31457279, default 2048): Using default value 2048
==> vagrant: Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): Value out of range.
==> vagrant: Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279):
==> vagrant: Device does not contain a recognized partition table
==> vagrant: Building a new DOS disklabel with disk identifier 0x38d6fb65.
==> vagrant: Do you really want to quit?
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
Here is my Vagrant file codes:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION_NO = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION_NO) do |config|
config.vm.define "vagrant" do |oracle|
oracle.vm.box = "CentOS7"
oracle.vm.boot_timeout = 5000
oracle.vm.box_url = "http://zzz.zzz.zzz/CentOS7.1.1503x86_64.box"
oracle.ssh.username = "root"
oracle.ssh.password = "vagrant"
config.vbguest.auto_update = false
oracle.vm.synced_folder "./share", "/root/share", :create => true, type: "nfs"
oracle.vm.provider :virtualbox do |vb|
vb.gui = true
vb.name = "CentOS7"
vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "1", "--nicpromisc2", "allow-all"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
vb.customize ["modifyvm", :id, "--name", "CentOS7"]
if !File.exist?("disk/oracle.vdi")
vb.customize [
'createhd',
'--filename', 'disk/oracle',
'--format', 'VDI',
'--size', 15360
]
vb.customize [
'storageattach', :id,
'--storagectl', "SATA",
'--port', 1, '--device', 0,
'--type', 'hdd', '--medium', 'disk/oracle.vdi'
]
end
end
oracle.vm.provision "shell", path: "bash_files/add-oracle-disk.sh"
oracle.vm.provision "shell", :inline => "localectl set-locale LANG=en_US.UTF-8"
oracle.vm.provision :chef_solo do |chef|
chef.custom_config_path = "Vagrantfile.chef"
chef.add_recipe "base"
end
end
end
Vagrant::Config.run do |config|
config.vm.network :bridged, auto_config: false
end
Here is my VM default disk size:
How and what value I need to put on my VagrantFile and add-oracle-disk.sh to be able to add disk space (10GB for example).
Please help.

Vagrant - how to detect windows host RAM and CPU

I'd like my vagrantfile to automatically set CPU and RAM based on the host specs.
I found this snippet:
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
It however doesn't do Windows (it sets default values 2 and 1024 instead).
Anyone got any clue how to do this?
I managed to figure it our and integrated it into a Git repo.
The code:
cpus = `wmic cpu get NumberOfCores`.split("\n")[2].to_i
mem = `wmic OS get TotalVisibleMemorySize`.split("\n")[2].to_i / 1024 /4
To unify the above answers into a complete example, your vagrant file should contain this provisioning section:
Note the mem_ratio and cpu_exec_cap variables which you can use to tune how much of the hosts RAM and CPU cycles to allocate to the VM.
config.vm.provider "virtualbox" do |v|
mem_ratio = 3/4
cpu_exec_cap = 75
host = RbConfig::CONFIG['host_os']
# Give VM 3/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^2 * mem_ratio
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 * mem_ratio
else # Windows folks
cpus = `wmic cpu get NumberOfCores`.split("\n")[2].to_i
mem = `wmic OS get TotalVisibleMemorySize`.split("\n")[2].to_i / 1024 * mem_ratio
end
puts "Provisioning VM with #{cpus} CPU's (at #{cpu_exec_cap}%) and #{mem/1024} GB RAM."
v.customize ["modifyvm", :id, "--memory", mem]
v.customize ["modifyvm", :id, "--cpus", cpus]
v.customize ["modifyvm", :id, "--cpuexecutioncap", cpu_exec_cap]
end
It is there in the script you have downloaded :
sorry Windows folks, I can't help you
else # sorry Windows folks, I can't help you
cpus = 2
mem = 1024
end
so you can change those default values but unfortunately windows does not provide easy to use commands to retrieve those informations
Now if you want to do it yourself, it should be possible, look at the wmic command, you can get a lot, and with little parsing you should have the necessary information - For example
C:\Users\fhenri>wmic os get freephysicalmemory
FreePhysicalMemory
1564244
C:\Users\fhenri>wmic.exe cpu get NumberOfCores
NumberOfCores
1
you could also look at systeminfo this is pretty verbose and locale specific so might be more difficult to parse

The host network interface with the given name could not be found

I'm running Windows 10, VirtualBox 5 and Vagrant 1.7.4 and trying to run openedx plateform.
After i run vagrant up i got:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["hostonlyif", "ipconfig", "VirtualBox Host-Only Ethernet Adapter #25", "--ip", "192.168.33.1", "--netmask", "255.255.255.0"]
Stderr: VBoxManage.exe: error: The host network interface with the given name could not be found
VBoxManage.exe: error: Details: code E_INVALIDARG (0x80070057), component HostWrap, interface IHost, callee IUnknown
VBoxManage.exe: error: Context: "FindHostNetworkInterfaceByName(Bstr(pszName).raw(), hif.asOutParam())" at line 204 of file VBoxManageHostonly.cpp
The Vagrant file:
Vagrant.require_version ">= 1.5.3"
VAGRANTFILE_API_VERSION = "2"
MEMORY = 4096
CPU_COUNT = 2
# map the name of the git branch that we use for a release
# to a name and a file path, which are used for retrieving
# a Vagrant box from the internet.
openedx_releases = {
"openedx/rc/aspen-2014-09-10" => {
:name => "aspen-fullstack-rc1", :file => "20141010-aspen-fullstack-rc1.box",
},
"aspen.1" => {
:name => "aspen-fullstack-1", :file => "20141028-aspen-fullstack-1.box",
},
"named-release/aspen" => {
:name => "aspen-fullstack-1", :file => "20141028-aspen-fullstack-1.box",
},
"named-release/birch.rc1" => {
:name => "birch-fullstack-rc1", :file => "20150204-birch-fullstack-rc1.box"
},
"named-release/birch.rc2" => {
:name => "birch-fullstack-rc2", :file => "20150211-birch-fullstack-rc2.box"
},
"named-release/birch.rc3" => {
:name => "birch-fullstack-rc3", :file => "20150213-birch-fullstack-rc3.box"
},
"named-release/birch" => {
:name => "birch-fullstack", :file => "20150224-birch-fullstack.box",
},
"named-release/birch.1" => {
:name => "birch-fullstack-1", :file => "birch-1-fullstack.box",
},
}
openedx_releases.default = {
:name => "kifli-fullstack", :file => "20140826-kifli-fullstack.box"
}
openedx_releases_vmware = {
"named-release/birch" => {
:name => "birch-fullstack-vmware", :file => "20150610-birch-fullstack-vmware.box",
},
}
openedx_releases_vmware.default = {
:name => "kifli-fullstack-vmware", :file => "20140829-kifli-fullstack-vmware.box",
}
rel = ENV['OPENEDX_RELEASE']
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Creates an edX fullstack VM from an official release
config.vm.box = openedx_releases[rel][:name]
config.vm.box_url = "http://files.edx.org/vagrant-images/#{openedx_releases[rel][:file]}"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.insert_key = true
config.vm.network :private_network, ip: "192.168.33.10"
config.hostsupdater.aliases = ["preview.localhost"]
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", MEMORY.to_s]
vb.customize ["modifyvm", :id, "--cpus", CPU_COUNT.to_s]
# Allow DNS to work for Ubuntu 12.10 host
# http://askubuntu.com/questions/238040/how-do-i-fix-name-service-for-vagrant-client
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
config.vm.provider :virtualbox do |v, override|
v.gui=true
end
["vmware_fusion", "vmware_workstation"].each do |vmware_provider|
config.vm.provider vmware_provider do |v, override|
override.vm.box = openedx_releases_vmware[rel][:name]
override.vm.box_url = "http://files.edx.org/vagrant-images/#{openedx_releases_vmware[rel][:file]}"
v.vmx["memsize"] = MEMORY.to_s
v.vmx["numvcpus"] = CPU_COUNT.to_s
end
end
end
When i comment this line, everything goes well
config.vm.network :private_network, ip: "192.168.33.10"
But i connot access to te LMS and CMS pages (Cuz i comment this line of course)
Any suggestion will be appreciated !
Finally ! i solved it ! Here is the solution :
1- I run VBox-Win10-fix-14040.exe as Admin (See this Discussion)
2- Run the VirtualBox.exe as Admin + Compatibility with Windows 7
3- And Vagrant up :)
That's it !
Simple solution: Delete the already created (default) Host only Ethernet Adapter from VirtualBox Preferences and run sh launch.sh (if you received error while installing Mirantis Openstack package).
P.S. I don't see a reason why it shouldn't work for Vagrant as well.
for windows 7 x86, maybe your virtual box update to latest version. Try to revert back to version 4. can choose release on march or september 2014
If you are getting error similar to below while trying to setup Mirantis Openstack package on Virtualbox:
VBoxManage.exe: error: The host network interface name 'VirtualBox Host-Only Et' could not be foundVBoxManage.exe: error: The host network interface named 'VirtualBox Host-Only Et' could not be found
Open the network.sh script and add | sed 's/\r$//' as follows before | egrep:
local found_iface=(execute VBoxManage list hostonlyifs | sed 's/\r$//'| egrep "Name: + $name\$" | awk '/Name/ { $1 = ""; print substr($0, 2) }')
local new_name=(execute VBoxManage list hostonlyifs | sed 's/\r$//' | egrep -A9 "Name: + $name\$" | awk '/Name/ { $1 = ""; print substr($0, 2) }')
local new_ip=(execute VBoxManage list hostonlyifs | sed 's/\r$//'| egrep -A9 "Name: + $name\$" | awk '/IPAddress:/ { print $2 }')
local new_mask=(execute VBoxManage list hostonlyifs | sed 's/\r$//'| egrep -A9 "Name: + $name\$" | awk '/NetworkMask:/ { print $2 }')
local new_dhcp=(execute VBoxManage list hostonlyifs | sed 's/\r$//'| egrep -A9 "Name: + $name\$" | awk '/DHCP:/ { print $2 }')
local id=execute VBoxManage hostonlyif create | sed 's/\r$//'| sed "s/'/_/g" | cut -d "_" -f2 | sed 's/\r$//'| sed "s/^_//" | sed "s/_$//"

Resources