Vagrant

 ·  ☕ 11  · 👀...

简介

技术 特性 适用场景 说明
Vagrant - 统一环境
- 快速搭建实验环境
- Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。
- vagrant官网
- Vagrant下载
- virtualbox下载
- box下载

Commands

管理Box

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
### 显示现有box
> vagrant box list

# 添加box
> vagrant box add "name" remoteUrl或localFile

### 删除box
> vagrant box remove name

### 添加官方提供的 box
> vagrant box add --provider virtualbox centos/7
> vagrant box add  --name centos/7  --provider virtualbox  /Users/iamwlb/Documents/workspace/vagrant/centos_virtualbox.box

管理虚拟机

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
### 初始化虚拟机
> vagrant init

### 启动虚拟机
> vagrant up
> vagrant up --debug &> vagrant.log

### 访问虚拟机
> vagrant ssh

### 停止虚拟机
> vagrant halt

### 销毁虚拟机
> vagrant destroy

### 重启虚拟机(重启虚拟机后会重新载入配置文件)
> vagrant reload

### 挂起(休眠)虚拟机
> vagrant suspend

### 从挂起状态恢复虚拟机(取消休眠)
> vagrant resume

### 输出ssh连接信息
> vagrant ssh-config

### 输出虚拟机状态(当前目录)
> vagrant status

### 输出虚拟机状态(本机)
> vagrant global-status

### 查看虚拟机ssh配置
> vagrant ssh-config 

### 打包
> vagrant package --base <MachineName>

管理插件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
### 列出已安装的插件
> vagrant plugin list

### 更新所有插件
> vagrant plugin update

### 更新某个插件
> vagrant plugin update <插件名称>

### 安装插件
> vagrant plugin install <插件名称>

### 卸载插件
> vagrant plugin uninstall <插件名称>

### 安装 vagrant-vbguest
> vagrant plugin install vagrant-vbguest

管理VirtualBox 虚拟机

1
2
3
4
5
### 获取虚拟机列表
> VBoxManage list vms

### 删除虚拟机
> VBoxManage unregistervm <虚拟机名称>

Vagrantfile文件详解

基本配置

1
2
3
Vagrant.configure("2") do |config|
    # ...
end

配置box

1
config.vm.box = "centos/7"

配置虚拟机名称

1
vb.name = "centos7"

配置CPU个数

1
vb.cpus = 2

配置内存

1
vb.memory = "1024"  

单位:MB

配置虚拟机网络

配置NAT

缺省创建。

配置bridge

1
2
3
4
5
6
7
8
#创建一个bridge桥接网络,指定IP,指定桥接适配器
config.vm.network "public_network", ip: "192.168.100.10", bridge: "en0: Wi-Fi (AirPort)"
#创建一个bridge桥接网络,指定IP
config.vm.network "public_network", ip: "192.168.0.17"
#创建一个bridge桥接网络,指定桥接适配器
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
#创建一个bridge桥接网络,不指定桥接适配器
config.vm.network "public_network"

配置host-only

#配置当前vm的host-only网络的IP地址为192.168.33.10
config.vm.network :private_network, ip: "192.168.33.10"
#host-only模式的IP可以不指定,采用dhcp自动生成的方式
config.vm.network "private_network", type: "dhcp”

示例(配置桥接、固定ip)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Vagrant.configure("2") do |config|
  config.vm.define :node1 do |node1|
    node1.vm.box = "centos/7"
    node1.vm.hostname = "node1"
    node1.vm.network "public_network", auto_config: false, bridge: "en0: Wi-Fi (AirPort)"
    node1.vm.provision "shell",
        run: "always",
        inline: "ifconfig enp0s8 192.168.100.10 netmask 255.255.0.0 up"  
    node1.vm.provision "shell",
        run: "always",
        inline: "route add default gw 192.168.100.1"
    node1.vm.provision "shell",
        run: "always",
        inline: "eval `route -n | awk '{ if ($8 == \"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
  end
end

配置hostname

1
config.vm.hostname = “node1"

指定vm的hostname,会覆盖vm中/etc/hostsname中的设置。
当有很多台虚拟服务器的时候,依靠hostname來做识别。比如安装了多台虚拟机时可以通过vagrant up hostname来启动指定虚拟机。

配置端口转发

1
2
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 81, host: 8081

配置启动virtualbox窗口

1
2
vb.gui = true  #启动virtualbox窗口
vb.gui = false #不启动virtualbox窗口

配置目录同步

1
config.vm.synced_folder "本地目录", "虚拟机目录"

