Skip to content

Commit cefdb91

Browse files
committed
Adds attach example
1 parent d3feea2 commit cefdb91

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ bitflags = "1.0"
2020
version = "0.4"
2121
path = "./lxc-sys"
2222

23+
[dev-dependencies]
24+
libc = "0.2"
25+
2326
[features]
2427
v1_0 = []
2528
v1_1 = ["v1_0"]

examples/attach.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::os::unix::io::AsRawFd;
2+
3+
fn main() -> std::io::Result<()> {
4+
let c =
5+
lxc::Container::new("apicontainer", None).expect("Failed to setup lxc_container struct");
6+
7+
if c.is_defined() {
8+
panic!("Container already exists");
9+
}
10+
11+
c.create(
12+
"download",
13+
None,
14+
None,
15+
::lxc::CreateFlags::QUIET,
16+
&["-d", "alpine", "-r", "3.14", "-a", "amd64"],
17+
)
18+
.expect("Failed to create container rootfs");
19+
20+
c.start(false, &[]).expect("Failed to start the container");
21+
22+
let mut options = lxc::attach::Options {
23+
stdout_fd: std::io::stdout().as_raw_fd(),
24+
stderr_fd: std::io::stderr().as_raw_fd(),
25+
stdin_fd: std::io::stdin().as_raw_fd(),
26+
27+
.. lxc::attach::Options::default()
28+
};
29+
30+
let r = c.attach(
31+
Some(lxc::attach::run_shell),
32+
std::ptr::null_mut() as *mut std::ffi::c_void,
33+
&mut options,
34+
);
35+
36+
match r {
37+
Ok(pid) => wait(pid),
38+
Err(e) => println!("{:?}", e),
39+
}
40+
41+
c.stop().expect("Failed to kill the container.");
42+
c.destroy().expect("Failed to destroy the container.");
43+
44+
Ok(())
45+
}
46+
47+
fn wait(pid: i32) {
48+
let mut status = 0;
49+
50+
unsafe {
51+
libc::waitpid(pid, &mut status, 0);
52+
}
53+
}

0 commit comments

Comments
 (0)