vagrant

# install Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# install vagrant、packer

brew cask install vagrant
brew install packer

vagrant -v
Vagrant 2.1.2

packer -v
1.2.4

box

packer-build

mkdir -p /data/box/ubuntu/iso;
cd /data/box/ubuntu/iso;

wget http://mirrors.163.com/ubuntu-releases/18.04/ubuntu-18.04-live-server-amd64.iso
git clone https://github.com/boxcutter/ubuntu

cd /data/box/ubuntu/ubuntu;
cp ubuntu1804.json ubuntu1804.json.back

vi ubuntu1804.json;
{
  "_comment": "Build with `packer build -var-file=ubuntu1804.json ubuntu.json`",
  "vm_name": "ubuntu1804",
  "cpus": "2",
  "disk_size": "65536",
  "iso_checksum": "7a1c2966f82268c14560386fbc467d58c3fbd2793f3b1f657baee609b80d39a8",
  "iso_checksum_type": "sha256",
  "iso_name": "ubuntu-18.04-server-amd64.iso",
  "iso_url": "http://mirrors.163.com/ubuntu-releases/18.04/ubuntu-18.04-live-server-amd64.iso",
  "memory": "2048",
  "preseed" : "preseed.cfg",
  "boot_command_prefix": "<esc><esc><enter><wait>"
}

packer build -force -var-file=ubuntu1804.json -only=virtualbox-iso ubuntu.json
进入手工操作:选择语言、网络、proxy、分区、配置用户名和密码,注意用户名和密码要和ubuntu.json中的ssh_ssh_username和ssh_password保持一致,也可以通过ubuntu1804.json覆盖(默认的ssh用户名和密码都是vagrant
),重启

最后输出
virtualbox-iso: ==> Clearing last login information
等待时间较长
==> virtualbox-iso: Gracefully halting virtual machine...
等待时间较长
    virtualbox-iso: Removing floppy drive...
==> virtualbox-iso: Preparing to export machine...
    virtualbox-iso: Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port 2656)
==> virtualbox-iso: Exporting virtual machine...
    virtualbox-iso: Executing: export ubuntu1804 --output output-ubuntu1804-virtualbox-iso/ubuntu1804.ovf
==> virtualbox-iso: Deregistering and deleting VM...
==> virtualbox-iso: Running post-processor: vagrant
==> virtualbox-iso (vagrant): Creating Vagrant box for 'virtualbox' provider
    virtualbox-iso (vagrant): Copying from artifact: output-ubuntu1804-virtualbox-iso/ubuntu1804-disk001.vmdk
    virtualbox-iso (vagrant): Copying from artifact: output-ubuntu1804-virtualbox-iso/ubuntu1804.ovf
    virtualbox-iso (vagrant): Renaming the OVF to box.ovf...
    virtualbox-iso (vagrant): Compressing: Vagrantfile
    virtualbox-iso (vagrant): Compressing: box.ovf
    virtualbox-iso (vagrant): Compressing: metadata.json
    virtualbox-iso (vagrant): Compressing: ubuntu1804-disk001.vmdk
Build 'virtualbox-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> virtualbox-iso: 'virtualbox' provider box: box/virtualbox/ubuntu1804-0.1.0.box

vagrant-package

vboxmanage list vms
# 从vbox的某个实例导出.box文件
vagrant package –-base newbox_default_1503366286622_12977 –-output ./CentOS7.box

vbox


# 创建工作目录

mkdir ~/Desktop/vm

# 进入工作目录

cd ~/Desktop/vm

# 增加box

vagrant box add my /data/box/ubuntu/ios/ubuntu/box/virtualbox/ubuntu1804-0.1.0.box
vagrant box list
# 初始化虚拟机

vagrant init my;

# 启动虚拟机
vagrant up;

# 登录虚拟机

打开 ~/Desktop/vm/Vagrantfile

## 虚拟机配置私有网络
config.vm.network "private_network", ip: "192.168.33.10"
## 虚拟机挂载宿主机磁盘
config.vm.synced_folder "/data", "/vagrant_data"

## 配置端口转发(guest是虚拟机host为宿主机,增加一个端口转发追加:forwarded_port一行)
config.vm.network "forwarded_port", guest: 3306, host: 3306,host_ip: "0.0.0.0"
config.vm.network :forwarded_port, guest: 3306, host: 3307,host_ip: "0.0.0.0"

//重启
vagrant reload

//ssh登录

ssh vagrant@127.0.0.1 -p 2222

vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  config.vm.box = "centos7_copy"
  config.ssh.keep_alive = true
  config.ssh.compression = true
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  config.ssh.insert_key = false
  config.ssh.forward_agent = false
  [11,12,13].each do |i|
    config.vm.define "n#{i}" do |node|
      node.vm.provider "virtualbox" do |vb|
          vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
          vb.customize ["modifyvm", :id, "--vram","10"]
          vb.customize ["storageattach",:id,"--storagectl", "IDE Controller","--port","0","--device","0","--type","dvddrive","--medium","/usr/share/virtualbox/VBoxGuestAdditions.iso"]
          vb.memory = "2048"
          vb.cpus = 2
      end
      node.vm.network "private_network", ip: "192.168.33.#{i}", auto_config: true
      node.vm.synced_folder "/data/vagrant/shell", "/shell"
      node.vm.network :forwarded_port, guest: 22, host: "2#{i}22", host_ip: "0.0.0.0", auto_correct:true
      #node.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: "true"
      node.vm.provision "shell", inline: <<-SHELL
        echo "vagrant:vagrant" | sudo chpasswd
        mkdir -p /data
        echo IP=\"192.168.33.#{i}\" >>/data/env.sh
        echo ETCD_IP=\"192.168.33.#{i}\" >>/data/env.sh
        echo ETCD_NAME=\"etcd-#{i}\" >>/data/env.sh
        echo KUBE_NODENAME=\"n#{i}\" >>/data/env.sh
        echo KUBE_NODE_HOSTNAME=\"n#{i}.dev\" >>/data/env.sh
        echo HOSTNAME_=\"n#{i}\" >>/data/env.sh
        hostnamectl set-hostname n#{i}
        timedatectl set-timezone Asia/Shanghai
        mkdir -p /mnt/cd
        echo "sudo mount -o ro /dev/cdrom /mnt/cd/ && cd /mnt/cd && sudo ./VBoxLinuxAdditions.run" >> /data/util.sh
      SHELL
    end
  end
end

cat /var/log/vboxadd-setup.log
Building the main Guest Additions module for kernel 5.8.3-1.el7.elrepo.x86_64.
Error building the module.  Build output follows.
make V=1 CONFIG_MODULE_SIG= -C /lib/modules/5.8.3-1.el7.elrepo.x86_64/build M=/tmp/vbox.0 SRCROOT=/tmp/vbox.0 -j2 modules
make[1]: warning: -jN forced in submake: disabling jobserver mode.
make -C /usr/src/kernels/5.8.3-1.el7.elrepo.x86_64 -f /usr/src/kernels/5.8.3-1.el7.elrepo.x86_64/Makefile modules
test -e include/generated/autoconf.h -a -e include/config/auto.conf || (		\
echo >&2;							\
echo >&2 "  ERROR: Kernel configuration is invalid.";		\
echo >&2 "         include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 "         Run 'make oldconfig && make prepare' on kernel src to fix it.";	\
echo >&2 ;							\
/bin/false)

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.

make[2]: *** [include/config/auto.conf] Error 1
make[1]: *** [__sub-make] Error 2
make: *** [vboxguest] Error 2
modprobe vboxguest failed




sudo ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.12 Guest Additions for Linux........
VirtualBox Guest Additions installer
Removing installed version 5.2.32 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: Building the modules for kernel
5.8.3-1.el7.elrepo.x86_64.

VirtualBox Guest Additions: Look at /var/log/vboxadd-setup.log to find out what
went wrong
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.




Building the main Guest Additions 6.1.12 module for kernel 5.8.3-1.el7.elrepo.x86_64.
Error building the module.  Build output follows.
make V=1 CONFIG_MODULE_SIG= CONFIG_MODULE_SIG_ALL= -C /lib/modules/5.8.3-1.el7.elrepo.x86_64/build M=/tmp/vbox.0 SRCROOT=/tmp/vbox.0 -j2 modules
make[1]: warning: -jN forced in submake: disabling jobserver mode.
make -C /usr/src/kernels/5.8.3-1.el7.elrepo.x86_64 -f /usr/src/kernels/5.8.3-1.el7.elrepo.x86_64/Makefile modules
test -e include/generated/autoconf.h -a -e include/config/auto.conf || (		\
echo >&2;							\
echo >&2 "  ERROR: Kernel configuration is invalid.";		\
echo >&2 "         include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 "         Run 'make oldconfig && make prepare' on kernel src to fix it.";	\
echo >&2 ;							\
/bin/false)

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.

make[2]: *** [include/config/auto.conf] Error 1
make[1]: *** [__sub-make] Error 2
make: *** [vboxguest] Error 2
Could not find the X.Org or XFree86 Window System, skipping.
modprobe vboxguest failed

以上错误内核太新了,回滚到4.4可以安装了
### 

Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.12 Guest Additions for Linux........
VirtualBox Guest Additions installer
Removing installed version 6.1.12 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: Building the modules for kernel
4.4.233-1.el7.elrepo.x86_64.
VirtualBox Guest Additions: Running kernel modules will not be replaced until
the system is restarted

ref