summaryrefslogtreecommitdiff
path: root/doc/source/user/metadata.rst
blob: 65f5bddc966ce5c5826a0f82bd36cafe39c4b873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
========
Metadata
========

Nova presents configuration information to instances it starts via a mechanism
called metadata. These mechanisms are widely used via helpers such as
`cloud-init`_ to specify things like the root password the instance should use.

This metadata is made available via either a *config drive* or the *metadata
service* and can be somewhat customised by the user using the *user data*
feature. This guide provides an overview of these features along with a summary
of the types of metadata available.

.. _cloud-init: https://cloudinit.readthedocs.io/en/latest/


Types of metadata
-----------------

There are three separate groups of users who need to be able to specify
metadata for an instance.

User provided data
~~~~~~~~~~~~~~~~~~

The user who booted the instance can pass metadata to the instance in several
ways. For authentication keypairs, the keypairs functionality of the nova API
can be used to upload a key and then specify that key during the nova boot API
request. For less structured data, a small opaque blob of data may be passed
via the :ref:`user data <metadata-userdata>` feature of the nova API. Examples
of such unstructured data would be the puppet role that the instance should use,
or the HTTP address of a server from which to fetch post-boot configuration
information.

Nova provided data
~~~~~~~~~~~~~~~~~~

Nova itself needs to pass information to the instance via its internal
implementation of the metadata system. Such information includes the requested
hostname for the instance and the availability zone the instance is in. This
happens by default and requires no configuration by the user or deployer.

Nova provides both an :ref:`OpenStack metadata API <metadata-openstack-format>`
and an :ref:`EC2-compatible API <metadata-ec2-format>`. Both the OpenStack
metadata and EC2-compatible APIs are versioned by date. These are described
later.

Deployer provided data
~~~~~~~~~~~~~~~~~~~~~~

A deployer of OpenStack may need to pass data to an instance. It is also
possible that this data is not known to the user starting the instance. An
example might be a cryptographic token to be used to register the instance with
Active Directory post boot -- the user starting the instance should not have
access to Active Directory to create this token, but the nova deployment might
have permissions to generate the token on the user's behalf. This is possible
using the :ref:`vendordata <metadata-vendordata>` feature, which must be
configured by your cloud operator.



.. _metadata-service:

The metadata service
--------------------

.. note::

    This section provides end user information about the metadata service. For
    deployment information about the metadata service, refer to the :doc:`admin
    guide </admin/metadata-service>`.

The *metadata service* provides a way for instances to retrieve
instance-specific data via a REST API. Instances access this service at
``169.254.169.254`` or at ``fe80::a9fe:a9fe``. All types of metadata,
be it user-, nova- or vendor-provided, can be accessed via this service.

.. versionchanged:: 22.0.0

    Starting with the Victoria release the metadata service is accessible
    over IPv6 at the link-local address ``fe80::a9fe:a9fe``.

.. note::

    As with all IPv6 link-local addresses, the metadata IPv6
    address is not complete without a zone identifier (in a Linux
    guest that is usually the interface name concatenated after
    a percent sign). Please also note that in URLs you should
    URL-encode the percent sign itself. For example, assuming
    that the primary network interface in the guest is ``ens2``
    substitute ``http://[fe80::a9fe:a9fe%25ens2]:80/...`` for
    ``http://169.254.169.254/...``.

Using the metadata service
~~~~~~~~~~~~~~~~~~~~~~~~~~

To retrieve a list of supported versions for the :ref:`OpenStack metadata API
<metadata-openstack-format>`, make a GET request to
``http://169.254.169.254/openstack``, which will return a list of directories:

.. code-block:: console

   $ curl http://169.254.169.254/openstack
   2012-08-10
   2013-04-04
   2013-10-17
   2015-10-15
   2016-06-30
   2016-10-06
   2017-02-22
   2018-08-27
   latest

Refer to :ref:`OpenStack format metadata <metadata-openstack-format>` for
information on the contents and structure of these directories.

To list supported versions for the :ref:`EC2-compatible metadata API
<metadata-ec2-format>`, make a GET request to ``http://169.254.169.254``, which
will, once again, return a list of directories:

.. code-block:: console

   $ curl http://169.254.169.254
   1.0
   2007-01-19
   2007-03-01
   2007-08-29
   2007-10-10
   2007-12-15
   2008-02-01
   2008-09-01
   2009-04-04
   latest

Refer to :ref:`EC2-compatible metadata <metadata-ec2-format>` for information on
the contents and structure of these directories.


.. _metadata-config-drive:

Config drives
-------------

.. note::

    This section provides end user information about config drives. For
    deployment information about the config drive feature, refer to the
    :doc:`admin guide </admin/config-drive>`.

*Config drives* are special drives that are attached to an instance when
it boots. The instance can mount this drive and read files from it to get
information that is normally available through the metadata service.

One use case for using the config drive is to pass a networking configuration
when you do not use DHCP to assign IP addresses to instances. For example, you
might pass the IP address configuration for the instance through the config
drive, which the instance can mount and access before you configure the network
settings for the instance.

