Debian Build Linux

From Variscite Wiki
Build Linux from source code

Toolchain installation for out of Flexbuild builds

To install the toolchain, follow the Debian Flexbuild Toolchain installation guide.

Setup the environment:

Set up a working directory

$ export WORKDIR=~/imx8mm-var-dart-linux
$ mkdir ${WORKDIR}

Define the cross-compiler and the architecture:

$ export CROSS_COMPILE=aarch64-linux-gnu-
$ export ARCH=arm64

Build Linux out of Yocto tree

Fetch source code:

$ cd $WORKDIR
$ git clone https://github.com/varigit/linux-imx.git -b imx_4.14.78_1.0.0_ga_var01 
$ cd  && \
  git checkout imx_4.14.78_1.0.0_ga_var01 && \
  cd ../

Build Kernel, Modules, and DTBs:

Clean and prepare the kernel:

$ cd ${WORKDIR}/
$ make mrproper
$ make imx8_var_defconfig
Customize the kernel configuration (optional step):
$ make menuconfig

Build kernel parts:

Build everything:
$ make -j$(nproc)

Build Image.gz only:
$ make -j$(nproc)  Image.gz

Build modules only:
$ make -j$(nproc)  modules

Build device trees only:
$ make -j$(nproc) dtbs


Install kernel modules to temporary directory

Create a temporary directory to install the modules:

Optional: delete any old installation from previous builds:
$ rm -rf $WORKDIR/rootfs

Create new directory:
$ mkdir -p $WORKDIR/rootfs

Install the modules to the temporary rootfs:

$ cd ${WORKDIR}/ && \
  make ARCH=arm64 \
    INSTALL_MOD_STRIP=1 \
    INSTALL_MOD_PATH=$WORKDIR/rootfs \
    modules_install


Notes:

If you replace the kernel image (Image.gz) on your system, you also need to reinstall the kernel modules, and vice versa.
If you just want to make a change in the device tree, you can compile and replace it without replacing the kernel image and modules.

Install the built kernel images, modules, and device trees on an SD card

Expression error: Unexpected >= operator.

Copy the Image.gz and device trees to the SD card boot partition, and install the modules in the SD card rootfs partition.
Assuming the rootfs partition is mounted on /media/user/root:

Install the kernel image and modules:
$ cd ${WORKDIR}/
$ kver=$(strings arch/arm64/boot/Image | grep -i "Linux version" | awk 'NR==1 {print $3}')
$ sudo cp arch/arm64/boot/Image.gz /media/user/root/boot/Image.gz-${kver}
$ sudo ln -fs Image.gz-${kver} /media/user/root/boot/Image.gz
$ sudo rsync -Kra $WORKDIR/rootfs/* /media/user/root

Install the device trees:
$ sudo cp arch/arm64/boot/dts/freescale/*imx*var*.dtb /media/user/root/boot/

Install the built kernel images, modules, and device trees using SSH on a running target

The example below uses ssh to install the kernel on a running device.

First, export a variable with the ip address of your target device:

$ export TARGET_IP=192.168.0.205

Then, copy the kernel image and device trees to the temporary directory, send them to the target using ssh, and install them with the correct user permissions:

$ mkdir -p $WORKDIR/rootfs/boot && \
  cp ${WORKDIR}//arch/arm64/boot/dts/freescale/*imx*var*.dtb $WORKDIR/rootfs/boot && \
  cp ${WORKDIR}//arch/arm64/boot/Image.gz $WORKDIR/rootfs/boot/ && \
  ssh root@${TARGET_IP} 'mkdir /tmp/rootfs' && \
  tar czf - -C $WORKDIR/rootfs . | ssh root@${TARGET_IP} 'tar xzf - -C /tmp/rootfs && \
  chown -R root:root /tmp/rootfs && cp -a /tmp/rootfs/boot/* /boot/ && cp -a /tmp/rootfs/lib/* /lib/ && \
  rm -r /tmp/rootfs'