read-only-rootfs.rst 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Creating a Read-Only Root Filesystem
  3. ************************************
  4. Suppose, for security reasons, you need to disable your target device's
  5. root filesystem's write permissions (i.e. you need a read-only root
  6. filesystem). Or, perhaps you are running the device's operating system
  7. from a read-only storage device. For either case, you can customize your
  8. image for that behavior.
  9. .. note::
  10. Supporting a read-only root filesystem requires that the system and
  11. applications do not try to write to the root filesystem. You must
  12. configure all parts of the target system to write elsewhere, or to
  13. gracefully fail in the event of attempting to write to the root
  14. filesystem.
  15. Creating the Root Filesystem
  16. ============================
  17. To create the read-only root filesystem, simply add the
  18. "read-only-rootfs" feature to your image, normally in one of two ways.
  19. The first way is to add the "read-only-rootfs" image feature in the
  20. image's recipe file via the :term:`IMAGE_FEATURES` variable::
  21. IMAGE_FEATURES += "read-only-rootfs"
  22. As an alternative, you can add the same feature
  23. from within your :term:`Build Directory`'s ``local.conf`` file with the
  24. associated :term:`EXTRA_IMAGE_FEATURES` variable, as in::
  25. EXTRA_IMAGE_FEATURES = "read-only-rootfs"
  26. For more information on how to use these variables, see the
  27. ":ref:`dev-manual/customizing-images:Customizing Images Using Custom \`\`IMAGE_FEATURES\`\` and \`\`EXTRA_IMAGE_FEATURES\`\``"
  28. section. For information on the variables, see
  29. :term:`IMAGE_FEATURES` and
  30. :term:`EXTRA_IMAGE_FEATURES`.
  31. Post-Installation Scripts and Read-Only Root Filesystem
  32. =======================================================
  33. It is very important that you make sure all post-Installation
  34. (``pkg_postinst``) scripts for packages that are installed into the
  35. image can be run at the time when the root filesystem is created during
  36. the build on the host system. These scripts cannot attempt to run during
  37. the first boot on the target device. With the "read-only-rootfs" feature
  38. enabled, the build system makes sure that all post-installation scripts
  39. succeed at file system creation time. If any of these scripts
  40. still need to be run after the root filesystem is created, the build
  41. immediately fails. These build-time checks ensure that the build fails
  42. rather than the target device fails later during its initial boot
  43. operation.
  44. Most of the common post-installation scripts generated by the build
  45. system for the out-of-the-box Yocto Project are engineered so that they
  46. can run during root filesystem creation (e.g. post-installation scripts
  47. for caching fonts). However, if you create and add custom scripts, you
  48. need to be sure they can be run during this file system creation.
  49. Here are some common problems that prevent post-installation scripts
  50. from running during root filesystem creation:
  51. - *Not using $D in front of absolute paths:* The build system defines
  52. ``$``\ :term:`D` when the root
  53. filesystem is created. Furthermore, ``$D`` is blank when the script
  54. is run on the target device. This implies two purposes for ``$D``:
  55. ensuring paths are valid in both the host and target environments,
  56. and checking to determine which environment is being used as a method
  57. for taking appropriate actions.
  58. - *Attempting to run processes that are specific to or dependent on the
  59. target architecture:* You can work around these attempts by using
  60. native tools, which run on the host system, to accomplish the same
  61. tasks, or by alternatively running the processes under QEMU, which
  62. has the ``qemu_run_binary`` function. For more information, see the
  63. :ref:`ref-classes-qemu` class.
  64. Areas With Write Access
  65. =======================
  66. With the "read-only-rootfs" feature enabled, any attempt by the target
  67. to write to the root filesystem at runtime fails. Consequently, you must
  68. make sure that you configure processes and applications that attempt
  69. these types of writes do so to directories with write access (e.g.
  70. ``/tmp`` or ``/var/run``).