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

Contents of /trunk/vz-create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Wed Mar 12 00:43:49 2008 UTC (16 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 3672 byte(s)
initial support for command line options, for now
--dist --arh --mirror and --split
1 #!/usr/bin/perl -w
2 #
3 # Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07
4 #
5 use strict;
6 use Shell qw/mkdir vzsplit rm/;
7 use IO::Prompt;
8 use Regexp::Common qw/net/;
9 use lib 'lib';
10 use VZ;
11 use Getopt::Long;
12
13 # default debian distribution
14 my $dist = 'etch';
15 # debian mirror to use
16 my $debian_mirror_uri = 'http://www.debian.org/debian';
17 my $arh = 'i386';
18 # split physicial machine in how meny virtual ones?
19 my $split = 4;
20 # swap size (Mb)
21 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";
48
49 my ($hostname, $ip) = ('localhost','');
50
51 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
66 }
67
68 # nuke arguments so that prompt doesn't get confused
69 @ARGV = ();
70
71 $ip ||= prompt('Enter IP: ', -require => {
72 'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/,
73 }) unless ($ip =~ /$RE{net}{IPv4}/);
74
75 $hostname ||= prompt('Enter hostname: ') unless ($hostname);
76
77 my @ip_split = split(/\./,$ip);
78
79 my $ve_id = sprintf('%d%03d', $ip_split[2], $ip_split[3]);
80
81 print "VEID: $ve_id hostname: $hostname ip: $ip\n";
82
83 warn ">> creating directories\n";
84
85 mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
86
87 warn ">> installing debian $dist $arh from $debian_mirror_uri\n";
88
89 if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
90
91 my $debootstrap = "debootstrap --arch $arh $dist $vz_root/private/$ve_id $debian_mirror_uri";
92 warn "# $debootstrap\n";
93 system($debootstrap);
94
95 } else {
96 warn "Debian allready installed in $vz_root/private/$ve_id\n";
97 }
98
99 my $conf_path = "$vz_conf/${ve_id}.conf";
100 warn ">> creating configuration file $conf_path\n";
101
102 if (-e $conf_path) {
103 warn "$conf_path allready exists, not touching it\n";
104 } else {
105 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: $!";
110 print $tmp "OSTEMPLATE=debian-3.1\n";
111 close($tmp);
112
113 # vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
114 vzctl('set', $ve_id, '--ipadd', $ip, '--save');
115 vzctl('set', $ve_id, '--hostname', $hostname, '--save');
116 vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
117 }
118
119 sub create_file {
120 my ($path, $code) = @_;
121 if (! -e $path) {
122 warn ">> creating $path\n";
123 open(my $tmp, '>', $path) || die "can't create $path: $!";
124 print $tmp $code->($path);
125 close($tmp);
126 }
127 }
128
129 create_file(
130 "$vz_root/private/$ve_id/etc/apt/sources.list",
131 "deb $debian_mirror_uri $dist main contrib non-free\n"
132 );
133
134 vzctl('start', $ve_id);
135
136 runscript( $ve_id, 'custom/00-all.sh' );
137
138 #vzctl('stop', $ve_id);
139
140 my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
141 vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;
142
143 my $login = prompt('create login: ');
144 if ($login) {
145 $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
146 vzctl('exec', $ve_id, "useradd --create-home $login");
147 vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
148 }
149
150 runscript( $ve_id, 'custom/50-hypertable.sh' );
151 print "OK: $ve_id created\n";

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26