通过HashiCorp Packer在EBS内创建多个分区

时间:2018-03-06 14:57:50

标签: amazon-web-services amazon-ec2 packer chroot

我正在尝试获取Amazon AMI并将多个分区添加到EBS卷然后生成新的AMI,我应该能够使用这个新生成的AMI启动新的EC2实例。我是通过HashiCorps Packer Tool尝试这个。

这是我期望的输出:

[root@ip-xx-xx-xx-xx ~]# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
xvda                      202:0    0   200G  0 disk
├─xvda1                   202:1    0    30G  0 part /
├─xvda2                   202:2    0 165.0G  0 part
│ └─appsvg-lv_root (dm-0) 253:0    0    15G  0 lvm  
│ └─appsvg-lv_u001 (dm-1) 253:0    0    50G  0 lvm  /u001
│ └─appsvg-lv_u002 (dm-2) 253:0    0    50G  0 lvm  /u002
│ └─appsvg-lv_u002 (dm-3) 253:0    0    50G  0 lvm  /u003
└─xvda3                   202:3    0   5.0G  0 part [SWAP]

我是否需要使用chroot_mount或ami_block_device_mappings(块设备映射数组)或launch_block_device_mappings ...

对此有点困惑......任何支持都会受到赞赏。感谢

2 个答案:

答案 0 :(得分:1)

使用UserData或使用压缩程序shell provisioner配置LVM

答案 1 :(得分:0)

我有完全相同的需求,我可以通过使用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"
 }
 ]
]
}