1*cf5a6c84SAndroid Build Coastguard Workermkroot - simple linux system builder 2*cf5a6c84SAndroid Build Coastguard Worker 3*cf5a6c84SAndroid Build Coastguard WorkerCompiles a toybox-based root filesystem and kernel that can boot under qemu. 4*cf5a6c84SAndroid Build Coastguard Worker 5*cf5a6c84SAndroid Build Coastguard WorkerPrebuilt binaries available from http://landley.net/bin/mkroot/latest 6*cf5a6c84SAndroid Build Coastguard Workerlaunched via ./run-qemu.sh (which assumes you have QEMU installed, KVM 7*cf5a6c84SAndroid Build Coastguard Workerworks in a pinch), and then run "exit" to shut down the emulated system. 8*cf5a6c84SAndroid Build Coastguard Worker 9*cf5a6c84SAndroid Build Coastguard WorkerThis project is a successor to https://landley.net/aboriginal/about.html 10*cf5a6c84SAndroid Build Coastguard Workerand shares most of the same goals, with a much simpler implementation. 11*cf5a6c84SAndroid Build Coastguard Worker 12*cf5a6c84SAndroid Build Coastguard Worker--- Quick Start 13*cf5a6c84SAndroid Build Coastguard Worker 14*cf5a6c84SAndroid Build Coastguard WorkerTo install the build prerequisites: download toybox source, linux kernel source, 15*cf5a6c84SAndroid Build Coastguard Workerand one or more musl cross compiler toolchain(s) in the "ccc" directory: 16*cf5a6c84SAndroid Build Coastguard Worker 17*cf5a6c84SAndroid Build Coastguard Worker $ cd toybox 18*cf5a6c84SAndroid Build Coastguard Worker $ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux 19*cf5a6c84SAndroid Build Coastguard Worker $ wget https://landley.net/bin/toolchains/latest/i686-linux-musl-cross.tar.xz 20*cf5a6c84SAndroid Build Coastguard Worker $ mkdir ccc 21*cf5a6c84SAndroid Build Coastguard Worker $ tar xvJCf ccc i686-linux-musl-cross.tar.xz 22*cf5a6c84SAndroid Build Coastguard Worker 23*cf5a6c84SAndroid Build Coastguard WorkerThen invoke mkroot like: 24*cf5a6c84SAndroid Build Coastguard Worker 25*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh CROSS=i686 LINUX=linux 26*cf5a6c84SAndroid Build Coastguard Worker $ root/i686/run-qemu.sh 27*cf5a6c84SAndroid Build Coastguard Worker 28*cf5a6c84SAndroid Build Coastguard WorkerThis project is a successor to https://landley.net/aboriginal/about.html 29*cf5a6c84SAndroid Build Coastguard Workerand shares most of the same goals, with a much simpler implementation. 30*cf5a6c84SAndroid Build Coastguard Worker 31*cf5a6c84SAndroid Build Coastguard Worker--- Building without a cross compiler (warning: glibc sucks) 32*cf5a6c84SAndroid Build Coastguard Worker 33*cf5a6c84SAndroid Build Coastguard WorkerRunning ./mkroot.sh with no arguments and no $CROSS_COMPILE environment 34*cf5a6c84SAndroid Build Coastguard Workervariable builds a statically linked root filesystem with the host's compiler. 35*cf5a6c84SAndroid Build Coastguard Worker 36*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh 37*cf5a6c84SAndroid Build Coastguard Worker 38*cf5a6c84SAndroid Build Coastguard WorkerYou can then chroot into it like this: 39*cf5a6c84SAndroid Build Coastguard Worker 40*cf5a6c84SAndroid Build Coastguard Worker $ sudo chroot output/host/root /init 41*cf5a6c84SAndroid Build Coastguard Worker $ ls -l 42*cf5a6c84SAndroid Build Coastguard Worker $ exit 43*cf5a6c84SAndroid Build Coastguard Worker 44*cf5a6c84SAndroid Build Coastguard WorkerUnfortunately, glibc doesn't properly support static linking, so if your host 45*cf5a6c84SAndroid Build Coastguard WorkerLinux uses glibc the build will spit out a bunch of warnings indicating 46*cf5a6c84SAndroid Build Coastguard Workerall sorts of glibc features won't work (DNS lookups always fail, ls -l can't 47*cf5a6c84SAndroid Build Coastguard Workerread names out of /etc/password, etc). This is a known problem with glibc, 48*cf5a6c84SAndroid Build Coastguard Workerbecause ex-maintainer Ulrich Drepper had a strong personal dislike of static 49*cf5a6c84SAndroid Build Coastguard Workerlinking and actively sabotaged it. 50*cf5a6c84SAndroid Build Coastguard Worker 51*cf5a6c84SAndroid Build Coastguard WorkerIf building on a non-glibc system, such as Alpine Linux, you're fine. 52*cf5a6c84SAndroid Build Coastguard WorkerOtherwise, you'll probably want to cross compile with a musl-libc toolchain 53*cf5a6c84SAndroid Build Coastguard Workerto avoid glibc's very long list of static linking bugs. (The resulting root 54*cf5a6c84SAndroid Build Coastguard Workerfilesystem is also significantly smaller: a stripped statically linked 55*cf5a6c84SAndroid Build Coastguard Worker"hello world" binary for x86-64 is 5420 bytes with musl-libc, and 682,696 bytes 56*cf5a6c84SAndroid Build Coastguard Workerwith glibc.)</p> 57*cf5a6c84SAndroid Build Coastguard Worker 58*cf5a6c84SAndroid Build Coastguard Worker--- Building with a cross compiler. 59*cf5a6c84SAndroid Build Coastguard Worker 60*cf5a6c84SAndroid Build Coastguard WorkerThe variable $CROSS_COMPILE indicates the toolchain prefix to apply to 61*cf5a6c84SAndroid Build Coastguard Workercommands such as "cc" and "ld". Since prefixed cross compiler names tend 62*cf5a6c84SAndroid Build Coastguard Workerto look like "armv5l-cc" this prefix tends to end with a dash. 63*cf5a6c84SAndroid Build Coastguard Worker 64*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh CROSS_COMPILE=armv5l- 65*cf5a6c84SAndroid Build Coastguard Worker 66*cf5a6c84SAndroid Build Coastguard WorkerIf you haven't added the cross compiler to your $PATH, you can specify 67*cf5a6c84SAndroid Build Coastguard Workera path as part of the prefix: 68*cf5a6c84SAndroid Build Coastguard Worker 69*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh CROSS_COMPILE=~/x86_64-linux-musl-cross/bin/x86_64-linux-musl-cross- 70*cf5a6c84SAndroid Build Coastguard Worker 71*cf5a6c84SAndroid Build Coastguard WorkerDon't forget the trailing dash. 72*cf5a6c84SAndroid Build Coastguard Worker 73*cf5a6c84SAndroid Build Coastguard WorkerAlternately, the variable $CROSS (as used in the Quick Start above) tells 74*cf5a6c84SAndroid Build Coastguard Workermkroot to look in the "ccc" directory for a cross compiler starting with 75*cf5a6c84SAndroid Build Coastguard Workera short name: 76*cf5a6c84SAndroid Build Coastguard Worker 77*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh CROSS=s390x 78*cf5a6c84SAndroid Build Coastguard Worker 79*cf5a6c84SAndroid Build Coastguard WorkerThat would look (using wildcards) for ccc/s390x-*cross/bin/s390x*-cc and 80*cf5a6c84SAndroid Build Coastguard Workerif found, work out the appropriate $CROSS_COMPILER prefix to use for the 81*cf5a6c84SAndroid Build Coastguard Workercorresponding other tools. Use "CROSS=help" to see the list of cross compilers 82*cf5a6c84SAndroid Build Coastguard Workercurrently available in the ccc directory. 83*cf5a6c84SAndroid Build Coastguard Worker 84*cf5a6c84SAndroid Build Coastguard WorkerYou only need to set one of $CROSS or $CROSS_COMPILE, the other gets derived 85*cf5a6c84SAndroid Build Coastguard Workerfrom the one you provided. 86*cf5a6c84SAndroid Build Coastguard Worker 87*cf5a6c84SAndroid Build Coastguard WorkerThe downloadable toolchains were built with toybox's scripts/mcm-buildall.sh 88*cf5a6c84SAndroid Build Coastguard Workerrunning in a fresh checkout of https://github.com/richfelker/musl-cross-make 89*cf5a6c84SAndroid Build Coastguard Workerand are available as prebuilt binaries from https://landley.net/bin/toolchains 90*cf5a6c84SAndroid Build Coastguard Worker(The "native" compilers run _on_ the target system, as well as producing 91*cf5a6c84SAndroid Build Coastguard Workerbinaries for them. Those are packaged as squashfs filesystems, to be loopback 92*cf5a6c84SAndroid Build Coastguard Workermounted within qemu.) 93*cf5a6c84SAndroid Build Coastguard Worker 94*cf5a6c84SAndroid Build Coastguard Worker--- Adding a kernel 95*cf5a6c84SAndroid Build Coastguard Worker 96*cf5a6c84SAndroid Build Coastguard WorkerOn the mkroot command line add LINUX= pointing to a kernel source directory: 97*cf5a6c84SAndroid Build Coastguard Worker 98*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh CROSS=sh4 LINUX=~/linux 99*cf5a6c84SAndroid Build Coastguard Worker 100*cf5a6c84SAndroid Build Coastguard WorkerThis will build a kernel for the appropriate target, package the filesystem 101*cf5a6c84SAndroid Build Coastguard Workeras cpio.gz for use by initramfs, and create a run-qemu.sh script to invoke 102*cf5a6c84SAndroid Build Coastguard Workerqemu. This results in the following files under root/$CROSS: 103*cf5a6c84SAndroid Build Coastguard Worker 104*cf5a6c84SAndroid Build Coastguard Worker initramfs.cpio.gz - the "fs" dir packaged for initramfs, plus any $MODULES 105*cf5a6c84SAndroid Build Coastguard Worker linux-kernel - the compiled kernel 106*cf5a6c84SAndroid Build Coastguard Worker linux.dtb - The device tree binary (if this target requires one) 107*cf5a6c84SAndroid Build Coastguard Worker run-qemu.sh - the qemu invocation to run it all 108*cf5a6c84SAndroid Build Coastguard Worker 109*cf5a6c84SAndroid Build Coastguard WorkerAnd also: 110*cf5a6c84SAndroid Build Coastguard Worker 111*cf5a6c84SAndroid Build Coastguard Worker fs/ - the generated root filesystem (you can chroot here) 112*cf5a6c84SAndroid Build Coastguard Worker docs/ - Additional information not needed to run qemu. 113*cf5a6c84SAndroid Build Coastguard Worker 114*cf5a6c84SAndroid Build Coastguard WorkerThe run-qemu.sh script will connect together the appropriate -kernel, -initrd, 115*cf5a6c84SAndroid Build Coastguard Workerand -dtb arguments to consume the provided files, as well as -m board and 116*cf5a6c84SAndroid Build Coastguard Worker-append "kernel command line arguments". The KARGS environment variable is 117*cf5a6c84SAndroid Build Coastguard Workeradded to the kernel command line arguments, and any additional arguments 118*cf5a6c84SAndroid Build Coastguard Workerprovided to the script are passed through to qemu, so you can do: 119*cf5a6c84SAndroid Build Coastguard Worker 120*cf5a6c84SAndroid Build Coastguard Worker $ KARGS="rdinit=/bin/sh" ./run-qemu.sh -hda blah.img 121*cf5a6c84SAndroid Build Coastguard Worker 122*cf5a6c84SAndroid Build Coastguard WorkerRunning the script should boot the kernel to a command prompt, with the 123*cf5a6c84SAndroid Build Coastguard Workerserial console connected to stdin and stdout of the qemu process so you can 124*cf5a6c84SAndroid Build Coastguard Workerjust type into it and see the output. The generated kernel config should 125*cf5a6c84SAndroid Build Coastguard Workerprovide basic NAT network support (as if behind a router) and block device 126*cf5a6c84SAndroid Build Coastguard Workersupport. 127*cf5a6c84SAndroid Build Coastguard Worker 128*cf5a6c84SAndroid Build Coastguard Worker--- Environment variables 129*cf5a6c84SAndroid Build Coastguard Worker 130*cf5a6c84SAndroid Build Coastguard WorkerAny "name=value" argument provided on the mkroot.sh command line will set 131*cf5a6c84SAndroid Build Coastguard Workeran environment variable, and any string without an = indicates a package 132*cf5a6c84SAndroid Build Coastguard Workerscript to run before building toybox (explained below). This is why CROSS= 133*cf5a6c84SAndroid Build Coastguard WorkerCROSS_COMPILE= and LINUX= were all set on the command line above. 134*cf5a6c84SAndroid Build Coastguard Worker 135*cf5a6c84SAndroid Build Coastguard WorkerFor portability reasons, mkroot.sh clears all environment variables at the 136*cf5a6c84SAndroid Build Coastguard Workerstart of its run, with the following exceptions: 137*cf5a6c84SAndroid Build Coastguard Worker 138*cf5a6c84SAndroid Build Coastguard WorkerLINUX - Linux kernel source directory. 139*cf5a6c84SAndroid Build Coastguard WorkerCROSS_COMPILE - Cross compiler prefix (sets $CROSS from prefix before first -) 140*cf5a6c84SAndroid Build Coastguard WorkerCROSS - Short target name (sets $CROSS_COMPILE from ccc) 141*cf5a6c84SAndroid Build Coastguard WorkerHOME - Absolute path to user's home directory. 142*cf5a6c84SAndroid Build Coastguard WorkerPATH - Executable path to find binaries. 143*cf5a6c84SAndroid Build Coastguard WorkerNOCLEAR - Don't clear environment variables. (Can't set on command line.) 144*cf5a6c84SAndroid Build Coastguard Worker 145*cf5a6c84SAndroid Build Coastguard WorkerOther interesting variables to set on the command line include: 146*cf5a6c84SAndroid Build Coastguard Worker 147*cf5a6c84SAndroid Build Coastguard WorkerNOAIRLOCK - don't do a hermetic build, just use the $PATH's tools. 148*cf5a6c84SAndroid Build Coastguard WorkerNOLOGPATH - don't use the command line recording wrapper 149*cf5a6c84SAndroid Build Coastguard WorkerNOLOG - don't record build output to root/build/log/$CROSS.[yn] 150*cf5a6c84SAndroid Build Coastguard WorkerNOTOYBOX - don't build toybox 151*cf5a6c84SAndroid Build Coastguard WorkerPENDING - extra commands to enable out of toys/pending 152*cf5a6c84SAndroid Build Coastguard WorkerKEXTRA - Additional kernel symbols to enable (in short CSV format) 153*cf5a6c84SAndroid Build Coastguard WorkerMODULES - Kernel modules to build (in short CSV format) 154*cf5a6c84SAndroid Build Coastguard Worker 155*cf5a6c84SAndroid Build Coastguard Worker--- Adding build modules 156*cf5a6c84SAndroid Build Coastguard Worker 157*cf5a6c84SAndroid Build Coastguard WorkerYou can run additional build scripts from the mkroot/packages directory by 158*cf5a6c84SAndroid Build Coastguard Workerlisting them on the command line: 159*cf5a6c84SAndroid Build Coastguard Worker 160*cf5a6c84SAndroid Build Coastguard Worker $ mkroot/mkroot.sh dropbear overlay OVERLAY=~/blah 161*cf5a6c84SAndroid Build Coastguard Worker 162*cf5a6c84SAndroid Build Coastguard WorkerAny "name=value" argument provided on the command line will set an environment 163*cf5a6c84SAndroid Build Coastguard Workervariable in mkroot (explained above), and any string that without an = 164*cf5a6c84SAndroid Build Coastguard Workerindicates a package script to run before building toybox. 165*cf5a6c84SAndroid Build Coastguard Worker 166*cf5a6c84SAndroid Build Coastguard WorkerThe provided build scripts mostly download source tarballs, cross compile them, 167*cf5a6c84SAndroid Build Coastguard Workerand install them into the root filesystem. Additional package build instructions 168*cf5a6c84SAndroid Build Coastguard Workerare available from the "Linux From Scratch" (http://linuxfromscratch.org/lfs) 169*cf5a6c84SAndroid Build Coastguard Workerand "Beyond Linux From Scratch" (http://linuxfromscratch.org/blfs) projects. 170*cf5a6c84SAndroid Build Coastguard Worker 171*cf5a6c84SAndroid Build Coastguard WorkerIf you specify any packages, the "plumbing" package is automatically read first 172*cf5a6c84SAndroid Build Coastguard Workerto provide the download, setupfor, and cleanup shell functions to fetch and 173*cf5a6c84SAndroid Build Coastguard Workermanage source tarballs, and set the $DOWNLOAD variable (defaulting to store 174*cf5a6c84SAndroid Build Coastguard Workerdownloaded tarballs in "./root_download"). 175*cf5a6c84SAndroid Build Coastguard Worker 176*cf5a6c84SAndroid Build Coastguard WorkerThe "overlay" script copies the $OVERLAY directory (default "./overlay") 177*cf5a6c84SAndroid Build Coastguard Workerinto the root filesystem, so you can add arbitrary additional files. 178*cf5a6c84SAndroid Build Coastguard Worker 179*cf5a6c84SAndroid Build Coastguard WorkerThe "dynamic" script attempts to copy dynamic libraries out of the 180*cf5a6c84SAndroid Build Coastguard Workertoolchain, to allow a dynamically linked root filesystem. It's a work in 181*cf5a6c84SAndroid Build Coastguard Workerprogress. (A debian host toolchain can have multiple gigabytes of shared 182*cf5a6c84SAndroid Build Coastguard Workerlibraries.) 183*cf5a6c84SAndroid Build Coastguard Worker 184*cf5a6c84SAndroid Build Coastguard WorkerThe "tests" script copies the toybox test suite into the new filesystem, 185*cf5a6c84SAndroid Build Coastguard Workerdownloads some test files, and adds some test modules to the kernel build. 186*cf5a6c84SAndroid Build Coastguard Worker 187*cf5a6c84SAndroid Build Coastguard Worker--- Creating new build modules 188*cf5a6c84SAndroid Build Coastguard Worker 189*cf5a6c84SAndroid Build Coastguard WorkerBuild scripts run after creating the directory layout and writing 190*cf5a6c84SAndroid Build Coastguard Workerthe the init script and etc files (resolv.conf/passwd/group), but before 191*cf5a6c84SAndroid Build Coastguard Workerbuilding toybox. 192*cf5a6c84SAndroid Build Coastguard Worker 193*cf5a6c84SAndroid Build Coastguard WorkerThese scripts are sourced, not run, so environment variables you set remain 194*cf5a6c84SAndroid Build Coastguard Workerin force. The following variables can affect the remaining mkroot.sh build: 195*cf5a6c84SAndroid Build Coastguard Worker 196*cf5a6c84SAndroid Build Coastguard WorkerNOTOYBOX - if set, toybox will not be installed into the new root filesystem 197*cf5a6c84SAndroid Build Coastguard WorkerKEXTRA - additional kernel symbols to enable (in same CSV format as $KCONF) 198*cf5a6c84SAndroid Build Coastguard WorkerQEMU_MORE - Additional qemu command line arguments added to run-qemu.sh 199*cf5a6c84SAndroid Build Coastguard Worker 200*cf5a6c84SAndroid Build Coastguard WorkerTo append instead of replacing (in case they're already set), you can use 201*cf5a6c84SAndroid Build Coastguard WorkerQEMU_MORE="$QEMU_MORE --blah" and KEXTRA="${KEXTRA+$KEXTRA,}"BLAH,BLAH,BLAH 202*cf5a6c84SAndroid Build Coastguard Worker 203*cf5a6c84SAndroid Build Coastguard WorkerIf you check your own build scripts into mkroot/packages without touching 204*cf5a6c84SAndroid Build Coastguard Workerany existing files, you should be able to "git pull --ff" to update your tree 205*cf5a6c84SAndroid Build Coastguard Workerwithout conflicts. Alternately, you can add your script directory to the start 206*cf5a6c84SAndroid Build Coastguard Workerof the $PATH and bash's "source" command will fall back to looking there next. 207