詳細検索

Bypass reboot by allowing Packer to do resize2fs in HVM as well

Avatar
by komi
4 min read

Bypass reboot by allowing Packer to do resize2fs in HVM as well
Translated from 日本語 • View original

Hello. This is Komiya. I tried to avoid reboot by allowing resize2fs in the hvm in packer.

I feel like I can do the same thing using cloud-init. Configuring cloud-init on CentOS 6 (HVM) to Exploit Amazon Linux | Developers.IO However, since I use a packer, it's easier to do it there. I couldn't log in when I created an ami that just put cloud-init without setting anything orz I don't want to do it unless I understand what I wrote in the configuration file and what exactly it would do behind the scenes, but it was a hassle to look it up. Sorry for being weak.

・The beginning of the matter When I created an hvm instance based on the CentOS6.5 AMI, I was supposed to run resize2fs automatically on the root volume by running it in the chef recipe, but the size did not change. It was supposed to be 10 to 50GB, but it was left at 10GB

・For the time being, learn about similar phenomena and countermeasures at the following URL Change disk size with EC2's CentOS-HVM | I was surprised that there was a way to expand it with Hack fdisk. But I was stuck with the reboot essential! I thought. To be specific, resize2fs does it by streaming chef recipes, so logging in is twice a hassle.

・I raised a problem or consulted with a pointy person who suggested that if there is a problem with the partition table, why not put something like the following URL in the packer What to do when the disk size does not change in CentOS on Amazon EC2 | Developers.IO ・I actually tried it and it was successful.

Add the following to the shell script in packer

+# Install growroot etc. from EPEL
+yum -y --enablerepo=epel install dracut-modules-growroot cloud-utils
+
+# Resize root partition table
+dracut --force --add growroot /boot/initramfs-$(uname -r).img

So, I built an AMI with packer and tried to run the chef recipe to the instance I created from that AMI, and when I df, the disk was expanded. I didn't have to reboot! ! Yay! I was happy.

・How to build my AMI with packer Install Packer - Packer by HashiCorpYou can check it out. The template looks like the following (if you are using VPC, you also need VPC_id or subnet_id). There may not be many people who don't use VPC these days. )

{
  "variables": {
    "aws_access_key": "AKB**********",
    "aws_secret_key": "Pga4**********",
    "aws_source_ami": "ami-53**********",
    "aws_region_name": "ap-so**********",
    "aws_instance_type": "t2.small",
    "aws_vpc_id": "vpc-0z**********",
    "aws_subnet_id": "subnet-0**********",
    "aws_create_date": ""
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "{{user `aws_region_name`}}",
    "source_ami": "{{user `aws_source_ami`}}",
    "instance_type": "{{user `aws_instance_type`}}",
    "ssh_username": "root",
    "ssh_timeout": "10m",
    "subnet_id": "{{user `aws_subnet_id`}}",
    "vpc_id": "{{user `aws_vpc_id`}}",
    "associate_public_ip_address": true,
    "ami_name": "AMI_standard_{{user `aws_create_date`}}",
    "tags": {
      "Name": "AMI_standard_{{user `aws_create_date`}}"
    }
  }],
  "provisioners": [{
    "type": "shell",
    "execute_command": "sh {{ . Path }} {{user `aws_region_name`}}",
    "script": "ami-deploy.sh"
  }]
}

*Specify the script to run on provisioners This page [Bulletin]Create templates for various virtual machines with Packer | Ryuzee.com also explains.  It is a good idea to prepare basic base settings in the script that you would do when making your own AMI.  Cloud-init, or the setting before the chef nagashis.  For example, general repositories, things that must be included in everything, SSH area, minimum users, auto-start settings, etc. *In the first place, the AMI specified in the template needs hvm. Please see Mr. Inada's article on how to make it. Converting ⇒ PV Instance to HVM Instance (CentOS6)

The build command runs as follows: (It is better not to pass it to the background because there are various execution status in the standard output.)

CREATE_DATE=`date +'%Y%m%d'`
packer build -var aws_create_date=${CREATE_DATE} templatefile.json

*It took about 20~30 minutes It seems that it can be run from Windows, but it seems that Linux that can be done with Linux is better at processing, so it seems that I will be stuck if I do it in Win. The psychological barrier is high and I don't feel motivated.

For the time being, I was able to achieve my goal in a way that was easy for me personally. I would like to look into cloud-init someday. Low priority of tasks in the brain (I feel like I will be lost if I don't have a forced opportunity).

Thank you for watching.

Related Articles