Ask Your Question
0

heat template - create instance, attach cinder volume, use it as LVM

asked 2015-11-22 06:22:39 -0500

boniamd gravatar image

updated 2015-11-22 06:31:07 -0500

Hi, I want to create heat template that serve the following purpose:

  1. Launch Instance from image
  2. Create Cinder Volume
  3. Attach the Volume to the instance
  4. Configure the volume on the instance as LVM, format the LVM as ext4, and finally mount it

So i create something like this:

 heat_template_version: 2015-04-30
description: >
  A template showing how to create a Nova instance, a Cinder volume and attach
  the volume to the instance. The template uses only Heat OpenStack native
  resource types.
parameters:
  key_name:
    type: string
    default: linux_key
    description:
      Name of an existing key pair to enable SSH access to the instance.
  instance_type:
    type: string
    description: Type of the instance to be created.
    default: m1.small
    constraints:
      - allowed_values: [m1.small, m1.medium, m1.large]
        description:
          Value must be one of 'm1.small', 'm1.medium' or 'm1.large'.
  image_id:
    type: string
    description: ID of the image to use for the instance to be created.
    default: linux_img
    constraints:
      - allowed_values: [ linux_img ]
        description:
          Image ID must be linux_img
  availability_zone:
    type: string
    description: The Availability Zone to launch the instance.
    default: nova
  volume_size:
    type: number
    description: Size of the volume to be created.
    default: 1
    constraints:
      - range: { min: 1, max: 1024 }
        description: must be between 1 and 1024 Gb.
resources:
  avoltest:
    type: OS::Nova::Server
    properties:
      availability_zone: { get_param: availability_zone }
      image: { get_param: image_id }
      flavor: { get_param: instance_type }
      key_name: { get_param: key_name }
      user_data_format: RAW
      user_data:
        str_replace:
          params:
            $LOGFILE: /tmp/installlog.log
            $VOLSIZE: { get_param: volume_size }
          template: |
            #!/bin/bash -v
              die() {
              _WC-NOTIFY_ --data-binary '{"status": "FAILURE"}'
                exit 1
              }
            yum install -y lvm2 >> $LOGFILE 2>&1 || die
            pvcreate /dev/sdb >> $LOGFILE 2>&1 || die
            vgcreate volgroup01 /dev/sdb >> $LOGFILE 2>&1 || die
            lvcreate -l+100%FREE -n lvol01 volgroup01 >> $LOGFILE 2>&1 || die
            mkfs.ext4 -F /dev/mapper/volgroup01-lvol01 >> $LOGFILE 2>&1 || die
            mkdir /data
            echo "/dev/mapper/volgroup01-lvol01 /data ext4 defaults 0 0" >> /etc/fstab
            mount -a
            df -ah >> $LOGFILE 2>&1 || die
  cinder_volume:
    type: OS::Cinder::Volume
    properties:
      size: { get_param: volume_size }
      availability_zone: { get_param: availability_zone }
  volume_attachment:
    type: OS::Cinder::VolumeAttachment
    properties:
      volume_id: { get_resource: cinder_volume }
      instance_uuid: { get_resource: avoltest }
      mountpoint: /dev/sdb
outputs:
  instance_ip:
    description: Public IP address of the newly created Nova instance.
    value: { get_attr: [avoltest, first_address] }

but with that template, the bash command for creating the lvm (pvcreate and so on) is executed first before the volume is being attached to the instance, so it failed.

Anyone have idea how to make it in order so that the bash commands on the instance will be executed after the volume is attached? Thank you

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-05-04 00:17:28 -0500

kcd gravatar image

There need to be more examples on how to do this, it is non-trivial

Here's mine

  config_mount_volume:
    type: OS::Heat::SoftwareConfig
    properties:
      group: ungrouped
      config:
        str_replace:
          template: |
            #!/bin/bash
            set -e
            voldata_id=PARAM_voldata_id
            mount_dir=PARAM_voldata_mount/
            echo "Mount volume ${voldata_id} to ${mount_dir}"
            voldata_dev="/dev/disk/by-id/virtio-$(echo ${voldata_id} | cut -c -20)"
            partition_dev="${voldata_dev}-part1"

            while [ ! -e ${voldata_dev} ]; do echo Waiting for volume ${voldata_id} to attach; sleep 1; done

            if [[ ! $(ls ${voldata_dev}-part*) ]]; then
              echo "No partitions found, create ${partition_dev}"
              echo ",+," | sfdisk ${voldata_dev}
            fi

            if [[ ! $(file -sL ${partition_dev} | grep ext4) ]]; then
              echo "Partitions not formated, format it as ext4"
              # yes /dev/disk/by-id/, partprobe and hdparm show it is there, but no it is is not ready
              sleep 1
              mkfs.ext4 ${partition_dev}
              file -sL /dev/disk/by-id/*
            fi
            mkdir -pv ${mount_dir}
            # mount on reboot
            echo "${partition_dev} ${mount_dir} ext4 defaults,nofail 0 0" >> /etc/fstab
            # mount now
            mount ${mount_dir}
          params:
            PARAM_voldata_id: { get_param: data_volume }
            PARAM_voldata_mount: { get_param: data_dir }
edit flag offensive delete link more
0

answered 2015-11-23 08:55:54 -0500

Hi boniamd,

In your case instead of executing custom script as part of User-Data on launching a VM, it can be executed by defining a separate resource software-config/deploy and make it as dependency to volume attachment task so that the script execution will be done only after volume is attached.

Please refer cirros-hello-world.yaml heat template on how to define a software config resource to execute shell script.

edit flag offensive delete link more

Comments

Hi Jayaprakash,

I am working on pushing scripts to VM while boot up. I used the link cirros-hello-world.yaml that you mentioned. Heat stack created successfully but there is no effect of the script on cirros. There is no file created. Could you please give me some advice

drn gravatar imagedrn ( 2015-11-27 05:37:22 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2015-11-22 06:22:39 -0500

Seen: 8,689 times

Last updated: Nov 23 '15