/[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 45 by dpavlin, Wed Mar 12 00:43:49 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    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    $dist = 'lenny';
26    $arh = 'amd64';
27    
28    GetOptions(
29            'dist=s'        => \$dist,
30            'arh=s'         => \$arh,
31            'mirror=s'      => \$debian_mirror_uri,
32            'split=i'       => \$split,
33    );
34    
35    check_root;
36    
37    my $config_file = $0;
38    $config_file =~ s!-create.pl!-tools.conf!;
39    warn "## $config_file\n";
40    if (-e $config_file) {
41            open(my $fh, '<', $config_file) || die "can't open $config_file: $!";
42            eval join("\n", <$fh>);
43            close($fh);
44            die "Error in $config_file: $@" if ($@);
45    }
46    
47  print "Creating new OpenVZ instance...\n";  print "Creating new OpenVZ instance...\n";
48    
49  my $arg = shift @ARGV;  my ($hostname, $ip) = ('localhost','');
50    
51  my ($hostname, $ip);  foreach my $arg ( @ARGV ) {
52    
53            if ($arg =~ m/$RE{net}{IPv4}/) {
54                    $ip = $arg;
55                    if ( my $h = ip2hostname($ip) ) {
56                            $hostname = $h;
57                    }
58            } elsif ($arg) {
59                    if ( my $addr = hostname2ip($arg) ) {
60                            ( $hostname, $ip ) = ( $arg, $addr );
61                    } else {
62                            $hostname = $arg;
63                    }
64            }
65    
 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/;  
66  }  }
67    
68    # nuke arguments so that prompt doesn't get confused
69    @ARGV = ();
70    
71  $ip ||= prompt('Enter IP: ', -require => {  $ip ||= prompt('Enter IP: ', -require => {
72          '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}/,
73  }) unless ($ip =~ /$RE{net}{IPv4}/);  }) unless ($ip =~ /$RE{net}{IPv4}/);
# Line 42  $hostname ||= prompt('Enter hostname: ') Line 76  $hostname ||= prompt('Enter hostname: ')
76    
77  my @ip_split = split(/\./,$ip);  my @ip_split = split(/\./,$ip);
78    
79  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);  
80    
81  print "VEID: $ve_id hostname: $hostname ip: $ip\n";  print "VEID: $ve_id hostname: $hostname ip: $ip\n";
82    
# Line 54  warn ">> creating directories\n"; Line 84  warn ">> creating directories\n";
84    
85  mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");  mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
86    
87  warn ">> installing debian\n";  warn ">> installing debian $dist $arh from $debian_mirror_uri\n";
88    
89  if (! -e "$vz_root/private/$ve_id/etc/debian_version") {  if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
90    
91          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";
92          warn "# $debootstrap\n";          warn "# $debootstrap\n";
93          system($debootstrap);          system($debootstrap);
94    
# Line 66  if (! -e "$vz_root/private/$ve_id/etc/de Line 96  if (! -e "$vz_root/private/$ve_id/etc/de
96          warn "Debian allready installed in $vz_root/private/$ve_id\n";          warn "Debian allready installed in $vz_root/private/$ve_id\n";
97  }  }
98    
99  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";  
100  warn ">> creating configuration file $conf_path\n";  warn ">> creating configuration file $conf_path\n";
101    
102  if (-e $conf_path) {  if (-e $conf_path) {
103          warn "$conf_path allready exists, not touching it\n";          warn "$conf_path allready exists, not touching it\n";
104  } else {  } else {
105          vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path);          vzsplit('-n', $split, '-s', $swap_size * 1024, '>', $conf_path);
106    
107            die "configuration file not created" unless -e $conf_path;
108    
109          open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";          open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
110          print $tmp "OSTEMPLATE=debian-3.1\n";          print $tmp "OSTEMPLATE=debian-3.1\n";
111          close($tmp);          close($tmp);
112    
113          vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');  #       vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
114          vzctl('set', $ve_id, '--ipadd', $ip, '--save');          vzctl('set', $ve_id, '--ipadd', $ip, '--save');
115          vzctl('set', $ve_id, '--hostname', $hostname, '--save');          vzctl('set', $ve_id, '--hostname', $hostname, '--save');
116            vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
117  }  }
118    
119  sub create_file {  sub create_file {
# Line 104  create_file( Line 131  create_file(
131          "deb $debian_mirror_uri $dist main contrib non-free\n"          "deb $debian_mirror_uri $dist main contrib non-free\n"
132  );  );
133    
134  vzctl('start', $ve_id, '--wait');  vzctl('start', $ve_id);
   
 my $customize_sh = <<'__END_OF_SH__';  
135    
136  # use shadow passwords  runscript( $ve_id, 'custom/00-all.sh' );
 pwconv  
137    
138  # upgrade to lastest version  #vzctl('stop', $ve_id);
 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  
139    
140  __END_OF_SH__  my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
141    vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;
142    
143  foreach my $l (split(/\n/, $customize_sh)) {  my $login = prompt('create login: ');
144          next if ($l =~ /^\s*$/);  if ($login) {
145          if ($l =~ /^#\s+(.+)$/) {          $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
146                  warn ">> $1\n";          vzctl('exec', $ve_id, "useradd --create-home $login");
147          } else {          vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
                 vzctl('exec', $ve_id, $l);  
         }  
148  }  }
149    
150  #vzctl('stop', $ve_id);  runscript( $ve_id, 'custom/50-hypertable.sh' );
151    print "OK: $ve_id created\n";

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

  ViewVC Help
Powered by ViewVC 1.1.26