近兩天在拜讀Pierre MAVRO撰寫的MariaDB High Performance這本書。書中的實驗環境是通過Vagrant搭建。Vagrant這個詞,本人已經多次在外文參考資料中看到,之前有過初步地嘗試,因沒有box而放棄。鑑於MariaDB的重要性,爲了能成功消化MariaDB High Performance中的知識,只能再次嘗試Vagrant,初步實現需求,現將操作過程及期間遇到的問題記錄下來,以饗後來者。

本人筆電的OS是CentOS Linux release 7.2.1511 (Core),內核版本是3.10.0-327.10.1.el7.x86_64,以下操作都在本人筆電上操作。

Prerequisites

Vagrant是一款用於創建、配置虛擬開發環境的軟件,依賴於VirtualBox,故而使用Vagrant須同時安裝VirtualBox

注意VirtualBox依賴於kernel-devel,且每次更新內核後,VirtualBox都需要重新安裝,否則會報錯,無法正常啓動。

Official Website

Download Page

Downloading Vagrant

Vagrant當前版本是1.8.1,根據其官方文檔INSTALLING VAGRANT所述,建議用戶不要使用系統包管理器(system package manager)安裝Vagrant,而是在官網下載。官網提供MAC OS XWINDOWSDEBIANCENTOS4種版本,此處選擇CENTOS 64-bit下載,鏈接是 https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.rpm,下載完成後不要忘記校驗,SHA256校驗碼地址

以下相關SHA256校驗碼

#https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_SHA256SUMS
2cfdbeec9e40376e49dae9d9f27511896e3b296f0e24f8731339bb3d32c48c93  vagrant_1.8.1.dmg
6468cc140f6f236c3ac4bc0bbb1cc2177114249bc344e0aba8c826eae9cc6eb2  vagrant_1.8.1.msi
ae93af8cacf20f2f8c4c6a111767e77988454d0238a001a37a1d1c115334efdb  vagrant_1.8.1_i686.deb
8a5773f52039addea0830efea18f127dcdff7f649699bbce3672c43da1e51a92  vagrant_1.8.1_i686.rpm
ed0e1ae0f35aecd47e0b3dfb486a230984a08ceda3b371486add4d42714a693d  vagrant_1.8.1_x86_64.deb
d2b2308cb1a7da656f6f7cd926287cd21f3c79bf29bc15f09d7471a93f1ef118  vagrant_1.8.1_x86_64.rpm

使用校驗命令生成的校驗碼

[flying@lemp ~]$ openssl dgst -sha256 ~/Downloads/vagrant_1.8.1_x86_64.rpm
SHA256(/home/flying/Downloads/vagrant_1.8.1_x86_64.rpm)= d2b2308cb1a7da656f6f7cd926287cd21f3c79bf29bc15f09d7471a93f1ef118
[flying@lemp ~]$ sha256sum ~/Downloads/vagrant_1.8.1_x86_64.rpm
d2b2308cb1a7da656f6f7cd926287cd21f3c79bf29bc15f09d7471a93f1ef118  /home/flying/Downloads/vagrant_1.8.1_x86_64.rpm
[flying@lemp ~]$

Downloading VirtualBox

VirtualBox當前版本是VirtualBox 5.0.16,在Linux下載頁選擇對應的發行版,此處選擇CentOS 7,鏈接是http://download.virtualbox.org/virtualbox/5.0.16/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm,SHA256校驗碼地址,MD5校驗碼地址

#https://www.virtualbox.org/download/hashes/5.0.16/SHA256SUMS
...
cd98ea1b1fee06a6fa7dd2fa1166da691ab8d01e709c38c144d83e8c7eaa54b3 *VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm
...

使用校驗命令生成的校驗碼

[flying@lemp ~]$ openssl dgst -sha256 ~/Downloads/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm
SHA256(/home/flying/Downloads/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm)= cd98ea1b1fee06a6fa7dd2fa1166da691ab8d01e709c38c144d83e8c7eaa54b3
[flying@lemp ~]$ sha256sum ~/Downloads/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm
cd98ea1b1fee06a6fa7dd2fa1166da691ab8d01e709c38c144d83e8c7eaa54b3  /home/flying/Downloads/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm
[flying@lemp ~]$

