1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Shell qw/rsync vzlist lvcreate mount umount lvremove which ssync lvdisplay mke2fs/; |
use Shell qw/rsync vzlist lvcreate mount umount lvremove which lvdisplay mke2fs vzctl/; |
5 |
|
|
6 |
my $vz = '/vz'; |
my $vz = '/vz'; |
7 |
my $conf = '/etc/vz/conf'; |
my $conf = '/etc/vz/conf'; |
8 |
|
|
9 |
my $snap_size = '100M'; |
my $snap_size = '100M'; |
10 |
|
|
11 |
my $VEID = shift @ARGV || die "Usage: $0 VEID\n\n",vzlist; |
my ( $action, $VEID ) = @ARGV; |
12 |
|
|
13 |
|
my $valid_actions = 'create|umount|destroy'; |
14 |
|
|
15 |
|
die "Usage: $0 ($valid_actions) VEID\n\n",vzlist unless $VEID && $action && $action =~ m/($valid_actions)/; |
16 |
|
|
17 |
|
if ( $action eq 'destroy' ) { |
18 |
|
my $vzlist = vzlist(); |
19 |
|
if ( $vzlist =~ m/$VEID/ ) { |
20 |
|
print "stop $VEID\n"; |
21 |
|
vzctl('stop', $VEID); |
22 |
|
} |
23 |
|
} |
24 |
|
|
25 |
|
if ( $action =~ m/(umount|destroy)/ ) { |
26 |
|
my $clone; |
27 |
|
open(my $m, '-|', 'mount') || die "can't open mount: $!"; |
28 |
|
while(<$m>) { |
29 |
|
my @v = split(/\s+/,$_); |
30 |
|
if ( $v[2] =~ m!/(private|root)/$VEID$! && $v[5] =~ m/bind/ ) { |
31 |
|
umount( $v[2] ); |
32 |
|
} elsif ( $v[2] =~ m!-clone-$VEID$! ) { |
33 |
|
$clone = $v[2]; |
34 |
|
} |
35 |
|
} |
36 |
|
umount( $clone ) if $clone; |
37 |
|
} |
38 |
|
|
39 |
|
#die "unknown action: $action" unless $action eq 'create'; |
40 |
|
exit unless $action eq 'create'; |
41 |
|
|
42 |
my $CVEID = "10$VEID"; |
my $CVEID = "10$VEID"; |
43 |
|
|
73 |
|
|
74 |
sub copy_files { |
sub copy_files { |
75 |
my ( $from, $to ) = @_; |
my ( $from, $to ) = @_; |
76 |
if ( which('ssync') ) { |
warn "rsync $from -> $to\n"; |
77 |
warn "ssync $from -> $to\n"; |
rsync('-raSHAD', "$from/", "$to/" ); |
|
ssync(qw{--log-mode file --log-path /dev/null},'-f',$from,'-t',$to); |
|
|
} else { |
|
|
warn "rsync $from -> $to\n"; |
|
|
rsync('-ra', "$from/", "$to/" ); |
|
|
} |
|
78 |
} |
} |
79 |
|
|
80 |
if ( $vz_lv ) { |
if ( $vz_lv ) { |
133 |
|
|
134 |
print "Cleanup\n"; |
print "Cleanup\n"; |
135 |
|
|
136 |
umount( "$vz_lv_path/$clone" ); |
#umount( "$vz_lv_path/$clone" ); |
137 |
umount( "$vz_lv_path/$snap" ); |
umount( "$vz_lv_path/$snap" ); |
138 |
|
|
139 |
lvremove( '-f', "$vz_lv_path/$snap" ); |
lvremove( '-f', "$vz_lv_path/$snap" ); |