/[vz-tools]/trunk/vz-clone.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-clone.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74 - (show annotations)
Mon Mar 9 19:09:22 2009 UTC (13 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 5103 byte(s)
rewrite /dev/mapper/vg-lv to /dev/vg/lv
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Shell qw/rsync lvcreate mount umount lvremove which lvdisplay mke2fs/;
5
6 use lib 'lib';
7 use VZ;
8
9 my $snap_size = '10G';
10
11 my ( $action, $VEID ) = @ARGV;
12
13 my $use_snapshot = 1;
14
15 my $valid_actions = 'create|mount|umount|destroy';
16
17 die "Usage: $0 ($valid_actions) VEID\n\n",`vzlist` unless $VEID && $action && $action =~ m{($valid_actions)};
18
19 my $CVEID = "10$VEID";
20
21 sub vzctl_stop {
22 my $veid = shift;
23 my $vzlist = vzlist();
24 if ( $vzlist =~ m/$veid/ ) {
25 print "stop $veid\n";
26 vzctl('stop', $veid);
27 }
28 }
29
30 if ( $action eq 'create' ) {
31 vzctl_stop( $CVEID );
32 } elsif ( $action eq 'destroy' ) {
33 vzctl_stop( $VEID );
34 }
35
36
37 if ( $action =~ m{(create|umount|destroy)} ) {
38 my $clone;
39 my $snap;
40 open(my $m, '-|', 'mount') || die "can't open mount: $!";
41 while(<$m>) {
42 my @v = split(/\s+/,$_);
43 if ( $v[2] =~ m{/(private|root)/($VEID|$CVEID)$} && $v[5] =~ m{bind} ) {
44 umount( $v[2] );
45 } elsif ( $v[2] =~ m{vz-clone-$CVEID$} ) {
46 $clone = $v[2];
47 } elsif ( $v[2] =~ m{-snap} ) {
48 $snap = $v[0];
49 umount( $snap );
50 }
51 }
52 umount( $clone ) if $clone;
53 if ( $action eq 'destroy' && -e $snap ) {
54 lvremove( '-f', $snap );
55 }
56
57 vzctl('destroy', $VEID) if $action eq 'destroy';
58
59 exit if $action =~ m{(umount|destroy)};
60 }
61
62
63 my $orig_conf = "/etc/vz/conf/$VEID.conf";
64
65 die "$VEID config $orig_conf doesn't exist\n" unless -e $orig_conf;
66
67 print "Clone VE $VEID -> $CVEID\n";
68
69 my $vz_lv;
70
71 open(my $m, '-|', "df $vz_root") || die "can't open df $vz_root: $!";
72 while(<$m>) {
73 next if ! m{^/};
74 my @v = split(/\s+/,$_);
75 $vz_lv = $v[0];
76 $vz_lv =~ s{^/dev/mapper/([^\-]+)-(.+)$}{/dev/$1/$2};
77 # rewrite /dev/mapper/vg-lv to /dev/vg/lv
78 warn "found LV $vz_lv for $vz_root\n";
79 last;
80 }
81
82 sub fs_quota {
83 my $id = shift;
84 open(my $q, '-|', "vzquota -b show $id") || die "can't exec vzquota show $id: $!";
85 my $l = <$q>;
86 $l =~ s/^\s+//;
87 my ( $usage, $soft, $hard ) = split(/\s+/,$l);
88 warn "quota for $id | $soft < $hard | usage: $usage\n";
89 return ( $usage, $soft, $hard ) if wantarray;
90 return $soft;
91 }
92
93 sub copy_files {
94 my ( $from, $to ) = @_;
95 return if $action ne 'create';
96 warn "rsync $from -> $to\n";
97 rsync('-raSHAD', '--delete', "$from/", "$to/" );
98 }
99
100 if ( $vz_lv ) {
101
102 my ( $vz_lv_path, $vz_lv_name ) = ( $1, $2 ) if ( $vz_lv =~ m{^(.+)/([^/]+)$} );
103
104 my $snap = $vz_lv_name . '-snap';
105 my $clone = "vz-clone-$CVEID";
106
107 sub do_mount {
108 my ( $from, $to ) = @_;
109 mkdir $to || die "can't create $to: $!";
110 print "Mounting $from to $to\n";
111 mount( $from, $to, '-o', 'noatime' );
112 }
113
114 sub test_mkdir {
115 my $dir = shift;
116 if ( ! -d $dir ) {
117 mkdir $dir || die "can't mkdir $dir: $!";
118 }
119 }
120
121 sub mount_bind {
122 my ( $from, $to ) = @_;
123 die "$from doesn't exist!" unless -d $from;
124 test_mkdir( $to );
125 mount( '--bind', $from, $to );
126 }
127
128 if ( $use_snapshot ) {
129
130 print "Creating $snap_size snapshot $snap from $vz_lv\n";
131 lvcreate( '--size', $snap_size, '--snapshot', '--name', $snap, $vz_lv );
132
133 do_mount( "$vz_lv_path/$snap", "/tmp/$snap" );
134
135 mount_bind( "/tmp/$snap/private/$VEID", "$vz_root/private/$CVEID" );
136 mount_bind( "/tmp/$snap/root/$VEID", "$vz_root/root/$CVEID" );
137
138 } else {
139
140 if ( lvdisplay( "$vz_lv_path/$clone" ) ) {
141 warn "using existing $vz_lv_path/$clone\n";
142 } else {
143 my $vg_name = $1 if ( $vz_lv_path =~ m{/([^/]+)/*$} );
144 my $clone_size = fs_quota( $VEID ) . 'k';
145 print "Creating LV $clone ($clone_size bytes) in VG $vg_name for $VEID clone filesystem\n";
146 lvcreate( '--size', $clone_size, '--name', $clone, $vg_name );
147 mke2fs( '-m', 0, '-j', "$vz_lv_path/$clone" );
148 }
149
150 do_mount( "$vz_lv_path/$clone", "/tmp/$clone" );
151
152 test_mkdir( "/tmp/$clone/private" );
153 test_mkdir( "/tmp/$clone/root" );
154
155 copy_files( "$vz_root/private/$VEID", "/tmp/$clone/private" );
156
157 mount_bind( "/tmp/$clone/private", "$vz_root/private/$CVEID" );
158 mount_bind( "/tmp/$clone/root", "$vz_root/root/$CVEID" );
159
160 }
161
162 } else {
163
164 copy_files( "$vz_root/private/$VEID", "$vz_root/private/$CVEID" );
165 }
166
167 exit if $action eq 'mount';
168
169 my $note = "# modified by $0\n";
170
171 my $ve_clone_ip;
172
173 sub fix_ip {
174 my $ip = shift;
175 $ip =~ s/['"]//g;
176 # $ip =~ s/^\d+\./10./;
177 $ip =~ s/^\d+\.\d+\./192.168./;
178 warn "$CVEID new IP number: $ip\n";
179 $ve_clone_ip = $ip;
180 return $note . qq{IP_ADDRESS="$ip"};
181 }
182
183 sub fix_hostname {
184 my $hostname = shift;
185 $hostname =~ s/['"]//g;
186 $hostname = "clone-$hostname";
187 warn "$CVEID new hostname: $hostname\n";
188 return $note . qq{HOSTNAME="$hostname"};
189 }
190
191 open(my $o, '<', "$vz_conf/$VEID.conf") || die "can't open $vz_conf/$VEID.conf: $!";
192 open(my $n, '>', "$vz_conf/$CVEID.conf") || die "can't open $vz_conf/$CVEID.conf: $!";
193 while(<$o>) {
194 s{^HOSTNAME=(.*)$}{fix_hostname($1)}ie;
195 s{^IP_ADDRESS=(.*)$}{fix_ip($1)}ie;
196 s{^(ONBOOT=).*$}{# modified by $0\n$1"no"}i;
197 print $n $_;
198 }
199
200 print $n qq{DISK_QUOTA="no"\n} if $use_snapshot; # don't wait for quota rebuild on startup
201
202 my $iptables="iptables -t nat -A POSTROUTING -s $ve_clone_ip -o eth0 -j MASQUERADE";
203 system( $iptables );
204 print "\nConfig file: $vz_conf/$CVEID.conf\nAdded NAT for new VE with: $iptables\nStart clone of $VEID with: vzctl start $CVEID\n"
205

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26