Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@

# Re-include template files for go:embed
!**/*.gotmpl
!**/*.tmpl

# Re-include embedded host-service module files
!pkg/sonic/hostservices/modules/**/*.yaml
!pkg/sonic/hostservices/modules/**/*.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*.so
*.dylib
bin/*
agent
agent_cli
Dockerfile.cross
dev

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GOBIN=$(shell go env GOBIN)
endif


PROTO_DIR=./internal/agent/proto
PROTO_DIR=./pkg/agent/proto

# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
Expand Down
1 change: 1 addition & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SPDX-PackageDownloadLocation = "https://github.com/ironcore-dev/sonic-operator"
"internal/agent/hack/**",
"internal/agent/proto/**",
"test/emulation/**",
"internal/agent/sonic/frr/frr.conf.tmpl",
]
precedence = "aggregate"
SPDX-FileCopyrightText = "SAP SE or an SAP affiliate company and IronCore contributors"
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"fmt"
"os"

commands "github.com/ironcore-dev/sonic-operator/internal/agent/agent_client/commands"
Expand All @@ -16,6 +17,7 @@ func main() {

// Execute the command
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
require (
cel.dev/expr v0.25.2 // indirect
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/alicebob/miniredis/v2 v2.39.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down Expand Up @@ -53,6 +54,8 @@ require (
github.com/go-openapi/swag/typeutils v0.25.1 // indirect
github.com/go-openapi/swag/yamlutils v0.25.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/cel-go v0.26.0 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
Expand All @@ -74,6 +77,7 @@ require (
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/vishvananda/netns v0.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0 // indirect
go.opentelemetry.io/otel v1.44.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cel.dev/expr v0.25.2 h1:K6j46C81hXtZQfuX60cVWQFBJahKSE2gfRbNuvr5bFs=
cel.dev/expr v0.25.2/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4=
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/alicebob/miniredis/v2 v2.39.0 h1:M7WbmV5BmV56L8KTG0rw6vEQ+woTOghpDgin2xv4A0g=
github.com/alicebob/miniredis/v2 v2.39.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -82,8 +84,12 @@ github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ=
github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/cel-go v0.26.0 h1:DPGjXackMpJWH680oGY4lZhYjIameYmR+/6RBdDGmaI=
github.com/google/cel-go v0.26.0/go.mod h1:A9O8OU9rdvrK5MQyrqfIxo1a0u4g3sF8KB6PUIaryMM=
github.com/google/gnostic-models v0.7.0 h1:qwTtogB15McXDaNqTZdzPJRHvaVJlAl+HVQnLmJEJxo=
Expand Down Expand Up @@ -190,6 +196,8 @@ github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zd
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
Expand Down
32 changes: 32 additions & 0 deletions hack/host-services/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# hack/host-services

Everything in this directory extends [sonic-hostservices](https://github.com/sonic-net/sonic-host-services) — the SONiC framework that allows containers to invoke privileged operations on the host via DBus.

## Purpose

Provide custom host modules that extend container-to-host communication beyond what the upstream `sonic-host-server` ships by default.

## Structure

| Path | Description |
|------|-------------|
| `modules/` | Custom host module implementations (Python, `HostModule` subclasses) |
| `install-custom-hostservice-modules` | Shell script that installs the modules into a running SONiC switch and restarts `sonic-hostservice.service` |

## Modules

### `modules/onie.py`

DBus endpoint (`host_modules.onie`) that sets ONIE install boot mode from within a container. Exposes a single method:

- **`set_install_mode()`** — mounts the `ONIE-BOOT` partition and writes the appropriate grub environment variables so the switch reboots into ONIE install mode on the next boot.

## Usage

Copy `modules/*.py` to `/usr/local/lib/python3.11/dist-packages/host_modules/` on the target switch, then run:

```bash
./install-custom-hostservice-modules
```

The script patches `sonic-host-server` to import and register the new modules, then restarts the service.
62 changes: 62 additions & 0 deletions hack/host-services/install-custom-hostservice-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
# Registers the onie host module into sonic-host-server if not already present.

set -euo pipefail


SERVER_SCRIPT="/usr/local/bin/sonic-host-server"

# --- Select modules directory based on SONiC build version ---
SONIC_VERSION_FILE="/etc/sonic/sonic_version.yml"
MODULES_DIR="modules/default"

if [ -f "$SONIC_VERSION_FILE" ]; then
if grep -q 'build_version' "$SONIC_VERSION_FILE" && grep 'build_version' "$SONIC_VERSION_FILE" | grep -q 'Edgecore-SONiC'; then
MODULES_DIR="modules/edge"
else
MODULES_DIR="modules/default"
fi
echo "Detected SONiC build version from $SONIC_VERSION_FILE, using modules directory: $MODULES_DIR"
fi

# Determine Python site-packages directory
PYTHON_SITE_PACKAGES=$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null)
if [ -z "$PYTHON_SITE_PACKAGES" ]; then
echo "ERROR: Could not determine Python site-packages directory" >&2
exit 1
fi

TARGET_DIR="$PYTHON_SITE_PACKAGES/host_modules"
sudo mkdir -p "$TARGET_DIR"

# Install onie.py if not already present
ONIE_SCRIPT="$TARGET_DIR/onie.py"
if [ ! -f "$ONIE_SCRIPT" ]; then
sudo cp "$MODULES_DIR/onie.py" "$ONIE_SCRIPT"
echo "Copied onie.py from $MODULES_DIR/ to $TARGET_DIR/"
fi
# --- 1. Add import to the import block ---
if grep -q ', onie' "$SERVER_SCRIPT"; then
echo "Import already present in $SERVER_SCRIPT, skipping."
else
sudo sed -i '/from host_modules import/ s/$/ , onie/' "$SERVER_SCRIPT"
echo "Added 'onie' import to $SERVER_SCRIPT"
fi

# --- 2. Add entry to register_dbus() mod_dict ---
if grep -q "'onie':" "$SERVER_SCRIPT"; then
echo "Registration entry already present in $SERVER_SCRIPT, skipping."
else
# Insert entry after 'config' entry in mod_dict
sudo sed -i "/'config': config_engine.Config('config'),/a\\ 'onie': onie.Onie('onie')," "$SERVER_SCRIPT"
echo "Added 'onie' registration entry to $SERVER_SCRIPT"
fi
fi
echo "Detected SONiC build version from $SONIC_VERSION_FILE, using modules directory: $MODULES_DIR"
fi



echo "Restarting sonic-hostservice..."
systemctl restart sonic-hostservice.service
echo "Done."
25 changes: 25 additions & 0 deletions hack/host-services/modules/default/dbus_mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
handlers:
- name: reboot
interface: org.SONiC.HostService.system_service
method: restart
args:
- name: delay_secs
type: i
value: 0
- name: save_config
interface: org.SONiC.HostService.config
method: save
args:
- name: delay_secs
type: i
value: 0
- name: onie
interface: org.SONiC.HostService.onie
method: reboot
args:
- name: delay_secs
type: i
value: 0


56 changes: 56 additions & 0 deletions hack/host-services/modules/default/onie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""ONIE boot mode module"""

import logging
from host_modules import host_service
from utils.run_cmd import _run_command

MOD_NAME = 'onie'

ONIE_BOOT_MOUNT = '/mnt/onie-boot'

logger = logging.getLogger(__name__)


class Onie(host_service.HostModule):
"""DBus endpoint that sets ONIE install boot mode"""

@host_service.method(host_service.bus_name(MOD_NAME), in_signature='', out_signature='is')
def set_install_mode(self):
"""Mount ONIE-BOOT partition and configure grub to boot into ONIE install mode"""

rc, _, _ = _run_command('mkdir -p ' + ONIE_BOOT_MOUNT)
if rc:
return rc, 'Failed to create mount point ' + ONIE_BOOT_MOUNT

rc, _, stderr = _run_command('mount LABEL=ONIE-BOOT ' + ONIE_BOOT_MOUNT)
if rc:
# Already mounted is acceptable — check /proc/mounts before failing
already_mounted = False
try:
with open('/proc/mounts') as f:
already_mounted = any(ONIE_BOOT_MOUNT in line for line in f)
except OSError:
pass
if not already_mounted:
errmsg = 'Failed to mount ONIE-BOOT: {}'.format(stderr)
logger.error('%s: %s', MOD_NAME, errmsg)
return rc, errmsg
logger.info('%s: ONIE-BOOT already mounted, continuing', MOD_NAME)

for cmd in [
'grub-editenv /host/grub/grubenv set next_entry=ONIE',
'grub-editenv ' + ONIE_BOOT_MOUNT + '/grub/grubenv set onie_mode=install',
]:
rc, _, stderr = _run_command(cmd)
if rc:
errmsg = 'Command failed: {!r}, stderr: {}'.format(cmd, stderr)
logger.error('%s: %s', MOD_NAME, errmsg)
return rc, errmsg

logger.info('%s: ONIE install mode set successfully', MOD_NAME)
return 0, 'ONIE install mode set successfully'


def register():
"""Return the class name"""
return Onie, MOD_NAME
25 changes: 25 additions & 0 deletions hack/host-services/modules/edge/dbus_mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
handlers:
- name: reboot
interface: org.SONiC.HostService.system_service
method: restart
args:
- name: delay_secs
type: i
value: 0
- name: save_config
interface: org.SONiC.HostService.config
method: save
args:
- name: delay_secs
type: i
value: 0
- name: onie
interface: org.SONiC.HostService.onie
method: reboot
args:
- name: delay_secs
type: i
value: 0


58 changes: 58 additions & 0 deletions hack/host-services/modules/edge/onie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""ONIE boot mode module"""

import logging
import subprocess
from host_modules import host_service

MOD_NAME = 'onie'

ONIE_BOOT_MOUNT = '/mnt/onie-boot'

logger = logging.getLogger(__name__)


class Onie(host_service.HostModule):
"""DBus endpoint that sets ONIE install boot mode"""

@staticmethod
def _run_command():
"""Mount ONIE-BOOT partition and configure grub to boot into ONIE install mode"""
cmds = [
['mkdir', '-p', ONIE_BOOT_MOUNT],
['mount', 'LABEL=ONIE-BOOT', ONIE_BOOT_MOUNT],
['grub-editenv', '/host/grub/grubenv', 'set', 'next_entry=ONIE'],
['grub-editenv', ONIE_BOOT_MOUNT + '/grub/grubenv', 'set', 'onie_mode=install'],
]

output = ''
rc = 0
for cmd in cmds:
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
# For the mount command, already mounted is acceptable
if cmd[0] == 'mount':
already_mounted = False
try:
with open('/proc/mounts') as f:
already_mounted = any(ONIE_BOOT_MOUNT in line for line in f)
except OSError:
pass
if already_mounted:
logger.info('%s: ONIE-BOOT already mounted, continuing', MOD_NAME)
continue
logger.error('%s: Exception when calling command %s -> %s', MOD_NAME, cmd, err)
rc = err.returncode
output = err.output
return rc, output

return rc, output

@host_service.method(host_service.bus_name(MOD_NAME), in_signature='', out_signature='is')
def set_install_mode(self):
return Onie._run_command()


def register():
"""Return the class name"""
return Onie, MOD_NAME
Loading
Loading