o
odinpkg.dev
packages / library / Yggdrasil

Yggdrasil

v0.0.1library

Lightweight Container Runtime

No license · updated 9 months ago

Yggdrasil - Mini Container Runtime

yggdrasil

A mini container runtime implementation in Odin, inspired by runc. This project provides basic container management functionality including create, start, list, delete, and exec operations.

yggdrasil

Container Runtime Comparison

Feature Yggdrasil runc Docker containerd
Binary Size 473 KB 13.5 MB 55.2 MB ~25 MB
Memory Usage ~1-2 MB ~5-10 MB ~50-100 MB ~20-30 MB
Lines of Code 572 lines ~50K+ lines ~1M+ lines ~200K+ lines
Dependencies None libc, libseccomp Many libc, gRPC
Container Creation
Container Start/Stop
Interactive Exec
Background Containers
Namespace Isolation
Overlay Filesystem
State Persistence
Image Management
Networking Basic
Resource Limits
Security Features Basic Advanced Advanced Advanced
Production Ready
Learning/Educational

Size Comparison

  • Yggdrasil: 473 KB (0.5 MB) - 122x smaller than Docker!
  • runc: 13.5 MB - 28x larger than Yggdrasil
  • Docker: 55.2 MB - 117x larger than Yggdrasil

Features

Core Container Operations

  • Create containers with custom rootfs and commands
  • Start containers in background mode
  • Stop containers with graceful shutdown
  • List containers with status and process information
  • Delete containers and clean up resources
  • Execute commands in running containers (interactive shell support)

Isolation & Security

  • Linux namespaces (PID, mount, UTS, network)
  • Overlay filesystem support for rootfs isolation
  • chroot filesystem isolation
  • Process isolation with fork/exec
  • Root privilege checking with helpful error messages

State Management

  • Container state persistence using state files
  • Background container execution (daemon mode)
  • Process monitoring and automatic status updates
  • Container lifecycle management

CLI Interface

  • Simple command-line interface
  • Helpful usage messages
  • Error handling with clear feedback
  • Status reporting for all operations

Prerequisites

  • Odin compiler (https://odin-lang.org/docs/install/)
  • Linux system with kernel support for namespaces
  • Root filesystem image (e.g., Alpine Linux)
  • Root privileges (required for namespace operations)

Building

./build.sh

This will create a yggdrasil binary in the current directory.

Usage

Create a container

./ygg create mycontainer --rootfs ./rootfs --hostname myhost -- /bin/sh

Options:

  • --rootfs <path>: Path to root filesystem (default: ./rootfs)
  • --hostname <name>: Container hostname
  • --net <mode>: Network mode - host|none (default: host)
  • -- <command>: Command to run in container

Start a container (runs in background)

sudo ./ygg start mycontainer

Stop a running container

sudo ./ygg stop mycontainer

List all containers

./ygg ps

Execute command in container

# Execute a single command
sudo ./ygg exec mycontainer /bin/ls

# Get interactive shell (like docker exec -it)
sudo ./ygg exec mycontainer /bin/sh

Delete a container

./ygg delete mycontainer

Container States

  • Created: Container configuration created but not started
  • Running: Container is currently running
  • Stopped: Container has finished execution
  • Deleted: Container has been removed

Architecture

The runtime uses several Linux features:

  1. Namespaces: Process, mount, and network isolation
  2. Overlay filesystem: Union mount for rootfs protection
  3. chroot: Change root directory for container filesystem
  4. fork/exec: Process management and command execution

File Structure

Yggdrasil/
├── main.odin          # Main runtime implementation
├── build.sh           # Build script
├── README.md          # This file
├── rootfs.gz          # Root filesystem image
└── containers/        # Container state directory (created at runtime)
    └── *.json         # Container state files

Example Workflow

  1. Prepare rootfs:

    # Download Alpine Linux rootfs
    wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.22.0-x86_64.tar.gz
    tar -xzf alpine-minirootfs-3.22.0-x86_64.tar.gz
    mv alpine-minirootfs-3.22.0-x86_64 rootfs
  2. Create and start container:

    ./ygg create test --rootfs ./rootfs -- /bin/sleep 3600
    sudo ./ygg start test  # Requires root privileges
  3. List containers:

    ./ygg ps
  4. Execute commands in container:

    sudo ./ygg exec test /bin/ls
    sudo ./ygg exec test /bin/sh  # Interactive shell
  5. Stop and clean up:

    sudo ./ygg stop test
    ./ygg delete test

Limitations

  • No TTY support (containers run without terminal)
  • Basic namespace joining (exec uses chroot instead of setns)
  • No resource limits or cgroups
  • No advanced networking configuration
  • No image management (requires manual rootfs setup)
  • No security profiles or seccomp
  • No volume mounts or bind mounts
  • No container orchestration features

Security Notes

  • This is a learning project and should not be used in production
  • Containers run with the same privileges as the host
  • No security isolation beyond basic namespaces
  • Always run in a safe environment for testing

License

This project is for educational purposes. Use at your own risk.