1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*
* Sahid Orentino Ferdjaoui <sahid.ferdjaoui@redhat.com>
*/
use std::convert::TryInto;
use std::ffi::CString;
use std::{mem, ptr, str};
use crate::domain::{Domain, DomainStatsRecord};
use crate::error::Error;
use crate::interface::Interface;
use crate::network::Network;
use crate::nodedev::NodeDevice;
use crate::nwfilter::NWFilter;
use crate::secret::Secret;
use crate::storage_pool::StoragePool;
use crate::util::c_ulong_to_u64;
extern "C" fn connect_callback(
ccreds: sys::virConnectCredentialPtr,
ncred: libc::c_uint,
cbdata: *mut libc::c_void,
) -> libc::c_int {
let callback: ConnectAuthCallback = unsafe {
// Safe because connect_callback is private and only used by
// Connect::open_auth(). In open_auth() we transmute the
// callback allocate in *void.
mem::transmute(cbdata)
};
let mut rcreds: Vec<ConnectCredential> = Vec::new();
for i in 0..ncred as isize {
// Safe because ccreds is allocated.
let c = unsafe { ConnectCredential::from_ptr(ccreds.offset(i)) };
rcreds.push(c);
}
callback(&mut rcreds);
for i in 0..ncred as isize {
if rcreds[i as usize].result.is_some() {
if let Some(ref result) = rcreds[i as usize].result {
unsafe {
// libvirt will call free() on 'result', so we must provide
// memory allocated by the C malloc impl
let bytes = result.as_bytes();
let buffer = ::libc::malloc(bytes.len() + 1);
::std::ptr::copy(bytes.as_ptr().cast(), buffer, bytes.len());
::std::ptr::write(buffer.add(bytes.len()) as *mut u8, 0u8);
// Safe because ccreds is allocated and the result
// is comming from Rust calls.
(*ccreds.offset(i)).resultlen = result.len() as libc::c_uint;
(*ccreds.offset(i)).result = buffer as *mut libc::c_char;
}
}
}
}
0
}
#[derive(Clone, Debug)]
pub struct NodeInfo {
/// Indicating the CPU model.
pub model: String,
/// Memory size in kilobytes.
pub memory: u64,
/// The number of active CPUs.
pub cpus: u32,
/// expected CPU frequency, 0 if not known or on unusual
/// architectures.
pub mhz: u32,
/// The number of NUMA cell, 1 for unusual NUMA topologies or
/// uniform memory access; check capabilities XML for the actual
/// NUMA topology
pub nodes: u32,
/// Number of CPU sockets per node if nodes > 1, 1 in case of
/// unusual NUMA topology.
pub sockets: u32,
/// Number of cores per socket, total number of processors in case
/// of unusual NUMA topology
pub cores: u32,
/// Number of threads per core, 1 in case of unusual numa topology
pub threads: u32,
}
// TODO(sahid): should support closure
pub type ConnectAuthCallback = fn(creds: &mut Vec<ConnectCredential>);
#[derive(Clone, Debug)]
pub struct ConnectCredential {
/// One of `ConnectCredentialType` constants
pub typed: i32,
/// Prompt to show to user.
pub prompt: String,
/// Additional challenge to show.
pub challenge: String,
/// Optional default result.
pub def_result: String,
/// Result to be filled with user response (or def_result).
pub result: Option<String>,
}
impl ConnectCredential {
/// # Safety
///
/// The caller must ensure that the pointer is valid.
pub unsafe fn from_ptr(cred: sys::virConnectCredentialPtr) -> ConnectCredential {
let mut default: String = String::from("");
if !(*cred).defresult.is_null() {
default = c_chars_to_string!((*cred).defresult, nofree);
}
ConnectCredential {
typed: (*cred).type_,
prompt: c_chars_to_string!((*cred).prompt, nofree),
challenge: c_chars_to_string!((*cred).challenge, nofree),
def_result: default,
result: None,
}
}
}
pub struct ConnectAuth {
/// List of supported `ConnectCredentialType` values.
creds: Vec<sys::virConnectCredentialType>,
/// Callback used to collect credentials.
callback: ConnectAuthCallback,
}
impl ConnectAuth {
pub fn new(
creds: Vec<sys::virConnectCredentialType>,
callback: ConnectAuthCallback,
) -> ConnectAuth {
ConnectAuth { creds, callback }
}
}
/// Provides APIs for the management of hosts.
///
/// See <https://libvirt.org/html/libvirt-libvirt-host.html>
#[derive(Debug)]
pub struct Connect {
ptr: Option<sys::virConnectPtr>,
}
impl Clone for Connect {
/// Creates a copy of connection.
///
/// Increments the internal reference counter on the given
/// connection. For each call to this method, there shall be a
/// correspodning call to [`close()`].
///
/// [`close()`]: Connect::close
///
/// # Examples
///
/// ````
/// use virt::connect::Connect;
///
/// let mut conn1 = Connect::open(Some("test:///default")).unwrap();
/// let mut conn2 = conn1.clone();
/// let mut conn3 = conn2.clone();
/// assert_eq!(Ok(1), conn1.close(), "conn1.close(), expected 1");
/// assert_eq!(Ok(1), conn2.close(), "conn2.close(), expected 1");
/// assert_eq!(Ok(0), conn3.close(), "conn3.close(), expected 0");
/// ````
fn clone(&self) -> Self {
self.add_ref().unwrap()
}
}
unsafe impl Send for Connect {}
unsafe impl Sync for Connect {}
impl Connect {
pub fn as_ptr(&self) -> sys::virConnectPtr {
self.ptr.unwrap()
}
/// # Safety
///
/// The caller must ensure that the pointer is valid.
pub unsafe fn from_ptr(ptr: sys::virConnectPtr) -> Connect {
Connect { ptr: Some(ptr) }
}
fn add_ref(&self) -> Result<Connect, Error> {
unsafe {
if sys::virConnectRef(self.as_ptr()) == -1 {
return Err(Error::last_error());
}
}
Ok(unsafe { Connect::from_ptr(self.as_ptr()) })
}
pub fn get_version() -> Result<u32, Error> {
let mut ver: libc::c_ulong = 0;
let ret = unsafe { sys::virGetVersion(&mut ver, ptr::null(), ptr::null_mut()) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(ver as u32)
}
/// This function should be called first to get a connection to
/// the Hypervisor and xen store.
///
/// If `uri` is "", if the LIBVIRT_DEFAULT_URI environment
/// variable is set, then it will be used. Otherwise if the client
/// configuration file has the "uri_default" parameter set, then
/// it will be used. Finally probing will be done to determine a
/// suitable default driver to activate. This involves trying each
/// hypervisor in turn until one successfully opens.
///
/// If connecting to an unprivileged hypervisor driver which
/// requires the libvirtd daemon to be active, it will
/// automatically be launched if not already running. This can be
/// prevented by setting the environment variable
/// LIBVIRT_AUTOSTART=0
///
/// URIs are documented at <https://libvirt.org/uri.html>
///
/// [`close()`] should be used to release the resources after the
/// connection is no longer needed.
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let mut conn = match Connect::open(Some("test:///default")) {
/// Ok(c) => c,
/// Err(e) => panic!("Unable to connect: {e}"),
/// };
///
/// conn.close();
/// ```
///
/// [`close()`]: Connect::close
pub fn open(uri: Option<&str>) -> Result<Connect, Error> {
let uri_buf = some_string_to_cstring!(uri);
let c = unsafe { sys::virConnectOpen(some_cstring_to_c_chars!(uri_buf)) };
if c.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Connect::from_ptr(c) })
}
/// This function should be called first to get a restricted
/// connection to the library functionalities. The set of APIs
/// usable are then restricted on the available methods to control
/// the domains.
///
/// See [`open()`] for notes about environment variables which can have
/// an effect on opening drivers and freeing the connection
/// resources.
///
/// [`open()`]: Connect::open
pub fn open_read_only(uri: Option<&str>) -> Result<Connect, Error> {
let uri_buf = some_string_to_cstring!(uri);
let c = unsafe { sys::virConnectOpenReadOnly(some_cstring_to_c_chars!(uri_buf)) };
if c.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Connect::from_ptr(c) })
}
pub fn open_auth(
uri: Option<&str>,
auth: &mut ConnectAuth,
flags: sys::virConnectFlags,
) -> Result<Connect, Error> {
let mut cauth =
// Safe because Rust forces to allocate all attributes of
// the struct ConnectAuth.
sys::virConnectAuth {
credtype: auth.creds.as_mut_ptr() as *mut libc::c_int,
ncredtype: auth.creds.len() as libc::c_uint,
cb: Some(connect_callback),
cbdata: auth.callback as *mut _,
};
let uri_buf = some_string_to_cstring!(uri);
let c = unsafe {
sys::virConnectOpenAuth(
some_cstring_to_c_chars!(uri_buf),
&mut cauth,
flags as libc::c_uint,
)
};
if c.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Connect::from_ptr(c) })
}
/// This function closes the connection to the hypervisor. This
/// should not be called if further interaction with the
/// hypervisor are needed especially if there is running domain
/// which need further monitoring by the application.
pub fn close(&mut self) -> Result<i32, Error> {
let ret = unsafe { sys::virConnectClose(self.as_ptr()) };
if ret == -1 {
return Err(Error::last_error());
}
// Because of add_ref() we must refrain from using the
// connection further.
self.ptr = None;
Ok(ret)
}
/// This returns a system hostname on which the hypervisor is
/// running (based on the result of the gethostname system call,
/// but possibly expanded to a fully-qualified domain name via
/// getaddrinfo). If we are connected to a remote system, then
/// this returns the hostname of the remote system.
pub fn get_hostname(&self) -> Result<String, Error> {
let n = unsafe { sys::virConnectGetHostname(self.as_ptr()) };
if n.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(n) })
}
pub fn get_capabilities(&self) -> Result<String, Error> {
let n = unsafe { sys::virConnectGetCapabilities(self.as_ptr()) };
if n.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(n) })
}
pub fn get_lib_version(&self) -> Result<u32, Error> {
let mut ver: libc::c_ulong = 0;
let ret = unsafe { sys::virConnectGetLibVersion(self.as_ptr(), &mut ver) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(ver as u32)
}
pub fn get_type(&self) -> Result<String, Error> {
let t = unsafe { sys::virConnectGetType(self.as_ptr()) };
if t.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(t, nofree) })
}
pub fn get_uri(&self) -> Result<String, Error> {
let t = unsafe { sys::virConnectGetURI(self.as_ptr()) };
if t.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(t) })
}
pub fn get_sys_info(&self, flags: u32) -> Result<String, Error> {
let sys = unsafe { sys::virConnectGetSysinfo(self.as_ptr(), flags as libc::c_uint) };
if sys.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(sys) })
}
pub fn get_max_vcpus(&self, domtype: Option<&str>) -> Result<u32, Error> {
let type_buf = some_string_to_cstring!(domtype);
let max = unsafe {
sys::virConnectGetMaxVcpus(self.as_ptr(), some_cstring_to_c_chars!(type_buf))
};
if max == -1 {
return Err(Error::last_error());
}
Ok(max as u32)
}
pub fn get_cpu_models_names(&self, arch: &str, flags: u32) -> Result<Vec<String>, Error> {
let mut names: *mut *mut libc::c_char = ptr::null_mut();
let arch_buf = CString::new(arch).unwrap();
let size = unsafe {
sys::virConnectGetCPUModelNames(
self.as_ptr(),
arch_buf.as_ptr(),
&mut names,
flags as libc::c_uint,
)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { c_chars_to_string!(*names.offset(x)) });
}
unsafe { libc::free(names as *mut libc::c_void) };
Ok(array)
}
pub fn is_alive(&self) -> Result<bool, Error> {
let t = unsafe { sys::virConnectIsAlive(self.as_ptr()) };
if t == -1 {
return Err(Error::last_error());
}
Ok(t == 1)
}
pub fn is_encrypted(&self) -> Result<bool, Error> {
let t = unsafe { sys::virConnectIsEncrypted(self.as_ptr()) };
if t == -1 {
return Err(Error::last_error());
}
Ok(t == 1)
}
pub fn is_secure(&self) -> Result<bool, Error> {
let t = unsafe { sys::virConnectIsSecure(self.as_ptr()) };
if t == -1 {
return Err(Error::last_error());
}
Ok(t == 1)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let domains = conn.list_domains().unwrap();
/// assert_eq!(domains.len(), 1);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_domains(&self) -> Result<Vec<u32>, Error> {
let mut ids: [libc::c_int; 512] = [0; 512];
let size = unsafe { sys::virConnectListDomains(self.as_ptr(), ids.as_mut_ptr(), 512) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<u32> = Vec::new();
for x in 0..size as usize {
array.push(ids[x] as u32);
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let ifaces = conn.list_interfaces().unwrap();
/// assert_eq!(ifaces.len(), 1);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_interfaces(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size =
unsafe { sys::virConnectListInterfaces(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let networks = conn.list_networks().unwrap();
/// assert_eq!(networks.len(), 1);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_networks(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size = unsafe { sys::virConnectListNetworks(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
#[allow(clippy::needless_range_loop)]
pub fn list_nw_filters(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size = unsafe { sys::virConnectListNWFilters(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
#[allow(clippy::needless_range_loop)]
pub fn list_secrets(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size = unsafe { sys::virConnectListSecrets(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let pools = conn.list_storage_pools().unwrap();
/// assert_eq!(pools.len(), 1);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_storage_pools(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size =
unsafe { sys::virConnectListStoragePools(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
pub fn list_all_domains(
&self,
flags: sys::virConnectListAllDomainsFlags,
) -> Result<Vec<Domain>, Error> {
let mut domains: *mut sys::virDomainPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllDomains(self.as_ptr(), &mut domains, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<Domain> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { Domain::from_ptr(*domains.offset(x)) });
}
unsafe { libc::free(domains as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_networks(
&self,
flags: sys::virConnectListAllNetworksFlags,
) -> Result<Vec<Network>, Error> {
let mut networks: *mut sys::virNetworkPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllNetworks(self.as_ptr(), &mut networks, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<Network> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { Network::from_ptr(*networks.offset(x)) });
}
unsafe { libc::free(networks as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_interfaces(
&self,
flags: sys::virConnectListAllInterfacesFlags,
) -> Result<Vec<Interface>, Error> {
let mut interfaces: *mut sys::virInterfacePtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllInterfaces(self.as_ptr(), &mut interfaces, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<Interface> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { Interface::from_ptr(*interfaces.offset(x)) });
}
unsafe { libc::free(interfaces as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_node_devices(
&self,
flags: sys::virConnectListAllNodeDeviceFlags,
) -> Result<Vec<NodeDevice>, Error> {
let mut nodedevs: *mut sys::virNodeDevicePtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllNodeDevices(self.as_ptr(), &mut nodedevs, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<NodeDevice> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { NodeDevice::from_ptr(*nodedevs.offset(x)) });
}
unsafe { libc::free(nodedevs as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_secrets(
&self,
flags: sys::virConnectListAllSecretsFlags,
) -> Result<Vec<Secret>, Error> {
let mut secrets: *mut sys::virSecretPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllSecrets(self.as_ptr(), &mut secrets, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<Secret> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { Secret::from_ptr(*secrets.offset(x)) });
}
unsafe { libc::free(secrets as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_storage_pools(
&self,
flags: sys::virConnectListAllStoragePoolsFlags,
) -> Result<Vec<StoragePool>, Error> {
let mut storages: *mut sys::virStoragePoolPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllStoragePools(self.as_ptr(), &mut storages, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<StoragePool> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { StoragePool::from_ptr(*storages.offset(x)) });
}
unsafe { libc::free(storages as *mut libc::c_void) };
Ok(array)
}
pub fn list_all_nw_filters(&self, flags: u32) -> Result<Vec<NWFilter>, Error> {
let mut filters: *mut sys::virNWFilterPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectListAllNWFilters(self.as_ptr(), &mut filters, flags as libc::c_uint)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<NWFilter> = Vec::new();
for x in 0..size as isize {
array.push(unsafe { NWFilter::from_ptr(*filters.offset(x)) });
}
unsafe { libc::free(filters as *mut libc::c_void) };
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let domains = conn.list_defined_domains().unwrap();
/// assert_eq!(domains.len(), 0);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_defined_domains(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size =
unsafe { sys::virConnectListDefinedDomains(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let ifaces = conn.list_defined_interfaces().unwrap();
/// assert_eq!(ifaces.len(), 0);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_defined_interfaces(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size = unsafe {
sys::virConnectListDefinedInterfaces(self.as_ptr(), names.as_mut_ptr(), 1024)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let pools = conn.list_defined_storage_pools().unwrap();
/// assert_eq!(pools.len(), 0);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_defined_storage_pools(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size = unsafe {
sys::virConnectListDefinedStoragePools(self.as_ptr(), names.as_mut_ptr(), 1024)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
///
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let networks = conn.list_defined_networks().unwrap();
/// assert_eq!(networks.len(), 0);
/// ```
#[allow(clippy::needless_range_loop)]
pub fn list_defined_networks(&self) -> Result<Vec<String>, Error> {
let mut names: [*mut libc::c_char; 1024] = [ptr::null_mut(); 1024];
let size =
unsafe { sys::virConnectListDefinedNetworks(self.as_ptr(), names.as_mut_ptr(), 1024) };
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<String> = Vec::new();
for x in 0..size as usize {
array.push(unsafe { c_chars_to_string!(names[x]) });
}
Ok(array)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_domains = conn.num_of_domains().unwrap();
/// assert_eq!(num_domains, 1);
/// ```
pub fn num_of_domains(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfDomains(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_ifaces = conn.num_of_interfaces().unwrap();
/// assert_eq!(num_ifaces, 1);
/// ```
pub fn num_of_interfaces(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfInterfaces(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_networks = conn.num_of_networks().unwrap();
/// assert_eq!(num_networks, 1);
/// ```
pub fn num_of_networks(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfNetworks(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_pools = conn.num_of_storage_pools().unwrap();
/// assert_eq!(num_pools, 1);
/// ```
pub fn num_of_storage_pools(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfStoragePools(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
pub fn num_of_nw_filters(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfNWFilters(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
pub fn num_of_secrets(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfSecrets(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_domains = conn.num_of_defined_domains().unwrap();
/// assert_eq!(num_domains, 0);
/// ```
pub fn num_of_defined_domains(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfDefinedDomains(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_ifaces = conn.num_of_defined_interfaces().unwrap();
/// assert_eq!(num_ifaces, 0);
/// ```
pub fn num_of_defined_interfaces(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfDefinedInterfaces(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_networks = conn.num_of_defined_networks().unwrap();
/// assert_eq!(num_networks, 0);
/// ```
pub fn num_of_defined_networks(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfDefinedNetworks(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let num_pools = conn.num_of_defined_storage_pools().unwrap();
/// assert_eq!(num_pools, 0);
/// ```
pub fn num_of_defined_storage_pools(&self) -> Result<u32, Error> {
let num = unsafe { sys::virConnectNumOfDefinedStoragePools(self.as_ptr()) };
if num == -1 {
return Err(Error::last_error());
}
Ok(num as u32)
}
/// Connect.close should be used to release the resources after the
/// connection is no longer needed.
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let hyp_version = conn.get_hyp_version().unwrap();
/// assert_eq!(hyp_version, 2);
/// ```
pub fn get_hyp_version(&self) -> Result<u32, Error> {
let mut hyver: libc::c_ulong = 0;
let ret = unsafe { sys::virConnectGetVersion(self.as_ptr(), &mut hyver) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(hyver as u32)
}
pub fn compare_cpu(
&self,
xml: &str,
flags: sys::virConnectCompareCPUFlags,
) -> Result<sys::virCPUCompareResult, Error> {
let xml_buf = CString::new(xml).unwrap();
let res = unsafe {
sys::virConnectCompareCPU(self.as_ptr(), xml_buf.as_ptr(), flags as libc::c_uint)
};
if res == sys::VIR_CPU_COMPARE_ERROR {
return Err(Error::last_error());
}
Ok(res as sys::virCPUCompareResult)
}
pub fn get_free_memory(&self) -> Result<u64, Error> {
let res = unsafe { sys::virNodeGetFreeMemory(self.as_ptr()) };
if res == 0 {
return Err(Error::last_error());
}
Ok(res)
}
pub fn get_node_info(&self) -> Result<NodeInfo, Error> {
let mut pinfo = mem::MaybeUninit::uninit();
let res = unsafe { sys::virNodeGetInfo(self.as_ptr(), pinfo.as_mut_ptr()) };
if res == -1 {
return Err(Error::last_error());
}
let pinfo = unsafe { pinfo.assume_init() };
Ok(NodeInfo {
model: unsafe { c_chars_to_string!(pinfo.model.as_ptr(), nofree) },
memory: c_ulong_to_u64(pinfo.memory),
cpus: pinfo.cpus,
mhz: pinfo.mhz,
nodes: pinfo.nodes,
sockets: pinfo.sockets,
cores: pinfo.cores,
threads: pinfo.threads,
})
}
pub fn set_keep_alive(&self, interval: i32, count: u32) -> Result<i32, Error> {
let ret = unsafe {
sys::virConnectSetKeepAlive(
self.as_ptr(),
interval as libc::c_int,
count as libc::c_uint,
)
};
if ret == -1 {
return Err(Error::last_error());
}
Ok(ret)
}
pub fn domain_xml_from_native(
&self,
nformat: &str,
nconfig: &str,
flags: u32,
) -> Result<String, Error> {
let nformat_buf = CString::new(nformat).unwrap();
let nconfig_buf = CString::new(nconfig).unwrap();
let ret = unsafe {
sys::virConnectDomainXMLFromNative(
self.as_ptr(),
nformat_buf.as_ptr(),
nconfig_buf.as_ptr(),
flags as libc::c_uint,
)
};
if ret.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(ret) })
}
pub fn domain_xml_to_native(
&self,
nformat: &str,
dxml: &str,
flags: u32,
) -> Result<String, Error> {
let nformat_buf = CString::new(nformat).unwrap();
let dxml_buf = CString::new(dxml).unwrap();
let ret = unsafe {
sys::virConnectDomainXMLToNative(
self.as_ptr(),
nformat_buf.as_ptr(),
dxml_buf.as_ptr(),
flags as libc::c_uint,
)
};
if ret.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(ret) })
}
pub fn get_domain_capabilities(
&self,
emulatorbin: Option<&str>,
arch: Option<&str>,
machine: Option<&str>,
virttype: Option<&str>,
flags: u32,
) -> Result<String, Error> {
let emulatorbin_buf = some_string_to_cstring!(emulatorbin);
let arch_buf = some_string_to_cstring!(arch);
let machine_buf = some_string_to_cstring!(machine);
let virttype_buf = some_string_to_cstring!(virttype);
let ret = unsafe {
sys::virConnectGetDomainCapabilities(
self.as_ptr(),
some_cstring_to_c_chars!(emulatorbin_buf),
some_cstring_to_c_chars!(arch_buf),
some_cstring_to_c_chars!(machine_buf),
some_cstring_to_c_chars!(virttype_buf),
flags as libc::c_uint,
)
};
if ret.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(ret) })
}
pub fn get_all_domain_stats(
&self,
stats: u32,
flags: u32,
) -> Result<Vec<DomainStatsRecord>, Error> {
let mut record: *mut sys::virDomainStatsRecordPtr = ptr::null_mut();
let size = unsafe {
sys::virConnectGetAllDomainStats(
self.as_ptr(),
stats as libc::c_uint,
&mut record,
flags as libc::c_uint,
)
};
if size == -1 {
return Err(Error::last_error());
}
let mut array: Vec<DomainStatsRecord> = Vec::new();
for x in 0..size as isize {
array.push(DomainStatsRecord {
ptr: unsafe { *record.offset(x) },
});
}
unsafe { libc::free(record as *mut libc::c_void) };
Ok(array)
}
pub fn baseline_cpu(
&self,
xmlcpus: &[&str],
flags: sys::virConnectBaselineCPUFlags,
) -> Result<String, Error> {
let mut xcpus: Vec<CString> = Vec::with_capacity(xmlcpus.len());
let mut xcpus_buf: Vec<*const libc::c_char> = Vec::with_capacity(xmlcpus.len());
for xml_cpu in xmlcpus {
let cstring = CString::new(*xml_cpu).unwrap();
xcpus_buf.push(cstring.as_ptr());
xcpus.push(cstring);
}
let ret = unsafe {
sys::virConnectBaselineCPU(
self.as_ptr(),
xcpus_buf.as_mut_ptr(),
xmlcpus.len() as libc::c_uint,
flags as libc::c_uint,
)
};
if ret.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(ret) })
}
pub fn find_storage_pool_sources(
&self,
kind: &str,
spec: Option<&str>,
flags: u32,
) -> Result<String, Error> {
let kind_buf = CString::new(kind).unwrap();
let spec_buf = some_string_to_cstring!(spec);
let n = unsafe {
sys::virConnectFindStoragePoolSources(
self.as_ptr(),
kind_buf.as_ptr(),
some_cstring_to_c_chars!(spec_buf),
flags as libc::c_uint,
)
};
if n.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(n) })
}
/// Connect.get_cells_free_memory should be used to get
/// information on free memory on individual NUMA nodes, starting
/// with `start_cell` and consecutive `max_cells`. Continuous NUMA
/// node IDs are expected. Returned values are in bytes.
pub fn get_cells_free_memory(
&self,
start_cell: i32,
max_cells: i32,
) -> Result<Vec<u64>, Error> {
let mut free_mems: Vec<libc::c_ulonglong> = Vec::with_capacity(max_cells as usize);
let size: i32 = unsafe {
sys::virNodeGetCellsFreeMemory(
self.as_ptr(),
free_mems.as_mut_ptr(),
start_cell as libc::c_int,
max_cells as libc::c_int,
)
};
if size < 0 {
return Err(Error::last_error());
}
unsafe { free_mems.set_len(size as usize) };
let mut array: Vec<u64> = Vec::new();
for x in free_mems.iter().take(size as usize) {
array.push(*x);
}
Ok(array)
}
/// Connect.get_free_pages should be used to get information on free memory
/// pages of size `pages` (in KiB) on individual `cell_count` NUMA nodes
/// starting with `start_cell. Returned is a vector of free page counts for
/// each NUMA node consecutively.
///
/// # Examples
///
/// ```
/// use virt::connect::Connect;
///
/// let conn = Connect::open(Some("test:///default")).unwrap();
/// let free_pages = conn.get_free_pages(&[4, 8, 2048, 1024 * 1024], 0, 2, 0);
/// ```
pub fn get_free_pages(
&self,
pages: &[u32],
start_cell: u32,
cell_count: u32,
flags: u32,
) -> Result<Vec<u64>, Error> {
let nentries = cell_count as usize * pages.len();
let mut counts = vec![0; nentries];
let size = unsafe {
sys::virNodeGetFreePages(
self.as_ptr(),
pages.len().try_into().unwrap(),
pages.as_ptr() as *mut _,
start_cell.try_into().unwrap(),
cell_count,
counts.as_mut_ptr(),
flags,
)
};
if size < 0 {
return Err(Error::last_error());
}
counts.truncate(size.try_into().unwrap());
Ok(counts)
}
}