Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
Add decentralized Firecracker microVM build/launch scripts with KVM check
Antonio Ojeac03b826e1ee2
diff --git a/scripts/build-rootfs.sh b/scripts/build-rootfs.shnew file mode 100755index 00000000..759a6bbe--- /dev/null+++ b/scripts/build-rootfs.sh@@ -0,0 +1,46 @@+#!/bin/bash+set -e++# Usage: ./scripts/build-rootfs.sh [GCS_URL]+GCS_URL=${1:-}++echo "Building Alpine Linux rootfs with Python, tun2proxy v0.8.3, and Chaos Agent..."+cat << 'EOF' > Dockerfile+FROM alpine:3.18+RUN apk add --no-cache python3 py3-pip openrc iproute2 socat curl++# Install tun2proxy+RUN curl -sSL -o /usr/local/bin/tun2proxy https://github.com/tun2proxy/tun2proxy/releases/download/v0.8.3/tun2proxy-linux-x86_64-musl \+ && chmod +x /usr/local/bin/tun2proxy++COPY cmd/chaos-agent /app/chaos-agent+RUN pip3 install --no-cache-dir -r /app/chaos-agent/requirements.txt --break-system-packages++COPY scripts/microvm-init.sh /sbin/init+RUN chmod +x /sbin/init+EOF++# Build the docker container+docker build -t chaos-rootfs -f Dockerfile .+CONTAINER_ID=$(docker create chaos-rootfs)+docker export $CONTAINER_ID > rootfs.tar+docker rm $CONTAINER_ID++# Create the ext4 image+dd if=/dev/zero of=rootfs.ext4 bs=1M count=500+mkfs.ext4 rootfs.ext4+mkdir -p /tmp/rootfs++# Use sudo to mount and untar, since we need root permissions to create files with correct ownership+sudo mount rootfs.ext4 /tmp/rootfs+sudo tar -xf rootfs.tar -C /tmp/rootfs+sudo umount /tmp/rootfs++rm rootfs.tar Dockerfile+echo "Rootfs built successfully at rootfs.ext4"++if [ -n "$GCS_URL" ]; then+ echo "Uploading rootfs.ext4 to $GCS_URL..."+ gcloud storage cp rootfs.ext4 "$GCS_URL/rootfs.ext4"+ echo "Upload complete!"+fidiff --git a/scripts/cloud-init.yaml b/scripts/cloud-init.yamlindex ff0224f5..10049be6 100644--- a/scripts/cloud-init.yaml+++ b/scripts/cloud-init.yaml@@ -14,6 +14,13 @@ packages:- aclruncmd:+ # Check if nested virtualization is enabled+ - |+ if [ ! -e /dev/kvm ]; then+ echo "ERROR: /dev/kvm not found! Nested virtualization is not enabled."+ exit 1+ fi+# Ensure KVM permissions allow execution- chmod 0666 /dev/kvm@@ -28,21 +35,34 @@ runcmd:- sysctl -w net.ipv4.ip_forward=1- echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/99-ipforward.conf- # Install SAM Mesh binaries- # Check if custom binaries were injected via GCP metadata (GCS URL)+ # Install SAM Mesh binaries & MicroVM files+ # Check if custom binaries/rootfs were injected via GCP metadata (GCS URL)- |BIN_URL=$(curl -s -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/sam-binaries-url" || true)if [ -n "$BIN_URL" ] && [ "$BIN_URL" != "null" ]; then- echo "Found custom binaries URL: $BIN_URL. Downloading from GCS..."- # Use gcloud storage (pre-installed on Google OS images) to download via the attached service account+ echo "Found custom artifacts URL: $BIN_URL. Downloading from GCS..."+ # Download SAM binariesgcloud storage cp "$BIN_URL/sam-node" /usr/local/bin/gcloud storage cp "$BIN_URL/sam-box" /usr/local/bin/chmod +x /usr/local/bin/sam-*++ # Download the pre-built MicroVM rootfs+ mkdir -p /opt/microvm+ gcloud storage cp "$BIN_URL/rootfs.ext4" /opt/microvm/rootfs.ext4elseecho "No custom binaries specified. Installing latest release from github..."export SAM_INSTALL_DIR=/usr/local/bincurl -sL https://sam-mesh.dev/install.sh | bashfi+ # Download the uncompressed Linux kernel for Firecracker+ - mkdir -p /opt/microvm+ - curl -fsSL -o /opt/microvm/vmlinux.bin https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/x86_64/kernels/vmlinux.bin++ # Download the launch script from the repo (since it's public) or we could embed it here.+ # Let's download it from the main branch since it's self-contained.+ - curl -fsSL -o /opt/microvm/launch-microvms.sh https://raw.githubusercontent.com/google/sam/main/scripts/launch-microvms.sh || echo "Failed to download launch script"+ - chmod +x /opt/microvm/launch-microvms.sh+- echo "SAM Host cloud-init bootstrap finished successfully."diff --git a/scripts/launch-microvms.sh b/scripts/launch-microvms.shnew file mode 100755index 00000000..db0f2fa8--- /dev/null+++ b/scripts/launch-microvms.sh@@ -0,0 +1,72 @@+#!/bin/bash+set -e++COUNT=${1:-1}+WORKDIR="/opt/microvm"++if [ ! -d "$WORKDIR" ]; then+ echo "Error: $WORKDIR does not exist. Did cloud-init finish?"+ exit 1+fi++echo "=== Launching $COUNT MicroVMs ==="+for i in $(seq 1 $COUNT); do+ VM_ID="vm-$i"+ UDS_PATH="/var/run/sam-box-$VM_ID.sock"+ API_SOCKET="/tmp/firecracker-$VM_ID.socket"++ # Clean up old sockets+ rm -f $UDS_PATH $API_SOCKET++ # Start sam-box on the host, listening on a dedicated UDS for this VM+ # The sam-box acts as the gateway to the mesh.+ mkdir -p /tmp/sam-box-$VM_ID+ sam-box run -u "$UDS_PATH" --data-dir "/tmp/sam-box-$VM_ID" > /var/log/sam-box-$VM_ID.log 2>&1 &++ # Start Firecracker+ firecracker --api-sock $API_SOCKET > /var/log/fc-$VM_ID.log 2>&1 &+ sleep 1++ # Setup VM Kernel+ curl -s -X PUT --unix-socket $API_SOCKET \+ http://localhost/boot-source \+ -H 'Content-Type: application/json' \+ -d "{+ \"kernel_image_path\": \"$WORKDIR/vmlinux.bin\",+ \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off\"+ }" > /dev/null++ # Setup VM Rootfs (using a copy so it's writable per VM)+ cp $WORKDIR/rootfs.ext4 $WORKDIR/rootfs-$VM_ID.ext4+ curl -s -X PUT --unix-socket $API_SOCKET \+ http://localhost/drives/rootfs \+ -H 'Content-Type: application/json' \+ -d "{+ \"drive_id\": \"rootfs\",+ \"path_on_host\": \"$WORKDIR/rootfs-$VM_ID.ext4\",+ \"is_root_device\": true,+ \"is_read_only\": false+ }" > /dev/null++ # Setup VM VSOCK+ # Guest CID 3 is typically the first available guest CID.+ # The host UDS will receive connections from the guest's socat.+ curl -s -X PUT --unix-socket $API_SOCKET \+ http://localhost/vsock \+ -H 'Content-Type: application/json' \+ -d "{+ \"vsock_id\": \"vsock0\",+ \"guest_cid\": 3,+ \"uds_path\": \"$UDS_PATH\"+ }" > /dev/null++ # Start VM+ curl -s -X PUT --unix-socket $API_SOCKET \+ http://localhost/actions \+ -H 'Content-Type: application/json' \+ -d '{ "action_type": "InstanceStart" }' > /dev/null++ echo "Started MicroVM $VM_ID via VSOCK -> sam-box gateway."+done++echo "=== All MicroVMs running! ==="diff --git a/scripts/microvm-init.sh b/scripts/microvm-init.shnew file mode 100644index 00000000..5f12e38f--- /dev/null+++ b/scripts/microvm-init.sh@@ -0,0 +1,30 @@+#!/bin/sh+mount -t proc proc /proc+mount -t sysfs sysfs /sys+mount -t devtmpfs devtmpfs /dev++# Setup localhost+ip link set dev lo up++# 1. Bridge local TCP to Host VSOCK (Firecracker CID 2 maps to host UDS)+# The host sam-box UDS will receive connections from this port+socat TCP-LISTEN:8080,fork,reuseaddr VSOCK-CONNECT:2:8080 &++# 2. Setup transparent network using tun2proxy+ip tuntap add dev tun0 mode tun+ip link set dev tun0 up+ip addr add 198.18.0.1/15 dev tun0+ip route add default via 198.18.0.1 dev tun0++# tun2proxy will catch all traffic on tun0 and forward to the socat TCP port (which goes to sam-box on host)+tun2proxy --tun tun0 --proxy http://127.0.0.1:8080 &++# Give network a second to settle+sleep 2++echo "Starting Chaos Agent Loop..."+cd /app/chaos-agent+python3 agent.py --mcp-url http://127.0.0.1:8080/mcp --inference-url http://127.0.0.1:8080/v1 > /var/log/agent.log 2>&1++echo "Agent completed. Powering off."+reboot -f