Resize AWS root ebs volume

4,162 views
Skip to first unread message

richard....@wealthwizards.com

unread,
Aug 17, 2017, 5:49:06 PM8/17/17
to Packer
Hi,

Really sorry to be asking this because I can see there are previous threads, e.g. "Configuring size of root volume on EC2" but they actually don't provide a clear answer and neither do the docs.

I'm building an AMI using the amazon-ebs builder. The reference AMI is a Debian stretch image with an 8GB root volume. The source images use /dev/xvda1 as the root partition.

I'd like my packer image to have a much larger volume, e.g. 40GB. Previous threads indicate this should be possible by adding:

        "ami_block_device_mappings": [ {
            "device_name": "/dev/xvda",
            "volume_size": 40,
            "delete_on_termination": true
        } ],
        "launch_block_device_mappings": [ {
            "device_name": "/dev/xvda",
            "volume_size": 40,
            "delete_on_termination": true
        } ]

It's not clear to me what the distinction between  ami_block_device_mappings and launch_block_device_mappings is. What what I understand of the docs they should be used when I want to add additional devices beyond the root vol, but the discussion in the other thread suggests using the code above is used to modify the root volume size.

I don't fully understand what it's doing, but more to the point, I can't make it work.

The reference image boots from /dev/xvda1, so presumably I need 

"device_name": "/dev/xvda"


but that throws this error:

The device 'xvda' is used in more than one block-device mapping

So, what device should I use is the source AMI is using /dev/xvda1 as root partition and what the difference between the two mappings?

(I know I may need to deal with fs resize, but I'll get to that when it's a problem)

Many Thanks,

Rich

Rickard von Essen

unread,
Aug 18, 2017, 1:30:04 AM8/18/17
to packe...@googlegroups.com
Setting this in ami_block_device_mappings makes your custom AMI come with a 40 GB root device but the instance launched by packer has the standard 8 GB. Example:
[...]
    amazon-ebs: Debian GNU/Linux 8 (jessie)
    amazon-ebs: Filesystem      Size  Used Avail Use% Mounted on    [df -h]
    amazon-ebs: /dev/xvda2      7.8G  819M  6.6G  11% /
    amazon-ebs: udev             10M     0   10M   0% /dev
    amazon-ebs: tmpfs           200M  4.2M  196M   3% /run
    amazon-ebs: tmpfs           500M     0  500M   0% /dev/shm
    amazon-ebs: tmpfs           5.0M     0  5.0M   0% /run/lock
    amazon-ebs: tmpfs           500M     0  500M   0% /sys/fs/cgroup
    amazon-ebs: NAME    MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT     [lsblk]
    amazon-ebs: xvda    202:0    0      8G  0 disk
    amazon-ebs: ├─xvda1 202:1    0 1007.5K  0 part
    amazon-ebs: └─xvda2 202:2    0      8G  0 part /
[...]

aws ec2 describe-images --image-ids <ami-id> --query 'Images[0].BlockDeviceMappings[0]'
{
    "DeviceName": "/dev/xvda",
    "Ebs": {
        "Encrypted": false,
        "DeleteOnTermination": true,
        "VolumeType": "standard",
        "VolumeSize": 40,
        "SnapshotId": "snap-04aa234cd2128d60a"
    }
}

While setting this in launch_block_device_mappins changes the device also for the instance packer launches for provisioning and since that is what is captured in your custom AMI that will have 40 GB too.
Example:
[...]
    amazon-ebs: Debian GNU/Linux 8 (jessie)
    amazon-ebs: Filesystem      Size  Used Avail Use% Mounted on    [df -h]
    amazon-ebs: /dev/xvda2       40G  825M   37G   3% /
    amazon-ebs: udev             10M     0   10M   0% /dev
    amazon-ebs: tmpfs           200M  4.2M  196M   3% /run
    amazon-ebs: tmpfs           500M     0  500M   0% /dev/shm
    amazon-ebs: tmpfs           5.0M     0  5.0M   0% /run/lock
    amazon-ebs: tmpfs           500M     0  500M   0% /sys/fs/cgroup
    amazon-ebs: NAME    MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT    [lsblk]
    amazon-ebs: xvda    202:0    0     40G  0 disk
    amazon-ebs: ├─xvda1 202:1    0 1007.5K  0 part
    amazon-ebs: └─xvda2 202:2    0     40G  0 part /
[...]

