Linux: Introduction to Linux development

From Compulab Mediawiki
Revision as of 15:28, 8 February 2016 by Grinberg (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Overview

This article describes the basics of Linux development, and is written to address the general case. Instructions that are specific to CompuLab Linux releases will be discussed in dedicated articles. This article is just an overview of typical steps in Linux development.

Getting the source code

While it is possible to download the source code manually as a tar ball, the proper way to obtain Linux source code is by using git, the source control system of choice for the Linux kernel. Working with git is essential to Linux development, so take the time to familiarize yourself with its interface.

  • Install git:
workstation-pc # sudo apt-get install git
  • cd into the development directory and invoke the git clone command on the Linux repository link:
workstation-pc # git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

You will now have a linux folder with the Linux source code on your workstation.

Getting to know the build system

The Linux kernel uses "make" for build automation and KConfig for build configuration. The make build automation program is invoked using:

workstation-pc # make <build target>

A list of the build targets can be obtained by invoking:

workstation-pc # make help

Below is a highlight of some of the common make targets:

Cleaning targets:
  clean		  - Remove most generated files but keep the config and
                    enough build support to build external modules
  mrproper	  - Remove all generated files + config + various backup files

Configuration targets:
  menuconfig	  - Update current config utilising a menu based program
  xconfig	  - Update current config utilising a Qt based front-end
  gconfig	  - Update current config utilising a GTK+ based front-end
  oldconfig	  - Update current config utilising a provided .config as base
  defconfig	  - New config with default from ARCH supplied defconfig
  savedefconfig   - Save current config as ./defconfig (minimal config)

Other generic targets:
  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)

Architecture specific targets (arm):
* zImage        - Compressed kernel image (arch/arm/boot/zImage)
  uImage        - U-Boot wrapped zImage

Execute "make" or "make all" to build all targets marked with [*] 
For further info see the ./README file

Configuring a build

Linux can support a vast variety of configurations, including multiple architectures, file systems, device drivers, networking features, security features, and more. The first step is to select which features are necessary for your use case. A set of prepared configurations can be found in arch/arm/configs folder. When working with CompuLab releases, a <product_name>_defconfig file will be found in this folder. When working with mainline kernel, one can rely on SoC specific defconfig files such as omap2plus_defconfig or mxs_defconfig to provide a base configuration.

  • Configure the kernel:
workstation-pc # make xyz_defconfig

After applying a kernel configuration, invoke menuconfig to fine-tune the configuration. menuconfig is a menu based kernel configuration program which provides a menu navigation across all the available kernel features. menuconfig can turn features on or off, and can select whether to compile device drivers into the kernel, as external modules, or not at all. Information about the various configuration options can be obtained using the help option (selected with the arrow keys, or the '?' key), and search for configuration options can be performed by pressing the '/' key.

  • Invoke menuconfig
workstation-pc # make menuconfig
.config - Linux/arm 4.2.0-cm-t43-4 Kernel Configuration
 ──────────────────────────────────────────────────────────────────────────────
  ┌───────────── Linux/arm 4.2.0-cm-t43-4 Kernel Configuration ─────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty │  
  │  submenus ----).  Highlighted letters are hotkeys.  Pressing <Y>        │  
  │  includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to │  
  │  exit, <?> for Help, </> for Search.  Legend: [*] built-in  [ ]         │  
  │ ┌─────────────────────────────────────────────────────────────────────┐ │  
  │ │        General setup  --->                                          │ │  
  │ │    [*] Enable loadable module support  --->                         │ │  
  │ │    -*- Enable the block layer  --->                                 │ │  
  │ │        System Type  --->                                            │ │  
  │ │        Bus support  --->                                            │ │  
  │ │        Kernel Features  --->                                        │ │  
  │ │        Boot options  --->                                           │ │  
  │ │        CPU Power Management  --->                                   │ │  
  │ │        Floating point emulation  --->                               │ │  
  │ │        Userspace binary formats  --->                               │ │  
  │ │        Power management options  --->                               │ │  
  │ │    [*] Networking support  --->                                     │ │  
  │ │        Device Drivers  --->                                         │ │  
  │ │        Firmware Drivers  ----                                       │ │  
  │ │        File systems  --->                                           │ │  
  │ │        Kernel hacking  --->                                         │ │  
  │ │        Security options  --->                                       │ │  
  │ │    -*- Cryptographic API  --->                                      │ │  
  │ │        Library routines  --->                                       │ │  
  │ │    [ ] Virtualization  ----                                         │ │  
  │ └─────────────────────────────────────────────────────────────────────┘ │  
  ├─────────────────────────────────────────────────────────────────────────┤  
  │        <Select>    < Exit >    < Help >    < Save >    < Load >         │  
  └─────────────────────────────────────────────────────────────────────────┘  

Building the kernel

  • Once the kernel is configured, type "make" to initiate a build.
workstation-pc # make
  • The resulting kernel images will appear in arch/arm/boot/
  • The resulting device tree blobs will appear in arch/arm/boot/dts/

Kernel module installation

Device drivers can be either compiled into the kernel, or built as loadable kernel modules. On Linux systems, modules reside in the /lib/modules/<kernel version>/ folder of the filesystem and are specific to a certain kernel version. Therefore, whenever the kernel version changes, the kernel modules on the filesystem must be updated as well. The Linux build system has special support for installing modules into a location on the host filesystem. This can be used to install modules into a mounted removable storage, a mounted remote storage, or an nfs exported folder on the host system. To perform module installation:

workstation-pc # INSTALL_MOD_PATH=/path/to/rootfs make modules_install

The modules_install build target will take care of going to the lib/modules/ folder in the rootfs, creating the <kernel version> folder, and copying the modules into that location.