配置多台虚拟机

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Vagrant.configure("2") do |config|
    config.vm.define :web do |web|
        web.vm.provider "virtualbox" do |v|
            v.customize ["modifyvm", :id, "--name", "web", "--memory", "1024"]
        end
        web.vm.box = "centos/7"
        web.vm.hostname = "node1"
        web.vm.network :private_network, ip : "11.11.11.1"
    end
    
    config.vm.define :db do |db|
        db.vm.provider "virtualbox" do |v|
            v.customize ["modifyvm", :id, "--name", "db", "--memory", "1024"]
        end
        db.vm.box = "centos/7"
        db.vm.hostname = "node2"
        db.vm.network :private_network, ip : "11.11.11.2"
    end
end

配置虚拟机ssh的用户名和密码

1
2
config.ssh.username = "root" 
config.ssh.password = "vagrant" 

配置Docker

安装Docker1.12.6

1
2
3
4
5
6
7
8
Vagrant.configure("2") do |config|
  config.vm.define :node1 do |node1|
    ...
    node1.ssh.username="root"
    node1.ssh.password="vagrant"
    node1.vm.provision :shell, :path => "InstallDocker.sh"
  end
end

InstallDocker.sh 内容如下:

1
2
3
4
5
#!/bin/bash

yum install -y docker
sudo systemctl enable docker && sudo systemctl start docker
docker -- version

安装Docker1.12.3

1
2
3
4
5
6
7
8
Vagrant.configure("2") do |config|
  config.vm.define :node1 do |node1|
    ...
    node1.ssh.username="root"
    node1.ssh.password="vagrant"
    node1.vm.provision :shell, :path => "InstallDocker.sh"
  end
end

InstallDocker.sh 内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash

yum install -y wget
mkdir -p ~/_src \
&& cd ~/_src \
&& wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-selinux-1.12.3-1.el7.centos.noarch.rpm \
&& wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.12.3-1.el7.centos.x86_64.rpm \
&& wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-debuginfo-1.12.3-1.el7.centos.x86_64.rpm \
&& yum localinstall -y docker-engine-selinux-1.12.3-1.el7.centos.noarch.rpm docker-engine-1.12.3-1.el7.centos.x86_64.rpm docker-engine-debuginfo-1.12.3-1.el7.centos.x86_64.rpm
sudo systemctl enable docker && sudo systemctl start docker
docker --version

配置阿里云镜像加速

1
2
3
4
5
6
Vagrant.configure("2") do |config|
  config.vm.define :node1 do |node1|
    ...
    node1.vm.provision :shell, :path => "ConfigDockerMirror.sh"
  end
end

ConfigDockerMirror.sh 内容如下:

1
2
3
4
sudo cp -n /lib/systemd/system/docker.service /lib/systemd/system/docker.service
sudo sed -i "s|ExecStart=/usr/bin/dockerd|ExecStart=/usr/bin/dockerd --registry-mirror=https://xxxxxxxx.mirror.aliyuncs.com|g" /lib/systemd/system/docker.service
sudo systemctl daemon-reload && sudo service docker restart
ps -ef | grep docker

1
2
3
4
5
6
#!/bin/bash

sudo cp -n /lib/systemd/system/docker.service /lib/systemd/system/docker.service
sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=https://xxxxxxxx.mirror.aliyuncs.com|g" /lib/systemd/system/docker.service
sudo systemctl daemon-reload && sudo service docker restart
ps -ef | grep docker

不同Docker版本,配置镜像加速要替换的关键字不同,需要注意。

配置registry仓库

1
2
3
4
5
config.vm.provision "shell",
    inline: <<-EOS
      echo 'DOCKER_OPTS="--insecure-registry 10.14.6.19:5000 ${DOCKER_OPTS}"' \
        >> /etc/default/docker
  EOS

完整示例

示例一

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Vagrant.configure("2") do |config|
    (1..3).each do |i|
        config.vm.define "node#{i}" do |node|
        # 设置虚拟机的Box
        node.vm.box = "ubuntu/trusty64"
        # 设置虚拟机的主机名
        node.vm.hostname="node#{i}"
        # 设置虚拟机的IP
        node.vm.network "private_network", ip: "192.168.59.#{i}"
        # 设置主机与虚拟机的共享目录
        node.vm.synced_folder "~/Desktop/share", "/home/vagrant/share"
        # VirtaulBox相关配置
        node.vm.provider "virtualbox" do |v|
            # 设置虚拟机的名称
            v.name = "node#{i}"
            # 设置虚拟机的内存大小  
            v.memory = 2048
            # 设置虚拟机的CPU个数
            v.cpus = 1
        end
  
        # 使用shell脚本进行软件安装和配置
        node.vm.provision "shell", inline: <<-SHELL
            # 安装docker 1.11.0
            wget -qO- https://get.docker.com/ | sed 's/docker-engine/docker-engine=1.11.0-0~trusty/' | sh
            usermod -aG docker vagrant
            
        SHELL
        end
    end
