-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontainer.rs
687 lines (587 loc) · 17.9 KB
/
container.rs
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
use super::ffi::{to_cstr, to_mut_cstr, to_nullable_cstr};
use std::ptr::{null, null_mut};
macro_rules! get {
( $container:ident . $prop:ident ) => {{
unsafe {
(*$container.inner).$prop
}
}};
( $container:ident . $prop:ident -> c_str ) => {{
let result = get!($container . $prop);
let str = if result.is_null() {
""
} else {
unsafe {
std::ffi::CStr::from_ptr(result)
.to_str()
.unwrap()
}
};
str.to_string()
}};
}
macro_rules! call {
( $container:ident . $method:ident( $( $arg:expr ),* ) -> [c_str] ) => {{
let result = unsafe {
(*$container.inner).$method.unwrap()($container.inner, $($arg,)*)
};
if result.is_null() {
Err($container.last_error())
} else {
let vec = super::ffi::vec_from_nta(result);
let vec = vec.iter()
.map(|e| {
let str = unsafe {
std::ffi::CStr::from_ptr(*e)
};
str.to_str()
.unwrap()
.to_string()
})
.collect();
Ok(vec)
}
}};
( $container:ident . $method:ident( $( $arg:expr ),* ) ) => {
unsafe {
(*$container.inner).$method.unwrap()($container.inner, $($arg,)*)
}
};
( $container:ident . $method:ident( $( $arg:expr ),* ) -> c_str ) => {{
let result = unsafe {
(*$container.inner).$method.unwrap()($container.inner, $($arg,)*)
};
let str = unsafe {
std::ffi::CStr::from_ptr(result)
};
str.to_str()
.unwrap()
.to_string()
}};
( $container:ident . $method:ident( $( $arg:expr ),* ) -> bool ) => {{
let result = unsafe {
(*$container.inner).$method.unwrap()($container.inner, $($arg,)*)
};
if result {
Ok(())
} else {
Err($container.last_error())
}
}};
( $container:ident . $method:ident( $( $arg:expr ),* ) -> int ) => {{
let result = unsafe {
(*$container.inner).$method.unwrap()($container.inner, $($arg,)*)
};
if result >= 0 {
Ok(())
} else {
Err($container.last_error())
}
}};
}
pub struct Container {
inner: *mut lxc_sys::lxc_container,
}
impl Container {
/**
* Create a new container.
*/
pub fn new(name: &str, config_path: Option<&std::path::Path>) -> std::result::Result<Self, String> {
let config_path = match config_path {
Some(path) => to_cstr(path.to_str().unwrap()),
None => null(),
};
let inner = unsafe {
lxc_sys::lxc_container_new(to_cstr(name), config_path)
};
Ok(Self { inner })
}
/**
* Add a reference to the specified container.
*/
pub fn get(&self) -> super::Result<()> {
let success = unsafe {
lxc_sys::lxc_container_get(self.inner)
};
if success == 0 {
Ok(())
} else {
Err(self.last_error())
}
}
/**
* Human-readable string representing last error.
*/
pub fn error_string(&self) -> String {
get!(self.error_string -> c_str)
}
/**
* Last error number.
*/
pub fn error_num(&self) -> i32 {
get!(self.error_num)
}
/**
* Whether container wishes to be daemonized.
*/
pub fn daemonize(&self) -> bool {
get!(self.daemonize)
}
/**
* Full path to configuration file.
*/
pub fn config_path(&self) -> String {
get!(self.config_path -> c_str)
}
/**
* Determine if `/var/lib/lxc/$name/config` exists.
*/
pub fn is_defined(&self) -> bool {
call!(self.is_defined())
}
/**
* Wait for container to reach a particular state.
*/
pub fn state(&self) -> String {
call!(self.state() -> c_str)
}
/**
* Determine if container is running.
*/
pub fn is_running(&self) -> bool {
call!(self.is_running())
}
/**
* Freeze running container.
*/
pub fn freeze(&self) -> super::Result<()> {
call!(self.freeze() -> bool)
}
/**
* Thaw a frozen container.
*/
pub fn unfreeze(&self) -> super::Result<()> {
call!(self.freeze() -> bool)
}
/**
* Determine process ID of the containers init process.
*/
pub fn init_pid(&self) -> i32 {
call!(self.init_pid())
}
/**
* Load the specified configuration for the container.
*/
pub fn load_config(&self, alt_file: &str) -> super::Result<()> {
call!(self.load_config(to_cstr(alt_file)) -> bool)
}
/**
* Start the container.
*/
pub fn start(&self, use_init: bool, argv: &[&str]) -> super::Result<()> {
let argv_ptr = if argv.is_empty() {
null_mut()
} else {
let mut argv: Vec<*mut i8> = argv.iter().map(|e| to_mut_cstr(*e)).collect();
argv.push(null_mut());
argv.as_mut_ptr()
};
call!(self.start(use_init as i32, argv_ptr) -> bool)
}
/**
* Stop the container.
*/
pub fn stop(&self) -> super::Result<()> {
call!(self.stop() -> bool)
}
/**
* Change whether the container wants to run disconnected from the terminal.
*/
pub fn want_daemonize(&self, state: bool) -> super::Result<()> {
call!(self.want_daemonize(state) -> bool)
}
/**
* Change whether the container wishes all file descriptors to be closed on
* startup.
*/
pub fn want_close_all_fds(&self, state: bool) -> super::Result<()> {
call!(self.want_close_all_fds(state) -> bool)
}
/**
* Return current config file name.
*/
pub fn config_file_name(&self) -> String {
call!(self.config_file_name() -> c_str)
}
/**
* Wait for container to reach a particular state.
*/
pub fn wait(&self, state: &str, timeout: i32) -> super::Result<()> {
call!(self.wait(to_cstr(state), timeout) -> bool)
}
/**
* Set a key/value configuration option.
*/
pub fn set_config_item(&self, key: &str, value: &str) -> super::Result<()> {
call!(self.set_config_item(to_cstr(key), to_cstr(value)) -> bool)
}
/**
* Delete the container.
*/
pub fn destroy(&self) -> super::Result<()> {
call!(self.destroy() -> bool)
}
/**
* Save configuaration to a file.
*/
pub fn save_config(&self, alt_file: &str) -> super::Result<()> {
call!(self.save_config(to_cstr(alt_file)) -> bool)
}
/**
* Create a container.
*/
pub fn create(
&self,
template: &str,
bdevtype: Option<&str>,
specs: Option<&mut lxc_sys::bdev_specs>,
flags: super::CreateFlags,
argv: &[&str],
) -> super::Result<()> {
let specs = match specs {
Some(specs) => &mut *specs,
None => null_mut(),
};
let mut argv: Vec<*mut i8> = argv.iter()
.map(|e| to_mut_cstr(*e))
.collect();
argv.push(null_mut());
call!(
self.create(
to_cstr(template),
match bdevtype {
Some(value) => to_cstr(value),
None => null(),
},
specs,
flags.bits(),
argv.as_ptr()
) -> bool
)
}
/**
* Rename a container.
*/
pub fn rename(&self, newname: &str) -> super::Result<()> {
call!(self.rename(to_cstr(newname)) -> bool)
}
/**
* Request the container reboot by sending it `SIGINT`.
*/
pub fn reboot(&self) -> super::Result<()> {
call!(self.reboot() -> bool)
}
/**
* Request the container shutdown by sending it `SIGPWR`.
*/
pub fn shutdown(&self, timeout: i32) -> super::Result<()> {
call!(self.shutdown(timeout) -> bool)
}
/**
* Completely clear the containers in-memory configuration.
*/
pub fn clear_config(&self) {
call!(self.clear_config())
}
/**
* Clear a configuration item.
*/
pub fn clear_config_item(&self, key: &str) -> super::Result<()> {
call!(self.clear_config_item(to_cstr(key)) -> bool)
}
/**
* Retrieve the value of a config item.
*/
pub fn get_config_item(&self, key: &str) -> String {
let size = call!(self.get_config_item(to_cstr(key), null_mut(), 0));
let mut retv = Vec::with_capacity(size as usize);
call!(self.get_config_item(to_cstr(key), retv.as_mut_ptr() as *mut i8, size));
String::from_utf8(retv).unwrap()
}
/**
* Retrieve the value of a config item from running container.
*/
pub fn get_running_config_item(&self, key: &str) -> String {
call!(self.get_running_config_item(to_cstr(key)) -> c_str)
}
/**
* Retrieve a list of config item keys given a key prefix.
*/
pub fn get_keys(&self, key: &str) -> String {
let size = call!(self.get_keys(to_cstr(key), null_mut(), 0));
let mut retv = Vec::with_capacity(size as usize);
call!(self.get_keys(to_cstr(key), retv.as_mut_ptr() as *mut i8, size));
String::from_utf8(retv).unwrap()
}
/**
* Obtain a list of network interfaces.
*/
pub fn get_interfaces(&self) -> Vec<String> {
call!(self.get_interfaces() -> [c_str]).unwrap_or_default()
}
/**
* Determine the list of container IP addresses.
*/
pub fn get_ips(&self, interfaces: &str, family: &str, scope: i32) -> Vec<String> {
call!(self.get_ips(to_cstr(interfaces), to_cstr(family), scope) -> [c_str])
.unwrap_or_default()
}
/**
* Retrieve the specified cgroup subsystem value for the container.
*/
pub fn get_cgroup_item(&self, subsys: &str) -> String {
let size = call!(self.get_cgroup_item(to_cstr(subsys), null_mut(), 0));
let mut retv = Vec::with_capacity(size as usize);
call!(self.get_cgroup_item(to_cstr(subsys), retv.as_mut_ptr() as *mut i8, size));
String::from_utf8(retv).unwrap()
}
/**
* Set the specified cgroup subsystem value for the container.
*/
pub fn set_cgroup_item(&self, subsys: &str, value: &str) -> super::Result<()> {
call!(self.set_cgroup_item(to_cstr(subsys), to_cstr(value)) -> bool)
}
/**
* Determine full path to the containers configuration file.
*
* Each container can have a custom configuration path. However by default
* it will be set to either the `LXCPATH` configure variable, or the
* `lxcpath` value in the `LXC_GLOBAL_CONF` configuration file
* (i.e. `/etc/lxc/lxc.conf`). The value for a specific container can be
* changed using `set_config_path`. There is no other way to specify this in
* general at the moment.
*/
pub fn get_config_path(&self) -> String {
call!(self.get_config_path() -> c_str)
}
/**
* Set the full path to the containers configuration file.
*/
pub fn set_config_path(&self, path: &str) -> super::Result<()> {
call!(self.set_config_path(to_cstr(path)) -> bool)
}
/**
* Copy a stopped container.
*/
pub fn clone(
&self,
newname: &str,
lxcpath: &str,
flags: i32,
bdevtype: &str,
bdevdata: &str,
newsize: u64,
hookargs: &[String],
) -> Self {
if !hookargs.is_empty() {
unimplemented!();
}
let inner = call!(self.clone(
to_cstr(newname),
to_cstr(lxcpath),
flags,
to_cstr(bdevtype),
to_cstr(bdevdata),
newsize,
null_mut()
));
Self { inner }
}
/**
* Allocate a console tty for the container.
*/
pub fn console_getfd(&self, ttynum: &mut i32, masterfd: &mut i32) -> super::Result<()> {
call!(self.console_getfd(ttynum, masterfd) -> int)
}
/**
* Allocate and run a console tty.
*/
pub fn console(
&self,
ttynum: i32,
stdinfd: i32,
stdoutfd: i32,
stderrfd: i32,
escape: i32,
) -> super::Result<()> {
call!(self.console(ttynum, stdinfd, stdoutfd, stderrfd, escape) -> int)
}
/**
* Create a sub-process attached to a container and run a function inside it.
*/
pub fn attach(
&self,
exec_function: super::attach::ExecFn,
exec_payload: &mut std::os::raw::c_void,
options: &mut super::attach::Options,
) -> super::Result<i32> {
let mut attached_process = 0;
let result =
call!(self.attach(exec_function, exec_payload, options, &mut attached_process) -> int);
match result {
Ok(()) => Ok(attached_process),
Err(err) => Err(err),
}
}
/**
* Run a program inside a container and wait for it to exit.
*/
pub fn attach_run_wait(
&self,
options: &mut super::attach::Options,
program: &str,
argv: &[&str],
) -> super::Result<i32> {
let mut argv: Vec<*const i8> = argv.iter().map(|e| to_cstr(*e)).collect();
argv.push(null());
let pid = call!(self.attach_run_wait(options, to_cstr(program), argv.as_ptr()));
if pid == -1 {
Err(self.last_error())
} else {
Ok(pid)
}
}
/**
* Create a container snapshot.
*
* Assuming default paths, snapshots will be created as
* `/var/lib/lxc/<c>/snaps/snap<n>` where `<c>` represents the container
* name and `<n>` represents the zero-based snapshot number.
*/
pub fn snapshot(&self, commentfile: &str) -> super::Result<()> {
call!(self.snapshot(to_cstr(commentfile)) -> int)
}
/**
* Obtain a list of container snapshots.
*/
pub fn snapshot_list(&self) -> Vec<super::Snapshot> {
let mut list = Vec::new();
call!(self.snapshot_list(&mut list.as_mut_ptr()));
list
}
/**
* Create a new container based on a snapshot.
*
* The restored container will be a copy (not snapshot) of the snapshot,
* and restored in the `lxcpath` of the original container.
*/
pub fn snapshot_restore(&self, snapname: &str, newname: &str) -> super::Result<()> {
call!(self.snapshot_restore(to_cstr(snapname), to_cstr(newname)) -> bool)
}
/**
* Destroy the specified snapshot.
*/
pub fn snapshot_destroy(&self, snapname: &str) -> super::Result<()> {
call!(self.snapshot_destroy(to_cstr(snapname)) -> bool)
}
/**
* Determine if the caller may control the container.
*/
pub fn may_control(&self) -> bool {
call!(self.may_control())
}
/**
* Add specified device to the container.
*/
pub fn add_device_node(&self, src_path: &str, dest_path: Option<&str>) -> super::Result<()> {
call!(self.add_device_node(to_cstr(src_path), to_nullable_cstr(dest_path)) -> bool)
}
/**
* Remove specified device from the container.
*/
pub fn remove_device_node(&self, src_path: &str, dest_path: Option<&str>) -> super::Result<()> {
call!(self.remove_device_node(to_cstr(src_path), to_nullable_cstr(dest_path)) -> bool)
}
/**
* Add specified netdev to the container.
*/
#[cfg(feature = "v1_1")]
pub fn attach_interface(&self, dev: &str, dst_dev: &str) -> super::Result<()> {
call!(self.attach_interface(to_cstr(dev), to_cstr(dst_dev)) -> bool)
}
/**
* Remove specified netdev from the container.
*/
#[cfg(feature = "v1_1")]
pub fn detach_interface(&self, dev: &str, dst_dev: &str) -> super::Result<()> {
call!(self.detach_interface(to_cstr(dev), to_cstr(dst_dev)) -> bool)
}
/**
* Checkpoint a container.
*/
#[cfg(feature = "v1_1")]
pub fn checkpoint(&self, directory: &str, stop: bool, verbose: bool) -> super::Result<()> {
call!(self.checkpoint(to_mut_cstr(directory), stop, verbose) -> bool)
}
/**
* Restore a container from a checkpoint.
*/
#[cfg(feature = "v1_1")]
pub fn restore(&self, directory: &str, verbose: bool) -> super::Result<()> {
call!(self.restore(to_mut_cstr(directory), verbose) -> bool)
}
/**
* Delete the container and all its snapshots.
*/
#[cfg(feature = "v1_1")]
pub fn destroy_with_snapshots(&self) -> super::Result<()> {
call!(self.destroy_with_snapshots() -> bool)
}
/**
* Destroy all the container's snapshot.
*/
#[cfg(feature = "v1_1")]
pub fn snapshot_destroy_all(&self) -> super::Result<()> {
call!(self.snapshot_destroy_all() -> bool)
}
/**
* An API call to perform various migration operations.
*/
#[cfg(feature = "v2_0")]
pub fn migrate(
&self,
cmd: u32,
opts: &mut super::migrate::Opts,
size: usize,
) -> super::Result<()> {
call!(self.migrate(cmd, opts, size as u32) -> int)
}
/**
* Query the console log of a container.
*/
#[cfg(feature = "v3_0")]
pub fn console_log(&self, log: &mut super::console::Log) -> super::Result<()> {
call!(self.console_log(log) -> int)
}
/**
* Request the container reboot by sending it `SIGINT`.
*/
#[cfg(feature = "v3_0")]
pub fn reboot2(&self, timetout: i32) -> super::Result<()> {
call!(self.reboot2(timetout) -> bool)
}
fn last_error(&self) -> super::Error
{
super::Error {
num: get!(self.error_num),
str: get!(self.error_string -> c_str),
}
}
}
impl Drop for Container {
fn drop(&mut self) {
unsafe {
lxc_sys::lxc_container_put(self.inner);
}
}
}