我有完全相同的需求,我能够通过使用 amazon-import 后处理器来完成它。
因为我需要一个比默认最小安装包更少的安装,加上构建一个包含 /var、/var/log、/var/log/audit 的不同分区的映像,并将 noexec 和 nosuid 等标志应用于我发现找到一种允许我使用 kickstart 文件的方法要容易得多,因为它在安装时支持所有这些选项。
这让我可以像使用 vsphere 或 virtualbox 一样构建图像,并且仍然将最终输出工件作为 AMI。以下打包程序模板将在 vsphere 上完成此操作。你会注意到这个模板中除了 cloud-init 之外没有任何分区信息或包安装,因为它都在我为这个模板编写的 centos7.cfg kickstart 文件中。
一件重要的事情是给你的 ami 命名,让它在名字中包含“centos”,并在你的 kickstart 文件中创建一个名为“centos”的用户帐户。这是因为 EC2 会尝试将您在启动实例时选择的 ssh 密钥复制到您的操作系统类型的默认用户帐户,该类型由与 ami 名称匹配的字符串确定,并且碰巧对于 CentOS 操作系统映像默认为 centos。
{
"builders": [{
"type": "vmware-iso",
"name": "centos7",
"guest_os_type": "centos-64",
"iso_checksum_type": "md5",
"iso_checksum": "5848f2fd31c7acf3811ad88eaca6f4aa",
"iso_url": "http://some-server.com/CentOS-7-x86_64-Minimal-1708.iso",
"ssh_username": "root",
"ssh_password": "password",
"floppy_files": [
"centos7.cfg"
],
"boot_command": [
"<wait><esc><esc>",
"linux inst.ks=hd:fd0:/centos7.cfg net.ifnames=0<enter>"
],
"boot_wait": "5s",
"disk_size": 100000,
"vmx_data": {
"cpuid.coresPerSocket": "1",
"ethernet0.present": "true",
"ethernet0.virtualDev": "vmxnet3",
"ethernet0.startConnected": "true",
"ethernet0.addressType": "generated",
"ethernet0.networkName": "VMWARE VLAN",
"virtualhw.version" : "7",
"config.version": "8",
"scsi0.virtualDev": "lsilogic",
"scsi0.present": "TRUE",
"ide1.0.present": "FALSE",
"memsize": "32768",
"numvcpus": "4",
"smc.present": "TRUE",
"hpet0.present": "TRUE",
"ich7m.present": "TRUE"
},
"remote_type": "esx5",
"remote_port": "22",
"remote_host": your.esxi.host.com",
"remote_username": "esxi_username",
"remote_password": "esxi_password",
"remote_datastore": "abcd-efgh-d1234k-sdfkl34",
"headless": false,
"ssh_wait_timeout": "600s",
"ssh_pty": true,
"shutdown_timeout": "60s",
"format": "ova"
}],
"provisioners": [
{
"type": "shell",
"execute_command": "{{ .Vars }} sudo -E bash {{ .Path }}",
"inline": "yum -y install cloud-init",
}
],
"post-processors" : [
[
{
"type": "amazon-import",
"access_key": "...",
"secret_key": "...",
"region": "us-east-1",
"s3_bucket_name": "mys3bucketname",
"license_type": "BYOL",
"ami_name": "something-with-centos-in-it"
}
]
]
}