Skip to content

Commit

Permalink
feat: implement mc run command
Browse files Browse the repository at this point in the history
This commit includes several major refactoring changes to improve the integration
between monocore and monofs components:

Core Changes:
- Add run command implementations
- Rename monokrun to mcrun for consistency
- Add monofs and mfsrun binaries to build system
- Update users crate to uzers to fix #143 and #144
- Improve layer caching and extraction logic
- Add log level configuration support

CLI Improvements:
- Update CLI argument handling for better UX
- Add support for custom layer paths
- Improve logging configuration

Build System:
- Split build target into monocore and monofs components
- Update Makefile to handle new binary targets
- Add build_libkrun target replacing deps target

Documentation:
- Add module documentation for management components
- Update README progress indicators
- Improve code comments and error messages
  • Loading branch information
appcypher committed Feb 20, 2025
1 parent 6dc87da commit e9f44a8
Show file tree
Hide file tree
Showing 42 changed files with 1,254 additions and 527 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ async-recursion = "1.1"
cfg-if = "1.0"
nfsserve = "0.10"
intaglio = "1.10"
users = "0.11"
uzers = "0.12"
63 changes: 40 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ HOME_BIN := $(HOME)/.local/bin
# Build Paths and Directories
# -----------------------------------------------------------------------------
MONOCORE_RELEASE_BIN := target/release/monocore
MONOKRUN_RELEASE_BIN := target/release/monokrun
MCRUN_RELEASE_BIN := target/release/mcrun
MONOFS_RELEASE_BIN := target/release/monofs
MFSRUN_RELEASE_BIN := target/release/mfsrun
EXAMPLES_DIR := target/release/examples
BENCHES_DIR := target/release
BUILD_DIR := build
Expand All @@ -43,23 +45,31 @@ endif
# -----------------------------------------------------------------------------
# Phony Targets Declaration
# -----------------------------------------------------------------------------
.PHONY: all build install clean deps example bench bin _run_example _run_bench _run_bin help uninstall
.PHONY: all build install clean build_libkrun example bench bin _run_example _run_bench _run_bin help uninstall monocore monofs

# -----------------------------------------------------------------------------
# Main Targets
# -----------------------------------------------------------------------------
all: build

build: deps $(MONOCORE_RELEASE_BIN) $(MONOKRUN_RELEASE_BIN)
build: build_libkrun
@$(MAKE) _build_monocore
@$(MAKE) _build_monofs

_build_monocore: $(MONOCORE_RELEASE_BIN) $(MCRUN_RELEASE_BIN)
@cp $(MONOCORE_RELEASE_BIN) $(BUILD_DIR)/
@cp $(MONOKRUN_RELEASE_BIN) $(BUILD_DIR)/
@echo "Build artifacts copied to $(BUILD_DIR)/"
@cp $(MCRUN_RELEASE_BIN) $(BUILD_DIR)/
@echo "Monocore build artifacts copied to $(BUILD_DIR)/"

_build_monofs: $(MONOFS_RELEASE_BIN) $(MFSRUN_RELEASE_BIN)
@cp $(MONOFS_RELEASE_BIN) $(BUILD_DIR)/
@cp $(MFSRUN_RELEASE_BIN) $(BUILD_DIR)/
@echo "Monofs build artifacts copied to $(BUILD_DIR)/"

# -----------------------------------------------------------------------------
# Binary Building
# -----------------------------------------------------------------------------
$(MONOCORE_RELEASE_BIN): deps
@mkdir -p $(BUILD_DIR)
$(MONOCORE_RELEASE_BIN): build_libkrun
cd monocore
ifeq ($(OS),Darwin)
RUSTFLAGS="-C link-args=-Wl,-rpath,@executable_path/../lib,-rpath,@executable_path" cargo build --release --bin monocore $(FEATURES)
Expand All @@ -68,23 +78,31 @@ else
RUSTFLAGS="-C link-args=-Wl,-rpath,\$$ORIGIN/../lib,-rpath,\$$ORIGIN" cargo build --release --bin monocore $(FEATURES)
endif

$(MONOKRUN_RELEASE_BIN): deps
$(MCRUN_RELEASE_BIN): build_libkrun
cd monocore
ifeq ($(OS),Darwin)
RUSTFLAGS="-C link-args=-Wl,-rpath,@executable_path/../lib,-rpath,@executable_path" cargo build --release --bin monokrun $(FEATURES)
RUSTFLAGS="-C link-args=-Wl,-rpath,@executable_path/../lib,-rpath,@executable_path" cargo build --release --bin mcrun $(FEATURES)
codesign --entitlements monocore.entitlements --force -s - $@
else
RUSTFLAGS="-C link-args=-Wl,-rpath,\$$ORIGIN/../lib,-rpath,\$$ORIGIN" cargo build --release --bin monokrun $(FEATURES)
RUSTFLAGS="-C link-args=-Wl,-rpath,\$$ORIGIN/../lib,-rpath,\$$ORIGIN" cargo build --release --bin mcrun $(FEATURES)
endif

