LVM2/Snapshot.pm

filter: Statementstmt 36.8% 46 / 125 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 11 / 22 Podpod 100% 8 / 8 totaltotal 34.0% 65 / 191 CC 16 SCAR 25.5
CC
16
Cov
29%
BUILD
30.0
umount
30.0
mount
20.0
CRAP 12.8
« LVM2/Utils.pm ↑ Summary LVM2/LV.pm »
1 package Linux::LVM2::Snapshot;
2 {
3 2 $Linux::LVM2::Snapshot::VERSION = '0.14';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $Linux::LVM2::Snapshot::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a class representing a LV snapshot in an Linux LVM2
9  
10 2 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20  
21 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use Sys::FS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Sys::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 has 'name' => (
28 'is' => 'ro',
29 'isa' => 'Str',
30 'lazy' => 1,
31 'builder' => '_init_name',
32 );
33  
34 2 has 'logger' => (
35 'is' => 'rw',
36 'isa' => 'Log::Tree',
37 'required' => 1,
38 );
39  
40 2 has 'lv' => (
41 'is' => 'ro',
42 'isa' => 'Linux::LVM2::LV',
43 );
44  
45 2 has 'source' => (
46 'is' => 'ro',
47 'isa' => 'Linux::LVM2::LV',
48 'required' => 1,
49 );
50  
51 2 has 'parent' => (
52 'is' => 'ro',
53 'isa' => 'Linux::LVM2',
54 'required' => 1,
55 );
56  
57 2 has 'clear_caches' => (
58 'is' => 'ro',
59 'isa' => 'Bool',
60 'default' => 0,
61 );
62  
63 2 has 'snapspace' => (
64 'is' => 'rw',
65 'isa' => 'Int',
66 'default' => '5', # GB
67 );
68  
69 2 has 'mount_point' => (
70 'is' => 'ro',
71 'isa' => 'Str',
72 );
73  
74 2 has 'verbose' => (
75 'is' => 'rw',
76 'isa' => 'Int', # Bool
77 'default' => 0,
78 );
79  
80 2 has 'sys' => (
81 'is' => 'rw',
82 'isa' => 'Sys::Run',
83 'lazy' => 1,
84 'builder' => '_init_sys',
85 );
86  
87 2 has 'fs' => (
88 'is' => 'rw',
89 'isa' => 'Sys::FS',
90 'lazy' => 1,
91 'builder' => '_init_fs',
92 );
93  
94 2 has '_created_mount_point' => (
95 'is' => 'rw',
96 'isa' => 'Bool',
97 'default' => 0,
98 );
99  
100 0 ▶sub _init_sys {
Subroutine _init_sys: not called CC=1 SCAR=6.9
101 0 my $self = shift;
102  
103 0 my $Sys = Sys::Run::->new( { 'logger' => $self->logger(), } );
104  
105 0 return $Sys;
106 }
107  
108 0 ▶sub _init_fs {
Subroutine _init_fs: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110  
111 0 my $FS = Sys::FS::->new(
112 {
113 'logger' => $self->logger(),
114 'sys' => $self->sys(),
115 }
116 );
117  
118 0 return $FS;
119 }
120  
121 0 ▶sub full_path {
Subroutine full_path: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123  
124 0 return $self->lv()->full_path();
125 }
126  
127 0 ▶sub mapper_path {
Subroutine mapper_path: not called CC=1 SCAR=6.9
Pod documented
128 0 my $self = shift;
129  
130 0 return $self->lv()->mapper_path();
131 }
132  
133 0 ▶sub BUILD {
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod documented
134 0 my $self = shift;
135  
136 # clear caches, free pagecache, dentries and inodes
137 0 ▶ $self->sys()->clear_caches() if $self->clear_caches();
Branch: 1 branch
BranchTrueFalseTotal
if $self->clear_caches 0 0 0
138  
139 # sync; sync; sync; lvcreate ...
140 0 my $cmd =
141 'sync; lvcreate -L'
142 . $self->snapspace()
143 . 'G --snapshot --name '
144 . $self->name()
145 . ' /dev/'
146 . $self->source()->vg()->name() . '/'
147 . $self->source()->name();
148 0 ▶ if ( !$self->sys()->run_cmd( $cmd, { RaiseError => 1, Verbose => 1, } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sys->run_cmd($cmd, {"RaiseError", 1, "Verbose", 1})) 0 0 0
149 0 my $msg = 'lvcreate failed. Could not create snapshot!';
150 0 $self->logger()->log( message => $msg, level => 'error' );
151 0 return;
152 }
153  
154 # set our lv object
155 0 $self->source()->vg()->update();
156 0 ▶ if ( $self->source()->vg()->lvs()->{ $self->name() } && $self->source()->vg()->lvs()->{ $self->name() }->isa('Linux::LVM2::LV') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source->vg->lvs->{$self->name} and $self->source->vg->lvs->{$self->name}->isa('Linux::LVM2::LV')) { } 0 0 0
Condition0%0/3
$self->source->vg->lvs->{$self->name} and $self->source->vg->lvs->{$self->name}->isa("Linux::LVM2::LV")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->source->vg->lvs->{$self->name} and $self->source->vg->lvs->{$self->name}->isa("Linux::LVM2::LV")
$self->source->vg->lvs->{$self->name} $self->source->vg->lvs->{$self->name}->isa("Linux::LVM2::LV")
Truth table0%0/3
A and B
A= $self->source->vg->lvs->{$self->name}B= $self->source->vg->lvs->{$self->name}->isa("Linux::LVM2::LV")
ABresult
0X0
100
111
157 0 $self->{'lv'} = $self->source()->vg()->lvs()->{ $self->name() };
158 0 $self->lv()->fs_type( $self->source()->fs_type() );
159 0 $self->lv()->fs_options( $self->source()->fs_options() );
160 0 return 1;
161 }
162 else {
163 0 my $msg = 'LV ' . $self->name() . ' not found!';
164 0 $self->logger()->log( message => $msg, level => 'error' );
165 0 return;
166 }
167 }
168  
169 0 ▶sub _init_name {
Subroutine _init_name: not called CC=3 SCAR=24.8
170 0 my $self = shift;
171  
172 # finds a free replisnapname
173 0 my $basename = 'replisnap';
174 0 my $try = 0;
175 0 ▶ while ( $self->parent()->is_lv( $self->source()->vg()->name(), $basename . $try ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent->is_lv($self->source->vg->name, $basename . $try)) 0 0 0
176 0 $try++;
177  
178 # safety guard
179 0 ▶ if ( $try > 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try > 1024) 0 0 0
180 0 my $msg = 'Could not find a free replisnap name within $try tries! Giving up.';
181 0 $self->logger()->log( message => $msg, level => 'error' );
182 0 return;
183 }
184 }
185  
186 # found an unused name for the
187 # snapshot
188 0 return $basename . $try;
189 }
190  
191 0 ▶sub mount {
Subroutine mount: not called CC=4 SCAR=30.0
Pod documented
192 0 my $self = shift;
193 0 my $mount_point = shift;
194  
195 0 ▶ if ( !$mount_point || !-d $mount_point ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mount_point and -d $mount_point) 0 0 0
Condition0%0/3
$mount_point and -d $mount_point
!ll&&!rl&&r
000
MC/DC0%0/2
$mount_point and -d $mount_point
$mount_point -d $mount_point
Truth table0%0/3
A and B
A= $mount_pointB= -d $mount_point
ABresult
0X0
100
111
196 0 $mount_point = File::Temp::tempdir( CLEANUP => 0 );
197 0 $self->_created_mount_point(1);
198 }
199  
200 0 ▶ if ( $self->fs()->mount( $self->full_path(), $mount_point, $self->lv()->fs_type(), 'ro,noatime', { Verbose => $self->verbose(), } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fs->mount($self->full_path, $mount_point, $self->lv->fs_type, 'ro,noatime', {'Verbose', $self->verbose})) { } 0 0 0
201 0 $self->{'mount_point'} = $mount_point;
202 0 return $mount_point;
203 }
204 else {
205 0 my $msg = 'Could not mount ' . $self->full_path() . ' at '.$mount_point;
206 0 $self->logger()->log( message => $msg, level => 'error' );
207 0 return;
208 }
209 }
210  
211 0 ▶sub umount {
Subroutine umount: not called CC=5 SCAR=34.0
Pod documented
212 0 my $self = shift;
213  
214 0 my $mounted_dev = $self->mapper_path();
215 0 ▶ if ( !$self->fs()->is_mounted($mounted_dev) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fs->is_mounted($mounted_dev)) { } 0 0 0
216 0 $mounted_dev = $self->full_path();
217 0 ▶ if ( !$self->fs()->is_mounted($mounted_dev) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fs->is_mounted($mounted_dev)) 0 0 0
218 0 my $msg = 'Tried to unmount snapshot (' . $self->full_path() . ') which does not appear to be mounted.';
219 0 $self->logger()->log( message => $msg, level => 'warning' );
220 }
221 }
222 else {
223 0 my $msg = 'Trying to unmount device ' . $mounted_dev;
224 0 $self->logger()->log( message => $msg, level => 'debug' );
225 0 ▶ if ( $self->fs()->umount( $mounted_dev, ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fs->umount($mounted_dev)) { } 0 0 0
226 0 ▶ if($self->_created_mount_point()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_created_mount_point) 0 0 0
227 0 $self->sys()->run_cmd( 'rm -rf ' . $self->mount_point() );
228 }
229 0 $msg = 'Unmounted snapshot ' . $mounted_dev . ' from ' . $self->mount_point();
230 0 $self->logger()->log( message => $msg, level => 'debug' );
231 0 return 1;
232 }
233 else {
234 0 $msg = 'Could not unmount ' . $mounted_dev;
235 0 $self->logger()->log( message => $msg, level => 'error' );
236 }
237 }
238  
239 0 return;
240 }
241  
242 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
243 0 my $self = shift;
244  
245 0 ▶ $self->umount()
Branch: 1 branch
BranchTrueFalseTotal
$self->umount or return 0 0 0
246 or return;
247  
248 # remove it
249 0 my $cmd = '/sbin/lvremove -f ' . $self->full_path();
250 0 ▶ if ( $self->sys()->run_cmd( $cmd, { Verbose => $self->verbose(), } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sys->run_cmd($cmd, {'Verbose', $self->verbose})) { } 0 0 0
251 0 my $msg = 'Removed snapshot LV ' . $self->full_path();
252 0 $self->logger()->log( message => $msg, level => 'debug' );
253 0 return 1;
254 }
255 else {
256 0 my $msg = 'Failed to remove snapshot LV ' . $self->full_path();
257 0 $self->logger()->log( message => $msg, level => 'debug' );
258 0 return;
259 }
260 }
261  
262 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264  
265 0 return $self->remove();
266 }
267  
268 0 ▶sub valid {
Subroutine valid: not called CC=1 SCAR=6.9
Pod documented
269 0 my $self = shift;
270 0 return $self->lv()->valid();
271 }
272  
273 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 2 __PACKAGE__->meta->make_immutable;
275  
276 2 1;
277  
278 =pod
279  
280 =encoding utf-8
281  
282 =head1 NAME
283  
284 Linux::LVM2::Snapshot - a class representing a LV snapshot in an Linux LVM2
285  
286 =head1 SYNOPSIS
287  
288 use Linux::LVM2::Snapshot;
289 my $Mod = Linux::LVM2::Snapshot::->new();
290  
291 =head1 DESCRIPTION
292  
293 This class models a snapshoted LV from an Linux LVM2 LV.
294  
295 =head1 ATTRIBUTES
296  
297 =head2 name
298  
299 The name of this snapshot LV
300  
301 =head2 logger
302  
303 An instance of Log::Tree
304  
305 =head2 lv
306  
307 The snapshot LV
308  
309 =head2 source
310  
311 The snapshoted LV
312  
313 =head2 parent
314  
315 Our parent, must be an instance of Linux::LVM2
316  
317 =head2 clear_caches
318  
319 UNDOCUMENTED
320  
321 =head2 snapspace
322  
323 Use this much GB for the snapshot
324  
325 =head2 mount_point
326  
327 UNDOCUMENTED
328  
329 =head2 verbose
330  
331 UNDOCUMENTED
332  
333 =head2 sys
334  
335 UNDOCUMENTED
336  
337 =head2 fs
338  
339 UNDOCUMENTED
340  
341 =head1 METHODS
342  
343 =head2 BUILD
344  
345 Invoked by Moose on instantiation. Create the snapshot.
346  
347 =head2 DEMOLISH
348  
349 Invoked by Moose on destruction. Removes the snapshot.
350  
351 =head2 full_path
352  
353 Return the full path to this LV.
354  
355 =head2 mapper_path
356  
357 Return the dev-mapper path to this LV.
358  
359 =head2 mount
360  
361 Try to mount this LV snapshot to the given mount point.
362  
363 =head2 remove
364  
365 Try to unmount this LV, if mounted, and remove the LV afterwards.
366  
367 =head2 umount
368  
369 Try to unmount this LV.
370  
371 =head2 valid
372  
373 Returns true unless the snapshot is 100% full.
374  
375 =head1 NAME
376  
377 Linux::LVM2::Snapshot - Model a Snapshot LV.
378  
379 =head1 AUTHOR
380  
381 Dominik Schulz <dominik.schulz@gauner.org>
382  
383 =head1 COPYRIGHT AND LICENSE
384  
385 This software is copyright (c) 2012 by Dominik Schulz.
386  
387 This is free software; you can redistribute it and/or modify it under
388 the same terms as the Perl 5 programming language system itself.
389  
390 =cut
391  
392 __END__
« LVM2/Utils.pm ↑ Summary LVM2/LV.pm »