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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26