Skip to content

About

Light weight Linux container runtime in C++, inspired by Liz Rice's amazing talk on containers!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

35 Commits

Folders and files

Repository files navigation

Container.cpp

                __       _
 _______  ___  / /____ _(_)__  ___ ____ _______  ___
/ __/ _ \/ _ \/ __/ _ `/ / _ \/ -_) __// __/ _ \/ _ \
\__/\___/_//_/\__/\_,_/_/_//_/\__/_/ (_)__/ .__/ .__/
                                         /_/  /_/

This is my attempt at implementing a simple Linux container runtime from scratch in a Virtual Machine on MacOS/arm64.

Table of Contents

Containers in a Nutshell

A container at the most fundamental level on Linux systems is just another process. A process is an instance of a running program.

Now then, what makes containers different from regular processes? You could say that a container has a much more 'contained' and controlled view of the system's resources. This is typically achieved by what's known as Namespaces.

Aptly named, they control what resources a process can 'see'. The second half of the puzzle are Control Groups.

Where namespaces decided what a process can see, Control Groups (or cgroups) decide how much system resources the said process can use. By controlling what system resources a process can see and use, we create the illusion that allows containers to behave as isolated units on the same host kernel.

Bare Minimum Objectives

To make it easy to stay on track. I'm going to define the bare minimum the program must accomplish in order for us to consider its process a container.

  1. Spawn a process given a command of the form ./container -fs <filesystem-root> -mem 16 -cpu 4 run <cmd> <args>
  2. Ensure that this process has its own namespace (pid, user, mount)
  3. Ensure that the root mount point is isolated from the parent process
  4. Enforce memory and CPU limits using cgroups

The reason we specify a -fs or file system flag will become apparent later. The same goes for why we need mount point isolation.

Virtual Machine Setup

Note: You can skip this step if you're running Linux already.

From here on out, I assume that you're on an arm64 processor, specifically an Apple Silicon. Changing these steps for x86 shouldn't be difficult. If you're unsure what cpu architecture your computer is using, run uname -m. I'm going to make another assumption that you have homebrew installed as well.

Install Linux Machines (Lima)

Lima will help setup a light weight Linux Virtual Machine on your computer.

brew install lima
limactl start

Note: On first run, Lima will ask you to download Ubuntu. I've only experimented on Alpine Linux and the following steps will assume you're running an Alpine box.

Alpine Linux Build Tools

Alpine Linux is an extremely lightweight Linux distro commonly used in embedded environments (see busybox). I've chosen this distro because it's simple to use. However, because it's so lightweight we're missing a few tools and binaries, namely some C++ build tools.

sudo apk update
sudo apk add g++ procps make musl-dev cmake

Alpine Linux MiniFS

A file system controls file organization and access. You can think of this as a very rudimentary 'docker image'. Of course I'm simplifying, but this is where our container's root will be. The reason for this is to further isolate the container from the host. All the container's ever going to know is that these are the files available to it, nothing else.

limactl shell default
sudo mkdir -p /opt/alpine-rootfs
wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/alpine-minirootfs-3.24.0-aarch64.tar.gz
sudo tar -xzf alpine-minirootfs-3.24.0-aarch64.tar.gz -C /opt/alpine-rootfs

Experimenting

Compile and run the program inside the Linux Virtual Machine.

g++ -std=gnu++17 container.cpp -o container
./container -fs /opt/alpine-rootfs run /bin/sh

Or use the 'compile.sh' script after you've given it execution permissions.

Hostnames

On your Lima box, running hostname should give you 'lima-default'

# lima
hostname
> lima-default

Inside the container you should get 'host-container'

#container
hostname
> host-container

ps

Here is the result of running ps on the virtual box then container.

ps

top

Similarly, running top at the virtual box and then container level.

top

Caveats

  1. No proper error handling, killing on failure.
  2. Messy argument parsing, see cpu flag code.

References

About

Light weight Linux container runtime in C++, inspired by Liz Rice's amazing talk on containers!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages