# 虚机安装

## Vagrant创建虚机

### 方式一

* 创建目录：mkdir centos7
* 初始化Vagrantfile文件：vagrant init centos/7
* 创建虚机：vagrant up

### 方式二

*   下载一个合适的box。下载地址如下

    下载方式一：[vagrant官网下载镜像方式](https://my.oschina.net/cxgphper/blog/1940644)

    下载方式二：[http://cloud.centos.org/centos/7/vagrant/x86\_64/images/](http://cloud.centos.org/centos/7/vagrant/x86_64/images/)

    下载方式三：[https://www.vagrantbox.es/](https://www.vagrantbox.es/)
*   建立了如下的开发环境目录：

    ```bash
    $ /Users/helei/vagrant
    $ cd /Users/helei/vagrant
    ```
*   完成一个box的安装

    ```bash
    $ vagrant box add {title} {url}
    $ vagrant init {title}
    $ vagrant up
    ```

    vagrant box add 是添加box的命令

    其中｛title｝可以自行设置，我这里使用的是 centos7 ，｛url｝是下载到本地box路径。我的路径是：/Users/helei/vagrant/centos-7.0-x86\_64.box

    ```
    # 安装box
    $ vagrant box add CentOs7 /Users/helei/vagrant/centos-7.0-x86_64.box
    ```

    输出内容：

    ```
    Downloading or copying the box...
    Extracting box...te: 47.5M/s, Estimated time remaining: --:--:--)
    Successfully added box 'base' with provider 'virtualbox'!
    ```

    box中的镜像文件被放到了：/Users/helei/.vagrant.d/boxes/，如果在window系统中应该是放到了： C:\Users\当前用户名.vagrant.d\boxes\目录下。

    ```
    # 如果是才add 的box，就必须执行本步骤，初始化一次后，以后启动系统，就不需要执行本步骤。
    $ vagrant init CentOs7
    ```

    输出内容：

    ```
    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.
    ```

    这样就会在当前目录生成一个 Vagrantfile的文件，里面有很多配置信息，后面我在慢慢说，默认不做任何配置改动，也是可以启动系统的。

    ```
    # 启动系统
    $ vagrant up
    ```

    输出内容：

    ```
    Bringing machine 'default' up with 'virtualbox' provider...
    [default] Importing base box 'base'...
    [default] Matching MAC address for NAT networking...
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    ...
    ```

## Vagrantfile文件模板

```ruby
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "docker-node1",
        :eth1 => "192.168.205.10",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "docker-node2",
        :eth1 => "192.168.205.11",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos7"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, ip: opts[:eth1]
      end
  end

end
```

```ruby
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "docker-node1",
        :eth1 => "192.168.205.10",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "docker-node2",
        :eth1 => "192.168.205.11",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos7"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, ip: opts[:eth1]
      end
  end

#  config.vm.synced_folder "./labs", "/home/vagrant/labs"
#  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end
```

## Vagrant基本操作

* 启动虚机：vagrant up
* 查看虚机状态：vagrant status
* 显示当前用户Vagrant的所有环境状态：vagrant global-status
* 进入虚机：vagrant ssh docker-node1
* 删除虚机：vagrant destroy \[name|id]

## 参考文章

* [vagrant系列教程(一)：vagrant的安装与初识（转）](https://www.cnblogs.com/ajianbeyourself/p/5919503.html)
