/[vz-tools]/trunk/vz-create.pl
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 /trunk/vz-create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations)
Sun Jan 7 15:21:36 2007 UTC (17 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 3869 byte(s)
added telnet and finger packages, setup diskspace for VE
1 #!/usr/bin/perl -w
2 #
3 # Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07
4 #
5 use strict;
6 use Shell qw/host mkdir vzsplit rm/;
7 use IO::Prompt;
8 use Regexp::Common qw/net/;
9
10 # default debian distribution
11 my $dist = 'etch';
12 # debian mirror to use
13 my $debian_mirror_uri = 'http://debian.carnet.hr/debian';
14 # split physicial machine in how meny virtual ones?
15 my $ve_total = 2;
16 # swap size (Mb)
17 my $swap_size = 512;
18 # diskspace
19 my $diskspace = '2G:4G';
20
21 print "Creating new OpenVZ instance...\n";
22
23 my $arg = shift @ARGV;
24
25 my ($hostname, $ip);
26
27 if ($arg =~ m/$RE{net}{IPv4}/) {
28 $ip = $arg;
29 chomp($hostname);
30 $hostname = host($arg);
31 $hostname =~ s/^.*\s(\S+)$/$1/;
32 } else {
33 $hostname = $arg;
34 $ip = host($arg);
35 chomp($ip);
36 $ip =~ s/^.*\s(\S+)$/$1/;
37 }
38
39 $ip ||= prompt('Enter IP: ', -require => {
40 'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/,
41 }) unless ($ip =~ /$RE{net}{IPv4}/);
42
43 $hostname ||= prompt('Enter hostname: ') unless ($hostname);
44
45 my @ip_split = split(/\./,$ip);
46
47 my $ve_id = sprintf('%03d%03d', $ip_split[2], $ip_split[3]);
48
49 my $vz_root = '/vz';
50
51 die "need to know vz_root, and $vz_root doesn't exist: $!\n" unless (-e $vz_root);
52
53 print "VEID: $ve_id hostname: $hostname ip: $ip\n";
54
55 warn ">> creating directories\n";
56
57 mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
58
59 warn ">> installing debian\n";
60
61 if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
62
63 my $debootstrap = "debootstrap --arch i386 $dist $vz_root/private/$ve_id $debian_mirror_uri";
64 warn "# $debootstrap\n";
65 system($debootstrap);
66
67 } else {
68 warn "Debian allready installed in $vz_root/private/$ve_id\n";
69 }
70
71 sub vzctl {
72 my @args = @_;
73 warn "## vzctl ", join(" ",@args), "\n";
74 system "vzctl", @args;
75 }
76
77 my $conf_path = "/etc/vz/conf/${ve_id}.conf";
78 warn ">> creating configuration file $conf_path\n";
79
80 if (-e $conf_path) {
81 warn "$conf_path allready exists, not touching it\n";
82 } else {
83 vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path);
84
85 open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
86 print $tmp "OSTEMPLATE=debian-3.1\n";
87 close($tmp);
88
89 vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
90 vzctl('set', $ve_id, '--ipadd', $ip, '--save');
91 vzctl('set', $ve_id, '--hostname', $hostname, '--save');
92 vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
93 }
94
95 sub create_file {
96 my ($path, $code) = @_;
97 if (! -e $path) {
98 warn ">> creating $path\n";
99 open(my $tmp, '>', $path) || die "can't create $path: $!";
100 print $tmp $code->($path);
101 close($tmp);
102 }
103 }
104
105 create_file(
106 "$vz_root/private/$ve_id/etc/apt/sources.list",
107 "deb $debian_mirror_uri $dist main contrib non-free\n"
108 );
109
110 vzctl('start', $ve_id, '--wait');
111
112 my $customize_sh = <<'__END_OF_SH__';
113
114 # use shadow passwords
115 pwconv
116
117 # upgrade to lastest version
118 apt-get update
119 apt-get upgrade
120
121 # install additional packages
122 apt-get -y install vim less ssh sudo screen telnet finger
123
124 # remove unwanted packages
125 apt-get -y remove nano
126
127 # apt-iselect helper
128 wget -O /usr/local/bin/apt-iselect http://www.rot13.org/~dpavlin/projects/scripts/apt-iselect && chmod 755 /usr/local/bin/apt-iselect
129
130 # lock root user
131 usermod -L root
132
133 # disable getty
134 sed -i -e '/getty/d' /etc/inittab
135
136 # sane permissions for /root directory
137 chmod 700 /root
138
139 # disable sync() for syslog
140 sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/syslog.conf
141
142 # fix /etc/mtab
143 rm -f /etc/mtab
144 ln -s /proc/mounts /etc/mtab
145
146 # remove unneeded packages
147 dpkg --purge modutils
148 dpkg --purge ppp pppoeconf pppoe pppconfig
149
150 # disable services
151 update-rc.d -f klogd remove
152 update-rc.d -f quotarpc remove
153 update-rc.d -f exim4 remove
154 update-rc.d -f inetd remove
155
156 # clean packages
157 apt-get clean
158
159 __END_OF_SH__
160
161 foreach my $l (split(/\n/, $customize_sh)) {
162 next if ($l =~ /^\s*$/);
163 if ($l =~ /^#\s+(.+)$/) {
164 warn ">> $1\n";
165 } else {
166 vzctl('exec', $ve_id, $l);
167 }
168 }
169
170 #vzctl('stop', $ve_id);
171

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26