Cloning Linux

$ git clone –depth 1 git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

For this exercise, we just clone the repository, without all the history. If you are on your own machine, feel free to do as you like.

Build Linux

There is multiple ways to build linux.

tips:

  • make help can help a lot
  • there is lots of things in ~linux/Documentation/kbuild/makefiles.txt

You should always try to avoid building in the same directory than the sources. In order to do that, you can specify a build directory with KBUILD_OUTPUT environment variable.

In order to speed up the build a little, you can mount a tmpfs over the build dir, to have all the files stored in ram instead of on disk:

$ export KSRC=~/repo/linux
$ export KBUILD_OUTPUT=~/build/linux
$ mkdir -p $KBUILD_OUTPUT
$ make -C $KSRC defconfig

The configuration is by default in $KBUILD_OUTPUT/.config. In order to not destroy it you can use a variable $KCONFIG_CONFIG with the absolute path to the configuration file.

make menuconfig and its friends can be used to manage the various configuration variables. You can also use ~linux/scripts/kconfig/merge_config.sh to create multiple configuration fragments.

It takes time to have a working .config, you should try to not lose it, and try to version them (not in the linux repository!).

After that you can build your first kernel:

$ make -C $KSRC -j16

(adapt -j$N to your machine)

After a while, you will have a kernel that can be used. The kernel will be in $KBUILD_OUTPUT/arch/x86/boot/bzImage. In order to install modules and kernel headers you can use:

$ make -C $KSRC INSTALL_HDR_PATH=$INSTALL_PATH/usr headers_install
$ make -C $KSRC INSTALL_MOD_PATH=$INSTALL_PATH modules_install

It will be a good idea to start having some kind of script to avoir typing all that all the time.

We will try to build a simple kernel with some options:

  • raid support should be deactivated
  • virtio devices should be enabled, with virtio nic driver enabled in kernel (ie not as a module)

building Linux

$ sudo apt install build-essential git libncurses5-dev bc

We need to setup some variables (please adjust accordingly).

$ cat env.sh export KSRC=/nfs/linux export KBUILD_OUTPUT=$HOME/build/linux export ROOTFS=$HOME/debian-rootfs

export KERNEL=$KBUILD_OUTPUT/arch/x86/boot/bzImage $ . env.sh $ mkdir -p $KBUILD_OUTPUT $ make -C $KSRC defconfig

$ make -C $KSRC