end

示例二

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- 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|
  config.vm.define :node1 do |config|
    config.vm.box = "centos/7"
    config.vm.hostname = "node1"
    # VirtaulBox相关配置
        config.vm.provider "virtualbox" do |v|
            # 设置虚拟机的名称
            v.name = "node1"
            # 设置虚拟机的内存大小  
            v.memory = 2048
            # 设置虚拟机的CPU个数
            v.cpus = 1
        end
    config.vm.network "public_network", auto_config: false, bridge: "en0: Wi-Fi (AirPort)"
    config.vm.provision "shell",
        run: "always",
        inline: "ifconfig enp0s8 192.168.100.11 netmask 255.255.0.0 up"
    config.vm.provision "shell",
        run: "always",
        inline: "route add default gw 192.168.100.1"
    config.vm.provision "shell",
        run: "always",
        inline: "eval `route -n | awk '{ if ($8 == \"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
    config.ssh.username="root"
    config.ssh.password="vagrant"
  end

  config.vm.define :node2 do |config|
    config.vm.box = "centos/7"
    config.vm.hostname = "node2"
    config.vm.provider "virtualbox" do |v|
            # 设置虚拟机的名称
            v.name = "node2"
            # 设置虚拟机的内存大小  
            v.memory = 2048
            # 设置虚拟机的CPU个数
            v.cpus = 1
        end
    config.vm.network "public_network", auto_config: false, bridge: "en0: Wi-Fi (AirPort)"
    config.vm.provision "shell",
        run: "always",
        inline: "ifconfig enp0s8 192.168.100.12 netmask 255.255.0.0 up"
    config.vm.provision "shell",
        run: "always",
        inline: "route add default gw 192.168.100.1"
    config.vm.provision "shell",
        run: "always",
        inline: "eval `route -n | awk '{ if ($8 == \"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
    config.ssh.username="root"
    config.ssh.password="vagrant"
  end

  config.vm.define :node3 do |config|
    config.vm.box = "centos/7"
    config.vm.hostname = "node3"
    config.vm.provider "virtualbox" do |v|
            # 设置虚拟机的名称
            v.name = "node3"
            # 设置虚拟机的内存大小  
            v.memory = 2048
            # 设置虚拟机的CPU个数
            v.cpus = 1
        end
    config.vm.network "public_network", auto_config: false, bridge: "en0: Wi-Fi (AirPort)"
    config.vm.provision "shell",
        run: "always",
        inline: "ifconfig enp0s8 192.168.100.13 netmask 255.255.0.0 up"
    config.vm.provision "shell",
        run: "always",
        inline: "route add default gw 192.168.100.1"
    config.vm.provision "shell",
        run: "always",
        inline: "eval `route -n | awk '{ if ($8 == \"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
    config.ssh.username="root"
    config.ssh.password="vagrant"
  end
end

配置启动脚本

方法一:手动控制脚本的启动

1
config.vm.provision :shell, :path => "xxx.sh"
1
> vagrant up node1 ----provision

方法二:脚本自动启动

1
2
3
config.vm.provision "shell", run: "always" do |s|
   s.path = 'xxx'
end

Windows下修改 Vagrant Box 加载目录

背景

在Vagrant中添加box时,加载目录默认在 ~/.vagrant.d/,具体的目录结构是C:\Users\Your Username.vagrant.d,由于是在C盘中,在一些配置不高的机子中用起Vagrant,磁盘容量会显得捉襟见肘,这时会考虑更改加载box的目录。

修改 Vagrant Box 加载目录

永久设置用户的环境变量

1
> setx VAGRANT_HOME "/your/path"

永久设置系统的环境变量

1
> setx VAGRANT_HOME "/your/path" /M

临时设置环境变量

1
> set VAGRANT_HOME="/your/path"

配置VPN路由

1
2
3
config.vm.provision "shell",
    run: "always",
    inline: "ip route add 222.128.108.50/32 via 172.18.30.254 metric 1"

基于 CentOS 创建 Vagrant 基础 box

环境说明

