User-Data and Meta-Data

CloudStack provides API access to attach up to 2KB of data after base64 encoding to a deployed VM. Using HTTP POST(via POST body), you can send up to 32K of data after base64 encoding. Deployed VMs also have access to instance metadata via the virtual router.

Create virtual machine thru the API: deployVirtualMachine using the parameter userdata= to include user-data formated in base64.

Accessed user-data from VM. Once the IP address of the virtual router is known, use the following steps to retrieve user-data:

  1. Run the following command to find the virtual router.

    # cat /var/lib/dhclient/dhclient-eth0.leases | grep dhcp-server-identifier | tail -1
    
  2. Access user-data by running the following command using the result of the above command

    # curl http://10.1.1.1/latest/user-data
    

Meta Data can be accessed similarly, using a URL of the form http://10.1.1.1/latest/meta-data/{metadata type}. (For backwards compatibility, the previous URL http://10.1.1.1/latest/{metadata type} is also supported.) For metadata type, use one of the following:

  • service-offering. A description of the VMs service offering
  • availability-zone. The Zone name
  • local-ipv4. The guest IP of the VM
  • local-hostname. The hostname of the VM
  • public-ipv4. The first public IP for the router. (E.g. the first IP of eth2)
  • public-hostname. This is the same as public-ipv4
  • instance-id. The instance name of the VM

Using Cloud-Init

Cloud-Init can be use to access an interpret user-data from virtual machines. Cloud-Init be installed into templates and also require CloudStack password and sshkey scripts (Adding Password Management to Your Templates and using ssh keys). User password management and resetSSHKeyForVirtualMachine API are not yet supported by cloud-init.

  1. Install cloud-init package into a template:

    # yum install cloud-init
      or
    $ sudo apt-get install cloud-init
    
  2. Create datasource configuration file: /etc/cloud/cloud.cfg.d/99_cloudstack.cfg

    datasource:
      CloudStack: {}
      None: {}
    datasource_list:
      - CloudStack
    

user-data example

This example use cloud-init to Upgrade Operating-System of the newly created VM:

#cloud-config

# Upgrade the instance on first boot
# (ie run apt-get upgrade)
#
# Default: false
# Aliases: apt_upgrade
package_upgrade: true

base64 formated:

I2Nsb3VkLWNvbmZpZw0KDQojIFVwZ3JhZGUgdGhlIGluc3RhbmNlIG9uIGZpcnN0IGJvb3QNCiMgKGllIHJ1biBhcHQtZ2V0IHVwZ3JhZGUpDQojDQojIERlZmF1bHQ6IGZhbHNlDQojIEFsaWFzZXM6IGFwdF91cGdyYWRlDQpwYWNrYWdlX3VwZ3JhZGU6IHRydWUNCg==

Refer to Cloud-Init CloudStack datasource documentation for latest capabilities. Cloud-Init and Cloud-Init CloudStack datasource are not supported by Apache CloudStack community.