/[pxelator]/lib/PXElator/lxc.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/PXElator/lxc.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 439 - (show annotations)
Sun Sep 20 16:12:03 2009 UTC (14 years, 6 months ago) by dpavlin
File size: 1959 byte(s)
Create really working Linux Containers from filesystem directory and network setup

1 package lxc;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use file;
8
9 use Data::Dump qw/dump/;
10
11 sub create {
12 my ( $hostname, $path, $ip, $netmask, $server_ip ) = @_;
13
14 die "$path: $!" unless -d $path;
15
16 file::append "$path/etc/initab" => "$_\n" foreach split(/\n/, qq|
17 z6:6:respawn:/sbin/sulogin
18 1:2345:respawn:/sbin/getty 38400 console
19 c1:12345:respawn:/sbin/getty 38400 tty1 linux
20 c2:12345:respawn:/sbin/getty 38400 tty2 linux
21 c3:12345:respawn:/sbin/getty 38400 tty3 linux
22 c4:12345:respawn:/sbin/getty 38400 tty4 linux
23 |);
24
25 file::append '/etc/fstab' => 'cgroup /cgroup cgroup rw 0 0' && mkdir '/cgroup';
26 system "mount /cgroup";
27
28 system "lxc-stop --name $hostname";
29 system "lxc-destroy --name $hostname";
30
31 file::replace "$path/etc/hostname" => $hostname;
32 file::append "$path/etc/hosts" => "$ip $hostname";
33
34 file::replace "$path/etc/network/interfaces" => qq|
35 auto eth0 lo
36
37 iface lo inet loopback
38
39 iface eth0 inet static
40 address $ip
41 netmask $netmask
42 gateway $server_ip
43 |;
44
45 file::replace "$path/etc/resolv.conf" => "nameserver $server_ip";
46
47 my $conf = "/virtual/$hostname.conf";
48
49 file::replace $conf => qq|
50 lxc.utsname = $hostname
51 lxc.tty = 4
52 lxc.pts = 1024
53 lxc.network.type = veth
54 lxc.network.flags = up
55 lxc.network.link = virtual
56 lxc.network.name = eth0
57 lxc.network.mtu = 1500
58 #lxc.mount = MNTFILE
59 lxc.rootfs = $path
60 lxc.cgroup.devices.deny = a
61 # /dev/null and zero
62 lxc.cgroup.devices.allow = c 1:3 rwm
63 lxc.cgroup.devices.allow = c 1:5 rwm
64 # consoles
65 lxc.cgroup.devices.allow = c 5:1 rwm
66 lxc.cgroup.devices.allow = c 5:0 rwm
67 lxc.cgroup.devices.allow = c 4:0 rwm
68 lxc.cgroup.devices.allow = c 4:1 rwm
69 # /dev/{,u}random
70 lxc.cgroup.devices.allow = c 1:9 rwm
71 lxc.cgroup.devices.allow = c 1:8 rwm
72 lxc.cgroup.devices.allow = c 136:* rwm
73 lxc.cgroup.devices.allow = c 5:2 rwm
74 # rtc
75 lxc.cgroup.devices.allow = c 254:0 rwm
76 |;
77
78 system "lxc-create --name $hostname --rcfile $conf";
79
80 warn "created $hostname $ip $path";
81 }
82
83 warn 'loaded';
84
85 1;

  ViewVC Help
Powered by ViewVC 1.1.26