create Linux container using lxc
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 15:10:31 +0000 (15:10 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 15:10:31 +0000 (15:10 +0000)
lib/PXElator/lxc.pm [new file with mode: 0644]
lib/PXElator/t/lxc.t [new file with mode: 0755]

diff --git a/lib/PXElator/lxc.pm b/lib/PXElator/lxc.pm
new file mode 100644 (file)
index 0000000..7fb08e8
--- /dev/null
@@ -0,0 +1,70 @@
+package lxc;
+
+use warnings;
+use strict;
+use autodie;
+
+use file;
+
+use Data::Dump qw/dump/;
+
+sub create {
+       my ( $hostname, $ip, $path ) = @_;
+
+       file::append "$path/etc/initab" => $_ foreach qw(
+z6:6:respawn:/sbin/sulogin
+1:2345:respawn:/sbin/getty 38400 console
+c1:12345:respawn:/sbin/getty 38400 tty1 linux
+c2:12345:respawn:/sbin/getty 38400 tty2 linux
+c3:12345:respawn:/sbin/getty 38400 tty3 linux
+c4:12345:respawn:/sbin/getty 38400 tty4 linux
+       );
+
+       file::append '/etc/fstab' => 'cgroup /cgroup cgroup rw 0 0' && mkdir '/cgroup';
+       system "mount /cgroup";
+
+       system "lxc-stop    -n $hostname";
+       system "lxc-destroy -n $hostname";
+
+       file::replace "$path/etc/hostname" => $hostname;
+       file::append  "$path/hosts"        => "$ip $hostname";
+
+       my $conf = "/virtual/$hostname.conf";
+
+       file::replace $conf => qq|
+lxc.utsname = $hostname
+lxc.tty = 4
+lxc.pts = 1024
+lxc.network.type = veth
+lxc.network.flags = up
+lxc.network.link = br0
+lxc.network.name = eth0
+lxc.network.mtu = 1500
+#lxc.mount = MNTFILE
+lxc.rootfs = $path
+lxc.cgroup.devices.deny = a
+# /dev/null and zero
+lxc.cgroup.devices.allow = c 1:3 rwm
+lxc.cgroup.devices.allow = c 1:5 rwm
+# consoles
+lxc.cgroup.devices.allow = c 5:1 rwm
+lxc.cgroup.devices.allow = c 5:0 rwm
+lxc.cgroup.devices.allow = c 4:0 rwm
+lxc.cgroup.devices.allow = c 4:1 rwm
+# /dev/{,u}random
+lxc.cgroup.devices.allow = c 1:9 rwm
+lxc.cgroup.devices.allow = c 1:8 rwm
+lxc.cgroup.devices.allow = c 136:* rwm
+lxc.cgroup.devices.allow = c 5:2 rwm
+# rtc
+lxc.cgroup.devices.allow = c 254:0 rwm
+       |;
+
+       system "lxc-create -n $hostname -f $conf";
+
+       warn "created $hostname $ip $path";
+}
+
+warn 'loaded';
+
+1;
diff --git a/lib/PXElator/t/lxc.t b/lib/PXElator/t/lxc.t
new file mode 100755 (executable)
index 0000000..71c80df
--- /dev/null
@@ -0,0 +1,12 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 2;
+use Data::Dump qw/dump/;
+
+use_ok 'lxc';
+
+ok( lxc::create( 'webpac2.lib', '10.60.0.100', '/mnt/webpac2' ), 'create' );