Installing Packages

CentOS7 Minimal Installation默認安裝有openssh,故不需再安裝。安裝使用sudo yum localinstall命令即可。

sudo yum install -y kernel-devel
sudo yum localinstall -y /PATH/FRON/VirtualBox-5.0-5.0.16_105871_el7-1.x86_64.rpm
sudo yum localinstall -y /PATH/FRON/vagrant_1.8.1_x86_64.rpm

VagrantVirtualBox默認的路徑是/opt

[flying@lemp ~]$ ls -ldh /opt/vagrant/
drwxr-xr-x 4 root root 31 Mar  9 21:54 /opt/vagrant/
[flying@lemp ~]$ ls -ldh /opt/VirtualBox/
drwxr-xr-x 16 flying flying 4.0K Mar 21 12:15 /opt/VirtualBox/
[flying@lemp ~]$

Version Checking

  • Vagrant使用的命令是vagrant
  • VirtualBox使用的命令是vboxmanage
[flying@lemp ~]$ vboxmanage -v
5.0.16r105871
[flying@lemp ~]$ vagrant -v
Vagrant 1.8.1
[flying@lemp ~]$

Using Vagrant

Vagrant官方文檔 https://www.vagrantup.com/docs/

其常用命令有 * vagrant init: 在當前目錄下初始化配置文件Vagrantfile * vagrant up: 啓動vagrant虛擬機 * vagrant ssh: 使用ssh登錄vagrant虛擬機 * vagrant halt: 關閉vagrant虛擬機 * vagrant destroy: 銷燬目錄下已創建vagrant虛擬機

通常在具體目錄下執行vagrant init時加上box名稱,如hashicorp/precise64(Ubuntu),會自動下載該box,但是下載速度非常慢,也不是我所需要的類Linux發行版。

Downloading box

Vagrant中地box可理解爲Virtualbox中的虛擬機文件。Vagrant官方允許第三方製作box並分享,地址是Vagrant Boxes。本人在此處選擇的Provider filtervirtualbox,在第二頁找到 centos/7,當前版本是v1602.02,於2016年2月26號更新。其提供安裝命令是

vagrant init centos/7; vagrant up --provider virtualbox

執行該命令,會發現下載速度異常緩慢,不過其中顯示了文件下載鏈接是 https://atlas.hashicorp.com/centos/boxes/7/versions/1602.02/providers/virtualbox.box,本人嘗試使用代理下載,速度仍不理想,後通過離線下載解決。網盤下載地址:

链接: http://pan.baidu.com/s/1nue8jsl 密码: 4799

下載到本地,有483MB

[flying@lemp Downloads]$ ls -lh CentOS-7-x86_64-Vagrant-1602_02.VirtualBox.box
-rw-rw-r-- 1 flying flying 483M Mar 21 09:44 CentOS-7-x86_64-Vagrant-1602_02.VirtualBox.box
[flying@lemp Downloads]$

Add Box

只有將box添加入vagrant才能使用,命令是vagrant box,執行vagrant box --help,即可看到具體參數。在vagrant中,如果對相關命令使用不熟悉,可在命令後添加--help查看使用幫助,如vagrant box add --help

此處在家目錄下創建目錄lempstacker,在該目錄下進行初始化操作,box名該爲lemp

#創建目錄lempstacker
[flying@lemp ~]$ mkdir -pv lempstacker
mkdir: created directory ‘lempstacker’

#進入目錄lempstacker
[flying@lemp ~]$ cd lempstacker/

#打印當前路徑
[flying@lemp lempstacker]$ pwd
/home/flying/lempstacker

#添加box,命名爲lemp
[flying@lemp lempstacker]$ vagrant box add --name lemp ~/Downloads/CentOS-7-x86_64-Vagrant-1602_02.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'lemp' (v0) for provider:
    box: Unpacking necessary files from: file:///home/flying/Downloads/CentOS-7-x86_64-Vagrant-1602_02.VirtualBox.box
