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