11 #include <libvirt/libvirt.h> 12 #include "../Connection.hpp" 13 #include "../Domain.hpp" 14 #include "../Network.hpp" 15 #include "../NodeDevice.hpp" 16 #include "../StoragePool.hpp" 18 #include "../utility.hpp" 24 if (virGetVersion(&ret,
nullptr,
nullptr))
25 throw std::runtime_error{
"virGetVersion"};
32 template <
typename Callback>
33 template <
typename Container>
35 if constexpr (std::is_same_v<Container, std::vector<enums::connection::CredentialType>>)
39 std::copy(c.begin(), c.end(), std::back_inserter(
cred_types));
45 ret.credtype =
reinterpret_cast<int*
>(
cred_types.data());
46 ret.ncredtype = gsl::narrow_cast<
unsigned>(
cred_types.size());
47 ret.cbdata =
reinterpret_cast<void*
>(&
callback);
49 ret.cb = +[](virConnectCredentialPtr creds_ptr,
unsigned int ncreds,
void* cbdata) ->
int {
50 std::vector<ConnectCredential> creds{};
51 creds.reserve(ncreds);
52 std::transform(creds_ptr, creds_ptr + ncreds, std::back_inserter(creds), [](
const auto& cred) {
return ConnectCredential{cred}; });
53 const bool ret = (*
reinterpret_cast<Callback*
>(cbdata))(creds);
55 for (
const auto& [t, p, c, d, res] : creds) {
56 it->result =
static_cast<char*
>(malloc(it->resultlen = res.length() + 1u));
57 std::copy(res.begin(), res.end(), it->result);
66 inline Connection::Connection(gsl::czstring<> name,
bool rd_only) noexcept {
67 underlying = rd_only ? virConnectOpenReadOnly(name) : virConnectOpen(name);
70 template <
typename Callback>
72 virConnectAuth c_auth = auth;
73 underlying = virConnectOpenAuth(name, &c_auth,
to_integral(flags));
76 constexpr Connection::Connection(
Connection&& conn) noexcept : underlying(conn.underlying) { conn.underlying =
nullptr; }
80 underlying = conn.underlying;
81 conn.underlying =
nullptr;
87 virConnectClose(underlying);
91 if (virConnectRef(underlying))
92 throw std::runtime_error{
"virConnectRef"};
96 virConnectRegisterCloseCallback(underlying, reinterpret_cast<virConnectCloseFunc>(cb), data.release(), &data.get_deleter());
100 if (virConnectRegisterCloseCallback(underlying, reinterpret_cast<virConnectCloseFunc>(cb),
nullptr,
nullptr))
101 throw std::runtime_error{
"virConnectRegisterCloseCallback"};
105 if (virConnectUnregisterCloseCallback(underlying, reinterpret_cast<virConnectCloseFunc>(cb)))
106 throw std::runtime_error{
"unregisterCloseCallback"};
110 if (virConnectUnregisterCloseCallback(underlying, reinterpret_cast<virConnectCloseFunc>(cb)))
111 throw std::runtime_error{
"unregisterCloseCallback"};
115 if (virConnectSetKeepAlive(underlying, interval, count))
116 throw std::runtime_error{
"virConnectSetKeepAlive"};
120 return virConnectFindStoragePoolSources(underlying, type, srcSpec, 0u);
122 #if LIBVIR_VERSION_NUMBER >= 5002000 123 [[nodiscard]]
inline gsl::zstring<> Connection::getStoragePoolCapabilities()
const noexcept {
124 return virConnectGetStoragePoolCapabilities(underlying, 0u);
134 if (virConnectGetLibVersion(underlying, &ret))
135 throw std::runtime_error{
"virConnectGetLibVersion"};
158 std::vector<int> ret{};
159 const auto max_size = numOfDomains();
160 ret.resize(gsl::narrow_cast<unsigned>(max_size));
161 const auto res = virConnectListDomains(underlying, ret.data(), max_size);
163 throw std::runtime_error{
"virConnectListDomains"};
164 ret.resize(gsl::narrow_cast<unsigned>(res));
168 template <>
auto Connection::listDefinedDomains<gsl::zstring<>>()
const {
169 std::vector<gsl::owner<gsl::zstring<>>> buf{};
170 const auto max_size = numOfDomains();
171 buf.resize(gsl::narrow_cast<unsigned>(max_size));
172 const auto res = virConnectListDefinedDomains(underlying, buf.data(), max_size);
174 throw std::runtime_error{
"virConnectListDefinedDomains"};
175 constexpr
auto d = +[](gsl::owner<char*> p) { std::free(p); };
176 using D = decltype(d);
177 std::vector<std::unique_ptr<char[], D>> ret;
178 ret.reserve(gsl::narrow_cast<unsigned>(res));
179 std::transform(std::make_move_iterator(buf.begin()), std::make_move_iterator(buf.end()), std::back_inserter(ret),
180 [=](gsl::owner<gsl::zstring<>> str) {
return std::unique_ptr<char[], D>(str, d); });
184 template <>
auto Connection::listDefinedDomains<std::string>()
const {
185 std::vector<gsl::owner<gsl::zstring<>>> buf{};
186 const auto max_size = numOfDefinedDomains();
187 buf.resize(gsl::narrow_cast<unsigned>(max_size));
188 const auto res = virConnectListDefinedDomains(underlying, buf.data(), max_size);
190 throw std::runtime_error{
"virConnectListDefinedDomains"};
191 std::vector<std::string> ret;
192 ret.reserve(gsl::narrow_cast<unsigned>(res));
193 std::transform(std::make_move_iterator(buf.begin()), std::make_move_iterator(buf.end()), std::back_inserter(ret),
194 [](gsl::owner<gsl::zstring<>> str) {
195 const std::string ret{str};
203 std::vector<Domain> ret;
204 gsl::owner<virDomainPtr*> domains;
206 const auto res = virConnectListAllDomains(underlying, &domains,
to_integral(flags));
208 throw std::runtime_error{
"virConnectListAllDomains"};
210 ret.reserve(static_cast<unsigned>(res));
211 std::for_each(domains, domains + res, [&](virDomainPtr d) { ret.emplace_back(d); });
218 virDomainStatsRecordPtr* ptr;
221 throw std::runtime_error{
"virConnectGetAllDomainStats"};
223 std::vector<Domain::StatsRecord> recs;
224 recs.reserve(static_cast<std::size_t>(res));
225 std::transform(*ptr, *ptr + res, std::back_inserter(recs), [](
const virDomainStatsRecord& rec) {
return Domain::StatsRecord{rec}; });
226 virDomainStatsRecordListFree(ptr);
236 return Domain{virDomainLookupByUUID(underlying, uuid)};
240 return Domain{virDomainLookupByUUIDString(underlying, uuid_str)};
245 return virDomainSaveImageDefineXML(underlying, file, dxml,
to_integral(flags)) >= 0;
253 return Network{virNetworkLookupByUUID(underlying, uuid)};
259 return Network{virNetworkLookupByUUIDString(underlying, uuid_str)};
265 virNodeGetInfo(underlying, &ret);
272 std::vector<unsigned long long> ret{};
273 ret.resize(nodeGetInfo().nodes);
274 const int res = virNodeGetCellsFreeMemory(underlying, ret.data(), 0, gsl::narrow_cast<
int>(ret.size()));
276 throw std::runtime_error{
"virNodeGetCellsFreeMemory"};
281 return virt::meta::light::wrap_opram_owning_set_destroyable_arr<Network>(underlying, virConnectListAllNetworks,
to_integral(flags));
293 return virt::meta::heavy::wrap_oparm_owning_fill_freeable_arr<std::string>(underlying, virConnectNumOfDefinedNetworks,
294 virConnectListDefinedNetworks);
298 return virt::meta::heavy::wrap_oparm_owning_fill_freeable_arr<std::string>(underlying, virConnectNumOfNetworks, virConnectListNetworks);
302 return virt::meta::heavy::wrap_opram_owning_set_destroyable_arr<Network>(underlying, virConnectListAllNetworks,
to_integral(flags));
306 return meta::light::wrap_opram_owning_set_destroyable_arr<NodeDevice, UniqueNullTerminatedSpan>(underlying, virConnectListAllNodeDevices,
310 return meta::heavy::wrap_opram_owning_set_destroyable_arr<NodeDevice>(underlying, virConnectListAllNodeDevices,
to_integral(flags));
315 underlying, [=](decltype(underlying) u) {
return virNodeNumOfDevices(u, capability.data(), 0); },
316 [=](decltype(underlying) u,
char** ptr,
int max) {
return virNodeListDevices(u, capability.data(), ptr, max, 0); });
320 return meta::heavy::wrap_oparm_owning_fill_freeable_arr<std::string>(
321 underlying, [=](decltype(underlying) u) {
return virNodeNumOfDevices(u, capability.data(), 0); },
322 [=](decltype(underlying) u,
char** ptr,
int max) {
return virNodeListDevices(u, capability.data(), ptr, max, 0); });
328 return NodeDevice{virNodeDeviceLookupSCSIHostByWWN(underlying, wwnn, wwpn, 0)};
332 return meta::light::wrap_opram_owning_set_destroyable_arr<StoragePool>(underlying, virConnectListAllStoragePools,
to_integral(flags));
335 return meta::heavy::wrap_opram_owning_set_destroyable_arr<StoragePool>(underlying, virConnectListAllStoragePools,
to_integral(flags));
347 return StoragePool{virStoragePoolLookupByName(underlying, name)};
350 return StoragePool{virStoragePoolLookupByTargetPath(underlying, path)};
353 return StoragePool{virStoragePoolLookupByUUID(underlying, uuid)};
356 return StoragePool{virStoragePoolLookupByUUIDString(underlying, uuidstr)};
360 return StorageVol{virStorageVolLookupByKey(underlying, key)};
363 return StorageVol{virStorageVolLookupByPath(underlying, path)};
366 inline bool Connection::restore(gsl::czstring<> from) noexcept {
return virDomainRestore(underlying, from) == 0; }
369 return virDomainRestoreFlags(underlying, from, dxml,
to_integral(flags)) == 0;
Flags
Definition: Decls.hpp:40
auto listDefinedStoragePoolsNames() const noexcept
Definition: Connection.hpp:337
gsl::zstring findStoragePoolSources(gsl::czstring<> type, gsl::czstring<>) const noexcept
Definition: Connection.hpp:119
bool isAlive() const noexcept
Definition: Connection.hpp:147
std::vector< std::string > extractDevicesNames(const std::string &capability) const
Definition: Connection.hpp:319
constexpr auto to_integral(E e) noexcept
Definition: Base.hpp:32
unsigned long getVersion()
Definition: Connection.hpp:22
Connection & operator=(const Connection &conn) noexcept=delete
auto listAllDomains(enums::connection::list::domains::Flag flags) const -> std::vector< Domain >
Definition: Connection.hpp:202
auto listAllDevices(enums::connection::list::devices::Flags flags=enums::connection::list::devices::Flags::DEFAULT) const noexcept
Definition: Connection.hpp:305
Flags
Definition: Decls.hpp:85
StorageVol storageVolLookupByKey(gsl::czstring<> key) const noexcept
Definition: Connection.hpp:359
unsigned long long nodeGetFreeMemory() const
Definition: Connection.hpp:269
Domain domainLookupByName(gsl::czstring<>) const noexcept
Definition: Connection.hpp:232
Definition: Domain.hpp:463
Network networkLookupByUUID(gsl::basic_zstring< const unsigned char > uuid) const noexcept
Definition: Connection.hpp:252
Definition: ListStoragePoolsFlag.hpp:8
NodeDevice deviceLookupSCSIHostByWWN(gsl::czstring<> wwnn, gsl::czstring<> wwpn) const noexcept
Definition: Connection.hpp:327
Network networkLookupByUUIDString(gsl::czstring<> uuid_str) const noexcept
Definition: Connection.hpp:258
auto listDefinedNetworksNames() const noexcept
Definition: Connection.hpp:284
Definition: Connection.hpp:47
auto listStoragePoolsNames() const noexcept
Definition: Connection.hpp:340
T passive
Definition: utility.hpp:48
std::vector< unsigned long long > nodeGetCellsFreeMemory() const
Definition: Connection.hpp:271
Definition: Connection.hpp:21
Flags
Definition: Decls.hpp:47
gsl::czstring getType() const noexcept
Definition: Connection.hpp:143
void unregisterCloseCallback(void(*cb)(Data &))
Definition: Connection.hpp:104
void setKeepAlive(int interval, unsigned count)
Definition: Connection.hpp:114
bool isEncrypted() const noexcept
Definition: Connection.hpp:149
int numOfDomains() const noexcept
Definition: Connection.hpp:153
int numOfDefinedStoragePools() const noexcept
Definition: Connection.hpp:343
virNodeInfo nodeGetInfo() const
Definition: Connection.hpp:263
gsl::zstring getHostname() const noexcept
Definition: Connection.hpp:130
Network networkLookupByName(gsl::czstring<> name) const noexcept
Definition: Connection.hpp:256
auto getAllDomainStats(enums::domain::stats::Types stats, enums::connection::get_all_domains::stats::Flags flags)
Definition: Connection.hpp:217
~Connection()
Definition: Connection.hpp:85
StoragePool storagePoolLookupByUUID(gsl::basic_zstring< const unsigned char > uuid) const noexcept
Definition: Connection.hpp:352
std::vector< std::string > extractDefinedNetworksNames() const
Definition: Connection.hpp:292
int numOfStoragePools() const noexcept
Definition: Connection.hpp:344
bool isSecure() const noexcept
Definition: Connection.hpp:151
StoragePool storagePoolLookupByName(gsl::czstring<> name) const noexcept
Definition: Connection.hpp:346
Definition: NodeDevice.hpp:33
std::vector< NodeDevice > extractAllDevices(enums::connection::list::devices::Flags flags=enums::connection::list::devices::Flags::DEFAULT) const
Definition: Connection.hpp:309
StorageVol storageVolLookupByPath(gsl::czstring<> path) const noexcept
Definition: Connection.hpp:362
NodeDevice deviceLookupByName(gsl::czstring<> name) const noexcept
Definition: Connection.hpp:325
Domain domainLookupByUUID(gsl::basic_zstring< const unsigned char >) const noexcept
Definition: Connection.hpp:235
int getMaxVcpus(gsl::czstring<> type) const noexcept
Definition: Connection.hpp:139
Definition: Connection.hpp:33
Definition: utility.hpp:122
Definition: AdminConnection.hpp:11
Definition: Network.hpp:14
bool domainSaveImageDefineXML(gsl::czstring<> file, gsl::czstring<> dxml, enums::domain::SaveRestoreFlag flags) noexcept
Definition: Connection.hpp:244
Domain domainLookupByID(int) const noexcept
Definition: Connection.hpp:230
ConnectCredential(const virConnectCredential &in) noexcept
Definition: Connection.hpp:29
unsigned long getLibVersion() const
Definition: Connection.hpp:132
Definition: StoragePool.hpp:11
Definition: StorageVol.hpp:10
void ref()
Definition: Connection.hpp:90
std::vector< StoragePool > extractAllStoragePools(enums::connection::list::storage_pool::Flag) const
Definition: Connection.hpp:334
Domain domainLookupByUUIDString(gsl::czstring<>) const noexcept
Definition: Connection.hpp:239
Definition: SaveRestoreFlag.hpp:9
std::vector< enums::connection::CredentialType > cred_types
Definition: Connection.hpp:43
ConnectionAuth(Container c, Callback callback)
Definition: Connection.hpp:34
std::vector< std::string > extractNetworksNames() const
Definition: Connection.hpp:297
auto listNetworksNames() const noexcept
Definition: Connection.hpp:288
int numOfDefinedDomains() const noexcept
Definition: Connection.hpp:155
passive< gsl::zstring<> > getSysInfo(unsigned flags) const noexcept
Definition: Connection.hpp:141
passive< gsl::zstring<> > getURI() const noexcept
Definition: Connection.hpp:145
Flag
Definition: Decls.hpp:68
SaveImageXMLFlag
Definition: Decls.hpp:55
Definition: Domain.hpp:96
auto listAllNetworks(enums::connection::list::networks::Flag) const noexcept
Definition: Connection.hpp:280
void registerCloseCallback(void(*cb)(Data &), std::unique_ptr< Data > data=nullptr)
Definition: Connection.hpp:95
UniqueZstring domainSaveImageGetXMLDesc(gsl::czstring<> file, enums::domain::SaveImageXMLFlag flags) const noexcept
Definition: Connection.hpp:248
gsl::zstring getCapabilities() const noexcept
Definition: Connection.hpp:128
auto listDevicesNames(const std::string &capability) const noexcept
Definition: Connection.hpp:313
Definition: ListDomainFlag.hpp:8
auto listAllStoragePools(enums::connection::list::storage_pool::Flag flags) const noexcept
Definition: Connection.hpp:331
Types
Definition: Decls.hpp:11
StoragePool storagePoolLookupByUUIDString(gsl::czstring<> uuidstr) const noexcept
Definition: Connection.hpp:355
auto listDomains() const -> std::vector< int >
Definition: Connection.hpp:157
bool restore(gsl::czstring<> from) noexcept
Definition: Connection.hpp:366
std::vector< Network > extractAllNetworks(enums::connection::list::networks::Flag) const
Definition: Connection.hpp:301
Callback callback
Definition: Connection.hpp:44
CredentialType
Definition: Decls.hpp:27
StoragePool storagePoolLookupByTargetPath(gsl::czstring<> path) const noexcept
Definition: Connection.hpp:349