制作CentOS7的基础Box。

操作步骤

配置

安装常用软件

1
> yum -y install vim wget net-tools lrzsz lsof unzip git bash-completion

修改hostname

1
2
> hostnamectl vagrant
> reboot

将CentOS的yum源更换为国内的阿里源

参考 将CentOS的yum源更换为国内的阿里源

配置Root密码

1
> passwd

配置无密码执行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
> visudo

> chmod 0777 /etc/sudoers
> vim /etc/sudoers
##### 在root下面增加一行 #####
vagrant ALL=(ALL) NOPASSWD:ALL

# User privilege specification
root ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) NOPASSWD:ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

vagrant ALL=(ALL) NOPASSWD:ALL
Defaults:vagrant !requiretty
##### 内容结束 #####
> chmod 0440 /etc/sudoers
> su - vagrant
> sudo ls

配置SSH

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
> yum list installed | grep openssh-server
> yum install openssh-server
> vim /etc/ssh/sshd_config
##### 配置如下内容 #####
Port 22
ListenAddress 0.0.0.0
ListenAddress ::
PermitRootLogin yes
PasswordAuthentication yes
UseDNS no
##### 内容结束 #####
> systemctl restart sshd

> cd /home/vagrant
> mkdir .ssh
> wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
### 如果上面链接失效,可以用下面的
> wget https://github.com/hashicorp/vagrant/tree/master/keys/vagrant.pub -O .ssh/authorized_keys
> chmod 700 .ssh
> chmod 600 .ssh/authorized_keys
> chown -R vagrant:vagrant .ssh

关闭防火墙

1
2
> systemctl stop firewalld.service
> systemctl disable firewalld.service

禁用Selinux

参考 CentOS7中关闭selinux

升级内核

参考 升级内核

禁用虚拟内存

1
2
> swapoff -a
> sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

安装增强功能

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 原始做法
> yum install -y bzip2 gcc gcc-devel gcc-c++ gcc-c++-devel make kernel-d
> mkdir /mnt/cdrom
> mount -t auto -r /dev/cdrom /mnt/cdrom
> cd /mnt/cdrom
> ./VBoxLinuxAdditions.run

> yum update
> yum groupinstall "Development Tools"
> yum install -y gcc make perl kernel-devel
> yum -y install xorg-x11-drivers xorg-x11-utils
> reboot
> mkdir /mnt/cdrom
> mount -t auto -r /dev/cdrom /mnt/cdrom
> cd /mnt/cdrom
> ./VBoxLinuxAdditions.run
# 查看服务是否启动
> systemctl status vboxadd-service.service

# 配置环境变量
> vim /etc/profile
###### 尾部增加如下 ###### 
KERN_DIR=/usr/src/kernels/3.10.0-1062.1.1.el7.x86_64
export KERN_DIR
##### 内容结束 ##### 
> source /etc/profile

# 官方建议的命令
> yum update
> yum install gcc make perl kernel-devel

> yum groupinstall "Development Tools"
> yum install -y bzip2 gcc gcc-devel gcc-c++ gcc-c++-devel make kernel-devel
> yum -y install xorg-x11-drivers xorg-x11-utils
> reboot

> yum install -y bzip2 gcc gcc-devel gcc-c++ gcc-c++-devel make kernel-devel
> yum install -y libXt
> yum install -y libXmu

> yum install -y epel-release
> yum -y install xorg-x11-drivers xorg-x11-utils
> yum install -y libXt.{i686,x86_64}
> yum install -y dkms binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers
> yum install -y kernel-devel

> yum update
> yum install kernel-headers
> yum install kernel-devel
> yum install gcc* 
> yum install make
> yum install bzip2
> reboot

> yum -y update
> yum -y install g++ gcc gcc-c++ make kernel-*
> yum -y install bzip2*
> yum install virtualbox-guest-dkms
> yum install build-essential module-assistant
> yum install dkms build-essential linux-headers-$(uname -r)
> yum install linux-generic

> yum update
> yum install gcc kernel-devel kernel-headers dkms make bzip2 
> reboot

# 【VirtualBox】->【设备】->【安装增强功能】
> mkdir /mnt/cdrom
> mount -t auto -r /dev/cdrom /mnt/cdrom 
> cd /mnt/cdrom
> ./VBoxLinuxAdditions.run
# 避免出现 xorg 警告提醒
> ./VBoxLinuxAdditions.run --nox11

清理

