File: Storage.pm

package info (click to toggle)
amanda 1%3A3.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,420 kB
  • sloc: ansic: 197,218; perl: 109,331; xml: 16,126; sh: 4,180; makefile: 2,810; awk: 502; lex: 407; yacc: 347; tcl: 118; sql: 19; sed: 16; php: 2
file content (245 lines) | stat: -rw-r--r-- 8,993 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright (c) 2012-2012 Zmanda, Inc.  All Rights Reserved.
# Copyright (c) 2013-2016 Carbonite, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Carbonite Inc., 756 N Pastoria Ave
# Sunnyvale, CA 94085, or: http://www.zmanda.com

package Amanda::Storage;

use strict;
use warnings;
use Data::Dumper;
use vars qw( @ISA );
use IPC::Open2;

use Amanda::Paths;
use Amanda::Util;
use Amanda::Config qw( :getconf );
use Amanda::Device qw( :constants );
use Amanda::Debug qw( debug );
use Amanda::MainLoop;
use Amanda::Policy;
use Amanda::Changer;
use Amanda::Paths;

=head1 NAME

Amanda::Storage -- interface to storage scripts

=head1 SYNOPSIS

    use Amanda::Storage;

    # load defaulf storage and changer
    my $storage = Amanda::Storage->new(tapelist => $tapelist);
    # load specific storage and changer
    my $storage = Amanda::Storage->new(storage_name => $storage,
				       changer_name => $changer,
				       tapelist     => $tapelist);
    my $chg = $storage->{'chg'};
    $storage->quit();

=head1 INTERFACE

All operations in the module return immediately, and take as an argument a
callback function which will indicate completion of the changer operation -- a
kind of continuation.  The caller should run a main loop (see
L<Amanda::MainLoop>) to allow the interactions with the changer script to
continue.

A new object is created with the C<new> function as follows:

  my $storage = Amanda::Storage->new(storage_name   => $storage_name,
				     changer_name   => $changer_name,
				     tapelist       => $tapelist,
				     labelstr       => $labelstr,
				     autolabel      => $autolabel,
				     meta_autolabel => $meta_autolabel);

to create a named storage (a name provided by the user, either specifying a
storage directly or specifying a storage definition).

If there is a problem creating the new object, then the result is a
Amanda::Changer::Error.

Thus the usual recipe for creating a new storage is

  my $storage = Amanda::Storage->new(tapelist => $tapelist);
  if ($storage->isa("Amanda::Changer::Error")) {
    die("Error creating storage: $sorage");
  }
  my $chg = $storage->{'chg'};
  if ($chg->isa("Amanda::Changer::Error")) {
    die("Error creating changer:  $chg");
  }

C<tapelist> must be an Amanda::Tapelist object. It is required if you want to
use $chg->volume_is_labelable(), $chg->make_new_tape_label(),
$chg->make_new_meta_label(), $res->make_new_tape_label() or
$res->make_new_meta_label().
C<labelstr> must be like getconf($CNF_LABELSTR), that value is used if C<labelstr> is not set.
C<autolabel> must be like getconf($CNF_AUTOLABEL), that value is used if C<autolabel> is not set.
C<meta_autolabel> must be like getconf($CNF_META_AUTOLABEL), that value is used if C<meta_autolabel> is not set.
=head2 MEMBER VARIABLES

Note that these variables are not set until after the subclass constructor is
finished.

=over 4

=item C<< $storage>{'storage_name'} >>

Gives the name of the storage.  This name will make sense to the user.
It should be used to describe the storage in messages to the user.

=cut

sub DESTROY {
    my $self = shift;

    debug("Storage '$self->{'storage_name'}' not quit") if defined $self->{'storage_name'};
}

