Skip to content

c444b774/linux

 
 

Repository files navigation

Introduction

LKL (Linux Kernel Library) is aiming to allow reusing the Linux kernel code as extensively as possible with minimal effort and reduced maintenance overhead.

Examples of how LKL can be used are: creating userspace applications (running on Linux and other operating systems) that can read or write Linux filesystems or can use the Linux networking stack, creating kernel drivers for other operating systems that can read Linux filesystems, bootloaders support for reading/writing Linux filesystems, etc.

With LKL, the kernel code is compiled into an object file that can be directly linked by applications. The API offered by LKL is based on the Linux system call interface.

LKL is implemented as an architecture port in arch/lkl. It uses host operations defined by the application or a host library (tools/lkl/lib).

Supported hosts

The supported hosts for now are POSIX and Windows userspace applications.

Building LKL, the host library and LKL based tools

$ make tools/lkl

will build LKL as a object file, it will install it in tools/lkl/lib together with the headers files in tools/lkl/include then will build the host library, tests and a few of application examples:

  • tests/boot - a simple applications that uses LKL and exercises the basic LKL APIs

  • fs2tar - a tool that converts a filesystem image to a tar archive

  • cptofs/cpfromfs - a tool that copies files to/from a filesystem image

  • lklfuse - a tool that can mount a filesystem image in userspace, without root priviledges, using FUSE

Building LKL on FreeBSD

$ pkg install binutils gcc49 gnubc

#If you don't have a gcc binary:
$ ln -sf /usr/local/bin/gcc49 /usr/local/bin/gcc

#Prefer ports binutils and GNU bc(1):
$ export PATH=/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/lib64/ccache

$ gmake tools/lkl

Building LKL on Ubuntu

$ sudo apt-get install libfuse-dev libarchive-dev xfsprogs

# Optional, if you would like to be able to run tests
$ sudo apt-get install btrfs-tools

$ make tools/lkl

# To check that everything works:
$ cd tools/lkl
$ make test

Building LKL for Windows

In order to build LKL for Win32 the mingw cross compiler needs to be installed on the host (e.g. on Ubuntu the following packages are required: binutils-mingw-w64-i686, gcc-mingw-w64-base, gcc-mingw-w64-i686 mingw-w64-common, mingw-w64-i686-dev).

Due to a bug in mingw regarding weak symbols the following patches needs to be applied to mingw-binutils:

https://sourceware.org/ml/binutils/2015-10/msg00234.html

and i686-w64-mingw32-gas, i686-w64-mingw32-ld and i686-w64-mingw32-objcopy need to be recompiled.

With that pre-requisites fullfilled you can now build LKL for Win32 with the following command:

$ make CROSS_COMPILE=i686-w64-mingw32- tools/lkl

Building LKL on Windows

To build on Windows, certain GNU tools need to be installed. These tools can come from several different projects, such as cygwin, unxutils, gnu-win32 or busybox-w32. Below is one minimal/modular set-up based on msys2.

Common build dependencies:

  • MSYS2 (provides GNU bash and many other utilities)
  • Extra utilities from MSYS2/pacman: bc, base-devel

General considerations:

  • No spaces in pathnames (source, prefix, destination,...)!
  • Make sure that all utilities are in the PATH.
  • Win64 (and MinGW 64-bit crt) is LLP64, which causes conflicts in size of "long" in the Linux source. Linux (and lkl) can (currently) not be built on LLP64.
  • Cygwin (and msys2) are LP64, like linux.

For MSYS2 (and Cygwin):

Msys2 will install a gcc tool chain as part of the base-devel bundle. Binutils (2.26) is already patched for NT weak externals. Using the msys2 shell, cd to the lkl sources and run:

$ make tools/lkl

For MinGW:

Install mingw-w64-i686-toolchain via pacman, mingw-w64-i686-binutils (2.26) is already patched for NT weak externals. Start a MinGW Win32 shell (64-bit will not work, see above) and run:

$ make tools/lkl

LKL hijack library

LKL hijack library (liblkl-hijack.so) is used to replace system calls used by an application on the fly so that the application can use LKL instead of the kernel of host operating system. LD_PRELOAD is used to dynamically override system calls with this library when you execute a program.

You can usually use this library via a wrapper script.

$ cd tools/lkl
$ ./bin/lkl-hijack.sh ip address show

There are environmental variables to configure the behavior of LKL. The followings are the list of those variable for your environment.

  • LKL_HIJACK_NET_IFTYPE

    The interface type in host operating system to connect to LKL. The following example specifies a tap interface.

    $ LKL_HIJACK_NET_IFTYPE=tap LKL_HIJACK_NET_IFPARAMS=tap0 lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_IFPARAMS

    Additional configuration parameters for the interface specified by LKL_HIJACK_NET_IFTYPE. The parameters depend on the interface type (LKL_HIJACK_NET_IFTYPE).

    $ LKL_HIJACK_NET_IFTYPE=tap LKL_HIJACK_NET_IFPARAMS=tap0 lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_IP

    the IP address of the interface specified by LKL_HIJACK_NET_TAP.

    $ LKL_HIJACK_NET_IP=198.51.100.5 lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_NETMASK_LEN

    the network mask length of the interface specified by LKL_HIJACK_NET_TAP.

    $ LKL_HIJACK_NET_IP=198.51.100.5 LKL_HIJACK_NET_NETMASK_LEN=24 lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_MAC

    the MAC address of the interface specified by LKL_HIJACK_NET_TAP.

    $ LKL_HIJACK_NET_MAC="aa:bb:cc:dd:ee:ff" lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_GATEWAY

    the gateway IP address of LKL network stack.

     $ LKL_HIJACK_NET_GATEWAY=198.51.100.5 lkl-hijack.sh ip address show
  • LKL_HIJACK_NET_MTU

    the MTU size of the interface specified by LKL_HIJACK_NET_TAP.

     $ LKL_HIJACK_NET_MTU=1280 lkl-hijack.sh ip address show
  • LKL_HIJACK_DEBUG

    increate the verbose level of debug information.

     $ LKL_HIJACK_DEBUG=1 lkl-hijack.sh ip address show

FAQ

Q: How is LKL different from UML?

A: UML prodivides a full OS environment (e.g. user/kernel separation, user processes) and also has requirements (a filesystem, processes, etc.) that makes it hard to use it for standalone applications. UML also relies heavily on Linux hosts. On the other hand LKL is designed to be linked directly with the application and hence does not have user/kernel separation which makes it easier to use it in standalone applications.

Q: How is LKL different from LibOS?

A: LibOS re-implements high-level kernel APIs for timers, softirqs, scheduling, sysctl, SLAB/SLUB, etc. LKL behaves like any arch port, implementing the arch level operations requested by the Linux kernel. LKL also offers a host interface so that support for multiple hosts can be implemented.

About

Linux kernel source tree

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 97.0%
  • Assembly 1.9%
  • C++ 0.6%
  • Makefile 0.3%
  • Perl 0.1%
  • Objective-C 0.1%