1
2
3
4
5
6
> yum clean all
> rm -rf /var/cache/yum 
> rm -rf /tmp/*
> rm -f /var/log/wtmp /var/log/btmp
> history -c
> shutdown -h now

导出box

1
2
3
4
5
6
7
8
9
# 查看虚拟机名称
> VBoxManage list vms

# 导出box
```bash
> vagrant package --base <虚拟机名称> --output BoxName.box

# 举例
> vagrant package --base CentOS7 --output CentOS7.4.box

基于 Ubuntu18.04 创建 Vagrant 基础 box

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
### 配置阿里云源
> vim /etc/apt/sources.list
##### 增加如下内容 #####
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
##### 内容结束 #####
> sudo apt-get update
> sudo apt-get upgrade

### 切换到 root用户
> sudo su -

### 配置 sudo 免密
> vim /etc/sudoers
##### 最后一行增加如下内容 #####
%sudo   ALL=(ALL:ALL) ALL
vagrant ALL=(ALL:ALL) NOPASSWD:ALL
##### 内容结束 #####

### 测试 sudo 免密
> su - vagrant
> sudo ls

### 配置 root 密码 为 vagrant
> passwd

### 配置ssh
> cd /home/vagrant
> mkdir .ssh
> wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
### 如果上面链接失效,可以用下面的
> wget https://github.com/hashicorp/vagrant/tree/master/keys/vagrant.pub -O .ssh/authorized_keys
> chmod 700 .ssh
> chmod 600 .ssh/authorized_keys
> chown -R vagrant:vagrant .ssh
> vim /etc/ssh/sshd_config
##### 添加如下内容 #####
PasswordAuthentication yes
PermitRootLogin yes
UseDNS no
##### 内容结束 #####
> service ssh restart

### 安装客户端增强
> sudo apt-get install linux-headers-generic build-essential dkms -y
# 【VirtualBox】->【设备】->【安装增强功能】
> mkdir /media/cdrom
> sudo mount /dev/cdrom /media/cdrom
> cd /media/cdrom
> sudo ./VBoxLinuxAdditions.run
> reboot
> shutdown now

### 查看虚拟机名称
> VBoxManage list vms

### 导出box
```bash
> vagrant package --base <虚拟机名称> --output BoxName.box

VirtualBox

开启嵌套虚拟化

1
2
3
4
##### 列出Virtualbox列表
> VBoxManage list vms
##### 开启虚拟化
> VBoxManage "ZStack-dev" --nested-hw-virt on

常见错误

Implementation of the USB 2.0 controller not found

问题描述

1
2
3
4
5
Command: ["startvm", "cc1c60dc-c946-45a0-9c75-7f6a6533c66a", "--type", "headless"]
Stderr: VBoxManage.exe: error: Implementation of the USB 2.0 controller not found!
VBoxManage.exe: error: Because the USB 2.0 controller state is part of the saved VM state, the VM cannot be started. To fix this problem, either install the 'Oracle VM VirtualBox Extension Pack' or disable USB 2.0 support in the VM settings.
VBoxManage.exe: error: Note! This error could also mean that an incompatible version of the 'Oracle VM VirtualBox Extension Pack' is installed (VERR_NOT_FOUND)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

解决办法

安装Virtualbox Extension Pack。
下载地址:http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html#extpack

unable to mount VirtualBox shared folders

问题描述

1
2
3
4
5
6
7
8
9
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 vagrant /vagrant
The error output from the command was:
/sbin/mount.vboxsf: mounting failed with the error: No such device

解决办法

1
> vagrant plugin install vagrant-vbguest

重启后ip变了

1
2
3
4
### 错误的方式,这样重启以后,节点的ip会变的不确定
> reboot
### 正确的重启方式
> vagrant reload node1

Insecure world writable dir /usr/local in PATH, mode 040777

问题描述

1
/opt/vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /usr/local in PATH, mode 040777

解决办法

1
> chmod 755 /usr/local

Network is unreachable 错误

问题描述

1
2
3
4
5
node3: SIOCADDRT: Network is unreachable
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.

Kernel headers not found for target kernel

问题描述

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.0.14 Guest Additions for Linux........
VirtualBox Guest Additions installer
Removing installed version 6.0.14 of VirtualBox Guest Additions...
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: Kernel headers not found for target kernel
3.10.0-693.el7.x86_64. Please install them and execute
  /sbin/rcvboxadd setup
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.

解决办法

1
2
3
> yum update kernel -y
> yum install kernel-headers kernel-devel gcc make -y
> reboot

参考


Wanglibing
Wanglibing
Engineer,Lifelong learner