# this is a "virtual" constructor which instantiates objects of different
# classes based on its argument.  Subclasses should not try to chain up!
sub new {
    my $class = shift;
    $class eq 'Amanda::Storage'
	or die("Do not call the Amanda::Storage constructor from subclasses");
    my %params = @_;
    my $storage_name = $params{'storage_name'};
    my $changer_name = $params{'changer_name'};
    my ($uri, $cc);

    if (defined $changer_name and !$changer_name) {
        return Amanda::Changer::Error->new('fatal',
		source_filename => __FILE__,
		source_line     => __LINE__,
		code            => 1150000,
		severity	=> $Amanda::Message::ERROR);
    }

    if (!defined $storage_name) {
	my $il = getconf($CNF_STORAGE);
	$storage_name = $il->[0];
    }

    my $self = undef;

    # Create a storage
    if (!$storage_name) {
	return Amanda::Changer::Error->new('fatal',
		source_filename => __FILE__,
		source_line     => __LINE__,
		code            => 1150001,
		severity	=> $Amanda::Message::ERROR);
    }
    my $st = Amanda::Config::lookup_storage($storage_name);
    if (!$st) {
	return Amanda::Changer::Error->new('fatal',
		source_filename => __FILE__,
		source_line     => __LINE__,
		code            => 1150002,
		severity	=> $Amanda::Message::ERROR,
		storage    => $storage_name);
    }

    my $tpchanger = storage_getconf($st, $STORAGE_TPCHANGER);
    if (!exists $params{'changer_name'}) {
	$changer_name = $tpchanger if !$changer_name;
	if (!$changer_name || $changer_name eq '') {
	    my $tapedev = storage_getconf($st, $STORAGE_TAPEDEV);
	    if (!$tapedev || $tapedev eq '') {
		return Amanda::Changer::Error->new('fatal',
		    source_filename => __FILE__,
		    source_line     => __LINE__,
		    code            => 1150003,
		    severity	    => $Amanda::Message::ERROR,
		    storage         => $storage_name);
	    }
	    $changer_name = $tapedev;
	}
    }

    $self = {
	storage_name   => $storage_name,
    };
    $self->{'labelstr'} = storage_getconf($st, $STORAGE_LABELSTR);
    $self->{'autolabel'} = storage_getconf($st, $STORAGE_AUTOLABEL);
    $self->{'meta_autolabel'} = storage_getconf($st, $STORAGE_META_AUTOLABEL);
    $self->{'tpchanger'} = $tpchanger;
    $self->{'runtapes'} = storage_getconf($st, $STORAGE_RUNTAPES);
    $self->{'taperscan_name'} = storage_getconf($st, $STORAGE_TAPERSCAN);
    $self->{'tapetype_name'} = storage_getconf($st, $STORAGE_TAPETYPE);
    $self->{'max_dle_by_volume'} = storage_getconf($st, $STORAGE_MAX_DLE_BY_VOLUME);
    $self->{'taperalgo'} = storage_getconf($st, $STORAGE_TAPERALGO);
    $self->{'taper_parallel_write'} = storage_getconf($st, $STORAGE_TAPER_PARALLEL_WRITE);
    $self->{'policy'} = Amanda::Policy->new(policy => storage_getconf($st, $STORAGE_POLICY));
    $self->{'tapepool'} = storage_getconf($st, $STORAGE_TAPEPOOL);
    $self->{'eject_volume'} = storage_getconf($st, $STORAGE_EJECT_VOLUME);
    $self->{'erase_volume'} = storage_getconf($st, $STORAGE_ERASE_VOLUME);
    $self->{'device_output_buffer_size'} = storage_getconf($st, $STORAGE_DEVICE_OUTPUT_BUFFER_SIZE);
    $self->{'seen_device_output_buffer_size'} = storage_seen($st, $STORAGE_DEVICE_OUTPUT_BUFFER_SIZE);
    $self->{'autoflush'} = storage_getconf($st, $STORAGE_AUTOFLUSH);
    $self->{'flush_threshold_dumped'} = storage_getconf($st, $STORAGE_FLUSH_THRESHOLD_DUMPED);
    $self->{'flush_threshold_scheduled'} = storage_getconf($st, $STORAGE_FLUSH_THRESHOLD_SCHEDULED);
    $self->{'taperflush'} = storage_getconf($st, $STORAGE_TAPERFLUSH);
    $self->{'report_use_media'} = storage_getconf($st, $STORAGE_REPORT_USE_MEDIA);
    $self->{'report_next_media'} = storage_getconf($st, $STORAGE_REPORT_NEXT_MEDIA);
    $self->{'interactivity'} = storage_getconf($st, $STORAGE_INTERACTIVITY);
    $self->{'set_no_reuse'} = storage_getconf($st, $STORAGE_SET_NO_REUSE);
    $self->{'dump_selection'} = storage_getconf($st, $STORAGE_DUMP_SELECTION);
    $self->{'erase_on_failure'} = storage_getconf($st, $STORAGE_ERASE_ON_FAILURE);
    $self->{'erase_on_full'} = storage_getconf($st, $STORAGE_ERASE_ON_FULL);
    bless $self, $class;

    $self->{'tapetype'} = lookup_tapetype($self->{'tapetype_name'});
    $self->{'chg'} = Amanda::Changer->new($changer_name, storage => $self,
					  tapelist => $params{'tapelist'},
					  no_validate => $params{'no_validate'})
				if defined $changer_name;
    return $self;

}

sub erase_no_retention {
    my $self = shift;

    my @command = ("$sbindir/amrmtape", Amanda::Config::get_config_name(), "--remove-no-retention", "--keep-label", "--erase", "-ostorage=$self->{'storage_name'}");
    debug("Running: " . join(' ', @command));
    my ($child_out, $child_in);
    my $pid = open2($child_out, $child_in, @command);
    close($child_in);
    while (<$child_out>) {
	debug($_);
    }
    close($child_out);
    waitpid($pid, 0);
}

sub quit {
    my $self = shift;

    delete $self->{'storage_name'};
    delete $self->{'labelstr'};
    delete $self->{'autolabel'};
    delete $self->{'tpchanger'};
    $self->{'chg'}->quit() if defined $self->{'chg'};
    delete $self->{'chg'};
}

1;