Skip to content

Commit 58d3252

Browse files
committed
add multiOS usecase for platform agnostic system
1 parent 34f334a commit 58d3252

File tree

9 files changed

+139
-1
lines changed

9 files changed

+139
-1
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
jobs:
2+
build:
3+
strategy:
4+
matrix:
5+
os: [ubuntu-latest, macos-latest, windows-latest]
6+
runs-on: ${{ matrix.os }}

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ uuid = { version = "1.8.0", features = ["v4"] }
2525
libvirt = "0.1.0"
2626
governor = { version = "0.8.0", features = ["std", "nohashmap"] }
2727

28+
[target.'cfg(target_os = "linux")'.dependencies]
29+
libvirt = "0.1.0"
30+
31+
[target.'cfg(target_os = "macos")'.dependencies]
32+
core-graphics = "0.24.0"
33+
metal = { version = "0.31.0", features = ["private"] }
34+
35+
[target.'cfg(target_os = "windows")'.dependencies]
36+
winapi = { version = "0.3", features = ["dxgi", "d3dcommon"] }
37+
dxgi = "0.1.7"
38+
2839
[lib]
2940
name = "gpu_share_vm_manager"
3041
path = "src/lib.rs"

docs/platform_support.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Platform Feature Matrix
2+
3+
| Feature | Linux | MacOS | Windows |
4+
|-----------------|-------|-------|---------|
5+
| GPU Passthrough ||| ⚠️ WSL2 |
6+
| KVM Acceleration||||
7+
| Metal API ||||
8+
| DirectX 12 ||||

src/api/routes.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ use crate::core::vm::{VMStatus, VMConfig};
7979
use crate::gpu::device::{GPUManager, GPUDevice, GPUConfig};
8080
use crate::monitoring::metrics::{MetricsCollector, ResourceMetrics};
8181
use crate::api::middleware::rate_limit::{rate_limit_layer, GlobalRateLimit, RateLimitExceeded};
82+
use crate::utils::os::{current_platform};
8283

8384
fn handle_error(err: impl std::fmt::Display) -> StatusCode {
8485
error!("Operation failed: {}", err);
@@ -200,6 +201,26 @@ async fn create_vm(
200201
disk_size_gb: params.disk_size_gb.unwrap_or(20),
201202
};
202203

204+
#[cfg(target_os = "linux")]
205+
{
206+
// Linux-specific VM creation
207+
}
208+
209+
#[cfg(target_os = "macos")]
210+
{
211+
// MacOS hypervisor framework usage
212+
}
213+
214+
#[cfg(target_os = "windows")]
215+
{
216+
// Hyper-V integration
217+
}
218+
219+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
220+
{
221+
return Err(Error::UnsupportedPlatform(current_platform().to_string()));
222+
}
223+
203224
let vm = libvirt.create_vm(&config).await
204225
.map_err(handle_error)?;
205226

src/core/libvirt.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ pub struct LibvirtManager {
1515
impl LibvirtManager {
1616
// hehe connect me senpai! ^_^
1717
pub fn new() -> Result<Self> {
18+
#[cfg(target_os = "linux")]
19+
let uri = "qemu:///system";
20+
21+
#[cfg(target_os = "macos")]
22+
let uri = "qemu:///session";
23+
24+
#[cfg(target_os = "windows")]
25+
let uri = "qemu:///system";
26+
1827
info!("Initializing libvirt connection - let the show begin!");
19-
let conn = Connect::open(Some("qemu:///system"))?;
28+
let conn = Connect::open(Some(uri))?;
2029
Ok(Self { conn })
2130
}
2231

src/errors/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ pub enum GpuError {
7676
message: String,
7777
driver_name: String,
7878
},
79+
80+
#[cfg(target_os = "macos")]
81+
#[error("Metal API error: {0}")]
82+
MetalError(String),
83+
84+
#[cfg(target_os = "windows")]
85+
#[error("DXGI error: {0}")]
86+
DxgiError(#[from] dxgi::Error),
87+
88+
#[error("Unsupported operation on {0}")]
89+
UnsupportedPlatform(String),
7990
}
8091

8192
#[derive(Debug)]

src/gpu/device.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,40 @@ fn read_gpu_temperature(path: &Path) -> Result<f64> {
194194
fn read_gpu_utilization(path: &Path) -> Result<f64> {
195195
let util_str = fs::read_to_string(path.join("gpu_busy_percent"))?.trim().to_string();
196196
Ok(util_str.parse()?)
197+
}
198+
199+
#[cfg(target_os = "linux")]
200+
fn get_gpu_info() -> Result<Vec<GPUDevice>> {
201+
// Linux-specific implementation using sysfs
202+
Ok(Vec::new())
203+
}
204+
205+
#[cfg(target_os = "macos")]
206+
fn get_gpu_info() -> Result<Vec<GPUDevice>> {
207+
use core_graphics::display::CGDisplay;
208+
let mut gpus = Vec::new();
209+
for display in CGDisplay::active_displays()? {
210+
gpus.push(GPUDevice {
211+
id: format!("display-{}", display),
212+
vendor_id: "Apple".into(),
213+
// MacOS specific GPU info
214+
});
215+
}
216+
Ok(gpus)
217+
}
218+
219+
#[cfg(target_os = "windows")]
220+
fn get_gpu_info() -> Result<Vec<GPUDevice>> {
221+
// Windows implementation using DXGI
222+
use dxgi::Factory;
223+
let factory = Factory::new()?;
224+
let mut gpus = Vec::new();
225+
for adapter in factory.adapters() {
226+
gpus.push(GPUDevice {
227+
id: adapter.get_info().name,
228+
vendor_id: "NVIDIA/AMD/Intel".into(),
229+
// Windows specific data
230+
});
231+
}
232+
Ok(gpus)
197233
}

src/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[macro_export]
2+
macro_rules! platform_require {
3+
(linux) => {
4+
#[cfg(not(target_os = "linux"))]
5+
compile_error!("This feature is only available on Linux");
6+
};
7+
(macos) => {
8+
#[cfg(not(target_os = "macos"))]
9+
compile_error!("This feature is only available on macOS");
10+
};
11+
(windows) => {
12+
#[cfg(not(target_os = "windows"))]
13+
compile_error!("This feature is only available on Windows");
14+
};
15+
}

src/utils/os.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[cfg(target_os = "linux")]
2+
pub fn current_platform() -> Platform {
3+
Platform::Linux
4+
}
5+
6+
#[cfg(target_os = "macos")]
7+
pub fn current_platform() -> Platform {
8+
Platform::MacOS
9+
}
10+
11+
#[cfg(target_os = "windows")]
12+
pub fn current_platform() -> Platform {
13+
Platform::Windows
14+
}
15+
16+
pub enum Platform {
17+
Linux,
18+
MacOS,
19+
Windows,
20+
Unknown,
21+
}

0 commit comments

Comments
 (0)