3 |
# Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07 |
# Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07 |
4 |
# |
# |
5 |
use strict; |
use strict; |
6 |
use Shell qw/host mkdir vzsplit rm/; |
use Shell qw/mkdir vzsplit rm/; |
7 |
use IO::Prompt; |
use IO::Prompt; |
8 |
use Regexp::Common qw/net/; |
use Regexp::Common qw/net/; |
9 |
|
use lib 'lib'; |
10 |
|
use VZ; |
11 |
|
use Getopt::Long; |
12 |
|
|
13 |
# default debian distribution |
# default debian distribution |
14 |
my $dist = 'etch'; |
my $dist = 'etch'; |
15 |
# debian mirror to use |
# debian mirror to use |
16 |
my $debian_mirror_uri = 'http://debian.carnet.hr/debian'; |
my $debian_mirror_uri = 'http://www.debian.org/debian'; |
17 |
|
my $arh = 'i386'; |
18 |
# split physicial machine in how meny virtual ones? |
# split physicial machine in how meny virtual ones? |
19 |
my $ve_total = 2; |
my $split = 4; |
20 |
# swap size (Mb) |
# swap size (Mb) |
21 |
my $swap_size = 512; |
my $swap_size = 512; |
22 |
|
# diskspace |
23 |
|
my $diskspace = '2G:4G'; |
24 |
|
|
25 |
|
GetOptions( |
26 |
|
'dist=s' => \$dist, |
27 |
|
'arh=s' => \$arh, |
28 |
|
'mirror=s' => \$debian_mirror_uri, |
29 |
|
'split=i' => \$split, |
30 |
|
); |
31 |
|
|
32 |
|
check_root; |
33 |
|
|
34 |
|
my $config_file = $0; |
35 |
|
$config_file =~ s!-create.pl!-tools.conf!; |
36 |
|
warn "## $config_file\n"; |
37 |
|
if (-e $config_file) { |
38 |
|
open(my $fh, '<', $config_file) || die "can't open $config_file: $!"; |
39 |
|
eval join("\n", <$fh>); |
40 |
|
close($fh); |
41 |
|
die "Error in $config_file: $@" if ($@); |
42 |
|
} |
43 |
|
|
44 |
print "Creating new OpenVZ instance...\n"; |
print "Creating new OpenVZ instance...\n"; |
45 |
|
|
46 |
my $arg = shift @ARGV; |
my ($hostname, $ip) = ('localhost',''); |
47 |
|
|
48 |
my ($hostname, $ip); |
foreach my $arg ( @ARGV ) { |
49 |
|
|
50 |
|
if ($arg =~ m/$RE{net}{IPv4}/) { |
51 |
|
$ip = $arg; |
52 |
|
if ( my $h = ip2hostname($ip) ) { |
53 |
|
$hostname = $h; |
54 |
|
} |
55 |
|
} elsif ($arg) { |
56 |
|
if ( my $addr = hostname2ip($arg) ) { |
57 |
|
( $hostname, $ip ) = ( $arg, $addr ); |
58 |
|
} else { |
59 |
|
$hostname = $arg; |
60 |
|
} |
61 |
|
} |
62 |
|
|
|
if ($arg =~ m/$RE{net}{IPv4}/) { |
|
|
$ip = $arg; |
|
|
chomp($hostname); |
|
|
$hostname = host($arg); |
|
|
$hostname =~ s/^.*\s(\S+)$/$1/; |
|
|
} else { |
|
|
$hostname = $arg; |
|
|
$ip = host($arg); |
|
|
chomp($ip); |
|
|
$ip =~ s/^.*\s(\S+)$/$1/; |
|
63 |
} |
} |
64 |
|
|
65 |
|
# nuke arguments so that prompt doesn't get confused |
66 |
|
@ARGV = (); |
67 |
|
|
68 |
$ip ||= prompt('Enter IP: ', -require => { |
$ip ||= prompt('Enter IP: ', -require => { |
69 |
'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/, |
'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/, |
70 |
}) unless ($ip =~ /$RE{net}{IPv4}/); |
}) unless ($ip =~ /$RE{net}{IPv4}/); |
73 |
|
|
74 |
my @ip_split = split(/\./,$ip); |
my @ip_split = split(/\./,$ip); |
75 |
|
|
76 |
my $ve_id = sprintf('%03d%03d', $ip_split[2], $ip_split[3]); |
# 192.168.42.1 -> 42 001 |
77 |
|
# 10.60.0.3 -> 60 003 |
78 |
my $vz_root = '/vz'; |
# 10.60.1.42 -> 1 042 |
79 |
|
|
80 |
die "need to know vz_root, and $vz_root doesn't exist: $!\n" unless (-e $vz_root); |
my $ve_id = sprintf('%d%03d', $ip_split[2] || $ip_split[1], $ip_split[3]); |
81 |
|
|
82 |
|
if ( $ve_id < 101 ) { |
83 |
|
$ve_id += 100; |
84 |
|
warn "# VEID moved to $ve_id because 0 .. 100 are reserved\n"; |
85 |
|
} |
86 |
|
|
87 |
print "VEID: $ve_id hostname: $hostname ip: $ip\n"; |
print "VEID: $ve_id hostname: $hostname ip: $ip\n"; |
88 |
|
|
90 |
|
|
91 |
mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id"); |
mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id"); |
92 |
|
|
93 |
warn ">> installing debian\n"; |
warn ">> installing debian $dist $arh from $debian_mirror_uri\n"; |
94 |
|
|
95 |
if (! -e "$vz_root/private/$ve_id/etc/debian_version") { |
if (! -e "$vz_root/private/$ve_id/etc/debian_version") { |
96 |
|
|
97 |
my $debootstrap = "debootstrap --arch i386 $dist $vz_root/private/$ve_id $debian_mirror_uri"; |
my $debootstrap = "debootstrap --arch $arh $dist $vz_root/private/$ve_id $debian_mirror_uri"; |
98 |
warn "# $debootstrap\n"; |
warn "# $debootstrap\n"; |
99 |
system($debootstrap); |
system($debootstrap); |
100 |
|
|
102 |
warn "Debian allready installed in $vz_root/private/$ve_id\n"; |
warn "Debian allready installed in $vz_root/private/$ve_id\n"; |
103 |
} |
} |
104 |
|
|
105 |
sub vzctl { |
my $conf_path = "$vz_conf/${ve_id}.conf"; |
|
my @args = @_; |
|
|
warn "## vzctl ", join(" ",@args), "\n"; |
|
|
system "vzctl", @args; |
|
|
} |
|
|
|
|
|
my $conf_path = "/etc/vz/conf/${ve_id}.conf"; |
|
106 |
warn ">> creating configuration file $conf_path\n"; |
warn ">> creating configuration file $conf_path\n"; |
107 |
|
|
108 |
if (-e $conf_path) { |
if (-e $conf_path) { |
109 |
warn "$conf_path allready exists, not touching it\n"; |
warn "$conf_path allready exists, not touching it\n"; |
110 |
} else { |
} else { |
111 |
vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path); |
vzsplit('-n', $split, '-s', $swap_size * 1024, '>', $conf_path); |
112 |
|
|
113 |
|
die "configuration file not created" unless -e $conf_path; |
114 |
|
|
115 |
open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!"; |
open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!"; |
116 |
print $tmp "OSTEMPLATE=debian-3.1\n"; |
print $tmp "OSTEMPLATE=debian-3.1\n"; |
117 |
close($tmp); |
close($tmp); |
118 |
|
|
119 |
vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save'); |
# vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save'); |
120 |
vzctl('set', $ve_id, '--ipadd', $ip, '--save'); |
vzctl('set', $ve_id, '--ipadd', $ip, '--save'); |
121 |
vzctl('set', $ve_id, '--hostname', $hostname, '--save'); |
vzctl('set', $ve_id, '--hostname', $hostname, '--save'); |
122 |
|
vzctl('set', $ve_id, '--diskspace', $diskspace, '--save'); |
123 |
} |
} |
124 |
|
|
125 |
sub create_file { |
sub create_file { |
139 |
|
|
140 |
vzctl('start', $ve_id); |
vzctl('start', $ve_id); |
141 |
|
|
142 |
my $customize_sh = <<'__END_OF_SH__'; |
runscript( $ve_id, 'custom/00-all.sh' ); |
143 |
|
|
144 |
# use shadow passwords |
#vzctl('stop', $ve_id); |
|
pwconv |
|
|
|
|
|
# upgrade to lastest version |
|
|
apt-get update |
|
|
apt-get upgrade |
|
|
|
|
|
# install additional packages |
|
|
apt-get -y install vim less ssh |
|
|
|
|
|
# lock root user |
|
|
usermod -L root |
|
|
|
|
|
# disable getty |
|
|
sed -i -e '/getty/d' /etc/inittab |
|
|
|
|
|
# sane permissions for /root directory |
|
|
chmod 700 /root |
|
|
|
|
|
# disable sync() for syslog |
|
|
sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/syslog.conf |
|
|
|
|
|
# fix /etc/mtab |
|
|
rm -f /etc/mtab |
|
|
ln -s /proc/mounts /etc/mtab |
|
|
|
|
|
# remove unneeded packages |
|
|
dpkg --purge modutils |
|
|
dpkg --purge ppp pppoeconf pppoe pppconfig |
|
|
|
|
|
# disable services |
|
|
update-rc.d -f klogd remove |
|
|
update-rc.d -f quotarpc remove |
|
|
update-rc.d -f exim4 remove |
|
|
update-rc.d -f inetd remove |
|
|
|
|
|
# clean packages |
|
|
apt-get clean |
|
145 |
|
|
146 |
__END_OF_SH__ |
my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' ); |
147 |
|
vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd; |
148 |
|
|
149 |
foreach my $l (split(/\n/, $customize_sh)) { |
my $login = prompt('create login: '); |
150 |
next if ($l =~ /^\s*$/); |
if ($login) { |
151 |
if ($l =~ /^#\s+(.+)$/) { |
$passwd = prompt( -prompt => "$login passwd: ", -echo=>'*'); |
152 |
warn ">> $1\n"; |
vzctl('exec', $ve_id, "useradd --create-home $login"); |
153 |
} else { |
vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" ); |
|
vzctl('exec', $ve_id, $l); |
|
|
} |
|
154 |
} |
} |
155 |
|
|
156 |
#vzctl('stop', $ve_id); |
#runscript( $ve_id, 'custom/50-hypertable.sh' ); |
157 |
|
print "OK: $ve_id created\n"; |