如何制作一个vagrant的base box

虽然这里http://www.vagrantbox.es/已经罗列出了许多个base box,但有些时候,可能根据自身的业务需要一些特殊的box,比如,笔者遇到的,需要一个带桌面环境和浏览器测试用的box,这个在网上可没有找到,无奈之下,只能自己装虚拟机,做一个base box出来,以便使用。本文主要针对ubuntu的base box的制作。

步骤如下:(环境 OS:ubuntu12.04_Desktop  virtualbox:4.1.12)

1.在物理机安装好ubuntu系统,在此系统上安装virtualbox

2.下载一个需要的ubuntu版本的iso镜像

3.创建一个新的虚拟机

  • 给虚拟机起个名字为”vagrant-precise32″
  • 确保您分配足够的磁盘空间,40GB足够了。
  • 确保默认的内存分配也不用太高,建议360M。
  • 如果不需要,禁用音频,USB控制器等。大多数应用程序都不需要播放音乐!因此,节约资源,禁用这些功能。
  • 确保网络控制器设置为NAT,为了端口转发正常工作。

4.启动虚拟机,安装ubuntu,确保配置如下(非强制,如打算分发此box,建议遵循如下约定)

  • Hostname:vagrant-[os-name],例:vagrant-ubuntu-Steve
  • Domain: vagrantup.com
  • Root Password: vagrant
  • Main account login: vagrant
  • Main account password: vagrant

5.安装完毕后,进入虚拟机

6.根据自己需要,安装一些常用软件

sudo apt-get -y install vim

7.创建admin组

sudo groupadd admin

8.添加vagrant用户到admin组

sudo usermod -G admin vagrant

9.修改sudoers文件

sudo vim /etc/sudoers      #添加或修改如下行(有些发行版还需Disable requiretty):

Defaults env_keep=”SSH_AUTH_SOCK”

%admin (ALL)=ALL    NOPASSWD: ALL

10.安装puppet(chef没有计划装进去,如需要可安装ruby,rubygems,chef)

sudo apt-get install puppet puppetmaster

11.安装ssh

sudo apt-get install openssh-server

12.安装vagrant的public keys

mkdir ~/.ssh/
cd ~/.ssh
wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub
mv vagrant.pub authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

也可以点击下面链接本地下载:

vagrant.zip

13.Install virtual box guest additions(为了共享文件夹,端口转发等)

(1) First, build the necessary packages. You may have to look these up for your system, but they should be fairly similar. On Ubuntu and Debian based systems they are as follows:

$ sudo apt-get install linux-headers-$(uname -r) build-essential

(2) Next, make sure to insert the guest additions image by using the GUI and clicking on “Devices” followed by “Install Guest Additions.”. Then run the following to mount the CD Rom:

$ sudo mount /dev/cdrom /media/cdrom

(3) And finally, run the shell script which matches your system. For linux on x86, it is the following:

$ sudo sh /media/cdrom/VBoxLinuxAdditions.run

If you didn’t install a Desktop environment when you installed the operating system, as recommended to reduce size, the install of the VirtualBox additions should warn you about the lack of OpenGL or Window System Drivers, but you can safely ignore this.

(4) 添加共享文件夹

$ sudo mkdir /vagrant

14.清除缓存

sudo apt-get clean

15.关闭虚拟机,进入virtual box directory(~/VirtualBox VM/vagrant-precise32)

vagrant package --output ubuntu_32.box --base vagrant-precise32

总结:以上步骤完成后生成的ubuntu_32.box就可以拿来用了,用vagrant box add ,vagrant init等自己测试看看吧。

 

6 thoughts on “如何制作一个vagrant的base box

  1. 还有最新版的vagrant1.6版本的经过我实际测试发展和之前版本不兼容!暂时还不清楚什么原因!!

Comments are closed.