==> box: Successfully added box 'lemp' (v0) for 'virtualbox'!

#查看vagrant中已有的box
[flying@lemp lempstacker]$ vagrant box list
lemp      (virtualbox, 0)
[flying@lemp lempstacker]$

init

在目錄lempstacker中進行初始化操作,指定用名爲lemp的創建虛擬機

#初始化
[flying@lemp lempstacker]$ vagrant init lemp
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

#列出目錄文件
[flying@lemp lempstacker]$ ls
Vagrantfile

#查看新創建配置文件中內容
[flying@lemp lempstacker]$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "lemp"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end
[flying@lemp lempstacker]$

可看到config.vm.box = "lemp",即爲指定box。也可直接執行vagrant init,然後再改配置文件Vagrantfile的這一行,默認是config.vm.box = "base"

Up and SSH Login

vagrant up可理解爲是啓動虛擬機

[flying@lemp lempstacker]$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'lemp'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: lempstacker_default_1458541540228_96558
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it\'s present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /home/flying/lempstacker/ => /home/vagrant/sync
[flying@lemp lempstacker]$

vagrant ssh通過ssh登錄

#ssh登錄
[flying@lemp lempstacker]$ vagrant ssh

#查看當前用戶
[vagrant@bogon ~]$ whoami
vagrant

#日期
[vagrant@bogon ~]$ date
Mon Mar 21 02:26:47 EDT 2016

#查看發行版本信息
[vagrant@bogon ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

#查看內核信息
[vagrant@bogon ~]$ uname -r
3.10.0-327.10.1.el7.x86_64

#查看ip信息 (注意默認虛擬機只能與主機通信)
[vagrant@localhost ~]$ ip addr | grep -E "inet\>"
    inet 127.0.0.1/8 scope host lo
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0

#時區信息
[vagrant@bogon ~]$ timedatectl
      Local time: Mon 2016-03-21 02:27:16 EDT
  Universal time: Mon 2016-03-21 06:27:16 UTC
        RTC time: Mon 2016-03-21 06:27:16
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2016-03-13 01:59:59 EST
                  Sun 2016-03-13 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2016-11-06 01:59:59 EDT
                  Sun 2016-11-06 01:00:00 EST

#查看系統區域和鍵盤佈局
[vagrant@bogon ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us
[vagrant@bogon ~]$ hostnamectl
   Static hostname: localhost.localdomain
Transient hostname: bogon
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70d63a0c469944bda91cd7743c2a3275
           Boot ID: 90e6951d05eb46d597570f894f93cb58
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.10.1.el7.x86_64
      Architecture: x86-64

#關機 (關閉虛擬機)
[vagrant@bogon ~]$ shutdown -h now
==== AUTHENTICATING FOR org.freedesktop.login1.power-off ===
Authentication is required for powering off the system.
Authenticating as: root
Password:

#使用sudo再次執行關機命令
[vagrant@bogon ~]$ sudo !!
sudo shutdown -h now
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.
[flying@lemp lempstacker]$

可以看到 * SSH連接主機地址是127.0.0.1:2222,默認虛擬機只能與主機通信,如果要跟其他主機進行通信,需要修改目錄中地配置文件Vagrantfile * 登錄用戶是vagrant密碼vagrant,具有sudo權限,執行sudo時無需輸入用戶密碼

Bridged Connection

更改配置文件Vagrantfile,使之能與其它主機通信。# config.vm.network "public_network",取消註釋再次登錄即可。

可在目錄中執行如下命令修改

sed -i 's@# config.vm.network "public_network"@config.vm.network "public_network"@g' ./Vagrantfile

再次啓動並登錄,會提示選擇可用的橋接網路接口Available bridged network interfaces,此處選擇無線(wlp3s0),出現提示default: Which interface should the network bridge to?時,選擇對應的序號即可,此處wlp3s0的序號是1,故選擇1

#修改配置文件內容
[flying@lemp lempstacker]$ sed -i 's@# config.vm.network "public_network"@config.vm.network "public_network"@g' Vagrantfile

#啓動並SSH登錄
[flying@lemp lempstacker]$ vagrant up && vagrant ssh
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...

#出現選項
==> default: Available bridged network interfaces:
1) wlp3s0
2) virbr0
3) enp5s0
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.

