Giter Club home page Giter Club logo

mac-linux-headers's Introduction

README

On OS X we don't have some headers from a Linux world which seems to be essential for building a Linux kernel. This small project aims to provide the missing headers and a short readme for those who (like me) wants to work with Linux on Mac as native as possible.

Building a Linux kernel

  1. Install libelf with
brew install libelf
  1. Clone the repository and copy its files into /usr/local/include:
git clone [email protected]:vgribov/mac-linux-headers.git
cp -r mac-linux-headers/* /usr/local/include
  1. Install some GNU stuff to make feel kernel's Makefiles at home:
brew install --with-default-names coreutils gnu-sed grep

To make Linux ELF binaries you'll need a cross compiler. I found a nice one at https://github.com/richfelker/musl-cross-make.

Now you should be able to build the kernel with

make x86_64_defconfig
make CROSS_COMPILE='x86_64-linux-musl-'

To be able to use a menuconfig you should also install gettext and ncurses and link them:

brew install gettext ncurses
brew link --force gettext
brew link --force ncurses

Build and run a minimal Linux in Qemu

This part is based on https://gitlab.eurecom.fr/snippets/23, so all credits go to mailto:[email protected].

  1. Download and unpack your preferred Linux kernel version:
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.1.51.tar.xz
tar xvf linux-4.1.51.tar.xz
  1. Prepare a basic Linux kernel configuration:
cd linux-4.1.51
mkdir build
make O=./build allnoconfig
cd ./build
make menuconfig
  1. Configure the kernel according to the following:
64-bit kernel ---> yes
General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support ---> yes
General setup ---> Configure standard kernel features ---> Enable support for printk ---> yes
Executable file formats / Emulations ---> Kernel support for ELF binaries ---> yes
Executable file formats / Emulations ---> Kernel support for scripts starting with #! ---> yes
Device Drivers ---> Generic Driver Options ---> Maintain a devtmpfs filesystem to mount at /dev ---> yes
Device Drivers ---> Generic Driver Options ---> Automount devtmpfs at /dev, after the kernel mounted the rootfs ---> yes
Device Drivers ---> Character devices ---> Enable TTY ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> 8250/16550 and compatible serial support ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> Console on 8250/16550 and compatible serial port ---> yes
File systems ---> Pseudo filesystems ---> /proc file system support ---> yes
File systems ---> Pseudo filesystems ---> sysfs file system support ---> yes
  1. Build the kernel:
make -j8
cd ..
  1. Download and unpack your preferred Busybox version:
wget http://busybox.net/downloads/busybox-1.28.3.tar.bz2
tar xvf busybox-1.28.3.tar.bz2
  1. Prepare a basic Busybox configuration:
cd busybox-1.28.3
mkdir build
make O=./build defconfig
cd ./build
make menuconfig
  1. Configure Busybox according the following:
Busybox Settings ---> Build Options ---> Build BusyBox as a static binary (no shared libs) ---> yes
  1. Build and install Busybox:
make -j8
make install
cd ../..
  1. Create an initramfs:
mkdir initramfs
cd initramfs
mkdir -p bin sbin etc proc sys usr/bin usr/sbin
cp -a ../busybox-1.28.3/_install/* .
  1. Add an init script to the initramfs with the following content:
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys

printf "\nBoot took %s seconds\n\n" $(awk '{ print $1 }' < /proc/uptime)

exec /bin/sh
  1. Create the initramfs archive:
chmod +x init
find . | cpio -ov --format=newc | gzip > ../initramfs.cpio.gz
cd ..
  1. Install Qemu
brew install qemu

Now you should be able to run and test your minimal Linux with:

qemu-system-x86_64 -kernel $PWD/linux-4.1.51/build/arch/x86_64/boot/bzImage \
                   -initrd $PWD/initramfs.cpio.gz \
                   -nographic -serial mon:stdio \
                   -append 'console=ttyS0 earlyprintk=ttyS0'

Press Ctrl-a x to quit.

mac-linux-headers's People

Contributors

vgribov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.