简体   繁体   English

如何在使用打包程序创建 AMI 期间调整根文件系统的大小?

[英]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.我遇到了一些 apache 日志填得太快以至于根文件系统不够大的问题。

I am using an AMI created with packer and centos 6.我正在使用使用 packer 和 centos 6 创建的 AMI。

How can I resize the root FS during the AMI creation to have it ready for later usage?如何在 AMI 创建期间调整根 FS 的大小以使其准备好供以后使用?

To achieve my result I added an user_data_file script in packer config like this:为了实现我的结果,我在打包程序配置中添加了一个 user_data_file 脚本,如下所示:

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

Therefore I added the follow code in the packer user_data_file;因此我在打包器 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.我使用了 puppet 的 facter 来获取我需要的信息,但你可以使用任何你想要的东西,甚至可以让它保持不变。

# 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.分区将被扩展,并且在下一次重新启动期间,FS 将被调整为完整的卷大小,尽管它在 AMI 创建期间是不可见的。

I found some interesting info and projects:我发现了一些有趣的信息和项目:

linux-rootfs-resize project linux-rootfs-resize 项目

packer discussion 包装工讨论

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

ami-block-device-mappings-example ami-block-device-mappings-example

You can just add a Block Device Mapping insi你可以只添加一个块设备映射 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您必须检查您的 AMI 哪个设备名称使用它可以是 /dev/sda1 或 /dev/xvda

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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