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