aws ec2 describe-images --image-ids <ami-id> --query 'Images[0].BlockDeviceMappings[0]'
{
    "DeviceName": "/dev/xvda",
    "Ebs": {
        "Encrypted": false,
        "DeleteOnTermination": true,
        "VolumeType": "standard",
        "VolumeSize": 40,
        "SnapshotId": "snap-0132901385c9a556d"
    }
}

Hope that makes it clear.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/ea501630-7e60-43aa-a543-cb60f90cbaa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

richard....@wealthwizards.com

unread,
Aug 18, 2017, 7:43:34 AM8/18/17
to Packer
Hi Rickard,

Thanks for the response, that helps make the distinction clear. What I'm still blocked on is getting the root vol to be increased.

The source image boots from /dev/xvda(1). When I specify /dev/xvda as the device in ami_block_device_mappings

This builder config:

"builders": [{

     
"type": "amazon-ebs",
     
"communicator": "ssh",
     
"ssh_pty": "true",
     
"access_key": "{{user `aws_access_key`}}",
     
"secret_key": "{{user `aws_secret_key`}}",
     
"region": "eu-west-1",
     
"source_ami": "ami-88e60ff1",
     
"instance_type": "t2.nano",
     
"ssh_username": "admin",
     
"ami_name": "jenkins-slave-stretch{{timestamp}}",
     
"encrypt_boot": true,
     
"ami_block_device_mappings": [ {
         
"device_name": "/dev/xvda",
         
"volume_size": 42,
         
"volume_type": "gp2",
         
"encrypted": true,
         
"delete_on_termination": true
     
} ]
   
}],

It fails with the error:

Error creating AMI: InvalidBlockDeviceMapping: The device 'xvda' is used in more than one block-device mapping



If I specify a different device id then I end up with an additional disk on the image:


This builder config:


 
 "builders": [{

     
"type": "amazon-ebs",
     
"communicator": "ssh",
     
"ssh_pty": "true",
     
"access_key": "{{user `aws_access_key`}}",
     
"secret_key": "{{user `aws_secret_key`}}",
     
"region": "eu-west-1",
     
"source_ami": "ami-88e60ff1",
     
"instance_type": "t2.nano",
     
"ssh_username": "admin",
     
"ami_name": "jenkins-slave-stretch{{timestamp}}",
     
"encrypt_boot": true,
     
"ami_block_device_mappings": [ {
         
"device_name": "/dev/xvdb",
         
"volume_size": 42,
         
"volume_type": "gp2",
         
"encrypted": true,
         
"delete_on_termination": true
     
} ]
   
}],





Ends up like this:
















So I've so-far failed to extend the root volume of the ami produced by packer. What am I doing wrong?

Thanks again!

Rich






Rickard von Essen

unread,
Aug 18, 2017, 8:16:37 AM8/18/17
to packe...@googlegroups.com
You can't set encrypted to true to a existing device. Try remove that and I guess it should work. If you want to encrypt it use encrypt_boot: true.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.

richard....@wealthwizards.com

unread,
Aug 18, 2017, 9:45:59 AM8/18/17
to Packer
Hi,

Ok, so I've tried removing the encrypt options but still no joy:

   "builders": [{
     
"type": "amazon-ebs",
     
"communicator": "ssh",
     
"ssh_pty": "true",
     
"access_key": "{{user `aws_access_key`}}",
     
"secret_key": "{{user `aws_secret_key`}}",
     
"region": "eu-west-1",
     
"source_ami": "ami-88e60ff1",
     
"instance_type": "t2.nano",
     
"ssh_username": "admin",
     
"ami_name": "jenkins-slave-stretch{{timestamp}}",

     
"ami_block_device_mappings": [ {
         
"device_name": "/dev/xvda",
         
"volume_size": 42,
         
"volume_type": "gp2",

         
"delete_on_termination": true
     
} ]
   
}],

Gives:

Build 'amazon-ebs' errored: Error creating AMI: InvalidBlockDeviceMapping: The device 'xvda' is used in more than one block-device mapping
status code: 400, request id: 93848ef4-ad21-4c5f-aa35-29c66c545a6a

I tried both with and without  "encrypt_boot": true, in case that made any difference, but it didn't change either way.

Thanks again,

Rich
Message has been deleted

richard....@wealthwizards.com

unread,
Aug 18, 2017, 10:42:26 AM8/18/17
to Packer
Hey, thanks

I tried that too but it just creates an additional device on the system, it doesn't alter the existing block device. :(

Regards

Rich


On Friday, August 18, 2017 at 3:16:08 PM UTC+1, Daniel Berard wrote:
Try using xvdf. xvda is the default device. 

richard....@wealthwizards.com

