Skip to content

Commit

Permalink
dump notes wip exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Dec 9, 2024
0 parents commit 85de59d
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Minimal boot linux coreutils bash glibc

![qemu showing boot into minimal linux](./img/boot-qemu-example.png)

Goal is to have mimimal but realistic node with network capabilities build in (`ip`) and bootstrapped (similar to [alpine's netbot](https://boot.alpinelinux.org/) ([example](https://github.com/KarmaComputing/server-bootstrap/blob/494089caa2c88bbf37a739aa96561231d5847be5/.github/workflows/build-alpine-netboot-image-zfs.yml#L1))). [Clearlinux](https://github.com/clearlinux/distribution) is interesting. This is using glibc over musl (which alpine uses).


## The process in a nutshell

- Given a built linux kernel (e.g. `vmlinuz-lts`)
- Clone and build all the various packages you want
- Write an `init` script
- Build all the binaries + `init` script into a initramfs
- Run/boot with qemu



TODO: add [iproute2](https://github.com/iproute2/iproute2) for minimal routing.

## Things you need to build

- git clone & build GNU `bash` is in it's own repo
- git clone & build coreutils (`ls` , `date` , `touch` etc(`mount` is not in here- who knew)
- To get `mount` you need to build util-linux https://en.wikipedia.org/wiki/Util-linux
- is `ls` failing, did you forget to include `/usr/lib/x86_64-linux-gnu/libcap.so.2`? See `mkchroot.sh` helper from https://landley.net/writing/rootfs-programming.html

- Don't forget to `mount` linux virtual filesystems (oh wait, did you forget to build util-linux into your init?

Example built rootfs:
```
ls -lh rootfs.cpio.gz
-rw-rw-r-- 1 chris chris 16M Dec 8 23:40 rootfs.cpio.gz
```

# What does this repo not include (yet)

- Full clone/compile instructions
- Automated ci

### How do I build statically coreutils, do I even need to?

See https://lists.gnu.org/archive/html/coreutils/2019-04/msg00001.html

## `ls` , `cat` and `date` etc won't run without libc!

git clone https://sourceware.org/git/glibc.git

# Reading
See also

https://wiki.gentoo.org/wiki/Custom_Initramfs
https://unix.stackexchange.com/a/305406
https://landley.net/writing/rootfs-howto.html
https://landley.net/writing/rootfs-programming.html
6 changes: 6 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -exu

./create-init.sh
./build-rootfs.sh
15 changes: 15 additions & 0 deletions build-coreutils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -x

INITAL_WORKING_DIR=$PWD

cd coreutils
git checkout v9.5
./bootstrap
./configure LDFLAGS="-static"
make
find src/ -executable -maxdepth 1


cd $INITAL_WORKING_DIR
22 changes: 22 additions & 0 deletions build-libc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -xu

SCRATCH_SPACE_DIR=$1

INITAL_WORKING_DIR=$PWD

git clone https://sourceware.org/git/glibc.git
cd glibc
git checkout release/2.40/master
mkdir glibc-build
cd glibc-build
mkdir out
../configure --prefix=$(pwd)/out
make
make install

# Copy built libc objects/files over into scratch space
cp -r -n ./out/* $SCRATCH_SPACE_DIR

cd $INITAL_WORKING_DIR
9 changes: 9 additions & 0 deletions build-rootfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -eux

cd scratch-space

find . | cpio -o -H newc | gzip > ../rootfs.cpio.gz

cd -
8 changes: 8 additions & 0 deletions create-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -eux

echo "#!/usr/bin/bash" > scratch-space/init
echo "exec /usr/bin/bash" >> scratch-space/init
chmod +x scratch-space/init

Binary file added img/boot-qemu-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions mkchroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

# Credit:
# https://landley.net/writing/rootfs-programming.html

function mkchroot
{
[ $# -lt 2 ] && return

dest=$1
shift
for i in "$@"
do
# Get an absolute path for the file
[ "${i:0:1}" == "/" ] || i=$(which $i)
# Skip files that already exist at target.
[ -f "$dest/$i" ] && continue
if [ -e "$i" ]
then
# Create destination path
d=`echo "$i" | grep -o '.*/'` &&
mkdir -p "$dest/$d" &&
# Copy file
cat "$i" > "$dest/$i" &&
chmod +x "$dest/$i"
else
echo "Not found: $i"
fi
# Recursively copy shared libraries' shared libraries.
mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
done
}

mkchroot "$@"
5 changes: 5 additions & 0 deletions run-qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -eux

qemu-system-x86_64 -kernel vmlinuz-lts -initrd rootfs.cpio.gz --append "init=/usr/bin/init"

0 comments on commit 85de59d

Please sign in to comment.