Skip to content

创建适用于 Virtualbox 的配置文件

此配置文件适用于创建 Virtualbox Base Box


开始

json
{

"_comment": "Basic templates for packer,Build with `packer build -var-file variables-centos7.9.json centos.json`",

"builders": [

{

	"nested_virt": true,

	"rtc_time_base": "local",

	"gfx_controller": "vmsvga",

	"gfx_vram_size": "16",

	"gfx_accelerate_3d": true,

	"type": "virtualbox-iso",

	"guest_os_type": "RedHat_64",

	"hard_drive_interface": "sata",

	"vm_name": "{{user `default_vm_name`}}",

	"virtualbox_version_file": ".vbox_version",

	"guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",

	"guest_additions_url": "{{user `guest_additions_url`}}",

	"iso_checksum": "{{user `iso_checksum`}}",

	"iso_url": "{{user `mirror`}}/{{user `mirror_directory`}}/{{user `iso_name`}}",

	"http_directory": "{{user `http_directory`}}",

	"output_directory": "{{user `build_directory`}}/packer-{{user `default_vm_name`}}-virtualbox",

	"headless": "{{user `headless`}}",

	"shutdown_command": "sudo /sbin/halt -h -p",

	"cpus": "{{user `cpus`}}",

	"memory": "{{user `memory`}}",

	"disk_size": "{{user `disk_size`}}",

	"ssh_port": 22,

	"ssh_timeout": "30m",

	"ssh_username": "{{user `ssh_username`}}",

	"ssh_password": "{{user `ssh_password`}}",

	"boot_command": [

	"<up><wait><tab> text ks=http://{{.HTTPIP}}:{{.HTTPPort}}/{{user `ks_path`}} PACKER_AUTHORIZED_KEY=http://{{.HTTPIP}}:{{.HTTPPort}}/{{user `ssh_pubkey`}} <enter><wait>"

	],

	"boot_wait": "5s",

	"vboxmanage": [

	[

	"modifyvm",

	"{{.Name}}",

	"--vrde",

	"off"

	],

	[

	"modifyvm",

	"{{.Name}}",

	"--recording",

	"off"

	],

	[

	"modifyvm",

	"{{.Name}}",

	"--clipboard",

	"bidirectional"

	],

	[

	"modifyvm",

	"{{.Name}}",

	"--draganddrop",

	"bidirectional"

	],

	[

	"storagectl",

	"{{.Name}}",

	"--name",

	"SATA Controller",

	"--hostiocache",

	"on"

	],

	[

		"storageattach",

		"{{.Name}}",

		"--storagectl",

		"SATA Controller",

		"--port",

		"0",

		"--nonrotational",

		"on"

	]

	]

	}

],

	"sensitive-variables": [

		"ssh_password",

		"ssh_pubkey"

	],

	"provisioners": [

		{

		"execute_command": "{{.Vars}} sudo -E sh -eux '{{.Path}}'",

		"expect_disconnect": true,

		"scripts": [

		"{{pwd}}/scripts/base.sh",

		"{{pwd}}/scripts/vboxguest.sh",

		"{{pwd}}/scripts/cleanall.sh"

		],

		"type": "shell"

		}

	],

	"post-processors": [

		{

		"type": "vagrant",

		"output": "{{user `build_directory`}}/{{user `box_basename`}}.{{.Provider}}.box",

		"vagrantfile_template": "{{pwd}}/../_common/for_packer/Vagrantfile",

		"include": "{{pwd}}/../_common/for_packer/metadata.json"

		}

	]

}

下面是配置文件参数详细介绍:

