环境:

  • Mac OSX: 10.9.5
  • VirtualBox: 4.3.24
  • Vagrant: 1.7.2

常用命令

初始化和启动

mkdir PROJECT_FOLDER
cd PROJECT_FOLDER
vagrant init hashicorp/precise64
vagrant up

修改Vagrantfile 文件后重启

vagrant reload

关闭虚拟机

vagrant halt

删除虚拟机

vagrant destroy

创建私有的box

首先创建一个VirtualBox的虚拟机,配置如下

  • Name: precise64
  • Type: Linux
  • Version: Ubuntu64
  • Memory Size: 512MB (to taste)
  • New Virtual Disk: [Type: VMDK, Dynamically allocated, Name: box-disk.vmdk, Size: 80 GB]

创建完后还要修改一下配置

  • Video Memory: 8MB
  • CPU 2
  • Pointing Device: PS/2 Mouse
  • Disable audio
  • Disable USB
  • Ensure Network Adapter 1 is set to NAT
  • port-forwarding rule: [Name: ssh, Protocol: TCP, Host IP: blank, Host Port: 2222, Guest IP: blank, Guest Port: 22]

虚拟光驱加载ubuntu的iso镜像,然后启动虚拟机。安装的过程需要创建初始化用户, 此用户的用户名和密码都是vagrant。

使用root用户依次运行以下文件

将配置好的虚拟机打包成box文件

mkdir ~/box
cd ~/box
vagrant package --base "VM NAME"
mv package.box rocky-precise64.box

测试该box是否成功

vagrant box add rocky-precise64 ~/box/rocky-precise64.box
vagrant init rocky-precise64
vagrant up

参考:

  • http://docs.vagrantup.com/v2/boxes/base.html
  • https://blog.engineyard.com/2014/building-a-vagrant-box
  • http://vmassuchetto.github.io/2013/08/14/reducing-a-vagrant-box-size/