使用云形成在linux-Ec2实例中自动挂载ebs卷?

时间:2014-08-04 19:38:25

标签: linux amazon-web-services amazon-ec2 amazon-ebs amazon-cloudformation

写了一个CFT来创建附加了两个ebs卷的redhat实例。并且需要自动挂载或格式化来自cft本身的ebs卷。

CFT:

"BlockDeviceMappings": [
    {
        "DeviceName": "/dev/sda1",
        "Ebs": {
            "DeleteOnTermination": "true",
            "VolumeSize": "150",
            "VolumeType": "standard"
        }
    },
    {
        "DeviceName": "/dev/sdm",
        "Ebs": {
            "DeleteOnTermination": "true",
            "VolumeSize": "1000",
            "VolumeType": "standard"
        }
    }
]

需要挂载" DeviceName" :" / dev / sdm",自动显示此卷。

2 个答案:

答案 0 :(得分:9)

您需要向实例的UserData属性添加一个小脚本,或者启动与此BlockDeviceMappings关联的配置。 UserData在实例第一次引导时执行。当实例使用/ etc / fstab重新启动时,将自动重新装入设备。

    "UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
      "#!/bin/bash -v\n",
      "mkfs -t ext4 /dev/xvdm\n",
      "mkfs -t ext4 /dev/xvda1\n",
      "mkdir /opt/mount1 /opt/mount2\n",
      "mount /dev/xvdm /opt/mount1\n",
      "mount /dev/xvda1 /opt/mount2\n",
      "echo \"/dev/xvdm /opt/mount1 ext4 defaults,nofail 0 2\" >> /etc/fstab\n"
      "echo \"/dev/xvda1 /opt/mount2 ext4 defaults,nofail 0 2\" >> /etc/fstab\n"
    ]]}}

更多信息:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html

答案 1 :(得分:3)

我没有明确地在用户数据脚本中安装,而是更喜欢以下方法:

"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
  "#!/bin/bash -v\n",
  "mkfs -t ext4 /dev/xvdm\n",
  "echo \"/dev/xvdm /opt/mount1 ext4 defaults,nofail 0 2\" >> /etc/fstab\n",
  "mount -a\n"
]]}}

“mount -a”将尝试在/ etc / fstab中挂载所有条目,然后验证以前的附加操作。