bash
- nested_virt(boolean) - 嵌套虚拟化:false true。设置为 true 时,启用嵌套虚拟化 (VT-x/AMD-V)。当设置为 false 时,嵌套虚拟化被禁用。这是默认设置。
- rtc_time_base(string) - RTC 时基:UTC 或本地。设置为 true 时,RTC 设置为 UTC 时间。设置为 false 时,RTC 设置为本地时间。这是默认设置。
- disk_size(uint) - VM 创建的硬盘大小(以兆字节为单位)。默认情况下,这是 40000(大约 40 GB)。
- gfx_controller(string) - 要使用的图形控制器类型。当设置为 vboxvga 时,图形控制器是 VirtualBox VGA。这是默认设置。当设置为 vboxsvga 时,图形控制器是 VirtualBox SVGA。设置为 vmsvga 时,图形控制器为 VMware SVGA。当设置为 none 时,图形控制器被禁用。
- gfx_vram_size(uint) - 要使用的 VRAM 大小。默认情况下,这是 4 MiB。
- gfx_accelerate_3d(bool) - 3D 加速度:true false。设置为 true 时,启用 3D 加速。设置为 false 时,将禁用 3D 加速。这是默认设置。
- vm_name(string) - 这是新虚拟机的 OVF 文件的名称,不带文件扩展名。默认情况下,这是 packer-BUILDNAME,其中“BUILDNAME”是构建的名称。
- virtualbox_version_file( * string) - 虚拟机中用于上传包含用于创建虚拟机的 VirtualBox 版本的文件的路径。此信息可用于配置。默认情况下,这是 .vbox_version,通常会将其上传到主目录。设置为空字符串以跳过上传此文件,这在使用 none 通信器时很有用。
- guest_additions_mode(string) - 来宾添加可供来宾安装的方法。有效的选项为upload, attach或disable。如果模式是attach来宾添加 ISO 将作为 CD 设备附加到虚拟机。如果模式是upload 来宾添加 ISO 将上传到指定的路径 guest_additions_path。默认值为upload。如果disable使用,也不会下载来宾添加。
- guest_additions_path(string) - 来宾虚拟机上的路径,VirtualBox 来宾添加 ISO 将上传到该路径。默认情况下,这VBoxGuestAdditions.iso应该上传到用户的登录目录中。这是一个配置模板,其中Version 变量替换为 VirtualBox 版本。
- guest_additions_sha256(字符串)- 将上传到来宾 VM 的来宾添加 ISO SHA256 校验和。默认情况下,校验和将从 VirtualBox 网站下载,因此仅当您想明确校验和时才需要设置。
- guest_additions_url(string) - 要上传的访客添加 ISO URL。如果 ISO 位于本地路径,这也可以是文件 URL。默认情况下,VirtualBox 构建器将尝试在本地文件系统上查找来宾添加 ISO。如果本地不可用,构建器将从 Internet 下载适当的来宾添加 ISO。
- sound(string) - 默认为无。构建 VM 时用于声音的音频设备类型。一些可用的选项是 dsound、oss、alsa、pulse、coreaudio、null。
- usb(bool) - 指定在构建 VM 时是否启用 USB 总线。默认为假。
- headless(bool) - Packer 默认通过启动显示正在构建的机器的控制台的 GUI 来构建 VirtualBox 虚拟机。当此值设置为 true 时,机器将在没有控制台的情况下启动。
- "guest_additions_mode": "",  sound  usb 使用默认值即可,不用显示指定
- iso_checksum  iso_url 会将 centos.iso 下载到当前目录下的 packer_cache
- http_directory(string) - 使用 HTTP 服务器提供服务的目录路径。此目录中的文件可通过 HTTP 访问,并可从虚拟机请求。这对于托管 kickstart 文件等很有用。默认情况下,这是一个空字符串,这意味着不会启动 HTTP 服务器。
- output_directory(string) - 这是将创建生成的虚拟机的目录的路径。这可能是相对的或绝对的。如果是相对的,路径是相对于执行packer时的工作目录。在运行构建器之前,此目录不得存在或为空。默认情况下,这是 output-BUILDNAME,其中“BUILDNAME”是构建的名称。
- headless(bool) - Packer 默认通过启动显示正在构建的机器的控制台的 GUI 来构建 VirtualBox 虚拟机。当此值设置为 true 时,机器将在没有控制台的情况下启动。
- shutdown_command(string) - 用于在所有配置完成后正常关闭机器的命令。默认情况下,这是一个空字符串,它告诉 Packer 强行关闭机器,除非在脚本中发生关闭命令,因此可以安全地省略它。如果一个或多个脚本需要重新启动,建议将此留空,因为重新启动可能会失败并在最后一个脚本中指定最后的关闭命令。
- boot_command([]string) - 这是第一次启动虚拟机时要键入的命令数组。这些命令的目标应该是键入足以初始化操作系统安装程序的内容。也可以键入特殊键,下面有关引导命令的部分将介绍这些键。如果未指定,则假定安装程序将自行启动。
- 当指定了 ssh_private_key_file 需通过 SSHPublicKey 模板引擎将当前 SSH 密钥对的公钥注入到模板中。
- {{ .HTTPIP }} {{ .HTTPPort }}- 分别为http_directory配置参数指定的目录提供服务的 HTTP 服务器的 IP 和端口 。如果http_directory未指定,这些将是空白的!
- {{ .Name }} - 虚拟机的名称。启动的 HTTP 服务器 Packer IP 地址 ( ) 硬编码为 10.0.2.2。