early_userspace_support.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. =======================
  2. Early userspace support
  3. =======================
  4. Last update: 2004-12-20 tlh
  5. "Early userspace" is a set of libraries and programs that provide
  6. various pieces of functionality that are important enough to be
  7. available while a Linux kernel is coming up, but that don't need to be
  8. run inside the kernel itself.
  9. It consists of several major infrastructure components:
  10. - gen_init_cpio, a program that builds a cpio-format archive
  11. containing a root filesystem image. This archive is compressed, and
  12. the compressed image is linked into the kernel image.
  13. - initramfs, a chunk of code that unpacks the compressed cpio image
  14. midway through the kernel boot process.
  15. - klibc, a userspace C library, currently packaged separately, that is
  16. optimized for correctness and small size.
  17. The cpio file format used by initramfs is the "newc" (aka "cpio -H newc")
  18. format, and is documented in the file "buffer-format.txt". There are
  19. two ways to add an early userspace image: specify an existing cpio
  20. archive to be used as the image or have the kernel build process build
  21. the image from specifications.
  22. CPIO ARCHIVE method
  23. -------------------
  24. You can create a cpio archive that contains the early userspace image.
  25. Your cpio archive should be specified in CONFIG_INITRAMFS_SOURCE and it
  26. will be used directly. Only a single cpio file may be specified in
  27. CONFIG_INITRAMFS_SOURCE and directory and file names are not allowed in
  28. combination with a cpio archive.
  29. IMAGE BUILDING method
  30. ---------------------
  31. The kernel build process can also build an early userspace image from
  32. source parts rather than supplying a cpio archive. This method provides
  33. a way to create images with root-owned files even though the image was
  34. built by an unprivileged user.
  35. The image is specified as one or more sources in
  36. CONFIG_INITRAMFS_SOURCE. Sources can be either directories or files -
  37. cpio archives are *not* allowed when building from sources.
  38. A source directory will have it and all of its contents packaged. The
  39. specified directory name will be mapped to '/'. When packaging a
  40. directory, limited user and group ID translation can be performed.
  41. INITRAMFS_ROOT_UID can be set to a user ID that needs to be mapped to
  42. user root (0). INITRAMFS_ROOT_GID can be set to a group ID that needs
  43. to be mapped to group root (0).
  44. A source file must be directives in the format required by the
  45. usr/gen_init_cpio utility (run 'usr/gen_init_cpio -h' to get the
  46. file format). The directives in the file will be passed directly to
  47. usr/gen_init_cpio.
  48. When a combination of directories and files are specified then the
  49. initramfs image will be an aggregate of all of them. In this way a user
  50. can create a 'root-image' directory and install all files into it.
  51. Because device-special files cannot be created by a unprivileged user,
  52. special files can be listed in a 'root-files' file. Both 'root-image'
  53. and 'root-files' can be listed in CONFIG_INITRAMFS_SOURCE and a complete
  54. early userspace image can be built by an unprivileged user.
  55. As a technical note, when directories and files are specified, the
  56. entire CONFIG_INITRAMFS_SOURCE is passed to
  57. usr/gen_initramfs.sh. This means that CONFIG_INITRAMFS_SOURCE
  58. can really be interpreted as any legal argument to
  59. gen_initramfs.sh. If a directory is specified as an argument then
  60. the contents are scanned, uid/gid translation is performed, and
  61. usr/gen_init_cpio file directives are output. If a directory is
  62. specified as an argument to usr/gen_initramfs.sh then the
  63. contents of the file are simply copied to the output. All of the output
  64. directives from directory scanning and file contents copying are
  65. processed by usr/gen_init_cpio.
  66. See also 'usr/gen_initramfs.sh -h'.
  67. Where's this all leading?
  68. =========================
  69. The klibc distribution contains some of the necessary software to make
  70. early userspace useful. The klibc distribution is currently
  71. maintained separately from the kernel.
  72. You can obtain somewhat infrequent snapshots of klibc from
  73. https://www.kernel.org/pub/linux/libs/klibc/
  74. For active users, you are better off using the klibc git
  75. repository, at https://git.kernel.org/?p=libs/klibc/klibc.git
  76. The standalone klibc distribution currently provides three components,
  77. in addition to the klibc library:
  78. - ipconfig, a program that configures network interfaces. It can
  79. configure them statically, or use DHCP to obtain information
  80. dynamically (aka "IP autoconfiguration").
  81. - nfsmount, a program that can mount an NFS filesystem.
  82. - kinit, the "glue" that uses ipconfig and nfsmount to replace the old
  83. support for IP autoconfig, mount a filesystem over NFS, and continue
  84. system boot using that filesystem as root.
  85. kinit is built as a single statically linked binary to save space.
  86. Eventually, several more chunks of kernel functionality will hopefully
  87. move to early userspace:
  88. - Almost all of init/do_mounts* (the beginning of this is already in
  89. place)
  90. - ACPI table parsing
  91. - Insert unwieldy subsystem that doesn't really need to be in kernel
  92. space here
  93. If kinit doesn't meet your current needs and you've got bytes to burn,
  94. the klibc distribution includes a small Bourne-compatible shell (ash)
  95. and a number of other utilities, so you can replace kinit and build
  96. custom initramfs images that meet your needs exactly.
  97. For questions and help, you can sign up for the early userspace
  98. mailing list at https://www.zytor.com/mailman/listinfo/klibc
  99. How does it work?
  100. =================
  101. The kernel has currently 3 ways to mount the root filesystem:
  102. a) all required device and filesystem drivers compiled into the kernel, no
  103. initrd. init/main.c:init() will call prepare_namespace() to mount the
  104. final root filesystem, based on the root= option and optional init= to run
  105. some other init binary than listed at the end of init/main.c:init().
  106. b) some device and filesystem drivers built as modules and stored in an
  107. initrd. The initrd must contain a binary '/linuxrc' which is supposed to
  108. load these driver modules. It is also possible to mount the final root
  109. filesystem via linuxrc and use the pivot_root syscall. The initrd is
  110. mounted and executed via prepare_namespace().
  111. c) using initramfs. The call to prepare_namespace() must be skipped.
  112. This means that a binary must do all the work. Said binary can be stored
  113. into initramfs either via modifying usr/gen_init_cpio.c or via the new
  114. initrd format, an cpio archive. It must be called "/init". This binary
  115. is responsible to do all the things prepare_namespace() would do.
  116. To maintain backwards compatibility, the /init binary will only run if it
  117. comes via an initramfs cpio archive. If this is not the case,
  118. init/main.c:init() will run prepare_namespace() to mount the final root
  119. and exec one of the predefined init binaries.
  120. Bryan O'Sullivan <bos@serpentine.com>