Using the config drive
~~~~~~~~~~~~~~~~~~~~~~

To enable the config drive for an instance, pass the ``--config-drive true``
parameter to the :command:`openstack server create` command.

The following example enables the config drive and passes a user data file and
two key/value metadata pairs, all of which are accessible from the config
drive:

.. code-block:: console

   $ openstack server create --config-drive true --image my-image-name \
       --flavor 1 --key-name mykey --user-data ./my-user-data.txt \
       --property role=webservers --property essential=false MYINSTANCE

.. note::

    The Compute service can be configured to always create a config drive. For
    more information, refer to :doc:`the admin guide </admin/config-drive>`.

If your guest operating system supports accessing disk by label, you can mount
the config drive as the ``/dev/disk/by-label/configurationDriveVolumeLabel``
device. In the following example, the config drive has the ``config-2`` volume
label:

.. code-block:: console

   # mkdir -p /mnt/config
   # mount /dev/disk/by-label/config-2 /mnt/config

If your guest operating system does not use ``udev``, the ``/dev/disk/by-label``
directory is not present. You can use the :command:`blkid` command to identify
the block device that corresponds to the config drive. For example:

.. code-block:: console

   # blkid -t LABEL="config-2" -odevice
   /dev/vdb

Once identified, you can mount the device:

.. code-block:: console

   # mkdir -p /mnt/config
   # mount /dev/vdb /mnt/config

Once mounted, you can examine the contents of the config drive:

.. code-block:: console

   $ cd /mnt/config
   $ find . -maxdepth 2
   .
   ./ec2
   ./ec2/2009-04-04
   ./ec2/latest
   ./openstack
   ./openstack/2012-08-10
   ./openstack/2013-04-04
   ./openstack/2013-10-17
   ./openstack/2015-10-15
   ./openstack/2016-06-30
   ./openstack/2016-10-06
   ./openstack/2017-02-22
   ./openstack/latest

The files that appear on the config drive depend on the arguments that you pass
to the :command:`openstack server create` command. The format of this directory
is the same as that provided by the :ref:`metadata service <metadata-service>`,
with the exception that the EC2-compatible metadata is now located in the
``ec2`` directory instead of the root (``/``) directory. Refer to the
:ref:`metadata-openstack-format` and :ref:`metadata-ec2-format` sections for
information about the format of the files and subdirectories within these
directories.


Setting in image
~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: console

    $ openstack image set IMG-UUID --property img_config_drive=mandatory

The img_config_drive image metadata property can be used to force enable the config drive.
Setting img_config_drive specifies whether the image needs a config drive.


Nova metadata
-------------

As noted previously, nova provides its metadata in two formats: OpenStack format
and EC2-compatible format.

.. _metadata-openstack-format:

OpenStack format metadata
~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionchanged:: 12.0.0

    Support for network metadata was added in the Liberty release.

Metadata from the OpenStack API is distributed in JSON format. There are two
files provided for each version: ``meta_data.json`` and ``network_data.json``.
The ``meta_data.json`` file contains nova-specific information, while the
``network_data.json`` file contains information retrieved from neutron. For
example:

.. code-block:: console

   $ curl http://169.254.169.254/openstack/2018-08-27/meta_data.json

.. code-block:: json

   {
      "random_seed": "yu5ZnkqF2CqnDZVAfZgarG...",
      "availability_zone": "nova",
      "keys": [
          {
            "data": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n",
            "type": "ssh",
            "name": "mykey"
          }
      ],
      "hostname": "test.novalocal",
      "launch_index": 0,
      "meta": {
         "priority": "low",
         "role": "webserver"
      },
      "devices": [
          {
            "type": "nic",
            "bus": "pci",
            "address": "0000:00:02.0",
            "mac": "00:11:22:33:44:55",
            "tags": ["trusted"]
          },
          {
            "type": "disk",
            "bus": "ide",
            "address": "0:0",
            "serial": "disk-vol-2352423",
            "path": "/dev/sda",
            "tags": ["baz"]
          }
      ],
      "project_id": "f7ac731cc11f40efbc03a9f9e1d1d21f",
      "public_keys": {
          "mykey": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n"
      },
      "name": "test"
   }

.. code-block:: console

   $ curl http://169.254.169.254/openstack/2018-08-27/network_data.json

.. code-block:: json

   {
       "links": [
           {
               "ethernet_mac_address": "fa:16:3e:9c:bf:3d",
               "id": "tapcd9f6d46-4a",
               "mtu": null,
               "type": "bridge",
               "vif_id": "cd9f6d46-4a3a-43ab-a466-994af9db96fc"
           }
       ],
       "networks": [
           {
               "id": "network0",
               "link": "tapcd9f6d46-4a",
               "network_id": "99e88329-f20d-4741-9593-25bf07847b16",
               "type": "ipv4_dhcp"
           }
       ],
       "services": [
           {
               "address": "8.8.8.8",
               "type": "dns"
           }
       ]
   }