$(MONOFS_RELEASE_BIN):
cd monofs && cargo build --release --bin monofs $(FEATURES)

$(MFSRUN_RELEASE_BIN):
cd monofs && cargo build --release --bin mfsrun $(FEATURES)

# -----------------------------------------------------------------------------
# Installation
# -----------------------------------------------------------------------------
install: build
install -d $(HOME_BIN)
install -d $(HOME_LIB)
install -m 755 $(BUILD_DIR)/monocore $(HOME_BIN)/monocore
install -m 755 $(BUILD_DIR)/monokrun $(HOME_BIN)/monokrun
install -m 755 $(BUILD_DIR)/mcrun $(HOME_BIN)/mcrun
install -m 755 $(BUILD_DIR)/monofs $(HOME_BIN)/monofs
install -m 755 $(BUILD_DIR)/mfsrun $(HOME_BIN)/mfsrun
ln -sf $(HOME_BIN)/monocore $(HOME_BIN)/mc
@if [ -n "$(LIBKRUNFW_FILE)" ]; then \
install -m 755 $(LIBKRUNFW_FILE) $(HOME_LIB)/; \
Expand All @@ -103,14 +121,11 @@ install: build
# Development Tools
# -----------------------------------------------------------------------------
# Development binary target without RUSTFLAGS
$(MONOKRUN_RELEASE_BIN).dev: deps
$(MONOKRUN_RELEASE_BIN).dev: build_libkrun
cd monocore && cargo build --release --bin monokrun $(FEATURES)
ifeq ($(OS),Darwin)
codesign --entitlements monocore.entitlements --force -s - $(MONOKRUN_RELEASE_BIN)
endif
ifdef OVERLAYFS
sudo setcap cap_sys_admin+ep $(MONOKRUN_RELEASE_BIN)
endif

# Run examples
example: $(MONOKRUN_RELEASE_BIN).dev
Expand Down Expand Up @@ -139,7 +154,9 @@ clean:

uninstall:
rm -f $(HOME_BIN)/monocore
rm -f $(HOME_BIN)/monokrun
rm -f $(HOME_BIN)/mcrun
rm -f $(HOME_BIN)/monofs
rm -f $(HOME_BIN)/mfsrun
rm -f $(HOME_BIN)/mc
rm -f $(HOME_LIB)/libkrunfw.dylib
rm -f $(HOME_LIB)/libkrun.dylib
Expand All @@ -150,7 +167,7 @@ uninstall:
rm -f $(HOME_LIB)/$(notdir $(LIBKRUN_FILE)); \
fi

deps:
build_libkrun:
./scripts/build_libkrun.sh --no-clean

