用packer做vagrant的box

用packer做vagrant的box

我要被vagrant的龟速逼疯了

从墙外downlaod vagrant box 真的能把人逼疯了, 网上找了篇文章来自制box.比手动安装在加密钥的那种要方便的多,是利用hashicorp 自家的packer来做vagrantbox 还是挺方便的.

参考 使用Packer制作vagrant box

工具版本:

  • Vagrant 1.9.4
  • packer 1.1.0
  • virtualbox 5.0.40

步骤

1.下载packer

wget https://releases.hashicorp.com/packer/1.1.0/packer_1.1.0_linux_amd64.zip?_ga=2.240161283.1617285052.1505571331-1768028837.1490461176 
unzip packer_1.1.0_linux_amd64.zip
cp packer_1.1.0_linux_amd64/packer /bin

2.下载安装virtualbox

    略..

3.下载打包脚本

git clone https://github.com/boxcutter/centos

然后得到一个centos的目录,里面就是要用到的各种脚本了.

4.下载iso

cd centos # 刚刚clone的git库根目录
wget http://101.96.8.164/download.virtualbox.org/virtualbox/5.0.40/VBoxGuestAdditions_5.0.40.iso
wget http://mirrors.163.com/centos/7.3.1611/isos/x86_64/CentOS-7-x86_64-Minimal-1611.iso
# 预先将要用的iso下到刚刚的centos目录中

5.开始打包

修改配置

  • centos7.json
  • custom-script.sh

cd centos
cp centos7.json  centos7.json.bak

修改内容如下: 以163的centos7.3mini为例

{
  "_comment": "Build with `packer build -var-file=centos7.json centos.json`",
  "vm_name": "centos7",
  "cpus": "2",
  "disk_size": "65536",
  "http_directory": "kickstart/centos7",
  "iso_checksum": "71a7aa147877b413497cdff5b1e0aa5bc0c9484f",
  "iso_checksum_type": "sha1",
  "iso_name": "CentOS-7-x86_64-Minimal-1611.iso",
  "iso_url": "http://mirrors.163.com/centos/7.3.1611/isos/x86_64/CentOS-7-x86_64-Minimal-1611.iso",
  "memory": "2048",
  "parallels_guest_os_type": "centos7"
}

修改custom-script.sh
这里是将yum源默认改为163的

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache

开始打包

bin/box build centos7.json virtualbox

这时会自动跳出来virtualbox, 自动ks安装centos, 只需静静等待

看到:

Build 'virtualbox-iso' finished.

ok 制作结束

6. 加入box

打包结果在:
centos/box/virtualbox

vagrant box add centos-7.3-jj box/virtualbox/centos7-17.0909.1.box 
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos-7.3-jj' (v0) for provider: 
    box: Unpacking necessary files from: file:///home/jjmomark/projects/packer-vagrant/centos/centos/box/virtualbox/centos7-17.0909.1.box
==> box: Successfully added box 'centos-7.3-jj' (v0) for 'virtualbox'!
vagrant init centos-7.3-jj
vagrant up 

注意

virtualbox中不能有同名虚机.
如centos7的虚机
如有请先删除