#選擇出現地選項中任意一項,此處選擇1
    default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /home/flying/lempstacker/ => /home/vagrant/sync
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
Last login: Mon Mar 21 02:30:54 2016 from 10.0.2.2

#查看IP地址
[vagrant@localhost ~]$ ip addr | grep -E "inet\>"
    inet 127.0.0.1/8 scope host lo
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
    inet 192.168.0.106/24 brd 192.168.0.255 scope global dynamic eth1

#ping筆電IP
[vagrant@localhost ~]$ ping -c 5 192.168.0.107
PING 192.168.0.107 (192.168.0.107) 56(84) bytes of data.
64 bytes from 192.168.0.107: icmp_seq=1 ttl=64 time=0.706 ms
64 bytes from 192.168.0.107: icmp_seq=2 ttl=64 time=0.521 ms
64 bytes from 192.168.0.107: icmp_seq=3 ttl=64 time=0.515 ms
64 bytes from 192.168.0.107: icmp_seq=4 ttl=64 time=0.403 ms
64 bytes from 192.168.0.107: icmp_seq=5 ttl=64 time=0.397 ms

--- 192.168.0.107 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4027ms
rtt min/avg/max/mdev = 0.397/0.508/0.706/0.113 ms
[vagrant@localhost ~]$


#筆電ping虛擬機IP
[flying@lemp ~]$ ping -c 5 192.168.0.106
PING 192.168.0.106 (192.168.0.106) 56(84) bytes of data.
64 bytes from 192.168.0.106: icmp_seq=1 ttl=64 time=0.353 ms
64 bytes from 192.168.0.106: icmp_seq=2 ttl=64 time=0.407 ms
64 bytes from 192.168.0.106: icmp_seq=3 ttl=64 time=0.549 ms
64 bytes from 192.168.0.106: icmp_seq=4 ttl=64 time=0.567 ms
64 bytes from 192.168.0.106: icmp_seq=5 ttl=64 time=0.545 ms

--- 192.168.0.106 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.353/0.484/0.567/0.088 ms
[flying@lemp ~]$

可以看到網卡新增了IP192.168.0.106/24,且可以ping通。

vagrant halt

關閉虛擬機可在登錄情況下使用命令sudo shutodwn -h now,也可退出登錄後再使用命令vagrant halt

#退出SSH登錄
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

#關閉vagrant
[flying@lemp lempstacker]$ vagrant halt
==> default: Attempting graceful shutdown of VM...
[flying@lemp lempstacker]$

vagrant package

vagrant主要用於打造開發虛擬環境,配置好後,可將box打包導出,傳給其他人,從而保證開發環境的一致性。打包過程較長,需耐心等待。

此處將box打包爲love.box,保存路徑/tmp

注意: 使用vagrant package命令進行打包時,參數--base是指當前box的vms編號,而非box名,否則會報錯。

會提示lemp: VM not created

[flying@lemp lempstacker]$ vagrant package --base lemp --output /tmp/love.box
==> lemp: VM not created. Moving on...

正確操作

#查看vms
[flying@lemp lempstacker]$ vboxmanage list vms | grep "lemp"
"lempstacker_default_1458541540228_96558" {b128b8c0-2f43-43de-89b9-989d3e2af103}

#打包
[flying@lemp lempstacker]$ vagrant package --base "lempstacker_default_1458541540228_96558" --output /tmp/love.box
==> lempstacker_default_1458541540228_96558: Exporting VM...
==> lempstacker_default_1458541540228_96558: Compressing package to: /tmp/love.box

#查看/tmp/love.box
[flying@lemp lempstacker]$ ls -lh /tmp/love.box
-rw-rw-r-- 1 flying flying 472M Mar 21 15:07 /tmp/love.box
[flying@lemp lempstacker]$

其實,不指定--base也是可以的,不過需要在目錄下(如此處地lempstacker),默認指定當前目錄下的box爲base

#刪除文件/tmp/love.box
[flying@lemp lempstacker]$ sudo rm -rf /tmp/love.box

#打包,不指定--base
[flying@lemp lempstacker]$ vagrant package --output /tmp/love.box==> default: Exporting VM...
==> default: Compressing package to: /tmp/love.box

#查看/tmp/love.box
[flying@lemp lempstacker]$ ls -lh /tmp/love.box
-rw-rw-r-- 1 flying flying 472M Mar 21 15:12 /tmp/love.box
[flying@lemp lempstacker]$

如果要使用通過打包生成的box,使用命令vagrant box add即可,操作同上。不過 需要注意,使用打包生成的box在vagrant ssh時很可能會出現

Warning: Authentication failure. Retrying..

無法正常登錄。

此問題在 “Warning: Authentication failure. Retrying… “ after packaging box中有討論。

最簡單的方案是在配置文件Vagrantfile中添加config.ssh.insert_key = false。在登錄時需要顯式輸入用戶密碼(此處是vagrant,用戶名、密碼相同)。

一勞永逸的解決方案,先在配置文件Vagrantfile中添加config.ssh.insert_key = false,手動輸入密碼登錄虛擬機後,在虛擬機中執行如下命令

wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown -R vagrant:vagrant .ssh

關閉虛擬機後,重新打包即可。

使用 https://github.com/mitchellh/vagrant/blob/master/keys/vagrant.pub 這種方案,個人感覺太過繁瑣就沒有測試,有興趣者可自行研究。

vagrant destroy

如果不想再使用該虛擬機,可使用vagrant destroy將其銷燬。

[flying@lemp lempstacker]$ vboxmanage list vms | grep "lemp""lempstacker_default_1458541540228_96558" {b128b8c0-2f43-43de-89b9-989d3e2af103}

#銷燬虛擬機(會出現提示信息)
[flying@lemp lempstacker]$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

#再次查看,已經不存在
[flying@lemp lempstacker]$ vboxmanage list vms | grep "lemp"

#退到上一級目錄
[flying@lemp lempstacker]$ cd ..

#刪除目錄lempstacker
[flying@lemp ~]$ rm -rf lempstacker/
[flying@lemp ~]$

更新2016.03.26: 如果銷燬虛擬機時,不想出現提示,可通過添加參數--force實現,即執行

vagrant destroy --force [name]

Remove box

如果不再需要使用某一box,可用命令 vagrant box remove將其移除,此處移除lemp

[flying@lemp ~]$ vagrant box list
MariaDB10 (virtualbox, 0)
lemp      (virtualbox, 0)
[flying@lemp ~]$ vagrant box remove lemp
Removing box 'lemp' (v0) with provider 'virtualbox'...
[flying@lemp ~]$ vagrant box list
MariaDB10 (virtualbox, 0)
[flying@lemp ~]$

研究過程到此暫告一段落,如何手動創建box,之後再研究。


Vagrant Errors

  • Authentication failure

    Warning: Authentication failure. Retrying..
    

    參考“Warning: Authentication failure. Retrying… “ after packaging box

  • Failed to mount folders in Linux guest ``` Failed to mount folders in Linux guest. This is usually because the “vboxsf” file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was:

mount -t vboxsf -o uid=id -u vagrant,gid=getent group vagrant | cut -d: -f3 vagrant /vagrant mount -t vboxsf -o uid=id -u vagrant,gid=id -g vagrant vagrant /vagrant

The error output from the last command was:

mount: unknown filesystem type ‘vboxsf’ ``` 參考 Vagrant can’t mount shared folders in VirtualBoxVagrant error : Failed to mount folders in Linux guest


References


Change Logs

  • 2016.03.20 15:40 Mon Asia/Beijing
    • 初稿完成
  • 2016.03.21 22:56 Mon Asia/Beijing
    • 解決Authentication failure問題,添加Vagrant Errors目錄
  • 2016.03.26 18:10 Sat Asia/Beijing
    • 添加vagrant destroy --force

  • Note Time: 2016.03.20 15:40 Mon
  • Note Location: Asia/Beijing
  • Writer: lempstacker