# Catch-all target to allow example names and arguments
Expand All @@ -165,14 +182,14 @@ help:
@echo "======================"
@echo
@echo "Main Targets:"
@echo " make build - Build monocore and monokrun binaries"
@echo " make install - Install binaries and libraries to ~/.local/{bin,lib}"
@echo " make uninstall - Remove all installed components"
@echo " make clean - Remove build artifacts"
@echo " make deps - Build and install dependencies"
@echo " make build - Build all components (monocore and monofs)"
@echo " make install - Install binaries and libraries to ~/.local/{bin,lib}"
@echo " make uninstall - Remove all installed components"
@echo " make clean - Remove build artifacts"
@echo " make build_libkrun - Build libkrun dependency"
@echo
@echo "Development Tools:"
@echo " make example <name> [-- <args>] - Build and run an example"
@echo " make example <n> [-- <args>] - Build and run an example"
@echo " Example: make example microvm_shell -- arg1 arg2"
@echo
@echo "Note: For commands that accept arguments, use -- to separate them"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
| | • Sandbox Groups | ⬜️ | Shared network, volume and env management |
| **🛠️ CLI Tools** |
| | **monocore CLI** | 🟨 | Project and sandbox management interface |
| |`init` | ⬜️ | Interactive project initialization |
| |`init` | | Project initialization |
| |`add` | ⬜️ | Add sandboxes, builds, or groups to project |
| |`remove` | ⬜️ | Remove project components |
| |`list` | ⬜️ | List sandboxes, builds, or groups |
| |`log` | ⬜️ | View component logs with filtering |
| |`tree` | ⬜️ | Display component layer hierarchy |
| |`run` | ⬜️ | Execute defined component scripts |
| |`run` | 🟨 | Execute defined component scripts |
| |`start` | ⬜️ | Execute component start scripts |
| |`shell` | ⬜️ | Interactive sandbox shell access |
| |`tmp` | ⬜️ | Temporary sandbox creation from images |
Expand Down
95 changes: 81 additions & 14 deletions monocore/bin/mcrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
//! To run as a MicroVM:
//! ```bash
//! mcrun microvm \
//! --log-level=3 \
//! --root-path=/path/to/rootfs \
//! --num-vcpus=2 \
//! --ram-mib=1024 \
//! --mapped-dirs=/host/path:/guest/path \
//! --port-map=8080:80 \
//! --workdir-path=/app \
//! --exec-path=/usr/bin/python3 \
//! --args="-m" --args="http.server" --args="8080"
//! --arg="-m" \
//! --arg="http.server" \
//! --arg="8080" \
//! --mapped-dirs=/host/path:/guest/path \
//! --port-map=8080:80 \
//! --env=KEY=VALUE
//! ```
//!
//! ### Supervisor Mode
Expand All @@ -29,7 +34,19 @@
//! mcrun supervisor \
//! --log-dir=/path/to/logs \
//! --child-name=my_vm \
//! --db-path=/path/to/mcrun.db
//! --sandbox-db-path=/path/to/mcrun.db \
//! --log-level=3 \
//! --root-path=/path/to/rootfs \
//! --num-vcpus=2 \
//! --ram-mib=1024 \
//! --workdir-path=/app \
//! --exec-path=/usr/bin/python3 \
//! --arg="-m" \
//! --arg="http.server" \
//! --arg="8080" \
//! --mapped-dirs=/host/path:/guest/path \
//! --port-map=8080:80 \
//! --env=KEY=VALUE
//! ```
use std::env;
Expand Down Expand Up @@ -58,15 +75,16 @@ async fn main() -> Result<()> {

match args.subcommand {
McrunSubcommand::Microvm {
log_level,
root_path,
num_vcpus,
ram_mib,
mapped_dirs,
port_map,
workdir_path,
exec_path,
args,
env,
mapped_dirs,
port_map,
} => {
// Parse mapped directories
let mapped_dirs: Vec<PathPair> = mapped_dirs
Expand Down Expand Up @@ -94,20 +112,36 @@ async fn main() -> Result<()> {
.args(args.iter().map(|s| s.as_str()))
.env(env);

// Set log level if provided
if let Some(log_level) = log_level {
builder = builder.log_level(log_level.try_into()?);
}

// Set working directory if provided
if let Some(workdir_path) = workdir_path {
builder = builder.workdir_path(workdir_path);
}

// Build and start the MicroVM
let vm = builder.build()?;

tracing::info!("Starting MicroVM");
tracing::info!("starting µvm");
vm.start()?;
}
McrunSubcommand::Supervisor {
log_dir,
child_name,
sandbox_db_path,
log_level,
root_path,
num_vcpus,
ram_mib,
workdir_path,
exec_path,
args,
env,
mapped_dirs,
port_map,
} => {
// Get current executable path
let child_exe = env::current_exe()?;
Expand All @@ -116,16 +150,49 @@ async fn main() -> Result<()> {
let supervisor_pid = std::process::id();

// Create microvm monitor
let process_monitor =
MicroVmMonitor::new(supervisor_pid, sandbox_db_path, log_dir.clone()).await?;

// Compose child arguments - these are placeholders that will be overridden
let child_args = vec![
let process_monitor = MicroVmMonitor::new(
supervisor_pid,
sandbox_db_path,
log_dir.clone(),
root_path.clone(),
)
.await?;

// Compose child arguments
let mut child_args = vec![
"microvm".to_string(),
"--root-path=/".to_string(),
"--ram-mib=512".to_string(),
format!("--root-path={}", root_path.display()),
format!("--num-vcpus={}", num_vcpus),
format!("--ram-mib={}", ram_mib),
format!("--workdir-path={}", workdir_path.unwrap_or_default()),
format!("--exec-path={}", exec_path),
];

// Set args if provided
if !args.is_empty() {
child_args.push(format!("--args={}", args.join(",")));
}

// Set env if provided
if !env.is_empty() {
child_args.push(format!("--env={}", env.join(",")));
}

// Set mapped dirs if provided
if !mapped_dirs.is_empty() {
child_args.push(format!("--mapped-dirs={}", mapped_dirs.join(",")));
}

// Set port map if provided
if !port_map.is_empty() {
child_args.push(format!("--port-map={}", port_map.join(",")));
}

// Set log level if provided
if let Some(log_level) = log_level {
child_args.push(format!("--log-level={}", log_level));
}

// Compose child environment variables
let child_envs = vec![("RUST_LOG", "info")];

Expand Down
Loading

0 comments on commit e9f44a8

Please sign in to comment.