unread,
Aug 18, 2017, 11:18:27 AM8/18/17
to Packer

w00t w00t!!

finally got it working, thanks to all that helped. 

the working config is:

   "builders": [{
     "type": "amazon-ebs",
     "communicator": "ssh",
     "ssh_pty": "true",
     "access_key": "{{user `aws_access_key`}}",
     "secret_key": "{{user `aws_secret_key`}}",
     "region": "eu-west-1",
     "source_ami": "ami-88e60ff1",
     "instance_type": "t2.nano",
     "ssh_username": "admin",
     "ami_name": "jenkins-slave-stretch{{timestamp}}",
     "encrypt_boot": true,
     "ami_block_device_mappings": [ {
         "device_name": "xvda",
         "volume_size": 42,
         "volume_type": "gp2",
         "delete_on_termination": true
     } ]
   }],

The important bit is that while the source image uses /dev/xvda as the boot image the device name specified is "xvda". I don't know why this distinction is needed, but it works. 


ME2Digital

unread,
Aug 22, 2017, 4:08:25 AM8/22/17
to Packer
Hi all.

I have the same problem with packer 1.0.4

I use the following json

###
    "ami_block_device_mappings": [
    {
      "delete_on_termination": false,
      "device_name": "/dev/xvda",
      "volume_size": "25",
      "volume_type": "gp2"
    }
    ],
##

Full json: https://gist.github.com/git001/be7f0293bc16aac3a80801da2ffef1ac

with this provision.sh https://gist.github.com/git001/ae972d1884fd365f9ebbfc323c114476

I have tried several variants of
"device_name"

"device_name": "/dev/xvda",
"device_name": "/dev/sda",
"device_name": "xvda",

None of them increase the size of the atomic root device.

Please can someone shade some light on this, thanks.

You can find the output of a packer run here https://gist.github.com/git001/a0e95d80763ffbec58aa9b4898b5cfe0

thanks for help.

Regards
Aleks


Am Donnerstag, 17. August 2017 23:49:06 UTC+2 schrieb richard....@wealthwizards.com:
Hi,

Really sorry to be asking this because I can see there are previous threads, e.g. "Configuring size of root volume on EC2" but they actually don't provide a clear answer and neither do the docs.

I'm building an AMI using the amazon-ebs builder. The reference AMI is a Debian stretch image with an 8GB root volume. The source images use /dev/xvda1 as the root partition.

[snipp]

Rickard von Essen

unread,
Aug 22, 2017, 5:46:05 AM8/22/17
to packe...@googlegroups.com
Since you are running expansion of the fs at provisioning you should probably just switch to lunch_block_device_mappings.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.

ME2Digital

unread,
Aug 22, 2017, 6:11:17 PM8/22/17
to Packer


Am Dienstag, 22. August 2017 11:46:05 UTC+2 schrieb Rickard von Essen:
Since you are running expansion of the fs at provisioning you should probably just switch to lunch_block_device_mappings.

Thanks. I have now used the following config and this works.

###
    "launch_block_device_mappings": [
    {
      "delete_on_termination": false,
      "device_name": "/dev/sda1",
      "volume_size": 150,
      "volume_type": "gp2"
    }
    ],
###
 
Thanks for help

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Samvitha K

unread,
Nov 20, 2019, 5:19:10 PM11/20/19
to Packer
Once you expand the root disk i.e. /dev/xvda to desired size, may i know how to create custom filesystem partitions (/var, /home/, /var/log etc) on a AWS EBS disk during boot process using hashicorp packer? Is it possible and did anyone try this?

Megan Marsh

unread,
Nov 20, 2019, 5:30:01 PM11/20/19
to packe...@googlegroups.com
I believe that kind of custom partitioning has to happen as part of your provisioning scripts. Packer won't do it for you.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Samvitha K

unread,
Nov 20, 2019, 5:44:47 PM11/20/19
to Packer
The provisioner will come to rescue once the system boots but as i want to accomplish the custom partitions during the build process, i would like to know how to achieve this. the reason i am asking this is, many organizations doesnt want to have all filesystems under / and more over CIS benchmarks expects to have separate filesystems, so i would like to know how is this being actioned.
To unsubscribe from this group and stop receiving emails from it, send an email to packe...@googlegroups.com.

Megan Marsh

unread,
Nov 20, 2019, 6:10:44 PM11/20/19
to packe...@googlegroups.com
If it can be done using the block device mappings https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html then you can configure it in Packer. Otherwise, you have to do it yourself via provisioning scripts. 
Reply all
Reply to author
Forward
0 new messages