restart git repo
This commit is contained in:
commit
8a816b5e85
16 changed files with 1458 additions and 0 deletions
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