Skip to content
Open
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
339 changes: 216 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ sysinfo = "0.31.4"
log = "0.4.0"
env_logger = "0.11.8"
regex = "1.11.1"
[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.10.1"
objc2-io-kit = "0.3.2"
httparse = "1.10.1"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.150"

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.61.2", features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
"Win32_System_Console",
] }
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`krunkit` is a tool to launch configurable virtual machines using the [libkrun](https://github.com/containers/libkrun) platform.

> [!IMPORTANT]
> krunkit is only supported on hosts running macOS 14 or newer.
> The established krunkit path supports macOS 14 or newer. Experimental Windows support uses libkrun 2.0 and WHP.

## Installation

Expand Down Expand Up @@ -39,6 +39,14 @@ make
sudo make install
```

On Windows, `LIBKRUN_LIB_DIR` must point to the directory containing `krun.dll.lib` when invoking Cargo.
Place the matching `krun.dll` beside `krunkit.exe` to run it. For local development, `build.ps1` builds a
libkrun 2.0 tree and copies its DLL beside the executable:

```powershell
.\build.ps1 -LibkrunSource C:\src\libkrun
```

To build with `libkrun` from *Homebrew* or *MacPorts* use the appropriate `PREFIX`:

```
Expand Down
66 changes: 66 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
param (
[ValidateSet("all", "debug", "install", "clean")]
[string]$Task = "all",

[string]$Prefix = $env:PREFIX,

[string]$LibkrunSource = $env:LIBKRUN_SOURCE_DIR
)

$ErrorActionPreference = "Stop"
$BinName = "krunkit.exe"
$ReleasePath = "target\release\$BinName"
$DebugPath = "target\debug\$BinName"

function Build-Krunkit([string]$Profile) {
if ([string]::IsNullOrWhiteSpace($LibkrunSource)) {
throw "Specify -LibkrunSource or set LIBKRUN_SOURCE_DIR"
}

$ReleaseArg = @()
if ($Profile -eq "release") {
$ReleaseArg += "--release"
}
cargo build --manifest-path (Join-Path $LibkrunSource "Cargo.toml") -p libkrun --features "ffi blk net" @ReleaseArg
if ($LASTEXITCODE -ne 0) { throw "libkrun build failed" }

$env:LIBKRUN_LIB_DIR = Join-Path $LibkrunSource "target\$Profile"
cargo build @ReleaseArg
if ($LASTEXITCODE -ne 0) { throw "krunkit build failed" }

Copy-Item -Path (Join-Path $env:LIBKRUN_LIB_DIR "krun.dll") -Destination "target\$Profile\krun.dll" -Force
}

switch ($Task) {
"all" {
Build-Krunkit "release"
}

"debug" {
Build-Krunkit "debug"
}

"install" {
if ([string]::IsNullOrWhiteSpace($Prefix)) {
throw "Specify -Prefix or set PREFIX"
}

if (-not (Test-Path $ReleasePath)) {
Build-Krunkit "release"
}

$TargetBinDir = Join-Path $Prefix "bin"

if (-not (Test-Path $TargetBinDir)) {
New-Item -ItemType Directory -Path $TargetBinDir -Force | Out-Null
}

Copy-Item -Path $ReleasePath -Destination $TargetBinDir -Force
Copy-Item -Path "target\release\krun.dll" -Destination $TargetBinDir -Force
Write-Host "Successfully installed to: $TargetBinDir" -ForegroundColor Green
}

"clean" {
cargo clean
}
}
11 changes: 10 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SDPX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0

fn main() {
#[cfg(target_os = "macos")]
Expand All @@ -11,4 +11,13 @@ fn main() {

println!("cargo:rerun-if-env-changed=PREFIX");
}

#[cfg(target_os = "windows")]
{
let lib_dir = std::env::var("LIBKRUN_LIB_DIR")
.expect("LIBKRUN_LIB_DIR must point to the directory containing krun.dll.lib");
println!("cargo:rustc-link-search={lib_dir}");

println!("cargo:rerun-if-env-changed=LIBKRUN_LIB_DIR");
}
}
77 changes: 72 additions & 5 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# krunkit Command Line

`krunkit` can launch configurable virtual machines using macOS's hypervisor framework and the `libkrun` virtual
machine monitor library. The `libkrun` virtual machine configuration can be specified from command line arguments.
`krunkit` can launch configurable virtual machines using macOS's hypervisor framework or Windows Hypervisor
Platform and the `libkrun` virtual machine monitor library. The `libkrun` virtual machine configuration can be
specified from command line arguments.

Specifying a virtual machine's vCPU and RAM allocation is required. Adding devices is optional, yet most workloads
will require a root disk to be useful.
Expand Down Expand Up @@ -64,11 +65,9 @@ This configures a virtual machine to use two vCPUs and 2048 MiB of RAM:
--cpus 2 --memory 2048
```

## Bootloader Configuration

### EFI bootloader

`--bootloader efi` allows booting a disk image using EFI, which removes the need for providing external kernel/initrd/... jthe disk image bootloader will be started by the EFI firmware, which will in turn know which kernel it should be booting.
`--bootloader efi` allows booting a disk image using EFI, which removes the need for providing external kernel/initrd/... the disk image bootloader will be started by the EFI firmware, which will in turn know which kernel it should be booting.

#### Arguments
- `variable-store`: path to a file which EFI can use to store its variables
Expand Down Expand Up @@ -271,3 +270,71 @@ The table below provides some data on how offloading effects the gvproxy and vmn
| gvproxy | krunkit | false | 1.47 Gbits/s | 2.58 Gbits/s |
| gvproxy | vfkit | false | 1.43 Gbits/s | 2.84 Gbits/s |

## Bootloader Configuration on Windows

On Windows, krunkit boots `edk2/OVMF.fd` by default. Use `--firmware-path PATH` to select another UEFI image.

The Windows path connects `ttyS0` to the host terminal. It supports raw and QCOW2 `virtio-blk` images,
path-based `virtio-net,type=unixstream` devices, and `virtio-vsock` Unix socket mappings.

### Windows Limitations

On Windows, only the following devices can be specified with `--device`:

- `virtio-blk`
- `virtio-net,type=unixstream,path=...`; `unixgram`, `unixSocketPath`, and `fd` backends are unsupported.
- `virtio-vsock`
- `virtio-fs` (requires `permissionSemantics=complete`; `simplified` is not supported on Windows)

`virtio-serial`, `virtio-gpu`, and `virtio-input` are unsupported. `virtio-rng` is added
automatically, but must not be specified with `--device`.

The RESTful service (`--restful-uri`), `--timesync`, and nested virtualization (`--nested`) are not
supported on Windows. The default console is connected directly to the host terminal; use it instead of a
`virtio-serial` device for guest text output.

```powershell
krunkit.exe --cpus 2 --memory 2G `
--firmware-path C:\vm\OVMF.fd `
--device virtio-blk,path=C:\vm\disk.raw,format=raw
```

### SSH on Windows

Start gvproxy in a separate terminal. Its default virtual network assigns the guest `192.168.127.2`, and
`-ssh-port 2222` forwards host port 2222 to guest port 22:

```powershell
gvproxy.exe -listen-qemu unix://C:/vm/network.sock -ssh-port 2222
```

Add the matching network device when starting krunkit:

```powershell
krunkit.exe --cpus 2 --memory 2G `
--firmware-path C:\vm\OVMF.fd `
--device virtio-blk,path=C:\vm\rootfs.raw,format=raw `
--device virtio-net,type=unixstream,path=C:\vm\network.sock,mac=5a:94:ef:e4:0c:ee,offloading=true
```

The guest must run DHCP on the virtio network interface and have `sshd` listening on port 22. Connect with:

```powershell
ssh.exe -p 2222 USER@127.0.0.1
```

### vsock on Windows

The default `listen` action handles guest-initiated connections: libkrun connects guest port 1024 to an existing
host Unix socket.

```powershell
--device virtio-vsock,port=1024,socketURL=C:\vm\guest-to-host.sock,listen
```

Use `connect` for host-initiated connections. Libkrun listens at the host socket path and forwards accepted
connections to a service listening on guest CID 3, port 1024.

```powershell
--device virtio-vsock,port=1024,socketURL=C:\vm\host-to-guest.sock,connect
```
11 changes: 11 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: Apache-2.0

#[cfg(not(windows))]
mod unix;
#[cfg(windows)]
mod windows;

#[cfg(not(windows))]
pub use unix::KrunContext;
#[cfg(windows)]
pub use windows::KrunContext;
6 changes: 4 additions & 2 deletions src/context.rs → src/context/unix.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use super::*;

use crate::{
cmdline::Args,
status::{get_shutdown_eventfd, status_listener, RestfulUri},
virtio::KrunContextSet,
};
Expand All @@ -16,7 +15,9 @@ use std::{
io,
};

#[cfg(target_os = "macos")]
use crate::timesync::timesync_listener;
#[cfg(target_os = "macos")]
use crate::virtio::{VsockAction, VsockConfig};
use anyhow::{anyhow, Context};
use env_logger::{Builder, Env, Target};
Expand Down Expand Up @@ -199,6 +200,7 @@ impl TryFrom<Args> for KrunContext {
}
}

#[cfg(target_os = "macos")]
if let Some(timesync_port) = args.timesync {
let vsock_config = VsockConfig {
port: timesync_port,
Expand Down
Loading