/[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 8 by dpavlin, Sun Jan 7 13:35:10 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 = 2;  my $ve_total = 4;
19  # swap size (Mb)  # swap size (Mb)
20  my $swap_size = 512;  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";  print "Creating new OpenVZ instance...\n";
40    
41  my $arg = shift @ARGV;  my ($hostname, $ip) = ('localhost','');
42    
43  my ($hostname, $ip);  foreach my $arg ( @ARGV ) {
44    
45  if ($arg =~ m/$RE{net}{IPv4}/) {          if ($arg =~ m/$RE{net}{IPv4}/) {
46          $ip = $arg;                  $ip = $arg;
47          chomp($hostname);                  if ( my $h = ip2hostname($ip) ) {
48          $hostname = host($arg);                          $hostname = $h;
49          $hostname =~ s/^.*\s(\S+)$/$1/;                  }
50  } else {          } elsif ($arg) {
51          $hostname = $arg;                  if ( my $addr = hostname2ip($arg) ) {
52          $ip = host($arg);                          ( $hostname, $ip ) = ( $arg, $addr );
53          chomp($ip);                  } else {
54          $ip =~ s/^.*\s(\S+)$/$1/;                          $hostname = $arg;
55                    }
56            }
57  }  }
58    
59  $ip ||= prompt('Enter IP: ', -require => {  $ip ||= prompt('Enter IP: ', -require => {
# Line 42  $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 54  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 66  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 80  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);
100    
101          vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');  #       vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
102          vzctl('set', $ve_id, '--ipadd', $ip, '--save');          vzctl('set', $ve_id, '--ipadd', $ip, '--save');
103          vzctl('set', $ve_id, '--hostname', $hostname, '--save');          vzctl('set', $ve_id, '--hostname', $hostname, '--save');
104            vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
105  }  }
106    
107  sub create_file {  sub create_file {
# Line 104  create_file( Line 119  create_file(
119          "deb $debian_mirror_uri $dist main contrib non-free\n"          "deb $debian_mirror_uri $dist main contrib non-free\n"
120  );  );
121    
122  vzctl('start', $ve_id, '--wait');  vzctl('start', $ve_id);
123    
124  my $customize_sh = <<'__END_OF_SH__';  my $customize_sh = <<'__END_OF_SH__';
125    
# Line 112  my $customize_sh = <<'__END_OF_SH__'; Line 127  my $customize_sh = <<'__END_OF_SH__';
127  pwconv  pwconv
128    
129  # upgrade to lastest version  # upgrade to lastest version
130  apt-get update  apt-get -y update
131  apt-get upgrade  apt-get -y upgrade
132    
133  # install additional packages  # install additional packages
134  apt-get -y install vim less ssh sudo screen  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 && chmod 755 /usr/local/bin/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
142    
143  # lock root user  # lock root user
144  usermod -L root  usermod -L root
# Line 166  foreach my $l (split(/\n/, $customize_sh Line 182  foreach my $l (split(/\n/, $customize_sh
182    
183  #vzctl('stop', $ve_id);  #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";

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

  ViewVC Help
Powered by ViewVC 1.1.26