Back to home page

OSCL-LXR

 
 

    


0001 =======================
0002 Early userspace support
0003 =======================
0004 
0005 Last update: 2004-12-20 tlh
0006 
0007 
0008 "Early userspace" is a set of libraries and programs that provide
0009 various pieces of functionality that are important enough to be
0010 available while a Linux kernel is coming up, but that don't need to be
0011 run inside the kernel itself.
0012 
0013 It consists of several major infrastructure components:
0014 
0015 - gen_init_cpio, a program that builds a cpio-format archive
0016   containing a root filesystem image.  This archive is compressed, and
0017   the compressed image is linked into the kernel image.
0018 - initramfs, a chunk of code that unpacks the compressed cpio image
0019   midway through the kernel boot process.
0020 - klibc, a userspace C library, currently packaged separately, that is
0021   optimized for correctness and small size.
0022 
0023 The cpio file format used by initramfs is the "newc" (aka "cpio -H newc")
0024 format, and is documented in the file "buffer-format.txt".  There are
0025 two ways to add an early userspace image: specify an existing cpio
0026 archive to be used as the image or have the kernel build process build
0027 the image from specifications.
0028 
0029 CPIO ARCHIVE method
0030 -------------------
0031 
0032 You can create a cpio archive that contains the early userspace image.
0033 Your cpio archive should be specified in CONFIG_INITRAMFS_SOURCE and it
0034 will be used directly.  Only a single cpio file may be specified in
0035 CONFIG_INITRAMFS_SOURCE and directory and file names are not allowed in
0036 combination with a cpio archive.
0037 
0038 IMAGE BUILDING method
0039 ---------------------
0040 
0041 The kernel build process can also build an early userspace image from
0042 source parts rather than supplying a cpio archive.  This method provides
0043 a way to create images with root-owned files even though the image was
0044 built by an unprivileged user.
0045 
0046 The image is specified as one or more sources in
0047 CONFIG_INITRAMFS_SOURCE.  Sources can be either directories or files -
0048 cpio archives are *not* allowed when building from sources.
0049 
0050 A source directory will have it and all of its contents packaged.  The
0051 specified directory name will be mapped to '/'.  When packaging a
0052 directory, limited user and group ID translation can be performed.
0053 INITRAMFS_ROOT_UID can be set to a user ID that needs to be mapped to
0054 user root (0).  INITRAMFS_ROOT_GID can be set to a group ID that needs
0055 to be mapped to group root (0).
0056 
0057 A source file must be directives in the format required by the
0058 usr/gen_init_cpio utility (run 'usr/gen_init_cpio -h' to get the
0059 file format).  The directives in the file will be passed directly to
0060 usr/gen_init_cpio.
0061 
0062 When a combination of directories and files are specified then the
0063 initramfs image will be an aggregate of all of them.  In this way a user
0064 can create a 'root-image' directory and install all files into it.
0065 Because device-special files cannot be created by a unprivileged user,
0066 special files can be listed in a 'root-files' file.  Both 'root-image'
0067 and 'root-files' can be listed in CONFIG_INITRAMFS_SOURCE and a complete
0068 early userspace image can be built by an unprivileged user.
0069 
0070 As a technical note, when directories and files are specified, the
0071 entire CONFIG_INITRAMFS_SOURCE is passed to
0072 usr/gen_initramfs.sh.  This means that CONFIG_INITRAMFS_SOURCE
0073 can really be interpreted as any legal argument to
0074 gen_initramfs.sh.  If a directory is specified as an argument then
0075 the contents are scanned, uid/gid translation is performed, and
0076 usr/gen_init_cpio file directives are output.  If a directory is
0077 specified as an argument to usr/gen_initramfs.sh then the
0078 contents of the file are simply copied to the output.  All of the output
0079 directives from directory scanning and file contents copying are
0080 processed by usr/gen_init_cpio.
0081 
0082 See also 'usr/gen_initramfs.sh -h'.
0083 
0084 Where's this all leading?
0085 =========================
0086 
0087 The klibc distribution contains some of the necessary software to make
0088 early userspace useful.  The klibc distribution is currently
0089 maintained separately from the kernel.
0090 
0091 You can obtain somewhat infrequent snapshots of klibc from
0092 https://www.kernel.org/pub/linux/libs/klibc/
0093 
0094 For active users, you are better off using the klibc git
0095 repository, at https://git.kernel.org/?p=libs/klibc/klibc.git
0096 
0097 The standalone klibc distribution currently provides three components,
0098 in addition to the klibc library:
0099 
0100 - ipconfig, a program that configures network interfaces.  It can
0101   configure them statically, or use DHCP to obtain information
0102   dynamically (aka "IP autoconfiguration").
0103 - nfsmount, a program that can mount an NFS filesystem.
0104 - kinit, the "glue" that uses ipconfig and nfsmount to replace the old
0105   support for IP autoconfig, mount a filesystem over NFS, and continue
0106   system boot using that filesystem as root.
0107 
0108 kinit is built as a single statically linked binary to save space.
0109 
0110 Eventually, several more chunks of kernel functionality will hopefully
0111 move to early userspace:
0112 
0113 - Almost all of init/do_mounts* (the beginning of this is already in
0114   place)
0115 - ACPI table parsing
0116 - Insert unwieldy subsystem that doesn't really need to be in kernel
0117   space here
0118 
0119 If kinit doesn't meet your current needs and you've got bytes to burn,
0120 the klibc distribution includes a small Bourne-compatible shell (ash)
0121 and a number of other utilities, so you can replace kinit and build
0122 custom initramfs images that meet your needs exactly.
0123 
0124 For questions and help, you can sign up for the early userspace
0125 mailing list at https://www.zytor.com/mailman/listinfo/klibc
0126 
0127 How does it work?
0128 =================
0129 
0130 The kernel has currently 3 ways to mount the root filesystem:
0131 
0132 a) all required device and filesystem drivers compiled into the kernel, no
0133    initrd.  init/main.c:init() will call prepare_namespace() to mount the
0134    final root filesystem, based on the root= option and optional init= to run
0135    some other init binary than listed at the end of init/main.c:init().
0136 
0137 b) some device and filesystem drivers built as modules and stored in an
0138    initrd.  The initrd must contain a binary '/linuxrc' which is supposed to
0139    load these driver modules.  It is also possible to mount the final root
0140    filesystem via linuxrc and use the pivot_root syscall.  The initrd is
0141    mounted and executed via prepare_namespace().
0142 
0143 c) using initramfs.  The call to prepare_namespace() must be skipped.
0144    This means that a binary must do all the work.  Said binary can be stored
0145    into initramfs either via modifying usr/gen_init_cpio.c or via the new
0146    initrd format, an cpio archive.  It must be called "/init".  This binary
0147    is responsible to do all the things prepare_namespace() would do.
0148 
0149    To maintain backwards compatibility, the /init binary will only run if it
0150    comes via an initramfs cpio archive.  If this is not the case,
0151    init/main.c:init() will run prepare_namespace() to mount the final root
0152    and exec one of the predefined init binaries.
0153 
0154 Bryan O'Sullivan <bos@serpentine.com>