/[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 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 = 4;  my $split = 4;
20  # swap size (Mb)  # swap size (Mb)
21  my $swap_size = 512;  my $swap_size = 512;
22  # diskspace  # diskspace
23  my $diskspace = '2G:4G';  my $diskspace = '2G:4G';
24    
25  print "Creating new OpenVZ instance...\n";  $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  my $arg = shift @ARGV || '';  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";
48    
49  my ($hostname, $ip) = ('localhost','');  my ($hostname, $ip) = ('localhost','');
50    
51  if ($arg =~ m/$RE{net}{IPv4}/) {  foreach my $arg ( @ARGV ) {
52          $ip = $arg;  
53          chomp($hostname);          if ($arg =~ m/$RE{net}{IPv4}/) {
54          $hostname = host($arg);                  $ip = $arg;
55          $hostname =~ s/^.*\s(\S+)$/$1/;                  if ( my $h = ip2hostname($ip) ) {
56  } elsif ($arg) {                          $hostname = $h;
57          $hostname = $arg;                  }
58          $ip = host($arg);          } elsif ($arg) {
59          chomp($ip);                  if ( my $addr = hostname2ip($arg) ) {
60          $ip =~ s/^.*\s(\S+)$/$1/;                          ( $hostname, $ip ) = ( $arg, $addr );
61                    } else {
62                            $hostname = $arg;
63                    }
64            }
65    
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 44  $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 56  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 68  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";
# Line 109  create_file( Line 133  create_file(
133    
134  vzctl('start', $ve_id);  vzctl('start', $ve_id);
135    
136  my $customize_sh = <<'__END_OF_SH__';  runscript( $ve_id, 'custom/00-all.sh' );
   
 # use shadow passwords  
 pwconv  
   
 # upgrade to lastest version  
 apt-get -y update  
 apt-get -y upgrade  
   
 # install additional packages  
 apt-get -y install vim less ssh sudo screen telnet finger  
   
 # 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  
   
 __END_OF_SH__  
   
 foreach my $l (split(/\n/, $customize_sh)) {  
         next if ($l =~ /^\s*$/);  
         if ($l =~ /^#\s+(.+)$/) {  
                 warn ">> $1\n";  
         } else {  
                 vzctl('exec', $ve_id, $l);  
         }  
 }  
137    
138  #vzctl('stop', $ve_id);  #vzctl('stop', $ve_id);
139    
140  my $passwd = prompt('root passwd: ', -echo=>'*');  my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
141  vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if ($passwd);  vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;
142    
143  my $login = prompt('create login: ');  my $login = prompt('create login: ');
144  if ($login) {  if ($login) {
145          $passwd = prompt("$login passwd: ", -echo=>'*');          $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
146          vzctl('exec', $ve_id, "useradd --create-home $login");          vzctl('exec', $ve_id, "useradd --create-home $login");
147          vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );          vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
148  }  }
149    
150    runscript( $ve_id, 'custom/50-hypertable.sh' );
151    print "OK: $ve_id created\n";

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

  ViewVC Help
Powered by ViewVC 1.1.26