customizing-images.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Customizing Images
  3. ******************
  4. You can customize images to satisfy particular requirements. This
  5. section describes several methods and provides guidelines for each.
  6. Customizing Images Using ``local.conf``
  7. =======================================
  8. Probably the easiest way to customize an image is to add a package by
  9. way of the ``local.conf`` configuration file. Because it is limited to
  10. local use, this method generally only allows you to add packages and is
  11. not as flexible as creating your own customized image. When you add
  12. packages using local variables this way, you need to realize that these
  13. variable changes are in effect for every build and consequently affect
  14. all images, which might not be what you require.
  15. To add a package to your image using the local configuration file, use
  16. the :term:`IMAGE_INSTALL` variable with the ``:append`` operator::
  17. IMAGE_INSTALL:append = " strace"
  18. Use of the syntax is important; specifically, the leading space
  19. after the opening quote and before the package name, which is
  20. ``strace`` in this example. This space is required since the ``:append``
  21. operator does not add the space.
  22. Furthermore, you must use ``:append`` instead of the ``+=`` operator if
  23. you want to avoid ordering issues. The reason for this is because doing
  24. so unconditionally appends to the variable and avoids ordering problems
  25. due to the variable being set in image recipes and ``.bbclass`` files
  26. with operators like ``?=``. Using ``:append`` ensures the operation
  27. takes effect.
  28. As shown in its simplest use, ``IMAGE_INSTALL:append`` affects all
  29. images. It is possible to extend the syntax so that the variable applies
  30. to a specific image only. Here is an example::
  31. IMAGE_INSTALL:append:pn-core-image-minimal = " strace"
  32. This example adds ``strace`` to the ``core-image-minimal`` image only.
  33. You can add packages using a similar approach through the
  34. :term:`CORE_IMAGE_EXTRA_INSTALL` variable. If you use this variable, only
  35. ``core-image-*`` images are affected.
  36. Customizing Images Using Custom ``IMAGE_FEATURES`` and ``EXTRA_IMAGE_FEATURES``
  37. ===============================================================================
  38. Another method for customizing your image is to enable or disable
  39. high-level image features by using the
  40. :term:`IMAGE_FEATURES` and
  41. :term:`EXTRA_IMAGE_FEATURES`
  42. variables. Although the functions for both variables are nearly
  43. equivalent, best practices dictate using :term:`IMAGE_FEATURES` from within
  44. a recipe and using :term:`EXTRA_IMAGE_FEATURES` from within your
  45. ``local.conf`` file, which is found in the :term:`Build Directory`.
  46. To understand how these features work, the best reference is
  47. :ref:`meta/classes-recipe/image.bbclass <ref-classes-image>`.
  48. This class lists out the available
  49. :term:`IMAGE_FEATURES` of which most map to package groups while some, such
  50. as ``read-only-rootfs``, resolve as general configuration settings.
  51. In summary, the file looks at the contents of the :term:`IMAGE_FEATURES`
  52. variable and then maps or configures the feature accordingly. Based on
  53. this information, the build system automatically adds the appropriate
  54. packages or configurations to the
  55. :term:`IMAGE_INSTALL` variable.
  56. Effectively, you are enabling extra features by extending the class or
  57. creating a custom class for use with specialized image ``.bb`` files.
  58. Use the :term:`EXTRA_IMAGE_FEATURES` variable from within your local
  59. configuration file. Using a separate area from which to enable features
  60. with this variable helps you avoid overwriting the features in the image
  61. recipe that are enabled with :term:`IMAGE_FEATURES`. The value of
  62. :term:`EXTRA_IMAGE_FEATURES` is added to :term:`IMAGE_FEATURES` within
  63. ``meta/conf/bitbake.conf``.
  64. To illustrate how you can use these variables to modify your image, consider an
  65. example that selects the SSH server. The Yocto Project ships with two SSH
  66. servers you can use with your images: Dropbear and OpenSSH. Dropbear is a
  67. minimal SSH server appropriate for resource-constrained environments, while
  68. OpenSSH is a well-known standard SSH server implementation. By default, the
  69. ``core-image-sato`` image is configured to use Dropbear. The
  70. ``core-image-full-cmdline`` image includes OpenSSH. The ``core-image-minimal``
  71. image does not contain an SSH server.
  72. You can customize your image and change these defaults. Edit the
  73. :term:`IMAGE_FEATURES` variable in your recipe or use the
  74. :term:`EXTRA_IMAGE_FEATURES` in your ``local.conf`` file so that it
  75. configures the image you are working with to include
  76. ``ssh-server-dropbear`` or ``ssh-server-openssh``.
  77. .. note::
  78. See the ":ref:`ref-manual/features:image features`" section in the Yocto
  79. Project Reference Manual for a complete list of image features that ship
  80. with the Yocto Project.
  81. Customizing Images Using Custom .bb Files
  82. =========================================
  83. You can also customize an image by creating a custom recipe that defines
  84. additional software as part of the image. The following example shows
  85. the form for the two lines you need::
  86. IMAGE_INSTALL = "packagegroup-core-x11-base package1 package2"
  87. inherit core-image
  88. Defining the software using a custom recipe gives you total control over
  89. the contents of the image. It is important to use the correct names of
  90. packages in the :term:`IMAGE_INSTALL` variable. You must use the
  91. OpenEmbedded notation and not the Debian notation for the names (e.g.
  92. ``glibc-dev`` instead of ``libc6-dev``).
  93. The other method for creating a custom image is to base it on an
  94. existing image. For example, if you want to create an image based on
  95. ``core-image-sato`` but add the additional package ``strace`` to the
  96. image, copy the ``meta/recipes-sato/images/core-image-sato.bb`` to a new
  97. ``.bb`` and add the following line to the end of the copy::
  98. IMAGE_INSTALL += "strace"
  99. Customizing Images Using Custom Package Groups
  100. ==============================================
  101. For complex custom images, the best approach for customizing an image is
  102. to create a custom package group recipe that is used to build the image
  103. or images. A good example of a package group recipe is
  104. ``meta/recipes-core/packagegroups/packagegroup-base.bb``.
  105. If you examine that recipe, you see that the :term:`PACKAGES` variable lists
  106. the package group packages to produce. The ``inherit packagegroup``
  107. statement sets appropriate default values and automatically adds
  108. ``-dev``, ``-dbg``, and ``-ptest`` complementary packages for each
  109. package specified in the :term:`PACKAGES` statement.
  110. .. note::
  111. The ``inherit packagegroup`` line should be located near the top of the
  112. recipe, certainly before the :term:`PACKAGES` statement.
  113. For each package you specify in :term:`PACKAGES`, you can use :term:`RDEPENDS`
  114. and :term:`RRECOMMENDS` entries to provide a list of packages the parent
  115. task package should contain. You can see examples of these further down
  116. in the ``packagegroup-base.bb`` recipe.
  117. Here is a short, fabricated example showing the same basic pieces for a
  118. hypothetical packagegroup defined in ``packagegroup-custom.bb``, where
  119. the variable :term:`PN` is the standard way to abbreviate the reference to
  120. the full packagegroup name ``packagegroup-custom``::
  121. DESCRIPTION = "My Custom Package Groups"
  122. inherit packagegroup
  123. PACKAGES = "\
  124. ${PN}-apps \
  125. ${PN}-tools \
  126. "
  127. RDEPENDS:${PN}-apps = "\
  128. dropbear \
  129. portmap \
  130. psplash"
  131. RDEPENDS:${PN}-tools = "\
  132. oprofile \
  133. oprofileui-server \
  134. lttng-tools"
  135. RRECOMMENDS:${PN}-tools = "\
  136. kernel-module-oprofile"
  137. In the previous example, two package group packages are created with
  138. their dependencies and their recommended package dependencies listed:
  139. ``packagegroup-custom-apps``, and ``packagegroup-custom-tools``. To
  140. build an image using these package group packages, you need to add
  141. ``packagegroup-custom-apps`` and/or ``packagegroup-custom-tools`` to
  142. :term:`IMAGE_INSTALL`. For other forms of image dependencies see the other
  143. areas of this section.
  144. Customizing an Image Hostname
  145. =============================
  146. By default, the configured hostname (i.e. ``/etc/hostname``) in an image
  147. is the same as the machine name. For example, if
  148. :term:`MACHINE` equals "qemux86", the
  149. configured hostname written to ``/etc/hostname`` is "qemux86".
  150. You can customize this name by altering the value of the "hostname"
  151. variable in the ``base-files`` recipe using either an append file or a
  152. configuration file. Use the following in an append file::
  153. hostname = "myhostname"
  154. Use the following in a configuration file::
  155. hostname:pn-base-files = "myhostname"
  156. Changing the default value of the variable "hostname" can be useful in
  157. certain situations. For example, suppose you need to do extensive
  158. testing on an image and you would like to easily identify the image
  159. under test from existing images with typical default hostnames. In this
  160. situation, you could change the default hostname to "testme", which
  161. results in all the images using the name "testme". Once testing is
  162. complete and you do not need to rebuild the image for test any longer,
  163. you can easily reset the default hostname.
  164. Another point of interest is that if you unset the variable, the image
  165. will have no default hostname in the filesystem. Here is an example that
  166. unsets the variable in a configuration file::
  167. hostname:pn-base-files = ""
  168. Having no default hostname in the filesystem is suitable for
  169. environments that use dynamic hostnames such as virtual machines.