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

Annotation of /trunk/vz-clone.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74 - (hide annotations)
Mon Mar 9 19:09:22 2009 UTC (13 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 5103 byte(s)
rewrite /dev/mapper/vg-lv to /dev/vg/lv
1 dpavlin 20 #!/usr/bin/perl -w
2    
3     use strict;
4 dpavlin 73 use Shell qw/rsync lvcreate mount umount lvremove which lvdisplay mke2fs/;
5 dpavlin 20
6 dpavlin 73 use lib 'lib';
7     use VZ;
8 dpavlin 20
9 dpavlin 67 my $snap_size = '10G';
10 dpavlin 21
11 dpavlin 25 my ( $action, $VEID ) = @ARGV;
12 dpavlin 20
13 dpavlin 65 my $use_snapshot = 1;
14 dpavlin 27
15 dpavlin 29 my $valid_actions = 'create|mount|umount|destroy';
16 dpavlin 25
17 dpavlin 73 die "Usage: $0 ($valid_actions) VEID\n\n",`vzlist` unless $VEID && $action && $action =~ m{($valid_actions)};
18 dpavlin 25
19 dpavlin 27 my $CVEID = "10$VEID";
20    
21     sub vzctl_stop {
22     my $veid = shift;
23 dpavlin 25 my $vzlist = vzlist();
24 dpavlin 27 if ( $vzlist =~ m/$veid/ ) {
25     print "stop $veid\n";
26     vzctl('stop', $veid);
27 dpavlin 25 }
28     }
29    
30 dpavlin 27 if ( $action eq 'create' ) {
31     vzctl_stop( $CVEID );
32     } elsif ( $action eq 'destroy' ) {
33     vzctl_stop( $VEID );
34     }
35    
36    
37 dpavlin 66 if ( $action =~ m{(create|umount|destroy)} ) {
38 dpavlin 25 my $clone;
39 dpavlin 66 my $snap;
40 dpavlin 25 open(my $m, '-|', 'mount') || die "can't open mount: $!";
41     while(<$m>) {
42     my @v = split(/\s+/,$_);
43 dpavlin 66 if ( $v[2] =~ m{/(private|root)/($VEID|$CVEID)$} && $v[5] =~ m{bind} ) {
44 dpavlin 25 umount( $v[2] );
45 dpavlin 64 } elsif ( $v[2] =~ m{vz-clone-$CVEID$} ) {
46 dpavlin 25 $clone = $v[2];
47 dpavlin 66 } elsif ( $v[2] =~ m{-snap} ) {
48     $snap = $v[0];
49     umount( $snap );
50 dpavlin 25 }
51     }
52     umount( $clone ) if $clone;
53 dpavlin 66 if ( $action eq 'destroy' && -e $snap ) {
54     lvremove( '-f', $snap );
55     }
56 dpavlin 30
57 dpavlin 69 vzctl('destroy', $VEID) if $action eq 'destroy';
58    
59     exit if $action =~ m{(umount|destroy)};
60 dpavlin 25 }
61    
62    
63 dpavlin 20 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 dpavlin 21 my $vz_lv;
70 dpavlin 20
71 dpavlin 73 open(my $m, '-|', "df $vz_root") || die "can't open df $vz_root: $!";
72 dpavlin 21 while(<$m>) {
73 dpavlin 73 next if ! m{^/};
74 dpavlin 21 my @v = split(/\s+/,$_);
75 dpavlin 73 $vz_lv = $v[0];
76 dpavlin 74 $vz_lv =~ s{^/dev/mapper/([^\-]+)-(.+)$}{/dev/$1/$2};
77     # rewrite /dev/mapper/vg-lv to /dev/vg/lv
78 dpavlin 73 warn "found LV $vz_lv for $vz_root\n";
79     last;
80 dpavlin 21 }
81    
82 dpavlin 23 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 dpavlin 21 sub copy_files {
94     my ( $from, $to ) = @_;
95 dpavlin 29 return if $action ne 'create';
96 dpavlin 24 warn "rsync $from -> $to\n";
97 dpavlin 31 rsync('-raSHAD', '--delete', "$from/", "$to/" );
98 dpavlin 21 }
99    
100     if ( $vz_lv ) {
101    
102 dpavlin 64 my ( $vz_lv_path, $vz_lv_name ) = ( $1, $2 ) if ( $vz_lv =~ m{^(.+)/([^/]+)$} );
103 dpavlin 21
104     my $snap = $vz_lv_name . '-snap';
105 dpavlin 26 my $clone = "vz-clone-$CVEID";
106 dpavlin 23
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 dpavlin 27 if ( $use_snapshot ) {
129 dpavlin 21
130 dpavlin 27 print "Creating $snap_size snapshot $snap from $vz_lv\n";
131     lvcreate( '--size', $snap_size, '--snapshot', '--name', $snap, $vz_lv );
132 dpavlin 21
133 dpavlin 27 do_mount( "$vz_lv_path/$snap", "/tmp/$snap" );
134    
135 dpavlin 73 mount_bind( "/tmp/$snap/private/$VEID", "$vz_root/private/$CVEID" );
136     mount_bind( "/tmp/$snap/root/$VEID", "$vz_root/root/$CVEID" );
137 dpavlin 27
138 dpavlin 23 } else {
139 dpavlin 21
140 dpavlin 65 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 dpavlin 21
150 dpavlin 65 do_mount( "$vz_lv_path/$clone", "/tmp/$clone" );
151 dpavlin 21
152 dpavlin 65 test_mkdir( "/tmp/$clone/private" );
153     test_mkdir( "/tmp/$clone/root" );
154 dpavlin 21
155 dpavlin 73 copy_files( "$vz_root/private/$VEID", "/tmp/$clone/private" );
156 dpavlin 23
157 dpavlin 73 mount_bind( "/tmp/$clone/private", "$vz_root/private/$CVEID" );
158     mount_bind( "/tmp/$clone/root", "$vz_root/root/$CVEID" );
159 dpavlin 23
160 dpavlin 27 }
161    
162 dpavlin 21 } else {
163    
164 dpavlin 73 copy_files( "$vz_root/private/$VEID", "$vz_root/private/$CVEID" );
165 dpavlin 21 }
166    
167 dpavlin 29 exit if $action eq 'mount';
168    
169 dpavlin 21 my $note = "# modified by $0\n";
170    
171 dpavlin 50 my $ve_clone_ip;
172    
173 dpavlin 20 sub fix_ip {
174     my $ip = shift;
175 dpavlin 21 $ip =~ s/['"]//g;
176 dpavlin 33 # $ip =~ s/^\d+\./10./;
177     $ip =~ s/^\d+\.\d+\./192.168./;
178 dpavlin 20 warn "$CVEID new IP number: $ip\n";
179 dpavlin 50 $ve_clone_ip = $ip;
180 dpavlin 21 return $note . qq{IP_ADDRESS="$ip"};
181 dpavlin 20 }
182    
183 dpavlin 21 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 dpavlin 73 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 dpavlin 20 while(<$o>) {
194 dpavlin 62 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 dpavlin 20 print $n $_;
198     }
199    
200 dpavlin 65 print $n qq{DISK_QUOTA="no"\n} if $use_snapshot; # don't wait for quota rebuild on startup
201    
202 dpavlin 67 my $iptables="iptables -t nat -A POSTROUTING -s $ve_clone_ip -o eth0 -j MASQUERADE";
203     system( $iptables );
204 dpavlin 73 print "\nConfig file: $vz_conf/$CVEID.conf\nAdded NAT for new VE with: $iptables\nStart clone of $VEID with: vzctl start $CVEID\n"
205 dpavlin 66

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26