ubuntu18.04安装vagrant记录

ubuntu18.04安装vagrant记录

介绍

在不支持 cpu 虚拟化的 ubuntu 18.04 云主机上安装 vagrant + virtualbox,实现创建虚拟机实例。

安装 virtualbox

参考: https://forums.virtualbox.org/viewtopic.php?t=108215

virtualbox 6 及以上版本开始依赖cpu虚拟化,如 vt-x,所以需要安装 virtualbox 5 版本

virtualbox 5 下载地址:https://www.virtualbox.org/wiki/Download_Old_Builds_5_2

下载安装 virtualbox 5.2.44 deb 包后,右键使用ubuntu软件中心安装。

卸载 virtualbox (ubuntu 软件中心中找不到已安装的virturalbox)

1
sudo apt remove virtualbox virtualbox-*

安装 vagrant

参考:
https://developer.hashicorp.com/vagrant/downloads
https://stackoverflow.com/questions/24620599/error-vt-x-not-available-for-vagrant-machine-inside-virtualbox

下载安装 vagrant

1
2
3
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant

新建工作目录

1
mkdir ~/vagrant-test

执行 vagrant init 命令初始化 VagrantFile,因为需要使用32位系统镜像,所以指定了 jasonc/centos7-32bit

1
vagrant init jasonc/centos7-32bit

jasonc/centos7-32bit 镜像地址 https://app.vagrantup.com/jasonc/boxes/centos7-32bit

执行后,会生成 VagrantFile 文件

修改 VagrantFile,配置 cpu 和 内存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|

  config.vm.box = "jasonc/centos7-32bit"


  config.vm.provider "virtualbox" do |v|
    v.memory = 1024
    v.cpus = 1
  end

end

执行 vagrant up 命令,启动虚拟机实例

1
vagrant up

进入虚拟机实例

1
vagrant ssh

关闭虚拟机实例

1
vagrant halt

销毁虚拟机实例

1
vagrant destroy

查看已下载的镜像(box)

1
vagrant box list

vagrant 镜像查询:https://app.vagrantup.com

错误汇总

  1. 无法启动 centos7
1
2
vagrant init centos/7
vagrant up

出现错误

1
2
3
4
5
6
7
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "328357ce-9204-4c53-bbd1-279427c5350b", "--type", "headless"]

Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

virtual + vagrant 在没有vt-x的环境只能运行1核32位的系统。 https://app.vagrantup.com/jasonc/boxes/centos7-32bit

vagrant 配置cpu和内存 https://talk.ninghao.net/t/vagrant-ru-he-pei-zhi-shi-yong-cpu-he-xin-yu-nei-cun/597/3

  1. 启动vagrant实例时出现错误,找不到具体原因

启动虚拟机实例时超时,实例启动失败

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

找不到原因,是因为看不到虚拟机的启动过程。在Vagrantfile中的最后一个end前面加入:

1
2
3
    config.vm.provider "virtualbox" do |vb|
      vb.gui = true
    end

然后删掉目录下的.vagrant文件,命令行窗口重新运行 vagrant up 命令,这时就可以看到启动过程中到底哪里出了问题了。 我的启动卡在 default: SSH auth method: private key 的原因是 “VT-x/AMD-V 硬件加速在您的系统中不可用”,到BIOS中找到对应选项修改成enabled 就可以了。

参考: https://zhuanlan.zhihu.com/p/49538228
https://blog.csdn.net/m0_50546016/article/details/119176009
https://jimmysong.io/blog/vagrant-intro/

Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计