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

Annotation of /trunk/vz-create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (hide annotations)
Mon Apr 27 12:08:07 2009 UTC (13 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 3831 byte(s)
document IP address to VEID mungling
1 dpavlin 4 #!/usr/bin/perl -w
2     #
3     # Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07
4     #
5     use strict;
6 dpavlin 38 use Shell qw/mkdir vzsplit rm/;
7 dpavlin 4 use IO::Prompt;
8     use Regexp::Common qw/net/;
9 dpavlin 38 use lib 'lib';
10     use VZ;
11 dpavlin 45 use Getopt::Long;
12 dpavlin 4
13     # default debian distribution
14     my $dist = 'etch';
15     # debian mirror to use
16 dpavlin 18 my $debian_mirror_uri = 'http://www.debian.org/debian';
17 dpavlin 38 my $arh = 'i386';
18 dpavlin 4 # split physicial machine in how meny virtual ones?
19 dpavlin 45 my $split = 4;
20 dpavlin 4 # swap size (Mb)
21     my $swap_size = 512;
22 dpavlin 9 # diskspace
23     my $diskspace = '2G:4G';
24 dpavlin 4
25 dpavlin 45 GetOptions(
26     'dist=s' => \$dist,
27     'arh=s' => \$arh,
28     'mirror=s' => \$debian_mirror_uri,
29     'split=i' => \$split,
30     );
31    
32 dpavlin 38 check_root;
33    
34 dpavlin 18 my $config_file = $0;
35     $config_file =~ s!-create.pl!-tools.conf!;
36     warn "## $config_file\n";
37     if (-e $config_file) {
38     open(my $fh, '<', $config_file) || die "can't open $config_file: $!";
39     eval join("\n", <$fh>);
40     close($fh);
41     die "Error in $config_file: $@" if ($@);
42     }
43    
44 dpavlin 4 print "Creating new OpenVZ instance...\n";
45    
46 dpavlin 10 my ($hostname, $ip) = ('localhost','');
47 dpavlin 4
48 dpavlin 38 foreach my $arg ( @ARGV ) {
49    
50     if ($arg =~ m/$RE{net}{IPv4}/) {
51     $ip = $arg;
52 dpavlin 40 if ( my $h = ip2hostname($ip) ) {
53 dpavlin 38 $hostname = $h;
54     }
55     } elsif ($arg) {
56     if ( my $addr = hostname2ip($arg) ) {
57     ( $hostname, $ip ) = ( $arg, $addr );
58     } else {
59     $hostname = $arg;
60     }
61     }
62 dpavlin 44
63 dpavlin 4 }
64    
65 dpavlin 44 # nuke arguments so that prompt doesn't get confused
66     @ARGV = ();
67    
68 dpavlin 4 $ip ||= prompt('Enter IP: ', -require => {
69     'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/,
70     }) unless ($ip =~ /$RE{net}{IPv4}/);
71    
72     $hostname ||= prompt('Enter hostname: ') unless ($hostname);
73    
74     my @ip_split = split(/\./,$ip);
75    
76 dpavlin 77 # 192.168.42.1 -> 42 001
77     # 10.60.0.3 -> 60 003
78     # 10.60.1.42 -> 1 042
79 dpavlin 4
80 dpavlin 77 my $ve_id = sprintf('%d%03d', $ip_split[2] || $ip_split[1], $ip_split[3]);
81    
82 dpavlin 58 if ( $ve_id < 101 ) {
83     $ve_id += 100;
84     warn "# VEID moved to $ve_id because 0 .. 100 are reserved\n";
85     }
86    
87 dpavlin 4 print "VEID: $ve_id hostname: $hostname ip: $ip\n";
88    
89     warn ">> creating directories\n";
90    
91     mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
92    
93 dpavlin 38 warn ">> installing debian $dist $arh from $debian_mirror_uri\n";
94 dpavlin 4
95     if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
96    
97 dpavlin 38 my $debootstrap = "debootstrap --arch $arh $dist $vz_root/private/$ve_id $debian_mirror_uri";
98 dpavlin 4 warn "# $debootstrap\n";
99     system($debootstrap);
100    
101     } else {
102     warn "Debian allready installed in $vz_root/private/$ve_id\n";
103     }
104    
105 dpavlin 40 my $conf_path = "$vz_conf/${ve_id}.conf";
106 dpavlin 4 warn ">> creating configuration file $conf_path\n";
107    
108     if (-e $conf_path) {
109     warn "$conf_path allready exists, not touching it\n";
110     } else {
111 dpavlin 45 vzsplit('-n', $split, '-s', $swap_size * 1024, '>', $conf_path);
112 dpavlin 4
113 dpavlin 38 die "configuration file not created" unless -e $conf_path;
114    
115 dpavlin 4 open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
116     print $tmp "OSTEMPLATE=debian-3.1\n";
117     close($tmp);
118    
119 dpavlin 11 # vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
120 dpavlin 4 vzctl('set', $ve_id, '--ipadd', $ip, '--save');
121     vzctl('set', $ve_id, '--hostname', $hostname, '--save');
122 dpavlin 9 vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
123 dpavlin 4 }
124    
125     sub create_file {
126     my ($path, $code) = @_;
127     if (! -e $path) {
128     warn ">> creating $path\n";
129     open(my $tmp, '>', $path) || die "can't create $path: $!";
130     print $tmp $code->($path);
131     close($tmp);
132     }
133     }
134    
135     create_file(
136     "$vz_root/private/$ve_id/etc/apt/sources.list",
137     "deb $debian_mirror_uri $dist main contrib non-free\n"
138     );
139    
140 dpavlin 11 vzctl('start', $ve_id);
141 dpavlin 4
142 dpavlin 44 runscript( $ve_id, 'custom/00-all.sh' );
143 dpavlin 4
144     #vzctl('stop', $ve_id);
145    
146 dpavlin 44 my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
147     vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;
148 dpavlin 11
149     my $login = prompt('create login: ');
150     if ($login) {
151 dpavlin 44 $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
152 dpavlin 11 vzctl('exec', $ve_id, "useradd --create-home $login");
153     vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
154     }
155 dpavlin 40
156 dpavlin 63 #runscript( $ve_id, 'custom/50-hypertable.sh' );
157 dpavlin 40 print "OK: $ve_id created\n";

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26