::download:`Download</../../doc/api_schemas/network_data.json>` network_data.json JSON schema.

.. _metadata-ec2-format:

EC2-compatible metadata
~~~~~~~~~~~~~~~~~~~~~~~

The EC2-compatible API is compatible with version 2009-04-04 of the `Amazon EC2
metadata service`__ This means that virtual machine images designed for EC2 will
work properly with OpenStack.

The EC2 API exposes a separate URL for each metadata element. Retrieve a
listing of these elements by making a GET query to
``http://169.254.169.254/2009-04-04/meta-data/``. For example:

.. code-block:: console

   $ curl http://169.254.169.254/2009-04-04/meta-data/
   ami-id
   ami-launch-index
   ami-manifest-path
   block-device-mapping/
   hostname
   instance-action
   instance-id
   instance-type
   kernel-id
   local-hostname
   local-ipv4
   placement/
   public-hostname
   public-ipv4
   public-keys/
   ramdisk-id
   reservation-id
   security-groups

.. code-block:: console

   $ curl http://169.254.169.254/2009-04-04/meta-data/block-device-mapping/
   ami

.. code-block:: console

   $ curl http://169.254.169.254/2009-04-04/meta-data/placement/
   availability-zone

.. code-block:: console

   $ curl http://169.254.169.254/2009-04-04/meta-data/public-keys/
   0=mykey

Instances can retrieve the public SSH key (identified by keypair name when a
user requests a new instance) by making a GET request to
``http://169.254.169.254/2009-04-04/meta-data/public-keys/0/openssh-key``:

.. code-block:: console

   $ curl http://169.254.169.254/2009-04-04/meta-data/public-keys/0/openssh-key
   ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDYVEprvtYJXVOBN0XNKVVRNCRX6BlnNbI+US\
   LGais1sUWPwtSg7z9K9vhbYAPUZcq8c/s5S9dg5vTHbsiyPCIDOKyeHba4MUJq8Oh5b2i71/3B\
   ISpyxTBH/uZDHdslW2a+SrPDCeuMMoss9NFhBdKtDkdG9zyi0ibmCP6yMdEX8Q== Generated\
   by Nova

__ https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html


.. _metadata-userdata:

User data
---------

*User data* is a blob of data that the user can specify when they launch an
instance. The instance can access this data through the metadata service or
config drive. Commonly used to pass a shell script that the instance runs on
boot.

For example, one application that uses user data is the `cloud-init
<https://help.ubuntu.com/community/CloudInit>`__ system, which is an open-source
package from Ubuntu that is available on various Linux distributions and which
handles early initialization of a cloud instance.

You can place user data in a local file and pass it through the ``--user-data
<user-data-file>`` parameter at instance creation.

.. code-block:: console

   $ openstack server create --image ubuntu-cloudimage --flavor 1 \
       --user-data mydata.file VM_INSTANCE

.. note::

    The provided user data should not be base64-encoded, as it will be
    automatically encoded in order to pass valid input to the REST
    API, which has a limit of 65535 bytes after encoding.

Once booted, you can access this data from the instance using either the
metadata service or the config drive. To access it via the metadata service,
make a GET request to either
``http://169.254.169.254/openstack/{version}/user_data`` (OpenStack API) or
``http://169.254.169.254/{version}/user-data`` (EC2-compatible API). For
example:

.. code-block:: console

   $ curl http://169.254.169.254/openstack/2018-08-27/user_data

.. code-block:: shell

   #!/bin/bash
   echo 'Extra user data here'


.. _metadata-vendordata:

Vendordata
----------

.. note::

    This section provides end user information about the vendordata feature. For
    deployment information about this feature, refer to the :doc:`admin guide
    </admin/vendordata>`.

.. versionchanged:: 14.0.0

    Support for dynamic vendor data was added in the Newton release.

**Where configured**, instances can retrieve vendor-specific data from the
metadata service or config drive. To access it via the metadata service, make a
GET request to either
``http://169.254.169.254/openstack/{version}/vendor_data.json`` or
``http://169.254.169.254/openstack/{version}/vendor_data2.json``, depending on
the deployment. For example:

.. code-block:: console

   $ curl http://169.254.169.254/openstack/2018-08-27/vendor_data2.json

.. code-block:: json

   {
       "testing": {
           "value1": 1,
           "value2": 2,
           "value3": "three"
       }
   }

.. note::

    The presence and contents of this file will vary from deployment to
    deployment.


General guidelines
------------------

- Do not rely on the presence of the EC2 metadata in the metadata API or
  config drive, because this content might be removed in a future release. For
  example, do not rely on files in the ``ec2`` directory.

- When you create images that access metadata service or config drive data and
  multiple directories are under the ``openstack`` directory, always select the
  highest API version by date that your consumer supports.  For example, if your
  guest image supports the ``2012-03-05``, ``2012-08-05``, and ``2013-04-13``
  versions, try ``2013-04-13`` first and fall back to a previous version if
  ``2013-04-13`` is not present.