Skip to main content

libkrun_sys/
lib.rs

1//! Low-level FFI bindings to libkrun
2//!
3//! This crate provides raw, unsafe bindings to the libkrun C library.
4//! For a safe, idiomatic Rust API, use the higher-level wrapper in the boxlite crate.
5
6use std::os::raw::c_char;
7
8// Log constants from libkrun.h
9pub const KRUN_LOG_TARGET_DEFAULT: i32 = 0;
10pub const KRUN_LOG_TARGET_STDOUT: i32 = 1;
11pub const KRUN_LOG_TARGET_STDERR: i32 = 2;
12
13pub const KRUN_LOG_LEVEL_OFF: u32 = 0;
14pub const KRUN_LOG_LEVEL_ERROR: u32 = 1;
15pub const KRUN_LOG_LEVEL_WARN: u32 = 2;
16pub const KRUN_LOG_LEVEL_INFO: u32 = 3;
17pub const KRUN_LOG_LEVEL_DEBUG: u32 = 4;
18pub const KRUN_LOG_LEVEL_TRACE: u32 = 5;
19
20pub const KRUN_LOG_STYLE_AUTO: u32 = 0;
21pub const KRUN_LOG_STYLE_ALWAYS: u32 = 1;
22pub const KRUN_LOG_STYLE_NEVER: u32 = 2;
23
24// Disk format constants from libkrun.h
25pub const KRUN_DISK_FORMAT_RAW: u32 = 0;
26pub const KRUN_DISK_FORMAT_QCOW2: u32 = 1;
27
28extern "C" {
29    pub fn krun_init_log(target: i32, level: u32, style: u32, flags: u32) -> i32;
30    pub fn krun_set_log_level(level: u32) -> i32;
31    pub fn krun_create_ctx() -> i32;
32    pub fn krun_free_ctx(ctx_id: u32) -> i32;
33    pub fn krun_set_vm_config(ctx_id: u32, num_vcpus: u8, ram_mib: u32) -> i32;
34    pub fn krun_set_root(ctx_id: u32, root_path: *const c_char) -> i32;
35    pub fn krun_add_virtiofs(
36        ctx_id: u32,
37        mount_tag: *const c_char,
38        host_path: *const c_char,
39    ) -> i32;
40    pub fn krun_set_kernel(
41        ctx_id: u32,
42        kernel_path: *const c_char,
43        kernel_format: u32,
44        initramfs: *const c_char,
45        cmdline: *const c_char,
46    ) -> i32;
47    pub fn krun_set_exec(
48        ctx_id: u32,
49        exec_path: *const c_char,
50        argv: *const *const c_char,
51        envp: *const *const c_char,
52    ) -> i32;
53    pub fn krun_set_env(ctx_id: u32, envp: *const *const c_char) -> i32;
54    pub fn krun_set_workdir(ctx_id: u32, workdir_path: *const c_char) -> i32;
55    pub fn krun_split_irqchip(ctx_id: u32, enable: bool) -> i32;
56    pub fn krun_set_nested_virt(ctx_id: u32, enabled: bool) -> i32;
57    pub fn krun_set_gpu_options(ctx_id: u32, virgl_flags: u32) -> i32;
58    pub fn krun_set_rlimits(ctx_id: u32, rlimits: *const *const c_char) -> i32;
59    pub fn krun_set_port_map(ctx_id: u32, port_map: *const *const c_char) -> i32;
60    pub fn krun_add_vsock_port2(
61        ctx_id: u32,
62        port: u32,
63        filepath: *const c_char,
64        listen: bool,
65    ) -> i32;
66    pub fn krun_add_disk(
67        ctx_id: u32,
68        block_id: *const c_char,
69        disk_path: *const c_char,
70        read_only: bool,
71    ) -> i32;
72    pub fn krun_add_disk2(
73        ctx_id: u32,
74        block_id: *const c_char,
75        disk_path: *const c_char,
76        disk_format: u32,
77        read_only: bool,
78    ) -> i32;
79    pub fn krun_add_net_unixstream(
80        ctx_id: u32,
81        c_path: *const c_char,
82        fd: i32,
83        c_mac: *const u8,
84        features: u32,
85        flags: u32,
86    ) -> i32;
87    pub fn krun_add_net_unixgram(
88        ctx_id: u32,
89        c_path: *const c_char,
90        fd: i32,
91        c_mac: *const u8,
92        features: u32,
93        flags: u32,
94    ) -> i32;
95    pub fn krun_start_enter(ctx_id: u32) -> i32;
96
97    /// Set a file path to redirect the console output to.
98    ///
99    /// Must be called before `krun_start_enter`.
100    pub fn krun_set_console_output(ctx_id: u32, filepath: *const c_char) -> i32;
101
102    /// Set the uid before starting the microVM.
103    /// This allows virtiofsd to run with CAP_SETUID for proper ownership handling.
104    pub fn krun_setuid(ctx_id: u32, uid: libc::uid_t) -> i32;
105
106    /// Set the gid before starting the microVM.
107    pub fn krun_setgid(ctx_id: u32, gid: libc::gid_t) -> i32;
108
109    /// Configure a root filesystem backed by a block device with automatic remount.
110    ///
111    /// This allows booting from a disk image without needing to copy the init binary
112    /// into the disk. Libkrun creates a dummy virtiofs root, executes init from it,
113    /// and then switches to the disk-based root.
114    ///
115    /// Arguments:
116    /// - `ctx_id`: Configuration context ID
117    /// - `device`: Block device path (e.g., "/dev/vda", must be a previously configured block device)
118    /// - `fstype`: Filesystem type (e.g., "ext4", can be "auto" or NULL)
119    /// - `options`: Comma-separated mount options (can be NULL)
120    pub fn krun_set_root_disk_remount(
121        ctx_id: u32,
122        device: *const c_char,
123        fstype: *const c_char,
124        options: *const c_char,
125    ) -> i32;
126}