--- trunk/vz-create.pl 2007/01/07 13:35:10 8 +++ trunk/vz-create.pl 2009/02/27 18:38:19 63 @@ -3,37 +3,68 @@ # Dobrica Pavlinusic 2007-01-07 # use strict; -use Shell qw/host mkdir vzsplit rm/; +use Shell qw/mkdir vzsplit rm/; use IO::Prompt; use Regexp::Common qw/net/; +use lib 'lib'; +use VZ; +use Getopt::Long; # default debian distribution my $dist = 'etch'; # debian mirror to use -my $debian_mirror_uri = 'http://debian.carnet.hr/debian'; +my $debian_mirror_uri = 'http://www.debian.org/debian'; +my $arh = 'i386'; # split physicial machine in how meny virtual ones? -my $ve_total = 2; +my $split = 4; # swap size (Mb) my $swap_size = 512; +# diskspace +my $diskspace = '2G:4G'; + +GetOptions( + 'dist=s' => \$dist, + 'arh=s' => \$arh, + 'mirror=s' => \$debian_mirror_uri, + 'split=i' => \$split, +); + +check_root; + +my $config_file = $0; +$config_file =~ s!-create.pl!-tools.conf!; +warn "## $config_file\n"; +if (-e $config_file) { + open(my $fh, '<', $config_file) || die "can't open $config_file: $!"; + eval join("\n", <$fh>); + close($fh); + die "Error in $config_file: $@" if ($@); +} print "Creating new OpenVZ instance...\n"; -my $arg = shift @ARGV; +my ($hostname, $ip) = ('localhost',''); -my ($hostname, $ip); +foreach my $arg ( @ARGV ) { + + if ($arg =~ m/$RE{net}{IPv4}/) { + $ip = $arg; + if ( my $h = ip2hostname($ip) ) { + $hostname = $h; + } + } elsif ($arg) { + if ( my $addr = hostname2ip($arg) ) { + ( $hostname, $ip ) = ( $arg, $addr ); + } else { + $hostname = $arg; + } + } -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/; } +# nuke arguments so that prompt doesn't get confused +@ARGV = (); + $ip ||= prompt('Enter IP: ', -require => { 'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/, }) unless ($ip =~ /$RE{net}{IPv4}/); @@ -42,11 +73,12 @@ my @ip_split = split(/\./,$ip); -my $ve_id = sprintf('%03d%03d', $ip_split[2], $ip_split[3]); - -my $vz_root = '/vz'; +my $ve_id = sprintf('%d%03d', $ip_split[2], $ip_split[3]); -die "need to know vz_root, and $vz_root doesn't exist: $!\n" unless (-e $vz_root); +if ( $ve_id < 101 ) { + $ve_id += 100; + warn "# VEID moved to $ve_id because 0 .. 100 are reserved\n"; +} print "VEID: $ve_id hostname: $hostname ip: $ip\n"; @@ -54,11 +86,11 @@ mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id"); -warn ">> installing debian\n"; +warn ">> installing debian $dist $arh from $debian_mirror_uri\n"; if (! -e "$vz_root/private/$ve_id/etc/debian_version") { - 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"; warn "# $debootstrap\n"; system($debootstrap); @@ -66,27 +98,24 @@ warn "Debian allready installed in $vz_root/private/$ve_id\n"; } -sub vzctl { - my @args = @_; - warn "## vzctl ", join(" ",@args), "\n"; - system "vzctl", @args; -} - -my $conf_path = "/etc/vz/conf/${ve_id}.conf"; +my $conf_path = "$vz_conf/${ve_id}.conf"; warn ">> creating configuration file $conf_path\n"; if (-e $conf_path) { warn "$conf_path allready exists, not touching it\n"; } else { - vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path); + vzsplit('-n', $split, '-s', $swap_size * 1024, '>', $conf_path); + + die "configuration file not created" unless -e $conf_path; open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!"; print $tmp "OSTEMPLATE=debian-3.1\n"; close($tmp); - vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save'); +# vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save'); vzctl('set', $ve_id, '--ipadd', $ip, '--save'); vzctl('set', $ve_id, '--hostname', $hostname, '--save'); + vzctl('set', $ve_id, '--diskspace', $diskspace, '--save'); } sub create_file { @@ -104,65 +133,21 @@ "deb $debian_mirror_uri $dist main contrib non-free\n" ); -vzctl('start', $ve_id, '--wait'); - -my $customize_sh = <<'__END_OF_SH__'; +vzctl('start', $ve_id); -# use shadow passwords -pwconv +runscript( $ve_id, 'custom/00-all.sh' ); -# upgrade to lastest version -apt-get update -apt-get upgrade - -# install additional packages -apt-get -y install vim less ssh sudo screen - -# remove unwanted packages -apt-get -y remove nano - -# apt-iselect helper -wget -O /usr/local/bin/apt-iselect http://www.rot13.org/~dpavlin/projects/scripts/apt-iselect && chmod 755 /usr/local/bin/apt-iselect - -# 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 +#vzctl('stop', $ve_id); -__END_OF_SH__ +my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' ); +vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd; -foreach my $l (split(/\n/, $customize_sh)) { - next if ($l =~ /^\s*$/); - if ($l =~ /^#\s+(.+)$/) { - warn ">> $1\n"; - } else { - vzctl('exec', $ve_id, $l); - } +my $login = prompt('create login: '); +if ($login) { + $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*'); + vzctl('exec', $ve_id, "useradd --create-home $login"); + vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" ); } -#vzctl('stop', $ve_id); - +#runscript( $ve_id, 'custom/50-hypertable.sh' ); +print "OK: $ve_id created\n";