Skip to main content

microsandbox_protocol/
lib.rs

1//! `microsandbox-protocol` defines the shared protocol types used for communication
2//! between the host and the guest agent over CBOR-over-virtio-serial.
3
4#![warn(missing_docs)]
5
6mod error;
7
8//--------------------------------------------------------------------------------------------------
9// Constants: Host↔Guest Protocol
10//--------------------------------------------------------------------------------------------------
11
12/// Virtio-console port name for the agent channel.
13pub const AGENT_PORT_NAME: &str = "agent";
14
15/// Virtiofs tag for the runtime filesystem (scripts, heartbeat).
16pub const RUNTIME_FS_TAG: &str = "msb_runtime";
17
18/// Guest mount point for the runtime filesystem.
19pub const RUNTIME_MOUNT_POINT: &str = "/.msb";
20
21/// Guest directory for file mount virtiofs shares.
22pub const FILE_MOUNTS_DIR: &str = "/.msb/file-mounts";
23
24/// Guest path for named scripts (added to PATH by agentd).
25pub const SCRIPTS_PATH: &str = "/.msb/scripts";
26
27//--------------------------------------------------------------------------------------------------
28// Constants: Guest Init Environment Variables
29//--------------------------------------------------------------------------------------------------
30
31/// Environment variable carrying tmpfs mount specs for guest init.
32///
33/// Format: `path[,key=value,...][;path[,key=value,...];...]`
34///
35/// - `path` — guest mount path (required, always the first element)
36/// - `size=N` — size limit in MiB (optional)
37/// - `noexec` — mount with noexec flag (optional)
38/// - `ro` — mount read-only (optional)
39/// - `mode=N` — permission mode as octal integer (optional, e.g. `mode=1777`)
40///
41/// Entries are separated by `;`. Within an entry, the path comes first
42/// followed by comma-separated options. Options compose order-independently
43/// (e.g. `,ro,noexec` and `,noexec,ro` are equivalent).
44///
45/// Examples:
46/// - `MSB_TMPFS=/tmp,size=256` — 256 MiB tmpfs at `/tmp`
47/// - `MSB_TMPFS=/tmp,size=256;/var/tmp,size=128` — two tmpfs mounts
48/// - `MSB_TMPFS=/tmp` — tmpfs at `/tmp` with defaults
49/// - `MSB_TMPFS=/tmp,size=256,noexec` — with noexec flag
50/// - `MSB_TMPFS=/seed,size=64,ro` — read-only tmpfs
51pub const ENV_TMPFS: &str = "MSB_TMPFS";
52
53/// Environment variable specifying how agentd assembles the root filesystem.
54///
55/// Format: comma-separated `key=value` pairs, semicolons for multi-value fields.
56///
57/// Variants:
58/// - `kind=disk-image,device=/dev/vda[,fstype=ext4]`
59/// - `kind=oci-layered,lowers=/dev/vdb;/dev/vdc;/dev/vdd,lower_fstype=erofs,upper=/dev/vde,upper_fstype=ext4`
60/// - `kind=oci-flat,lower=/dev/vdb,lower_fstype=erofs,upper=/dev/vdc,upper_fstype=ext4`
61///
62/// Legacy format (`/dev/vda[,fstype=ext4]`) is accepted and treated as `kind=disk-image`.
63pub const ENV_BLOCK_ROOT: &str = "MSB_BLOCK_ROOT";
64
65/// Environment variable carrying the guest network interface configuration.
66///
67/// Format: `key=value,...`
68///
69/// - `iface=NAME` — interface name (required)
70/// - `mac=AA:BB:CC:DD:EE:FF` — MAC address (required)
71/// - `mtu=N` — MTU (optional)
72///
73/// Example:
74/// - `MSB_NET=iface=eth0,mac=02:5a:7b:13:01:02,mtu=1500`
75pub const ENV_NET: &str = "MSB_NET";
76
77/// Environment variable carrying the guest IPv4 network configuration.
78///
79/// Format: `key=value,...`
80///
81/// - `addr=A.B.C.D/N` — address with prefix length (required)
82/// - `gw=A.B.C.D` — default gateway (required)
83/// - `dns=A.B.C.D` — DNS server (optional)
84///
85/// Example:
86/// - `MSB_NET_IPV4=addr=100.96.1.2/30,gw=100.96.1.1,dns=100.96.1.1`
87pub const ENV_NET_IPV4: &str = "MSB_NET_IPV4";
88
89/// Environment variable carrying the guest IPv6 network configuration.
90///
91/// Format: `key=value,...`
92///
93/// - `addr=ADDR/N` — address with prefix length (required)
94/// - `gw=ADDR` — default gateway (required)
95/// - `dns=ADDR` — DNS server (optional)
96///
97/// Example:
98/// - `MSB_NET_IPV6=addr=fd42:6d73:62:2a::2/64,gw=fd42:6d73:62:2a::1,dns=fd42:6d73:62:2a::1`
99pub const ENV_NET_IPV6: &str = "MSB_NET_IPV6";
100
101/// Environment variable carrying virtiofs directory volume mount specs for guest init.
102///
103/// Format: `tag:guest_path[:ro][;tag:guest_path[:ro];...]`
104///
105/// - `tag` — virtiofs tag name (required, matches the tag used in `--mount`)
106/// - `guest_path` — mount point inside the guest (required)
107/// - `ro` — mount read-only (optional suffix)
108///
109/// Entries are separated by `;`.
110///
111/// Examples:
112/// - `MSB_DIR_MOUNTS=data:/data` — mount virtiofs tag `data` at `/data`
113/// - `MSB_DIR_MOUNTS=data:/data:ro` — mount read-only
114/// - `MSB_DIR_MOUNTS=data:/data;cache:/cache:ro` — two mounts
115pub const ENV_DIR_MOUNTS: &str = "MSB_DIR_MOUNTS";
116
117/// Environment variable carrying virtiofs **file** volume mount specs for guest init.
118///
119/// Used when the host path is a single file rather than a directory. The SDK
120/// wraps each file in an isolated staging directory (hard-linked to preserve
121/// the same inode) and shares that directory via virtiofs. Agentd mounts the
122/// share at [`FILE_MOUNTS_DIR`]`/<tag>/` and bind-mounts the file to the
123/// guest path.
124///
125/// Format: `tag:filename:guest_path[:ro][;tag:filename:guest_path[:ro];...]`
126///
127/// - `tag` — virtiofs tag name (required, matches the tag used in `--mount`)
128/// - `filename` — name of the file inside the virtiofs share (required)
129/// - `guest_path` — final file path inside the guest (required)
130/// - `ro` — mount read-only (optional suffix)
131///
132/// Entries are separated by `;`.
133///
134/// Examples:
135/// - `MSB_FILE_MOUNTS=fm_config:app.conf:/etc/app.conf`
136/// - `MSB_FILE_MOUNTS=fm_config:app.conf:/etc/app.conf:ro`
137/// - `MSB_FILE_MOUNTS=fm_a:a.sh:/usr/bin/a.sh;fm_b:b.sh:/usr/bin/b.sh`
138pub const ENV_FILE_MOUNTS: &str = "MSB_FILE_MOUNTS";
139
140/// Environment variable carrying disk-image volume mount specs for guest init.
141///
142/// Each spec describes one virtio-blk device attached for the sole purpose
143/// of being mounted at a guest path by agentd (distinct from the rootfs
144/// block device, which is described by [`ENV_BLOCK_ROOT`]).
145///
146/// Format: `id:guest_path[:fstype][:ro][;id:guest_path[:fstype][:ro];...]`
147///
148/// - `id` — the `virtio_blk_config.serial` value set by the VMM. Agentd
149///   resolves it to a device node via `/dev/disk/by-id/virtio-<id>`, or
150///   by scanning `/sys/block/*/serial` as a fallback.
151/// - `guest_path` — absolute mount path in the guest (required).
152/// - `fstype` — inner filesystem type (optional). When empty or absent,
153///   agentd probes `/proc/filesystems` to find a type that mounts cleanly.
154/// - `ro` — optional flag: mount read-only.
155///
156/// Entries are separated by `;`. The `fstype` slot is positional, not
157/// keyed. To express "no fstype + ro" the slot must be empty:
158/// `id:/path::ro`. The form `id:/path:ro` would parse as `fstype=ro`,
159/// not the readonly flag.
160///
161/// Examples:
162/// - `MSB_DISK_MOUNTS=data_12ab:/data:ext4` — ext4 disk at `/data`
163/// - `MSB_DISK_MOUNTS=seed_7f:/seed::ro` — autodetect fstype, read-only
164/// - `MSB_DISK_MOUNTS=a_1:/a:ext4;b_2:/b::ro` — two disks
165pub const ENV_DISK_MOUNTS: &str = "MSB_DISK_MOUNTS";
166
167/// Environment variable carrying the default guest user for agentd execs.
168///
169/// Format: `USER[:GROUP]` or `UID[:GID]`
170///
171/// - `USER`
172/// - `UID`
173/// - `USER:GROUP`
174/// - `UID:GID`
175///
176/// Example:
177/// - `MSB_USER=alice` — default to user `alice`
178/// - `MSB_USER=1000` — default to UID 1000
179/// - `MSB_USER=alice:developers` — default to user `alice` and group `developers`
180/// - `MSB_USER=1000:100` — default to UID 1000 and GID 100
181pub const ENV_USER: &str = "MSB_USER";
182
183/// Environment variable carrying the guest hostname for agentd.
184///
185/// Format: bare string
186///
187/// Example:
188/// - `MSB_HOSTNAME=worker-01`
189///
190/// agentd calls `sethostname()` and adds the name to `/etc/hosts`.
191/// Defaults to the sandbox name when not explicitly set.
192pub const ENV_HOSTNAME: &str = "MSB_HOSTNAME";
193
194/// Environment variable carrying the DNS name the guest uses to reach
195/// the sandbox host (Docker's `host.docker.internal` equivalent).
196///
197/// The host-side network stack emits this value via its
198/// `guest_env_vars()` method; agentd reads it into
199/// [`crate::exec`]-adjacent boot params and writes the mapping into
200/// `/etc/hosts`. The value the network stack emits is a fixed
201/// protocol constant — today always `host.microsandbox.internal`.
202pub const ENV_HOST_ALIAS: &str = "MSB_HOST_ALIAS";
203
204/// Environment variable carrying sandbox-wide resource limits.
205///
206/// Format: `resource=limit[:hard][;resource=limit[:hard];...]`
207///
208/// - `resource` — lowercase rlimit name such as `nofile` or `nproc`
209/// - `limit` — soft limit
210/// - `hard` — hard limit (optional; if omitted, uses the soft limit)
211///
212/// Examples:
213/// - `MSB_RLIMITS=nofile=65535`
214/// - `MSB_RLIMITS=nofile=65535:65535;nproc=4096:4096`
215///
216/// agentd applies these during PID 1 startup so every later guest process
217/// inherits the raised baseline instead of having to opt into per-exec rlimits.
218pub const ENV_RLIMITS: &str = "MSB_RLIMITS";
219
220/// Guest-side path to the CA certificate for TLS interception.
221///
222/// Placed by the sandbox process via the runtime virtiofs mount.
223/// agentd checks for this file during init and installs it into the guest
224/// trust store.
225pub const GUEST_TLS_CA_PATH: &str = "/.msb/tls/ca.pem";
226
227/// Guest-side path to a PEM bundle of the host's extra trusted CAs.
228///
229/// Placed by the sandbox process via the runtime virtiofs mount when
230/// host-CA trust is enabled (default). agentd checks for this file during
231/// init and appends it to the guest's trust bundle, so outbound TLS works
232/// even behind a corporate MITM proxy whose gateway CA is installed on
233/// the host but unknown to the guest.
234pub const GUEST_TLS_HOST_CAS_PATH: &str = "/.msb/tls/host-cas.pem";
235
236//--------------------------------------------------------------------------------------------------
237// Exports
238//--------------------------------------------------------------------------------------------------
239
240pub mod codec;
241pub mod core;
242pub mod exec;
243pub mod fs;
244pub mod heartbeat;
245pub mod message;
246
247pub use error::*;