vagrant(6)package基于当前虚机打包box

目的

当前虚机做了优化,想基于此虚机打包xx.box当作基础镜像

1.检查vagrant默认insecure_private_key

ll ~/.vagrant.d/insecure_private_key

➜  .vagrant.d ll ~/.vagrant.d/insecure_private_keys 
total 32
-rw-------  1 mvpbang  staff   411  4  2  2024 vagrant.key.ed25519
-rw-r--r--  1 mvpbang  staff    98  3  5 15:58 vagrant.key.ed25519.pub
-rw-------  1 mvpbang  staff  1675  4  2  2024 vagrant.key.rsa
-rw-r--r--  1 mvpbang  staff   381  3  5 15:58 vagrant.key.rsa.pub

默认情况~/.vagrant.d/insecure_private_keys只有私钥,需要人工生成对应公钥(添加到需要打包镜像虚机中)

ssh-keygen -y -f ~/.vagrant.d/insecure_private_keys/vagrant.key.rsa > ~/.vagrant.d/insecure_private_keys/vagrant.key.rsa.pub

ssh-keygen -y -f ~/.vagrant.d/insecure_private_keys/vagrant.key.ed25519 > ~/.vagrant.d/insecure_private_keys/vagrant.key.ed25519.pub

经过测试,默认~/.vagrant.d/insecure_private_key 是~/.vagrant.d/insecure_private_keys/vagrant.key.rsa

2.添加默认公钥到虚机内

vagrant.key.rsa.pub

/home/vagrant/.ssh/authorized_key
xxxxx  vagrant secure public key

添加到第一行

default insecure public key.png

3.检查第一块网卡nat类型

first interface nat eth0.png

4.关机打包box

4.1关机
vagrant halt

4.2获取实例名字
VBoxManage list vms

4.3package
vagrant package --base vgt-c7-101

5.导入box

vagrant box add centos7.o ./package.box

6.验证新的box

6.1Vagrantfile

Vagrant.configure("2") do |config|
    config.vm.box = "centos7.o"
    config.vm.hostname = "vgt-c7-102"
    config.vm.network "private_network", ip: "172.24.20.102", hostname: true
    config.vm.synced_folder ".", "/vagrant", disabled: true
    config.vm.provider "virtualbox" do |vb|
      vb.memory = 2048
      vb.cpus   = 2
      vb.name   = "vgt-c7-102"
    end
end

6.2up

vagrant up

启动日志

➜  2 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7.o'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vgt-c7-102
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default:
### 自动删除不安全的默认key,重新生成新的key pair
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...

login keyword info.png