Create really working Linux Containers from filesystem directory and network setup
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 16:12:03 +0000 (16:12 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 16:12:03 +0000 (16:12 +0000)
lib/PXElator/lxc.pm
lib/PXElator/t/lxc.t

index 7fb08e8..735bece 100644 (file)
@@ -9,25 +9,40 @@ use file;
 use Data::Dump qw/dump/;
 
 sub create {
-       my ( $hostname, $ip, $path ) = @_;
+       my ( $hostname, $path, $ip, $netmask, $server_ip ) = @_;
 
-       file::append "$path/etc/initab" => $_ foreach qw(
+       die "$path: $!" unless -d $path;
+
+       file::append "$path/etc/initab" => "$_\n" foreach split(/\n/, qq|
 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";
+       system "lxc-stop    --name $hostname";
+       system "lxc-destroy --name $hostname";
 
        file::replace "$path/etc/hostname" => $hostname;
-       file::append  "$path/hosts"        => "$ip $hostname";
+       file::append  "$path/etc/hosts"    => "$ip $hostname";
+
+       file::replace "$path/etc/network/interfaces" => qq|
+auto eth0 lo
+
+iface lo inet loopback
+
+iface eth0 inet static
+    address $ip
+    netmask $netmask
+    gateway $server_ip
+       |;
+
+       file::replace "$path/etc/resolv.conf" => "nameserver $server_ip";
 
        my $conf = "/virtual/$hostname.conf";
 
@@ -37,7 +52,7 @@ lxc.tty = 4
 lxc.pts = 1024
 lxc.network.type = veth
 lxc.network.flags = up
-lxc.network.link = br0
+lxc.network.link = virtual
 lxc.network.name = eth0
 lxc.network.mtu = 1500
 #lxc.mount = MNTFILE
@@ -60,7 +75,7 @@ lxc.cgroup.devices.allow = c 5:2 rwm
 lxc.cgroup.devices.allow = c 254:0 rwm
        |;
 
-       system "lxc-create -n $hostname -f $conf";
+       system "lxc-create --name $hostname --rcfile $conf";
 
        warn "created $hostname $ip $path";
 }
index 71c80df..ece27e6 100755 (executable)
@@ -9,4 +9,4 @@ use Data::Dump qw/dump/;
 
 use_ok 'lxc';
 
-ok( lxc::create( 'webpac2.lib', '10.60.0.100', '/mnt/webpac2' ), 'create' );
+ok( lxc::create( 'webpac2.lib' => '/virtual/webpac2.rot13.org/export', qw(172.16.10.42 255.255.250.0 172.16.10.1) ), 'create' );