CloudFormation格式EBS卷

时间:2017-05-17 15:35:31

标签: amazon-web-services amazon-ec2 mount amazon-cloudformation disk-partitioning

DESCRIPTION / SYMPTOMS

我在使用AWS CloudFormation模板在EC2实例上对EBS卷进行分区/格式化时遇到了困难。我有以下(缩写)卷和实例资源:

ApacheEbsVolume:
  Type: AWS::EC2::Volume
  Properties:
    Size: 2
    VolumeType: gp2
WordPress:
  Type: AWS::EC2::Instance
  Properties:
    InstanceType: !Ref InstanceType
    KeyName: !Ref KeyPair
    Volumes:
    - # Volume for Apache root
      Device: /dev/sdf
      VolumeId: !Ref ApacheEbsVolume

该实例的元数据中包含以下CloudFormation::Init命令:

mount:
  command: |
    dev=/dev/sdf
    lbl=apache
    sgdisk --largest-new=1 --change-name=1:$lbl $dev
    mkfs.ext4 -vL $lbl "$dev"1
    echo "LABEL=$lbl /var/www/html/ ext4 defaults,nofail 0 2" >> /etc/fstab
    mount --all

不幸的是,当我在堆栈达到CREATE_COMPLETE后在cfn-init检查/var/log/cfn-init.log日志时,我看到以下消息:

2017-05-17 14:34:55,321 [DEBUG] Command mount output: Creating new GPT entries.
Setting name!
partNum is 0
REALLY setting name!
The operation has completed successfully.
mke2fs 1.42.12 (29-Aug-2014)
The file /dev/sdf1 does not exist and no size was specified.
mount: can't find LABEL=apache

我还分别从lsblklsblk --fs获得以下输出:

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf    202:80   0   2G  0 disk /var/www/html

NAME    FSTYPE LABEL UUID                                 MOUNTPOINT
xvdf                                                      /var/www/html

问题

那里发生了很多奇怪的事情......日志说在/dev/sdf上成功创建了一个分区,但我没有看到lsblk的分区。所以看起来分区实际上是 not 创建的,这就是后续mkfsmount命令失败的原因。但对于我的生活,我不知道为什么分区不会创建。更奇怪的是,这个模板实际上附加了第二个EBS卷/dev/sdg(为了简洁起见,我在上面的代码中省略了所有相同的命令),并且该卷 成功分区,但是仍然无法格式化!

我觉得我做的一切都是正确的,只是打了一些愚蠢的CloudFormation怪癖,但任何关于使用CloudFormation::Init元数据进行分区/格式化的建议都会非常感激!

1 个答案:

答案 0 :(得分:0)

我可以看到,你没有正确创建分区。

我为你的场景写了一个CFT示例。随意试试吧。因此,我更喜欢JSON而不是yml,如果你想坚持你的原始版本,你可能需要转换它。

{  
   "AWSTemplateFormatVersion":"2010-09-09",
   "Description":"Mount an ELB",
   "Parameters":{  
      "data1Volume":{  
         "Description":"Size (GB) of application EBS volume for instance",
         "Type":"Number",
         "Default":"10",
         "MinValue":"10",
         "MaxValue":"1024"
      }
   },
   "Resources":{  
      "ec2insta01":{  
         "Type":"AWS::EC2::Instance",
         "Properties":{  
            "EbsOptimized":true,
            "BlockDeviceMappings":[  
               {  
                  "DeviceName":"/dev/sdf",
                  "Ebs":{  
                     "VolumeSize":{  
                        "Ref":"data1Volume"
                     },
                     "VolumeType":"gp2"
                  }
               }
            ],
            "UserData":{  
               "Fn::Base64":{  
                  "Fn::Join":[  
                     "",
                     [  
                        "#!/bin/bash -v\n",
                        "yum update -y aws-cfn-bootstrap\n",
                        "exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1\n",
                        "date > /home/ec2-user/starttime\n",
                        "#Partition new disks",
                        "\n",
                        "echo -e \"o\nn\np\n1\n\n\nw\" |fdisk /dev/xvdf",
                        "\n",
                        "#Make Folders to mount new disks",
                        "\n",
                        "mkdir /usr/myVol",
                        "\n",
                        "#Format disks",
                        "\n",
                        "mkfs.ext3 /dev/xvdf1",
                        "\n",
                        "#Mount New Disks",
                        "\n",
                        "mount /dev/xvdf1 /usr/myVol\n",
                        "Add mounts to FSTab so it will stick in reboot\n",
                        "echo \" LABEL=apache /dev/xvdf1 /usr/myVol ext3 defaults 0 2\" >> /etc/fstab\n",
                        "date > /home/ec2-user/stoptime\n",
                        "echo END"
                     ]
                  ]
               }
            }
         }
      }
   }
}