diff --git a/server/src/libvirt-connection.zig b/server/src/libvirt-connection.zig index e802908..7719b28 100644 --- a/server/src/libvirt-connection.zig +++ b/server/src/libvirt-connection.zig @@ -2,8 +2,11 @@ const std = @import("std"); const mem = std.mem; const heap = std.heap; +const h = @import("libvirt-helper.zig"); const c = @import("libvirt-c.zig").c; const err = @import("libvirt-error.zig"); +const Domain = @import("libvirt-domain.zig"); +const Pool = @import("libvirt-pool.zig"); const VirError = err.VirError; const Connection = @This(); @@ -22,15 +25,15 @@ pub fn deinit(self: *const Connection) void { _ = c.virConnectClose(self.ptr); } -// pub fn getURI(self: *const Connection) ![]u8 { -// const uri = c.virConnectGetURI(self.ptr); -// defer std.c.free(uri); -// const str = string(uri); -// return if (str.len == 0) handleError() else try self.allocator.dupe(u8, str); -// } -// pub fn freeURI(self: *const Connection, uri: []u8) void { -// self.allocator.free(uri); -// } +pub fn getURI(self: *const Connection) ![]u8 { + const uri = c.virConnectGetURI(self.ptr); + defer std.c.free(uri); + const str = h.string(uri); + return if (str.len == 0) err.handleError() else try self.allocator.dupe(u8, str); +} +pub fn freeURI(self: *const Connection, uri: []u8) void { + self.allocator.free(uri); +} // pub fn numOfPools(self: *const Connection) !u32 { // return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfStoragePools); @@ -60,12 +63,12 @@ pub fn deinit(self: *const Connection) void { // return if (pool) |p| Pool.init(p, self.allocator) else handleError(); // } -// pub fn numOfDomains(self: *const Connection) !u32 { -// return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains); -// } -// pub fn numOfDefinedDomains(self: *const Connection) !u32 { -// return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains); -// } +pub fn numOfDomains(self: *const Connection) VirError!u32 { + return Domain.numOfDomains(self); +} +pub fn numOfDefinedDomains(self: *const Connection) VirError!u32 { + return Domain.numOfDefinedDomains(self); +} // pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator { // return Domain.DomainIterator.init( diff --git a/server/src/libvirt-domain.zig b/server/src/libvirt-domain.zig index 1afe22a..75d2ebe 100644 --- a/server/src/libvirt-domain.zig +++ b/server/src/libvirt-domain.zig @@ -32,8 +32,14 @@ pub fn getDomainCapabilities() void {} pub fn listAllDomains() void {} pub fn listDefinedDomains() void {} pub fn listDomains() void {} -pub fn numOfDefinedDomains() void {} -pub fn numOfDomains() void {} +pub fn numOfDefinedDomains(conn: *const Connection) VirError!u32 { + const n = c.virConnectNumOfDomains(conn.ptr); + return if (n < 0) err.handleError() else @intCast(n); +} +pub fn numOfDomains(conn: *const Connection) VirError!u32 { + const n = c.virConnectNumOfDomains(conn.ptr); + return if (n < 0) err.handleError() else @intCast(n); +} pub fn abortJob() void {} pub fn abortJobFlags() void {} diff --git a/server/src/main.zig b/server/src/main.zig index 13fdd90..db39d26 100644 --- a/server/src/main.zig +++ b/server/src/main.zig @@ -10,8 +10,8 @@ pub fn main() !void { defer arena.deinit(); const allocator = arena.allocator(); - const connection = try libvirt.Connection.connect("qemu:///system", allocator); - defer connection.close(); + const connection = try libvirt.Connection.init("qemu:///system", allocator); + defer connection.deinit(); var flake_dir = try fs.cwd().openDir("flakes/windows/test", .{}); defer flake_dir.close(); @@ -42,52 +42,52 @@ pub fn main() !void { const uri = try connection.getURI(); defer connection.freeURI(uri); - // std.debug.print("uri: {s}\n", .{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 }); + std.debug.print("active: {d}, inactive: {d}\n", .{ num_active, num_inactive }); _ = .{ num_active, num_inactive }; - var domain_iter = try connection.iterateDomains(&[_]libvirt.Domain.ListFlags{ - libvirt.Domain.ListFlags.Active, - libvirt.Domain.ListFlags.Inactive, - }); - defer domain_iter.deinit(); + // var domain_iter = try connection.iterateDomains(&[_]libvirt.Domain.ListFlags{ + // libvirt.Domain.ListFlags.Active, + // libvirt.Domain.ListFlags.Inactive, + // }); + // 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 }); - _ = .{ name, active }; - } + // while (domain_iter.next()) |domain| { + // const active = domain.isActive(); + // const name = domain.getName(); + // // std.debug.print("name: {s}, active: {any}\n", .{ name, active }); + // _ = .{ name, active }; + // } - var pool_iter = try connection.iteratePools(&[_]libvirt.Pool.ListFlags{ - libvirt.Pool.ListFlags.Active, - libvirt.Pool.ListFlags.Inactive, - }); - defer pool_iter.deinit(); + // var pool_iter = try connection.iteratePools(&[_]libvirt.Pool.ListFlags{ + // libvirt.Pool.ListFlags.Active, + // libvirt.Pool.ListFlags.Inactive, + // }); + // defer pool_iter.deinit(); - while (pool_iter.next()) |pool| { - const name = try pool.getName(); - // std.debug.print("name: {s}\n", .{name}); - if (mem.eql(u8, name, "default")) { - const vol_str = try libvirt.String.init(allocator, volume_xml); - defer vol_str.deinit(); - const volume = pool.createVolume(vol_str, &[_]libvirt.Pool.Volume.CreateFlags{}) catch |err| blk: { - if (err == libvirt.VirError.StorageVolExist) { - break :blk try pool.lookupVolumeByName("wintest.qcow2"); - } else return err; - }; - _ = volume; - break; - } - } + // while (pool_iter.next()) |pool| { + // const name = try pool.getName(); + // // std.debug.print("name: {s}\n", .{name}); + // if (mem.eql(u8, name, "default")) { + // const vol_str = try libvirt.String.init(allocator, volume_xml); + // defer vol_str.deinit(); + // const volume = pool.createVolume(vol_str, &[_]libvirt.Pool.Volume.CreateFlags{}) catch |err| blk: { + // if (err == libvirt.VirError.StorageVolExist) { + // break :blk try pool.lookupVolumeByName("wintest.qcow2"); + // } else return err; + // }; + // _ = volume; + // break; + // } + // } - const dom_str = try libvirt.String.init(allocator, domain_beforeinstall_xml); - defer dom_str.deinit(); - const domain = try connection.createDomain(dom_str, &[_]libvirt.Domain.CreateFlags{}); - _ = domain; + // const dom_str = try libvirt.String.init(allocator, domain_beforeinstall_xml); + // defer dom_str.deinit(); + // const domain = try connection.createDomain(dom_str, &[_]libvirt.Domain.CreateFlags{}); + // _ = domain; } pub const DomainSpec = struct {