基于Vagrant快速构建Debian Linux环境

缘由

为啥自定义构建了Debian镜像,而不是用其他人制作的镜像

  1. 默认制作的镜像磁盘都比较小,后面调整比较麻烦(懒😁😁)
  2. 升级了内核
  3. 默认使用了阿里云的镜像源

镜像地址: debian 项目地址: debian-vagrant

构建

构建镜像box

git clone https://github.com/ysicing/debian-vagrant.git
cd debian-vagrant
# 需要提前安装好packer,virtualbox,vagrant工具
./build.sh
  1. 镜像默认会下载到iso目录下
  2. 生成镜像box默认路径是builds目录下

本地运行

# 在项目目录下执行
vagrant up
vagrant ssh

在线运行

vagrant init ysicing/debian
vagrant up
vagrant ssh

后续说明

基础环境:

Distributor ID:	Debian
Description:	Debian GNU/Linux 9.9 (stretch)
Release:	9.9
Codename:	stretch
Kernel: 4.19.0-0.bpo.4-amd64
Disk: 80 GiB

用户:

默认运行root用户登录, root/vagrant

附录 定制的Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box_check_update = false
  config.vm.provider 'virtualbox' do |vb|
   vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 1000 ]
  end
  config.vm.synced_folder ".", "/Maclocal", type: "nfs", nfs_udp: false
  config.vm.synced_folder "/Users/ysicing/Work/Godu/patch-builder", "/code/builder", type: "nfs", nfs_udp: false
  $num_instances = 1
  (1..$num_instances).each do |i|
    config.vm.define "localdev#{i}" do |localdev|
      localdev.vm.box = "ysicing/debian"
      localdev.vm.hostname = "localdev#{i}"
      ip = "172.20.0.#{i+100}"
      # 默认有NAT网络
      # 私有网络 host-only
      localdev.vm.network "private_network", ip: ip 
      # 公有网络 桥接
      localdev.vm.network "public_network"
      localdev.vm.provider "virtualbox" do |vb|
        vb.gui = false
        # 内存
        vb.memory = "6000"
        # cpu
        vb.cpus = 4
        vb.name = "localdev#{i}"
        vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
        vb.customize ["modifyvm", :id, "--ioapic", "on"]
        # cpu 使用率50%
        vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
      end
      localdev.vm.provision "shell", inline: "echo Hello, World"
      #localdev.vm.provision "shell", path: "install.sh", args: [i, ip]
    end
  end
end

推荐阅读