$ vagrant up Bringing machine 'myvm' up with 'virtualbox' provider... ==> myvm: Importing base box 'centos/7'... ==> myvm: Matching MAC address for NAT networking... ==> myvm: Checking if box 'centos/7' version '1905.1' is up to date... ==> myvm: Setting the name of the VM: demo-01_myvm_1562554459011_52775 ==> myvm: Clearing any previously set network interfaces... ==> myvm: Preparing network interfaces based on configuration... myvm: Adapter 1: nat ==> myvm: Forwarding ports... myvm: 22 (guest) => 2222 (host) (adapter 1) ==> myvm: Booting VM... ==> myvm: Waiting for machine to boot. This may take a few minutes... myvm: SSH address: 127.0.0.1:2222 myvm: SSH username: vagrant myvm: SSH auth method: private key myvm: myvm: Vagrant insecure key detected. Vagrant will automatically replace myvm: this with a newly generated keypair for better security. myvm: myvm: Inserting generated public key within guest... myvm: Removing insecure key from the guest if it's present... myvm: Key inserted! Disconnecting and reconnecting using new SSH key... ==> myvm: Machine booted and ready! ==> myvm: Checking for guest additions in VM... myvm: No guest additions were detected on the base box for this VM! Guest myvm: additions are required for forwarded ports, shared folders, host only myvm: networking, and more. If SSH fails on this machine, please install myvm: the guest additions and repackage the box to continue. myvm: myvm: This is not an error message; everything may continue to work properly, myvm: in which case you may ignore this message. ==> myvm: Setting hostname... ==> myvm: Rsyncing folder: /vagrant/demo-01/ => /vagrant
/vagrant/demo-05/ 就是我指令執行的所在目錄, /vagrant 就是對應到 VM 內的路徑
如果宿主主機上有檔案需要放在 VM 中, 可以寫成這樣的 Vagrantfile
1 2 3 4 5 6 7 8 9
# -*- mode: ruby -*- # vi: set ft=ruby :
Vagrant.configure("2") do |config| config.vm.define "myvm" do |myvm| myvm.vm.box = "centos/7" myvm.vm.synced_folder "/github", "/tmp/github" end end
但執行下去馬上 GG
1 2 3 4 5 6 7 8 9 10 11 12 13
myvm: /tmp/github => /github Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem "vboxsf" is not available. This filesystem is made available via the VirtualBox Guest Additions and kernel module. Please verify that these guest additions are properly installed in the guest. This is not a bug in Vagrant and is usually caused by a faulty Vagrant box. For context, the command attempted was:
mount -t vboxsf -o uid=1000,gid=1000 srv_github /srv/github
The error output from the command was:
mount: unknown filesystem type'vboxsf'
這時候只要加裝 plugin 就可以解決了 plugin 安裝部分 mac 跟 win 都是一樣.
1 2 3 4 5
$ vagrant plugin install vagrant-vbguest Installing the 'vagrant-vbguest' plugin. This can take a few minutes... Fetching: micromachine-2.0.0.gem (100%) Fetching: vagrant-vbguest-0.18.0.gem (100%) Installed the plugin 'vagrant-vbguest (0.18.0)'!
$ vagrant up Bringing machine 'myvm' up with 'virtualbox' provider... ==> myvm: Importing base box 'centos/7'... ==> myvm: Matching MAC address for NAT networking... ==> myvm: Checking if box 'centos/7' version '1905.1' is up to date... ==> myvm: Setting the name of the VM: demo-05_myvm_1562573100554_42608 ==> myvm: Clearing any previously set network interfaces... ==> myvm: Preparing network interfaces based on configuration... myvm: Adapter 1: nat ==> myvm: Forwarding ports... myvm: 22 (guest) => 2222 (host) (adapter 1) ==> myvm: Booting VM... ==> myvm: Waiting for machine to boot. This may take a few minutes... myvm: SSH address: 127.0.0.1:2222 myvm: SSH username: vagrant myvm: SSH auth method: private key myvm: Warning: Connection reset. Retrying... myvm: myvm: Vagrant insecure key detected. Vagrant will automatically replace myvm: this with a newly generated keypair for better security. myvm: myvm: Inserting generated public key within guest... myvm: Removing insecure key from the guest if it's present... myvm: Key inserted! Disconnecting and reconnecting using new SSH key... ==> myvm: Machine booted and ready! [myvm] No Virtualbox Guest Additions installation found. Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror01.idc.hinet.net * extras: mirror01.idc.hinet.net * updates: mirror01.idc.hinet.net Package binutils-2.27-34.base.el7.x86_64 already installed and latest version Package 1:make-3.82-23.el7.x86_64 already installed and latest version Package bzip2-1.0.6-13.el7.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package gcc.x86_64 0:4.8.5-36.el7_6.2 will be installed ...... 略 Installing Virtualbox Guest Additions 6.0.8 - guest version is unknown Verifying archive integrity... All good. Uncompressing VirtualBox 6.0.8 Guest Additions for Linux........ VirtualBox Guest Additions installer Copying additional installer modules ... Installing additional modules ... VirtualBox Guest Additions: Starting. VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules. This may take a while. VirtualBox Guest Additions: To build modules for other installed kernels, run VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup <version> VirtualBox Guest Additions: or VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup all VirtualBox Guest Additions: Building the modules for kernel 3.10.0-957.12.2.el7.x86_64. Redirecting to /bin/systemctl start vboxadd.service Redirecting to /bin/systemctl start vboxadd-service.service Unmounting Virtualbox Guest Additions ISO from: /mnt ==> myvm: Checking for guest additions in VM... ==> myvm: Rsyncing folder: /vagrant/demo-05/ => /vagrant ==> myvm: Mounting shared folders... myvm: /tmp/github => /github
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| cluster.each_with_index do |(hostname, info), index| config.vm.define hostname do |cfg| cfg.vm.provider :virtualbox do |vb, override| config.vm.box = BOX_IMAGE override.vm.network :private_network, ip: "#{info[:ip]}" override.vm.hostname = hostname vb.name = hostname vb.customize ["modifyvm", :id, "--memory", info[:mem], "--cpus", info[:cpus], "--hwvirtex", "on"] end # end provider end # end config end # end cluster end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
NODE_COUNT.times do |i| node_id = "node#{i}" config.vm.define node_id do |node| node.vm.box = BOX_IMAGE node.vm.hostname = "#{node_id}.intranet.local" node.vm.network :private_network, ip: "192.168.56.#{10+i}" end end end