/[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 18 - (show annotations)
Tue Apr 17 12:46:31 2007 UTC (16 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 4540 byte(s)
added external config file

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26