Skip to content

Feat: Add Port Range by ENV vars#362

Closed
Leay15 wants to merge 21 commits intosuperradcompany:mainfrom
AurIAL-Rocks:feat/add-docker-port-mapping
Closed

Feat: Add Port Range by ENV vars#362
Leay15 wants to merge 21 commits intosuperradcompany:mainfrom
AurIAL-Rocks:feat/add-docker-port-mapping

Conversation

@Leay15
Copy link
Copy Markdown

@Leay15 Leay15 commented Jan 19, 2026

This pull request introduces support for configuring a custom port range for sandbox port allocation in the microsandbox server, allowing administrators to control which ports are used for sandbox networking. It also standardizes the use of bash in project scripts for improved compatibility and reliability. It is a complement to #349

Sandbox Port Range Configuration:

  • Added two new environment variables, MICROSANDBOX_PORT_MIN and MICROSANDBOX_PORT_MAX, to allow specifying a minimum and maximum port for sandbox allocation. If these are set, the server will allocate ports only within this range; otherwise, it will fall back to dynamic OS allocation. (microsandbox-utils/lib/env.rs, microsandbox-server/lib/config.rs) [1] [2] [3] [4]
  • Updated the PortManager to support an optional port range, including logic to find available ports within the configured range and to fall back to OS allocation if none are available. (microsandbox-server/lib/port.rs) [1] [2] [3] [4]

Script Standardization:

  • Changed the shebang line in all shell scripts from /bin/sh to /usr/bin/env bash to ensure scripts are run with bash, which is required for some script features. (scripts/build_libkrun.sh, scripts/install_microsandbox.sh, scripts/package_microsandbox.sh, scripts/setup_env.sh) [1] [2] [3] [4]
  • Updated the Makefile to explicitly invoke bash when running build_libkrun.sh for consistency with the new script requirements. (Makefile)

@Leay15 Leay15 closed this Feb 1, 2026
@Leay15 Leay15 reopened this Feb 9, 2026
@Leay15 Leay15 marked this pull request as ready for review February 9, 2026 20:03
@Leay15
Copy link
Copy Markdown
Author

Leay15 commented Feb 9, 2026

This refers to #364 as the multiple PRs request 😄

@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kindly revert all shebang-related changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this back to what it was.

Comment on lines +23 to +28
/// Environment variable for the minimum port in the sandbox port range
pub const MICROSANDBOX_PORT_MIN_ENV_VAR: &str = "MICROSANDBOX_PORT_MIN";

/// Environment variable for the maximum port in the sandbox port range
pub const MICROSANDBOX_PORT_MAX_ENV_VAR: &str = "MICROSANDBOX_PORT_MAX";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use just one env var, MSB_PORT_RANGE, and the format is: <lower:u16>..[=]<upper:u16>.

if the lower is absent, its safe to say the format is invalid. the = is optional

/// Returns the port range for sandbox port allocation.
/// If both MICROSANDBOX_PORT_MIN and MICROSANDBOX_PORT_MAX are set,
/// returns Some((min, max)). Otherwise, returns None for dynamic allocation.
pub fn get_sandbox_port_range() -> Option<(u16, u16)> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can return a Range

Comment on lines +47 to +48
config.get_port_range_min().as_ref().copied(),
config.get_port_range_max().as_ref().copied(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per my other comment. if we used just one env, and returned a range, we can avoid this.

toksdotdev
toksdotdev previously approved these changes Feb 13, 2026
@toksdotdev toksdotdev self-requested a review February 13, 2026 06:59
@toksdotdev toksdotdev dismissed their stale review February 13, 2026 07:00

requested a few changes

@Leay15
Copy link
Copy Markdown
Author

Leay15 commented Feb 14, 2026

@toksdotdev All changes have been made based on your feedback; please let me know if any further changes are needed.

@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this back to what it was.

// Only upper exists (missing lower) should be invalid
assert_eq!(parse_sandbox_port_range("..2000"), None);
// Does not include '=' and lower < upper
assert_eq!(parse_sandbox_port_range("1000..2000"), Some(1000..=2000));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong. 1000..2000 shouldn't include 2000.

@Leay15
Copy link
Copy Markdown
Author

Leay15 commented Feb 15, 2026

@toksdotdev Done! Please let me know if any other changes are needed 👌🏻

@Leay15 Leay15 closed this Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants