User:Alexeymin/Kernel development in chroot using abuild

From postmarketOS

Are you tired of the fact that every time you build your kernel in pmbootstrap it starts building it from scratch? Does it take too much time and envkernel does not work/broken? Then this is for you!

Prepare config and chroots:

pmbootstrap -y zap
pmbootstrap init    # for your device

Start building your kernel but cancel early; download all source and checksum; wait until it and starts build, this way we'll have all sources unpacked and placed where needed, and dependencies prepared:

pmbootstrap checksum linux-your-kernel
pmbootstrap build --force --arch aarch64 linux-your-kernel
^C  # press Ctrl+C as soon as pmbootstrap log starts to show building process

Switch to native chroot:

pmbootstrap chroot

In native chroot verify that all deps are installed (this is run as root user inside chroot) (basically install everything from makedepends= of your APKBUILD):

apk add abuild build-base ccache git devicepkg-dev mkbootimg postmarketos-base ccache-cross-symlinks gcc-aarch64 g++-aarch64 crossdirect ncurses-dev bash bc bison elfutils-dev flex gmp-dev installkernel linux-headers openssl-dev perl sed

The following build commands (make or abuild invocations) are run in native chroot as pmos user, because abuild does not recommend running as root!

su pmos
export HOME=/home/pmos
cd /home/pmos/build   # the directory where APKBUILD file is

You can restart all your build from the beginning with this:

cd /home/pmos/build   # the directory where APKBUILD file is
rm -rf src
abuild fetch verify unpack prepare

Setup environment (done only once after su pmos), example for aarch64 mainline:

export ARCH=arm64
export CARCH=aarch64
export CROSS_COMPILE=aarch64-alpine-linux-musl-
export CC=aarch64-alpine-linux-musl-gcc

Example for armv7 downstream kernel compiled with gcc6:

export ARCH=arm
export CARCH=armv7
export CROSS_COMPILE=gcc6-armv7-alpine-linux-musleabihf-
export CC=gcc6-armv7-alpine-linux-musleabihf-gcc

Build (repeat multiple times during development): this does not restart full kernel build every time, rebuilds only changed files with appropriate flags and options specified in APKBUILD. Basically, abuild build calls your build() function from APKBUILD:

abuild -df build

OR manually, without "setup environment" stage, example commands:

cd /home/pmos/build/src   # or whatever the directory after unpacking kernel archive would be
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- menuconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- -j9
make ARCH=arm64 mrproper

If you want to create package:

abuild rootpkg

It will place build package in /mnt/pmbootstrap-packages path inside chroot, which is the same as ~/.local/var/pmbootstrap/packages/ in host system.

Hope it helps!

Note Keep in mind that running pmbootstrap zap in other terminals will delete all chroots so it's easy to lose all your work, everything inside a chroot! Use git / other ways to safely store your work.