restart git repo
This commit is contained in:
commit
8a816b5e85
16 changed files with 1458 additions and 0 deletions
51
server/build.zig
Normal file
51
server/build.zig
Normal file
|
@ -0,0 +1,51 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) !void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const translate_c = b.addTranslateC(.{
|
||||
.root_source_file = b.path("libvirt/include/libvirt/libvirt.h"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
translate_c.addIncludeDir("libvirt/include");
|
||||
|
||||
const output = b.addInstallFile(translate_c.getOutput(), "libvirt.zig");
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "server",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
exe.step.dependOn(&output.step);
|
||||
exe.linkLibC();
|
||||
exe.root_module.addIncludePath(b.path("libvirt/include"));
|
||||
// exe.linkSystemLibrary("libvirt");
|
||||
exe.addLibraryPath(b.path("libvirt/lib"));
|
||||
exe.addObjectFile(b.path("libvirt/lib/libvirt.so.0.10000.0"));
|
||||
exe.addObjectFile(b.path("libvirt/lib/libvirt-admin.so.0.10000.0"));
|
||||
exe.addObjectFile(b.path("libvirt/lib/libvirt-qemu.so.0.10000.0"));
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_exe_unit_tests.step);
|
||||
}
|
41
server/build.zig.zon
Normal file
41
server/build.zig.zon
Normal file
|
@ -0,0 +1,41 @@
|
|||
.{
|
||||
.name = "server",
|
||||
.version = "0.0.0",
|
||||
//.minimum_zig_version = "0.11.0",
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
},
|
||||
}
|
151
server/src/libvirt.zig
Normal file
151
server/src/libvirt.zig
Normal file
|
@ -0,0 +1,151 @@
|
|||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const heap = std.heap;
|
||||
|
||||
pub const c = @cImport({
|
||||
@cInclude("libvirt/libvirt.h");
|
||||
@cInclude("libvirt/libvirt-admin.h");
|
||||
@cInclude("libvirt/libvirt-lxc.h");
|
||||
@cInclude("libvirt/libvirt-qemu.h");
|
||||
@cInclude("libvirt/virterror.h");
|
||||
});
|
||||
|
||||
fn handleError() VirError {
|
||||
const err = c.virGetLastError();
|
||||
std.debug.print("err: {any}\n", .{err});
|
||||
return VirError.OK;
|
||||
// switch (err.*.code) {
|
||||
// 0 => return VirError.OK,
|
||||
// 1 => return VirError.InternalError,
|
||||
// 2 => return VirError.NoMemory,
|
||||
// // TODO rest
|
||||
// else => VirError.OK,
|
||||
// }
|
||||
}
|
||||
|
||||
pub const VirError = error{
|
||||
OK,
|
||||
InternalError,
|
||||
NoMemory,
|
||||
NoSupport,
|
||||
UnknownHost,
|
||||
NoConnect,
|
||||
InvalidConn,
|
||||
InvalidDomain,
|
||||
InvalidArg,
|
||||
OperationFailed,
|
||||
GetFailed,
|
||||
PostFailed,
|
||||
HttpError,
|
||||
SExprError,
|
||||
NoXen,
|
||||
XenCall,
|
||||
OsType,
|
||||
NoKernel,
|
||||
NoRoot,
|
||||
NoSource,
|
||||
NoTarget,
|
||||
NoName,
|
||||
NoOs,
|
||||
NoDevice,
|
||||
NoXenstore,
|
||||
DriverFull,
|
||||
CallFailed,
|
||||
XmlError,
|
||||
DomExist,
|
||||
OperationDenied,
|
||||
OpenFailed,
|
||||
ReadFailed,
|
||||
ParseFailed,
|
||||
ConfSyntax,
|
||||
// TODO rest
|
||||
};
|
||||
|
||||
pub const Connection = struct {
|
||||
conn: c.virConnectPtr,
|
||||
allocator: mem.Allocator,
|
||||
|
||||
pub fn connect(uri: []const u8, allocator: mem.Allocator) VirError!Connection {
|
||||
const connection = c.virConnectOpenAuth(@ptrCast(uri), c.virConnectAuthPtrDefault, 0);
|
||||
if (connection) |conn| return .{
|
||||
.conn = conn,
|
||||
.allocator = allocator,
|
||||
} else return handleError();
|
||||
}
|
||||
|
||||
pub fn close(self: *const Connection) void {
|
||||
_ = c.virConnectClose(self.conn);
|
||||
}
|
||||
|
||||
pub fn getURI(self: *const Connection) ![]u8 {
|
||||
const uri = c.virConnectGetURI(self.conn);
|
||||
defer std.c.free(uri);
|
||||
return try self.allocator.dupe(u8, mem.span(uri));
|
||||
}
|
||||
|
||||
pub fn freeURI(self: *const Connection, uri: []u8) void {
|
||||
self.allocator.free(uri);
|
||||
}
|
||||
|
||||
pub fn numOfDomains(self: *const Connection) !u32 {
|
||||
return @intCast(c.virConnectNumOfDomains(self.conn));
|
||||
}
|
||||
|
||||
pub fn numOfDefinedDomains(self: *const Connection) !u32 {
|
||||
return @intCast(c.virConnectNumOfDefinedDomains(self.conn));
|
||||
}
|
||||
|
||||
pub fn iterateDomains(self: *const Connection, flags: []const Domain.Flags) Domain.Iterator {
|
||||
var list: [*]c.virDomainPtr = undefined;
|
||||
var flags_int: c_uint = 0;
|
||||
for (flags) |f| flags_int |= @intFromEnum(f);
|
||||
const num = c.virConnectListAllDomains(self.conn, @ptrCast(&list), flags_int);
|
||||
return .{
|
||||
.list = list,
|
||||
.num = num,
|
||||
.curr = 0,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Domain = struct {
|
||||
ptr: c.virDomainPtr,
|
||||
|
||||
pub fn getName(self: *const Domain) []const u8 {
|
||||
const name = c.virDomainGetName(self.ptr);
|
||||
return mem.span(name);
|
||||
}
|
||||
|
||||
pub fn isActive(self: *const Domain) bool {
|
||||
const active = c.virDomainIsActive(self.ptr);
|
||||
return if (active == 0) false else true;
|
||||
}
|
||||
|
||||
pub const Flags = enum(c_uint) {
|
||||
ListDomainsActive = c.VIR_CONNECT_LIST_DOMAINS_ACTIVE,
|
||||
ListDomainsInactive = c.VIR_CONNECT_LIST_DOMAINS_INACTIVE,
|
||||
};
|
||||
|
||||
pub const Iterator = struct {
|
||||
list: [*]c.virDomainPtr,
|
||||
num: c_int,
|
||||
curr: usize,
|
||||
|
||||
pub fn deinit(self: *Iterator) void {
|
||||
var i: usize = 0;
|
||||
while (i < self.num) : (i += 1) _ = c.virDomainFree(self.list[i]);
|
||||
}
|
||||
|
||||
pub fn first(self: *Iterator) Domain {
|
||||
self.curr = 0;
|
||||
return .{ .ptr = self.list[self.curr] };
|
||||
}
|
||||
|
||||
pub fn next(self: *Iterator) ?Domain {
|
||||
if (self.curr >= self.num) return null;
|
||||
const ptr = self.list[self.curr];
|
||||
self.curr += 1;
|
||||
return .{ .ptr = ptr };
|
||||
}
|
||||
};
|
||||
};
|
85
server/src/main.zig
Normal file
85
server/src/main.zig
Normal file
|
@ -0,0 +1,85 @@
|
|||
const std = @import("std");
|
||||
const fs = std.fs;
|
||||
const libvirt = @import("libvirt.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// std.debug.print("{any}\n", .{libvirt.c.virConnectAuthPtr});
|
||||
|
||||
const connection = try libvirt.Connection.connect("qemu+ssh://jeeves@evil.lan/system", allocator);
|
||||
defer connection.close();
|
||||
|
||||
const uri = try connection.getURI();
|
||||
defer connection.freeURI(uri);
|
||||
std.debug.print("uri: {s}\n", .{uri});
|
||||
|
||||
const num_active = try connection.numOfDomains();
|
||||
const num_inactive = try connection.numOfDefinedDomains();
|
||||
std.debug.print("active: {d}, inactive: {d}\n", .{ num_active, num_inactive });
|
||||
|
||||
var domain_iter = connection.iterateDomains(&[_]libvirt.Domain.Flags{
|
||||
libvirt.Domain.Flags.ListDomainsActive,
|
||||
libvirt.Domain.Flags.ListDomainsInactive,
|
||||
});
|
||||
defer domain_iter.deinit();
|
||||
|
||||
while (domain_iter.next()) |domain| {
|
||||
const active = domain.isActive();
|
||||
const name = domain.getName();
|
||||
std.debug.print("name: {s}, active: {any}\n", .{ name, active });
|
||||
}
|
||||
|
||||
// const connection = libvirt.c.virConnectOpenAuth("qemu+ssh://jeeves@evil.lan/system", libvirt.c.virConnectAuthPtrDefault, 0);
|
||||
// if (connection) |conn| {
|
||||
// const conn_uri = libvirt.c.virConnectGetURI(conn);
|
||||
// defer std.c.free(@ptrCast(conn_uri));
|
||||
// if (conn_uri) |uri| {
|
||||
// std.debug.print("conn uri: {s}\n", .{uri});
|
||||
// }
|
||||
|
||||
// const num_active_domains = libvirt.c.virConnectNumOfDomains(conn);
|
||||
// const num_inactive_domains = libvirt.c.virConnectNumOfDefinedDomains(conn);
|
||||
|
||||
// std.debug.print("there are {d} active and {d} inactive domains\n", .{ num_active_domains, num_inactive_domains });
|
||||
|
||||
// var domain_list: [*]libvirt.c.virDomainPtr = undefined;
|
||||
// const flags = libvirt.c.VIR_CONNECT_LIST_DOMAINS_ACTIVE | libvirt.c.VIR_CONNECT_LIST_DOMAINS_INACTIVE;
|
||||
// const num_domains = libvirt.c.virConnectListAllDomains(conn, @ptrCast(&domain_list), flags);
|
||||
|
||||
// var i: usize = 0;
|
||||
// while (i < num_domains) : (i += 1) {
|
||||
// const active = libvirt.c.virDomainIsActive(domain_list[i]);
|
||||
// const name = libvirt.c.virDomainGetName(domain_list[i]);
|
||||
// std.debug.print("name: {s}, active: {any}\n", .{ name, active });
|
||||
// _ = libvirt.c.virDomainFree(domain_list[i]);
|
||||
// }
|
||||
// }
|
||||
|
||||
// var flake = try fs.cwd().createFile("flake.nix", .{});
|
||||
// defer flake.close();
|
||||
// try flake.writeAll(
|
||||
// \\{
|
||||
// \\ description = "vm-flake";
|
||||
// \\ inputs.oslib.url = "git+https://git.jeevio.xyz/jeeves/oslib";
|
||||
// \\ outputs = { self, oslib }: oslib.vmFlake {
|
||||
// \\
|
||||
// \\ };
|
||||
// \\}
|
||||
// );
|
||||
}
|
||||
|
||||
pub const DomainSpec = struct {
|
||||
os: Quad,
|
||||
preinstalledSoftware: []const []const u8,
|
||||
modules: []const []const u8,
|
||||
};
|
||||
|
||||
pub const Quad = struct {
|
||||
name: []const u8,
|
||||
version: []const u8,
|
||||
edition: []const u8,
|
||||
arch: []const u8,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue