Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

4.6 KB
Raw Blame
/** Host Transparent Huge Page policy shared by first boot and GCP OS Config. */
export const CLOUD_RUNNER_THP = {
  enabled: 'madvise',
  defrag: 'defer',
} as const

export type RunnerThpPolicy = {
  enabled: 'always' | 'madvise' | 'never'
  defrag: 'always' | 'defer' | 'defer+madvise' | 'madvise' | 'never'
}

const VALID_ENABLED = new Set(['always', 'madvise', 'never'])
const VALID_DEFRAG = new Set(['always', 'defer', 'defer+madvise', 'madvise', 'never'])

/** Shared by first boot and GCP OS Config policy enforcement. */
export const thpDeployment = (policy: RunnerThpPolicy = CLOUD_RUNNER_THP) => {
  if (!VALID_ENABLED.has(policy.enabled)) {
    throw new Error(`runner THP enabled policy must be always, madvise, or never (got ${policy.enabled})`)
  }
  if (!VALID_DEFRAG.has(policy.defrag)) {
    throw new Error(
      `runner THP defrag policy must be always, defer, defer+madvise, madvise, or never (got ${policy.defrag})`,
    )
  }

  const setup = `THP_SYSFS_DIR="\${BOXLITE_THP_SYSFS_DIR:-/sys/kernel/mm/transparent_hugepage}"
THP_TMPFILES_PATH="\${BOXLITE_THP_TMPFILES_PATH:-/etc/tmpfiles.d/boxlite-thp.conf}"
THP_ENABLED_PATH="$THP_SYSFS_DIR/enabled"
THP_DEFRAG_PATH="$THP_SYSFS_DIR/defrag"
THP_TMPFILES_CONTENT='w /sys/kernel/mm/transparent_hugepage/enabled - - - - ${policy.enabled}
w /sys/kernel/mm/transparent_hugepage/defrag - - - - ${policy.defrag}'

runner_thp_sysfs_has() {
  [ -r "$1" ] && grep -qF "[$2]" "$1"
}

runner_thp_tmpfiles_matches() {
  local mode
  [ ! -L "$THP_TMPFILES_PATH" ] &&
    [ -f "$THP_TMPFILES_PATH" ] &&
    mode=$(stat -c '%04a' "$THP_TMPFILES_PATH" 2>/dev/null || stat -f '%Mp%03Lp' "$THP_TMPFILES_PATH" 2>/dev/null || true) &&
    [ "$mode" = "0644" ] &&
    [ "$(cat "$THP_TMPFILES_PATH" 2>/dev/null)" = "$THP_TMPFILES_CONTENT" ]
}

runner_thp_matches() {
  runner_thp_tmpfiles_matches &&
    runner_thp_sysfs_has "$THP_ENABLED_PATH" '${policy.enabled}' &&
    runner_thp_sysfs_has "$THP_DEFRAG_PATH" '${policy.defrag}'
}

write_runner_thp_tmpfiles() {
  if runner_thp_tmpfiles_matches; then return 0; fi
  if [ ! -L "$THP_TMPFILES_PATH" ] && [ -d "$THP_TMPFILES_PATH" ]; then
    echo "FATAL: THP tmpfiles path is a directory: $THP_TMPFILES_PATH" >&2
    return 1
  fi
  local dir next
  dir="$(dirname "$THP_TMPFILES_PATH")"
  mkdir -p "$dir" || return 1
  next="$(mktemp "$dir/.boxlite-thp.XXXXXX")" || return 1
  if printf '%s\\n' "$THP_TMPFILES_CONTENT" > "$next" && chmod 0644 "$next"; then
    if mv -Tf "$next" "$THP_TMPFILES_PATH" 2>/dev/null; then
      return 0
    fi
    if [ -L "$THP_TMPFILES_PATH" ]; then
      if rm -f "$THP_TMPFILES_PATH" && mv -f "$next" "$THP_TMPFILES_PATH"; then
        return 0
      fi
    elif [ ! -d "$THP_TMPFILES_PATH" ] && mv -f "$next" "$THP_TMPFILES_PATH"; then
      return 0
    fi
  fi
  rm -f "$next"
  return 1
}

apply_runner_thp_value() {
  local path="$1" value="$2" content
  [ -e "$path" ] || { echo "FATAL: missing THP sysfs path $path" >&2; return 1; }
  if runner_thp_sysfs_has "$path" "$value"; then return 0; fi
  [ -w "$path" ] || { echo "FATAL: THP sysfs path is not writable: $path" >&2; return 1; }
  printf '%s\\n' "$value" > "$path" || { echo "FATAL: failed writing $value to $path" >&2; return 1; }
  content="$(cat "$path")" || { echo "FATAL: failed reading THP sysfs path $path" >&2; return 1; }
  if printf '%s\\n' "$content" | grep -qF '['; then
    printf '%s\\n' "$content" | grep -qF "[$value]" || {
      echo "FATAL: THP sysfs $path did not select [$value]" >&2
      return 1
    }
  else
    [ "$content" = "$value" ] || {
      echo "FATAL: THP sysfs $path did not store $value" >&2
      return 1
    }
  fi
}

install_runner_thp() {
  write_runner_thp_tmpfiles &&
    apply_runner_thp_value "$THP_ENABLED_PATH" '${policy.enabled}' &&
    apply_runner_thp_value "$THP_DEFRAG_PATH" '${policy.defrag}'
}
`

  const enforceBody = `set -euo pipefail
${setup}
# First boot installs its own THP policy before enabling or starting the service.
if ! systemctl is-enabled --quiet boxlite-runner 2>/dev/null &&
    ! systemctl is-active --quiet boxlite-runner; then exit 0; fi
if runner_thp_matches; then echo 'runner THP already current'; exit 0; fi
install_runner_thp
`

  return {
    install: `${setup}\ninstall_runner_thp\n`,
    policyScripts: {
      validate: `#!/bin/bash
set -euo pipefail
${setup}
if ! systemctl is-enabled --quiet boxlite-runner 2>/dev/null &&
    ! systemctl is-active --quiet boxlite-runner; then exit 100; fi
if runner_thp_matches; then exit 100; fi
echo "runner THP configuration needs updating"
exit 101
`,
      enforce: `#!/bin/bash
set -u
bash <<'BOXLITE_RUNNER_THP'
${enforceBody}BOXLITE_RUNNER_THP
status=$?
if [ "$status" -eq 0 ]; then exit 100; fi
exit 101
`,
    },
  }
}