Automounting ebs volume in linux-Ec2 instance using cloud formation?
Asked Answered
M

2

6

Wrote one CFT to create redhat instance with two ebs volume attached. And need automount or formatted the ebs volume from the cft itself.

CFT:

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

Need to mount "DeviceName" : "/dev/sdm", this volume automatically.

Melly answered 4/8, 2014 at 19:38 Comment(0)
B
9

You will need to add a small script to the UserData property of your instance or launch configuration that this BlockDeviceMappings is associated with. UserData is executed the first time the instance boots. The devices will automatically be remounted when the instance reboots using /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"
    ]]}}

More information: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html

Bibliotheca answered 15/10, 2014 at 16:55 Comment(0)
A
3

Rather than mounting explicitly in the user-data script, I prefer the following approach:

"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" will try to mount all the entries in /etc/fstab and in turn verify the previous append operation.

Aldwon answered 6/3, 2017 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.