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