简体   繁体   中英

How to resize the root filesystem during AMI creation with packer?

I faced the the problem that some apache logs filled so quick that the root filesystem was not big enough.

I am using an AMI created with packer and centos 6.

How can I resize the root FS during the AMI creation to have it ready for later usage?

To achieve my result I added an user_data_file script in packer config like this:

      "user_data_file": "/root/packer_userdata.sh", 

Therefore I added the follow code in the packer user_data_file; I used facter, from puppet, to get information I needed but you can use whatever you want or even having it as constant.

# resize root fs if the volume is bigger
echo "ROOT DISK RESIZING" > /tmp/root_disk_resize.log
# wait for facter
while ( ! /usr/bin/facter ); do sleep 15 ; done >> /tmp/root_disk_resize.log 2>&1
OS_RELEASE=$(facter operatingsystemmajrelease 2>&1 )
ROOT_DEVICE=$(facter ec2_block_device_mapping_root 2>&1 )
# Install epel
rpm -ivh https://ftp.fau.de/epel/epel-release-latest-${OS_RELEASE}.noarch.rpm >> /tmp/root_disk_resize.log 2>&1
yum install -y cloud-utils-growpart gdisk >> /tmp/root_disk_resize.log 2>&1
# resize partition
growpart -v  ${ROOT_DEVICE} 1   >> /tmp/root_disk_resize.log 2>&1
# resize filesystem
resize2fs -p ${ROOT_DEVICE}1    >> /tmp/root_disk_resize.log 2>&1
# remove epel to avoid to interfere with the rest of the installation
rpm -e epel-release        >> /tmp/root_disk_resize.log 2>&1

The partition will be extended and during the next reboot the FS will be resized to the full volume size although it is not viewable during the AMI creation.

I found some interesting info and projects:

linux-rootfs-resize project

packer discussion

autoresize-ebs-root-volume-on-aws-amis

ami-block-device-mappings-example

You can just add a Block Device Mapping insi

 "launch_block_device_mappings": [ { "device_name": "/dev/xvda", "volume_type": "gp2", "volume_size": 20, "delete_on_termination": true } ]

You must check you AMI which device name use it could be /dev/sda1 or /dev/xvda

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM