/[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

Diff of /trunk/vz-create.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 11 by dpavlin, Sun Jan 7 23:53:16 2007 UTC revision 40 by dpavlin, Fri Feb 8 23:02:25 2008 UTC
# Line 3  Line 3 
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    
12  # default debian distribution  # default debian distribution
13  my $dist = 'etch';  my $dist = 'etch';
14  # debian mirror to use  # debian mirror to use
15  my $debian_mirror_uri = 'http://debian.carnet.hr/debian';  my $debian_mirror_uri = 'http://www.debian.org/debian';
16    my $arh = 'i386';
17  # split physicial machine in how meny virtual ones?  # split physicial machine in how meny virtual ones?
18  my $ve_total = 4;  my $ve_total = 4;
19  # swap size (Mb)  # swap size (Mb)
# Line 18  my $swap_size = 512; Line 21  my $swap_size = 512;
21  # diskspace  # diskspace
22  my $diskspace = '2G:4G';  my $diskspace = '2G:4G';
23    
24  print "Creating new OpenVZ instance...\n";  $dist = 'testing';
25    $arh = 'amd64';
26    
27    check_root;
28    
29  my $arg = shift @ARGV || '';  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','');  my ($hostname, $ip) = ('localhost','');
42    
43  if ($arg =~ m/$RE{net}{IPv4}/) {  foreach my $arg ( @ARGV ) {
44          $ip = $arg;  
45          chomp($hostname);          if ($arg =~ m/$RE{net}{IPv4}/) {
46          $hostname = host($arg);                  $ip = $arg;
47          $hostname =~ s/^.*\s(\S+)$/$1/;                  if ( my $h = ip2hostname($ip) ) {
48  } elsif ($arg) {                          $hostname = $h;
49          $hostname = $arg;                  }
50          $ip = host($arg);          } elsif ($arg) {
51          chomp($ip);                  if ( my $addr = hostname2ip($arg) ) {
52          $ip =~ s/^.*\s(\S+)$/$1/;                          ( $hostname, $ip ) = ( $arg, $addr );
53                    } else {
54                            $hostname = $arg;
55                    }
56            }
57  }  }
58    
59  $ip ||= prompt('Enter IP: ', -require => {  $ip ||= prompt('Enter IP: ', -require => {
# Line 44  $hostname ||= prompt('Enter hostname: ') Line 64  $hostname ||= prompt('Enter hostname: ')
64    
65  my @ip_split = split(/\./,$ip);  my @ip_split = split(/\./,$ip);
66    
67  my $ve_id = sprintf('%03d%03d', $ip_split[2], $ip_split[3]);  my $ve_id = sprintf('%d%03d', $ip_split[2], $ip_split[3]);
   
 my $vz_root = '/vz';  
   
 die "need to know vz_root, and $vz_root doesn't exist: $!\n" unless (-e $vz_root);  
68    
69  print "VEID: $ve_id hostname: $hostname ip: $ip\n";  print "VEID: $ve_id hostname: $hostname ip: $ip\n";
70    
# Line 56  warn ">> creating directories\n"; Line 72  warn ">> creating directories\n";
72    
73  mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");  mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
74    
75  warn ">> installing debian\n";  warn ">> installing debian $dist $arh from $debian_mirror_uri\n";
76    
77  if (! -e "$vz_root/private/$ve_id/etc/debian_version") {  if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
78    
79          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";
80          warn "# $debootstrap\n";          warn "# $debootstrap\n";
81          system($debootstrap);          system($debootstrap);
82    
# Line 68  if (! -e "$vz_root/private/$ve_id/etc/de Line 84  if (! -e "$vz_root/private/$ve_id/etc/de
84          warn "Debian allready installed in $vz_root/private/$ve_id\n";          warn "Debian allready installed in $vz_root/private/$ve_id\n";
85  }  }
86    
87  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";  
88  warn ">> creating configuration file $conf_path\n";  warn ">> creating configuration file $conf_path\n";
89    
90  if (-e $conf_path) {  if (-e $conf_path) {
# Line 82  if (-e $conf_path) { Line 92  if (-e $conf_path) {
92  } else {  } else {
93          vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path);          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: $!";          open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
98          print $tmp "OSTEMPLATE=debian-3.1\n";          print $tmp "OSTEMPLATE=debian-3.1\n";
99          close($tmp);          close($tmp);
# Line 119  apt-get -y update Line 131  apt-get -y update
131  apt-get -y upgrade  apt-get -y upgrade
132    
133  # install additional packages  # install additional packages
134  apt-get -y install vim less ssh sudo screen telnet finger  apt-get -y --force-yes install vim less ssh sudo screen telnet finger
135    
136  # remove unwanted packages  # remove unwanted packages
137  apt-get -y remove nano  apt-get -y remove nano
138    
139  # apt-iselect helper  # apt-iselect helper
140  wget -O /usr/local/bin/apt-iselect http://www.rot13.org/~dpavlin/projects/scripts/apt-iselect  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  chmod 755 /usr/local/bin/apt-iselect
142    
143  # lock root user  # lock root user
# Line 179  if ($login) { Line 191  if ($login) {
191          vzctl('exec', $ve_id, "useradd --create-home $login");          vzctl('exec', $ve_id, "useradd --create-home $login");
192          vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );          vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
193  }  }
194    
195    print "OK: $ve_id created\n";

Legend:
Removed from v.11  
changed lines
  Added in v.40

  ViewVC Help
Powered by ViewVC 1.1.26