github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/website/source/docs/builders/amazon-chroot.html.md (about) 1 --- 2 description: | 3 The `amazon-chroot` Packer builder is able to create Amazon AMIs backed by an 4 EBS volume as the root device. For more information on the difference between 5 instance storage and EBS-backed instances, storage for the root device section 6 in the EC2 documentation. 7 layout: docs 8 page_title: 'Amazon AMI Builder (chroot)' 9 --- 10 11 # AMI Builder (chroot) 12 13 Type: `amazon-chroot` 14 15 The `amazon-chroot` Packer builder is able to create Amazon AMIs backed by an 16 EBS volume as the root device. For more information on the difference between 17 instance storage and EBS-backed instances, see the ["storage for the root 18 device" section in the EC2 19 documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ComponentsAMIs.html#storage-for-the-root-device). 20 21 The difference between this builder and the `amazon-ebs` builder is that this 22 builder is able to build an EBS-backed AMI without launching a new EC2 instance. 23 This can dramatically speed up AMI builds for organizations who need the extra 24 fast build. 25 26 \~> **This is an advanced builder** If you're just getting started with 27 Packer, we recommend starting with the [amazon-ebs 28 builder](/docs/builders/amazon-ebs.html), which is much easier to use. 29 30 The builder does *not* manage AMIs. Once it creates an AMI and stores it in your 31 account, it is up to you to use, delete, etc. the AMI. 32 33 ## How Does it Work? 34 35 This builder works by creating a new EBS volume from an existing source AMI and 36 attaching it into an already-running EC2 instance. Once attached, a 37 [chroot](https://en.wikipedia.org/wiki/Chroot) is used to provision the system 38 within that volume. After provisioning, the volume is detached, snapshotted, and 39 an AMI is made. 40 41 Using this process, minutes can be shaved off the AMI creation process because a 42 new EC2 instance doesn't need to be launched. 43 44 There are some restrictions, however. The host EC2 instance where the volume is 45 attached to must be a similar system (generally the same OS version, kernel 46 versions, etc.) as the AMI being built. Additionally, this process is much more 47 expensive because the EC2 instance must be kept running persistently in order to 48 build AMIs, whereas the other AMI builders start instances on-demand to build 49 AMIs as needed. 50 51 ## Configuration Reference 52 53 There are many configuration options available for the builder. They are 54 segmented below into two categories: required and optional parameters. Within 55 each category, the available configuration keys are alphabetized. 56 57 ### Required: 58 59 - `access_key` (string) - The access key used to communicate with AWS. [Learn 60 how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials) 61 62 - `ami_name` (string) - The name of the resulting AMI that will appear when 63 managing AMIs in the AWS console or via APIs. This must be unique. To help 64 make this unique, use a function like `timestamp` (see [configuration 65 templates](/docs/templates/configuration-templates.html) for more info) 66 67 - `secret_key` (string) - The secret key used to communicate with AWS. [Learn 68 how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials) 69 70 - `source_ami` (string) - The source AMI whose root volume will be copied and 71 provisioned on the currently running instance. This must be an EBS-backed 72 AMI with a root volume snapshot that you have access to. Note: this is not 73 used when `from_scratch` is set to true. 74 75 ### Optional: 76 77 - `ami_description` (string) - The description to set for the 78 resulting AMI(s). By default this description is empty. 79 80 - `ami_groups` (array of strings) - A list of groups that have access to 81 launch the resulting AMI(s). By default no groups have permission to launch 82 the AMI. `all` will make the AMI publicly accessible. 83 84 - `ami_product_codes` (array of strings) - A list of product codes to 85 associate with the AMI. By default no product codes are associated with 86 the AMI. 87 88 - `ami_regions` (array of strings) - A list of regions to copy the AMI to. 89 Tags and attributes are copied along with the AMI. AMI copying takes time 90 depending on the size of the AMI, but will generally take many minutes. 91 92 - `ami_users` (array of strings) - A list of account IDs that have access to 93 launch the resulting AMI(s). By default no additional users other than the 94 user creating the AMI has permissions to launch it. 95 96 - `ami_virtualization_type` (string) - The type of virtualization for the AMI 97 you are building. This option is required to register HVM images. Can be 98 "paravirtual" (default) or "hvm". 99 100 - `chroot_mounts` (array of array of strings) - This is a list of devices 101 to mount into the chroot environment. This configuration parameter 102 requires some additional documentation which is in the "Chroot Mounts" 103 section below. Please read that section for more information on how to 104 use this. 105 106 - `command_wrapper` (string) - How to run shell commands. This defaults to 107 `{{.Command}}`. This may be useful to set if you want to set environmental 108 variables or perhaps run it with `sudo` or so on. This is a configuration 109 template where the `.Command` variable is replaced with the command to 110 be run. Defaults to "{{.Command}}". 111 112 - `copy_files` (array of strings) - Paths to files on the running EC2 instance 113 that will be copied into the chroot environment prior to provisioning. Defaults 114 to `/etc/resolv.conf` so that DNS lookups work. 115 116 - `device_path` (string) - The path to the device where the root volume of the 117 source AMI will be attached. This defaults to "" (empty string), which 118 forces Packer to find an open device automatically. 119 120 - `enhanced_networking` (boolean) - Enable enhanced 121 networking (SriovNetSupport) on HVM-compatible AMIs. If true, add 122 `ec2:ModifyInstanceAttribute` to your AWS IAM policy. 123 124 - `force_deregister` (boolean) - Force Packer to first deregister an existing 125 AMI if one with the same name already exists. Default `false`. 126 127 - `force_delete_snapshot` (boolean) - Force Packer to delete snapshots associated with 128 AMIs, which have been deregistered by `force_deregister`. Default `false`. 129 130 - `from_scratch` (boolean) - Build a new volume instead of starting from an 131 existing AMI root volume snapshot. Default `false`. If true, `source_ami` is 132 no longer used and the following options become required: 133 `ami_virtualization_type`, `pre_mount_commands` and `root_volume_size`. The 134 below options are also required in this mode only: 135 136 - `ami_block_device_mappings` (array of block device mappings) - Add the block 137 device mappings to the AMI. A `device_name` entry matching `root_device_name` 138 should be set. The block device mappings allow for keys: 139 140 - `delete_on_termination` (boolean) - Indicates whether the EBS volume is 141 deleted on instance termination. Default `false`. **NOTE**: If this 142 value is not explicitly set to `true` and volumes are not cleaned up by 143 an alternative method, additional volumes will accumulate after 144 every build. 145 146 - `device_name` (string) - The device name exposed to the instance (for 147 example, "/dev/sdh" or "xvdh"). Required when specifying `volume_size`. 148 149 - `encrypted` (boolean) - Indicates whether to encrypt the volume or not 150 151 - `iops` (integer) - The number of I/O operations per second (IOPS) that the 152 volume supports. See the documentation on 153 [IOPs](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_EbsBlockDevice.html) 154 for more information 155 156 - `no_device` (boolean) - Suppresses the specified device included in the 157 block device mapping of the AMI 158 159 - `snapshot_id` (string) - The ID of the snapshot 160 161 - `virtual_name` (string) - The virtual device name. See the documentation on 162 [Block Device 163 Mapping](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html) 164 for more information 165 166 - `volume_size` (integer) - The size of the volume, in GiB. Required if not 167 specifying a `snapshot_id` 168 169 - `volume_type` (string) - The volume type. gp2 for General Purpose (SSD) 170 volumes, io1 for Provisioned IOPS (SSD) volumes, and standard for Magnetic 171 volumes 172 173 - `root_device_name` (string) - The root device name. For example, `xvda`. 174 175 - `mount_path` (string) - The path where the volume will be mounted. This is 176 where the chroot environment will be. This defaults to 177 `/mnt/packer-amazon-chroot-volumes/{{.Device}}`. This is a configuration template 178 where the `.Device` variable is replaced with the name of the device where 179 the volume is attached. 180 181 - `mount_partition` (integer) - The partition number containing the 182 / partition. By default this is the first partition of the volume. 183 184 - `mount_options` (array of strings) - Options to supply the `mount` command 185 when mounting devices. Each option will be prefixed with `-o` and supplied 186 to the `mount` command ran by Packer. Because this command is ran in a 187 shell, user discrestion is advised. See [this manual page for the mount 188 command](http://linuxcommand.org/man_pages/mount8.html) for valid file 189 system specific options 190 191 - `pre_mount_commands` (array of strings) - A series of commands to execute 192 after attaching the root volume and before mounting the chroot. This is not 193 required unless using `from_scratch`. If so, this should include any 194 partitioning and filesystem creation commands. The path to the device is 195 provided by `{{.Device}}`. 196 197 - `post_mount_commands` (array of strings) - As `pre_mount_commands`, but the 198 commands are executed after mounting the root device and before the extra 199 mount and copy steps. The device and mount path are provided by 200 `{{.Device}}` and `{{.MountPath}}`. 201 202 - `root_volume_size` (integer) - The size of the root volume in GB for the 203 chroot environment and the resulting AMI. Default size is the snapshot size 204 of the `source_ami` unless `from_scratch` is `true`, in which case 205 this field must be defined. 206 207 - `skip_region_validation` (boolean) - Set to true if you want to skip 208 validation of the `ami_regions` configuration option. Default `false`. 209 210 - `snapshot_tags` (object of key/value strings) - Tags to apply to snapshot. 211 They will override AMI tags if already applied to snapshot. 212 213 - `snapshot_groups` (array of strings) - A list of groups that have access to 214 create volumes from the snapshot(s). By default no groups have permission to create 215 volumes form the snapshot(s). `all` will make the snapshot publicly accessible. 216 217 - `snapshot_users` (array of strings) - A list of account IDs that have access to 218 create volumes from the snapshot(s). By default no additional users other than the 219 user creating the AMI has permissions to create volumes from the backing snapshot(s). 220 221 - `source_ami_filter` (object) - Filters used to populate the `source_ami` field. 222 Example: 223 224 ``` {.javascript} 225 "source_ami_filter": { 226 "filters": { 227 "virtualization-type": "hvm", 228 "name": "*ubuntu-xenial-16.04-amd64-server-*", 229 "root-device-type": "ebs" 230 }, 231 "owners": ["099720109477"], 232 "most_recent": true 233 } 234 ``` 235 236 This selects the most recent Ubuntu 16.04 HVM EBS AMI from Canonical. 237 NOTE: This will fail unless *exactly* one AMI is returned. In the above 238 example, `most_recent` will cause this to succeed by selecting the newest image. 239 240 - `filters` (map of strings) - filters used to select a `source_ami`. 241 NOTE: This will fail unless *exactly* one AMI is returned. 242 Any filter described in the docs for [DescribeImages](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html) 243 is valid. 244 245 - `owners` (array of strings) - This scopes the AMIs to certain Amazon account IDs. 246 This is helpful to limit the AMIs to a trusted third party, or to your own account. 247 248 - `most_recent` (bool) - Selects the newest created image when true. 249 This is most useful for selecting a daily distro build. 250 251 - `tags` (object of key/value strings) - Tags applied to the AMI. 252 253 ## Basic Example 254 255 Here is a basic example. It is completely valid except for the access keys: 256 257 ``` {.javascript} 258 { 259 "type": "amazon-chroot", 260 "access_key": "YOUR KEY HERE", 261 "secret_key": "YOUR SECRET KEY HERE", 262 "source_ami": "ami-e81d5881", 263 "ami_name": "packer-amazon-chroot {{timestamp}}" 264 } 265 ``` 266 267 ## Chroot Mounts 268 269 The `chroot_mounts` configuration can be used to mount specific devices within 270 the chroot. By default, the following additional mounts are added into the 271 chroot by Packer: 272 273 - `/proc` (proc) 274 - `/sys` (sysfs) 275 - `/dev` (bind to real `/dev`) 276 - `/dev/pts` (devpts) 277 - `/proc/sys/fs/binfmt_misc` (binfmt\_misc) 278 279 These default mounts are usually good enough for anyone and are sane defaults. 280 However, if you want to change or add the mount points, you may using the 281 `chroot_mounts` configuration. Here is an example configuration which only 282 mounts `/prod` and `/dev`: 283 284 ``` {.javascript} 285 { 286 "chroot_mounts": [ 287 ["proc", "proc", "/proc"], 288 ["bind", "/dev", "/dev"] 289 ] 290 } 291 ``` 292 293 `chroot_mounts` is a list of a 3-tuples of strings. The three components of the 294 3-tuple, in order, are: 295 296 - The filesystem type. If this is "bind", then Packer will properly bind the 297 filesystem to another mount point. 298 299 - The source device. 300 301 - The mount directory. 302 303 ## Parallelism 304 305 A quick note on parallelism: it is perfectly safe to run multiple *separate* 306 Packer processes with the `amazon-chroot` builder on the same EC2 instance. In 307 fact, this is recommended as a way to push the most performance out of your AMI 308 builds. 309 310 Packer properly obtains a process lock for the parallelism-sensitive parts of 311 its internals such as finding an available device. 312 313 ## Gotchas 314 315 One of the difficulties with using the chroot builder is that your provisioning 316 scripts must not leave any processes running or packer will be unable to unmount 317 the filesystem. 318 319 For debian based distributions you can setup a 320 [policy-rc.d](http://people.debian.org/~hmh/invokerc.d-policyrc.d-specification.txt) 321 file which will prevent packages installed by your provisioners from starting 322 services: 323 324 ``` {.javascript} 325 { 326 "type": "shell", 327 "inline": [ 328 "echo '#!/bin/sh' > /usr/sbin/policy-rc.d", 329 "echo 'exit 101' >> /usr/sbin/policy-rc.d", 330 "chmod a+x /usr/sbin/policy-rc.d" 331 ] 332 }, 333 334 // ... 335 336 { 337 "type": "shell", 338 "inline": [ 339 "rm -f /usr/sbin/policy-rc.d" 340 ] 341 } 342 ``` 343 344 ## Building From Scratch 345 346 This example demonstrates the essentials of building an image from scratch. A 347 15G gp2 (SSD) device is created (overriding the default of standard/magnetic). 348 The device setup commands partition the device with one partition for use as an 349 HVM image and format it ext4. This builder block should be followed by 350 provisioning commands to install the os and bootloader. 351 352 ``` {.javascript} 353 { 354 "type": "amazon-chroot", 355 "ami_name": "packer-from-scratch {{timestamp}}" 356 "from_scratch": true, 357 "ami_virtualization_type": "hvm", 358 "device_setup_commands": [ 359 "parted {{.Device}} mklabel msdos mkpart primary 1M 100% set 1 boot on print", 360 "mkfs.ext4 {{.Device}}1" 361 ], 362 "root_volume_size": 15, 363 "root_device_name": "xvda", 364 "ami_block_device_mappings": [ 365 { 366 "device_name": "xvda", 367 "delete_on_termination": true, 368 "volume_type": "gp2" 369 } 370 ] 371 } 372 ```