Skip to content

Instantly share code, notes, and snippets.

@hiboma
Created March 17, 2012 02:12
Show Gist options
  • Save hiboma/2054384 to your computer and use it in GitHub Desktop.
Save hiboma/2054384 to your computer and use it in GitHub Desktop.
require 'mkmf'
# http://www.ruby-lang.org/ja/old-man/html/mkmf.html
# http://brandish.xrea.jp/BlackCity/labo/labo_ruby_ext_2.html
# http://d.hatena.ne.jp/yarb/20090724/p1
dir_config('lxc')
if have_header('lxc/namespace.h') and have_library('lxc') and have_func("lxc_attach")
$libs = append_library($libs, "lxc")
create_makefile("lxc")
else
raise "lxc_attach is not defined"
end
#include "ruby.h"
#include "lxc/namespace.h"
static VALUE lxc_lxc_attach(VALUE, VALUE);
void
Init_lxc(void){
VALUE lxc;
lxc = rb_define_class("Lxc", rb_cObject);
rb_define_method(lxc, "attach", lxc_lxc_attach, 1);
}
static VALUE
lxc_lxc_attach(VALUE self, VALUE name) {
char *lxc_name = StringValuePtr(name);
pid_t pid;
int ret;
pid = get_init_pid(lxc_name);
if(pid == -1) {
rb_raise(rb_eRuntimeError, "container '%s' is not running", lxc_name);
}
ret = lxc_attach(pid);
if(ret == -1) {
rb_raise(rb_eRuntimeError, "failed to lxc_attach (pid %d)", pid);
}
return self;
}
@hiboma
Copy link
Author

hiboma commented Mar 17, 2012

ruby1.8 extconf.rb --with-lxc-header=/usr/local/lxc/include/ --with-lxc-lib=/usr/local/lxc/lib/
make
ruby1.8 -r./lxc -e 'Lxc.new.attach("hogehoge"); system("hostname"); '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment