Skip to content

Commit

Permalink
Adds attach example
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Sep 27, 2021
1 parent d3feea2 commit c1efccc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ bitflags = "1.0"
version = "0.4"
path = "./lxc-sys"

[dev-dependencies]
libc = "0.2"

[features]
v1_0 = []
v1_1 = ["v1_0"]
Expand Down
53 changes: 53 additions & 0 deletions examples/attach.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::os::unix::io::AsRawFd;

fn main() -> std::io::Result<()> {
let c =
lxc::Container::new("apicontainer", None).expect("Failed to setup lxc_container struct");

if c.is_defined() {
panic!("Container already exists");
}

c.create(
"download",
None,
None,
::lxc::CreateFlags::QUIET,
&["-d", "alpine", "-r", "3.14", "-a", "amd64"],
)
.expect("Failed to create container rootfs");

c.start(false, &[]).expect("Failed to start the container");

let mut options = lxc::attach::Options {
stdout_fd: std::io::stdout().as_raw_fd(),
stderr_fd: std::io::stderr().as_raw_fd(),
stdin_fd: std::io::stdin().as_raw_fd(),

..lxc::attach::Options::default()
};

let r = c.attach(
Some(lxc::attach::run_shell),
std::ptr::null_mut() as *mut std::ffi::c_void,
&mut options,
);

match r {
Ok(pid) => wait(pid),
Err(e) => println!("{:?}", e),
}

c.stop().expect("Failed to kill the container.");
c.destroy().expect("Failed to destroy the container.");

Ok(())
}

fn wait(pid: i32) {
let mut status = 0;

unsafe {
libc::waitpid(pid, &mut status, 0);
}
}

0 comments on commit c1efccc

Please sign in to comment.