How to make linux kernel with Nasm, Go binary, mini linux

Pre-install

Nasm simple example

[org 0x7c00]
mov ah, 0x0e
mov bx, hello

print:
mov al, [bx]
cmp al, 0
je end
int 0x10
inc bx
jmp print
end:

jmp $

hello:
db 'Hello world!',0

times 510-($-$$) db 0
dw 0xaa55 ; this bytes should end boot sector

Run go binary on Linux Kernel

package main

import (
"fmt"
)

func main() {
var data string
fmt.Println("Hello from your Go image!")
for {
fmt.Println("Enter something:")
fmt.Scanln(&data)
fmt.Printf("Echo: %s\n", data)
}
}
#!/bin/bash
SPATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cd $SPATH/gobinary
go build -ldflags="-extldflags=-static" -o init
cd ../
mkdir -p goram
mv $SPATH/gobinary/init $SPATH/goram/init
cd $SPATH/goram
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../goram.cpio.gz

Creating some mini-linux image.

curl https://busybox.net/downloads/busybox-1.34.1.tar.bz2 | tar Jz
cd busybox-1.33.2
make menuconfig
make -j4
make install
cd ..
mkdir -p bbram/{bin,sbin,etc,proc,sys,usr/bin,usr/sbin}
cp -a busybox-1.34.1/_install/* bbram/
#!/bin/sh

/bin/mount -t devtmpfs devtmpfs /dev
/bin/mount -t proc none /proc
/bin/mount -t sysfs none /sys

dmesg -n 1

cat << EOF

Boot took $(cut -d' ' -f1 /proc/uptime) seconds

__ __ _ _ _ _
| \/ (_) (_) | | (_)
| \ / |_ _ __ _ | | _ _ __ _ ___ __
| |\/| | | '_ \| | | | | | '_ \| | | \ \/ /
| | | | | | | | | | |____| | | | | |_| |> <
|_| |_|_|_| |_|_| |______|_|_| |_|\__,_/_/\_\



EOF
exec setsid cttyhack sh #https://busybox.net/FAQ.html#job_control
exec /bin/sh
cp init bbram/bin/init
chmod +x bbram/bin/init
cd bbram
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../bbram.cpio.gz
cd ../

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrey Nikishaev

Andrey Nikishaev

Machine Learning and Computer Vision Researcher. Founder LearnML.Today