Implement SparkOS MVP: init system, build infrastructure, and rootfs

Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-28 15:03:31 +00:00
parent 0ab69f4dbc
commit ae278dc384
18 changed files with 980 additions and 1 deletions

36
scripts/build.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash
# Quick build script for SparkOS development
set -e
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_ROOT"
echo "SparkOS Quick Build"
echo "==================="
echo ""
# Build init
echo "Building init system..."
make init
# Setup rootfs structure
echo ""
echo "Setting up root filesystem..."
./scripts/setup_rootfs.sh
# Install init
echo ""
echo "Installing init to rootfs..."
make install
echo ""
echo "Build complete!"
echo ""
echo "Next steps to create a full bootable system:"
echo " 1. Populate rootfs/bin with bash and essential utilities"
echo " (cp /bin/bash rootfs/bin/, etc.)"
echo " 2. Copy required libraries to rootfs/usr/lib and rootfs/lib"
echo " 3. Add a Linux kernel to rootfs/boot/vmlinuz"
echo " 4. Run: sudo make image"
echo ""

106
scripts/create_image.sh Executable file
View File

@@ -0,0 +1,106 @@
#!/bin/bash
# SparkOS Image Creation Script
# Creates a bootable dd-able disk image
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
IMAGE_FILE="$PROJECT_ROOT/sparkos.img"
IMAGE_SIZE="512M"
MOUNT_POINT="/tmp/sparkos_mount"
ROOTFS_DIR="$PROJECT_ROOT/rootfs"
echo "SparkOS Image Builder"
echo "====================="
echo ""
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: This script must be run as root"
echo "Usage: sudo $0"
exit 1
fi
# Check for required tools
REQUIRED_TOOLS="dd losetup mkfs.ext4 syslinux"
for tool in $REQUIRED_TOOLS; do
if ! command -v "$tool" &> /dev/null; then
echo "ERROR: Required tool '$tool' is not installed"
exit 1
fi
done
echo "Creating disk image ($IMAGE_SIZE)..."
dd if=/dev/zero of="$IMAGE_FILE" bs=1M count=512 status=progress
echo "Setting up loop device..."
LOOP_DEV=$(losetup -f)
losetup "$LOOP_DEV" "$IMAGE_FILE"
echo "Creating partition table..."
parted -s "$LOOP_DEV" mklabel msdos
parted -s "$LOOP_DEV" mkpart primary ext4 1MiB 100%
parted -s "$LOOP_DEV" set 1 boot on
# Reload partition table
partprobe "$LOOP_DEV" 2>/dev/null || true
sleep 1
# Get partition device
PART_DEV="${LOOP_DEV}p1"
if [ ! -e "$PART_DEV" ]; then
PART_DEV="${LOOP_DEV}1"
fi
echo "Creating ext4 filesystem..."
mkfs.ext4 -F "$PART_DEV"
echo "Mounting filesystem..."
mkdir -p "$MOUNT_POINT"
mount "$PART_DEV" "$MOUNT_POINT"
echo "Copying rootfs..."
if [ -d "$ROOTFS_DIR" ]; then
cp -a "$ROOTFS_DIR"/* "$MOUNT_POINT/"
else
echo "WARNING: rootfs directory not found, creating minimal structure"
mkdir -p "$MOUNT_POINT"/{bin,sbin,etc,proc,sys,dev,tmp,usr/{bin,sbin,lib},var,root,home}
fi
echo "Installing bootloader..."
mkdir -p "$MOUNT_POINT/boot/syslinux"
# Create syslinux config
cat > "$MOUNT_POINT/boot/syslinux/syslinux.cfg" << 'EOF'
DEFAULT linux
PROMPT 0
TIMEOUT 50
LABEL linux
SAY Booting SparkOS...
KERNEL /boot/vmlinuz
APPEND ro root=/dev/sda1 init=/sbin/init console=tty1
EOF
# Install syslinux
syslinux --install "$PART_DEV"
# Install MBR
dd if=/usr/lib/syslinux/mbr/mbr.bin of="$LOOP_DEV" bs=440 count=1 conv=notrunc 2>/dev/null || \
dd if=/usr/share/syslinux/mbr.bin of="$LOOP_DEV" bs=440 count=1 conv=notrunc 2>/dev/null || \
echo "WARNING: Could not install MBR, you may need to do this manually"
echo "Cleaning up..."
umount "$MOUNT_POINT"
rmdir "$MOUNT_POINT"
losetup -d "$LOOP_DEV"
echo ""
echo "SUCCESS! Bootable image created: $IMAGE_FILE"
echo ""
echo "To write to a USB drive:"
echo " sudo dd if=$IMAGE_FILE of=/dev/sdX bs=4M status=progress"
echo ""
echo "WARNING: Replace /dev/sdX with your actual USB drive device"
echo " This will DESTROY all data on the target drive!"

148
scripts/setup_rootfs.sh Executable file
View File

@@ -0,0 +1,148 @@
#!/bin/bash
# SparkOS Setup Script
# Sets up a minimal rootfs with bash and essential utilities
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
ROOTFS_DIR="$PROJECT_ROOT/rootfs"
echo "SparkOS Root Filesystem Setup"
echo "=============================="
echo ""
# Create directory structure
echo "Creating directory structure..."
mkdir -p "$ROOTFS_DIR"/{bin,sbin,etc,proc,sys,dev,tmp,usr/{bin,sbin,lib,lib64},var,root,home}
mkdir -p "$ROOTFS_DIR/etc"/{init.d,network}
mkdir -p "$ROOTFS_DIR/var"/{log,run}
# Set permissions
chmod 1777 "$ROOTFS_DIR/tmp"
chmod 700 "$ROOTFS_DIR/root"
# Create basic config files
echo "Creating configuration files..."
# /etc/hostname
echo "sparkos" > "$ROOTFS_DIR/etc/hostname"
# /etc/hosts
cat > "$ROOTFS_DIR/etc/hosts" << 'EOF'
127.0.0.1 localhost
127.0.1.1 sparkos
::1 localhost ip6-localhost ip6-loopback
EOF
# /etc/passwd
cat > "$ROOTFS_DIR/etc/passwd" << 'EOF'
root:x:0:0:root:/root:/bin/bash
EOF
# /etc/group
cat > "$ROOTFS_DIR/etc/group" << 'EOF'
root:x:0:
EOF
# /etc/fstab
cat > "$ROOTFS_DIR/etc/fstab" << 'EOF'
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devtmpfs /dev devtmpfs defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
EOF
# /etc/profile
cat > "$ROOTFS_DIR/etc/profile" << 'EOF'
# SparkOS System Profile
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PS1='SparkOS:\w\$ '
export HOME=/root
export TERM=linux
# Welcome message
echo "Welcome to SparkOS!"
echo "Type 'help' for available commands"
echo ""
EOF
# Create .bashrc for root
cat > "$ROOTFS_DIR/root/.bashrc" << 'EOF'
# SparkOS Root Bash Configuration
# Set prompt
PS1='SparkOS:\w# '
# Aliases
alias ls='ls --color=auto'
alias ll='ls -lah'
alias ..='cd ..'
# Environment
export EDITOR=vi
export PAGER=less
EOF
# Create a simple help script
cat > "$ROOTFS_DIR/bin/sparkos-help" << 'EOF'
#!/bin/sh
cat << 'HELP'
SparkOS - Minimal Linux Distribution
====================================
Available commands:
ls, cd, pwd - Navigate filesystem
cat, less - View files
mkdir, rm, cp - File operations
mount, umount - Mount filesystems
poweroff, reboot - System control
help - Show this help
This is a minimal system. To extend functionality:
1. Mount additional filesystems
2. Install packages (if package manager available)
3. Build from source
For more information: https://github.com/johndoe6345789/SparkOS
HELP
EOF
chmod +x "$ROOTFS_DIR/bin/sparkos-help"
ln -sf sparkos-help "$ROOTFS_DIR/bin/help"
# Create README
cat > "$ROOTFS_DIR/README.txt" << 'EOF'
SparkOS Root Filesystem
=======================
This is the root filesystem for SparkOS, a minimal Linux distribution.
Directory Structure:
/bin, /sbin - Essential binaries
/etc - Configuration files
/proc, /sys, /dev - Kernel interfaces
/tmp - Temporary files
/usr - User programs
/var - Variable data
/root - Root home directory
/home - User home directories
Note: This is a minimal system. You'll need to populate /bin and /usr/bin
with actual binaries (bash, coreutils, etc.) from a proper Linux system
or by cross-compiling.
EOF
echo ""
echo "Root filesystem structure created at: $ROOTFS_DIR"
echo ""
echo "Next steps:"
echo " 1. Build init: make init"
echo " 2. Install init: make install"
echo " 3. Copy bash and essential binaries to rootfs/bin/"
echo " 4. Create bootable image: sudo make image"
echo ""
echo "Note: You'll need to populate the rootfs with actual binaries"
echo " before creating a bootable image."