Imported Upstream version 3.2.1
[debian/amanda] / server-src / amcheck-device.pl
1 #! @PERL@
2 # Copyright (c) 2009, 2010 Zmanda Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use lib '@amperldir@';
21 use strict;
22 use warnings;
23
24 use Amanda::Util qw( :constants );
25 use Amanda::Config qw( :init :getconf );
26 use Amanda::Logfile qw( :logtype_t log_add $amanda_log_trace_log );
27 use Amanda::Debug;
28 use Amanda::Device qw( :constants );
29 use Amanda::MainLoop;
30 use Amanda::Changer;
31 use Amanda::Taper::Scan;
32 use Getopt::Long;
33
34 Amanda::Util::setup_application("amcheck-device", "server", $CONTEXT_CMDLINE);
35
36 my $config_overrides = new_config_overrides($#ARGV+1);
37 my $overwrite = 0;
38 Getopt::Long::Configure(qw{bundling});
39 GetOptions(
40     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
41     'w' => \$overwrite,
42 ) or usage();
43
44 sub usage {
45     print STDERR "USAGE: amcheck-device <config> [-w] <config-overwrites>";
46     exit 1;
47 }
48
49 if (@ARGV != 1) {
50     usage();
51 }
52
53 set_config_overrides($config_overrides);
54 config_init($CONFIG_INIT_EXPLICIT_NAME, $ARGV[0]);
55 my ($cfgerr_level, @cfgerr_errors) = config_errors();
56 if ($cfgerr_level >= $CFGERR_WARNINGS) {
57     config_print_errors();
58     if ($cfgerr_level >= $CFGERR_ERRORS) {
59         print STDERR "Errors processing config file";
60         exit 1;
61     }
62 }
63
64 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
65 my $exit_status = 0;
66
67 sub _user_msg_fn {
68         my %params = @_;
69         if (exists $params{'scan_failed'}) {
70             print STDERR "Taper scan algorithm did not find an acceptable volume.\n";
71             if ($params{'expected_label'} or $params{'expected_new'}) {
72                 my @exp;
73                 if ($params{'expected_label'}) {
74                     push @exp, "volume '$params{expected_label}'";
75                 }
76                 if ($params{'expected_new'}) {
77                     push @exp, "a new volume";
78                 }
79                 my $exp = join(" or ", @exp);
80                 print STDERR "    (expecting $exp)\n";
81             }
82         } elsif (exists($params{'text'})) {
83             print STDERR "$params{'text'}\n";
84         } elsif (exists($params{'scan_slot'})) {
85             print STDERR "slot $params{'slot'}:";
86         } elsif (exists($params{'search_label'})) {
87             print STDERR "Searching for label '$params{'label'}':";
88         } elsif (exists($params{'slot_result'}) ||
89                  exists($params{'search_result'})) {
90             if (defined($params{'err'})) {
91                 if (exists($params{'search_result'}) &&
92                     defined($params{'err'}->{'this_slot'})) {
93                     print STDERR "slot $params{'err'}->{'this_slot'}:";
94                 }
95                 print STDERR " $params{'err'}\n";
96             } else { # res must be defined
97                 my $res = $params{'res'};
98                 my $dev = $res->{'device'};
99                 if (exists($params{'search_result'})) {
100                     print STDERR "found in slot $res->{'this_slot'}:";
101                 }
102                 if ($dev->status == $DEVICE_STATUS_SUCCESS) {
103                     my $volume_label = $res->{device}->volume_label;
104                     if ($params{'active'}) {
105                         print STDERR " volume '$volume_label' is still active and cannot be overwritten\n";
106                     } elsif ($params{'does_not_match_labelstr'}) {
107                         print STDERR " volume '$volume_label' does not match labelstr '$params{'labelstr'}'\n";
108                     } elsif ($params{'not_in_tapelist'}) {
109                         print STDERR " volume '$volume_label' is not in the tapelist\n"
110                     } else {
111                         print STDERR " volume '$volume_label'\n";
112                     }
113                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED and
114                          $dev->volume_header and
115                          $dev->volume_header->{'type'} == $Amanda::Header::F_EMPTY) {
116                     print STDERR " contains an empty volume\n";
117                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED and
118                          $dev->volume_header and
119                          $dev->volume_header->{'type'} == $Amanda::Header::F_WEIRD) {
120                     print STDERR " contains a non-Amanda volume; check and relabel it with 'amlabel -f'\n";
121                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_ERROR) {
122                     my $message = $dev->error_or_status();
123                     print STDERR " Can't read label: $message\n";
124                 } else {
125                     my $errmsg = $res->{device}->error_or_status();
126                     print STDERR " $errmsg\n";
127                 }
128             }
129         } else {
130             print STDERR "UNKNOWN\n";
131         }
132 }
133
134 sub failure {
135     my ($msg, $finished_cb) = @_;
136     print STDERR "ERROR: $msg\n";
137     $exit_status = 1;
138     $finished_cb->();
139 }
140
141 sub do_check {
142     my ($finished_cb) = @_;
143     my ($res, $label, $mode);
144
145     my $steps = define_steps
146         cb_ref => \$finished_cb;
147
148     step start => sub {
149         my $chg = Amanda::Changer->new();
150
151         return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
152
153         my $taperscan = Amanda::Taper::Scan->new(changer => $chg);
154         $taperscan->scan(
155             result_cb => $steps->{'result_cb'},
156             user_msg_fn => \&_user_msg_fn
157         );
158     };
159
160     step result_cb => sub {
161         (my $err, $res, $label, $mode) = @_;
162         return failure($err, $finished_cb) if $err;
163
164         if (defined $res->{'device'}->volume_label()) {
165             $res->set_label(label => $res->{'device'}->volume_label(),
166                             finished_cb => $steps->{'set_labeled'});
167         } else {
168             $steps->{'set_labeled'}->(undef);
169         };
170     };
171
172     step set_labeled => sub {
173         my $modestr = ($mode == $ACCESS_APPEND)? "append" : "write";
174         my $slot = $res->{'this_slot'};
175         if (defined $res->{'device'}->volume_label()) {
176             print "Will $modestr to volume '$label' in slot $slot.\n";
177         } else {
178             print "Will $modestr label '$label' to new volume in slot $slot.\n";
179         }
180
181         $steps->{'check_access_type'}->();
182     };
183
184     step check_access_type => sub {
185         my $mat = $res->{'device'}->property_get('medium_access_type');
186         if (defined $mat and $mat == $MEDIA_ACCESS_MODE_WRITE_ONLY) {
187             print "WARNING: Media access mode is WRITE_ONLY; dumps may not be recoverable\n";
188         }
189
190         if (getconf_seen($CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
191             my $dobs = getconf($CNF_DEVICE_OUTPUT_BUFFER_SIZE);
192             my $block_size = $res->{'device'}->property_get("BLOCK_SIZE");
193             if ($block_size * 2 > $dobs) {
194                 print "WARNING: DEVICE-OUTPUT-BUFFER-SIZE is not at least twice the block size of the device, it should be increased for better throughput\n";
195             }
196         }
197         $steps->{'check_overwrite'}->();
198     };
199
200     step check_overwrite => sub {
201         # if we're not overwriting, just release the reservation
202         if (!$overwrite) {
203             print "NOTE: skipping tape-writable test\n";
204             return $steps->{'release'}->();
205         }
206
207         if ($mode != $ACCESS_WRITE) {
208             my $modestr = Amanda::Device::DeviceAccessMode_to_string($mode);
209             print "NOTE: taperscan specified access mode $modestr; skipping volume-writeable test\n";
210             return $steps->{'release'}->();
211         }
212
213         print "Writing label '$label' to check writablility\n";
214         if (!$res->{'device'}->start($ACCESS_WRITE, $label, "X")) {
215             print "ERROR: writing to volume: " . $res->{'device'}->error_or_status(), "\n";
216             $exit_status = 1;
217         } else {
218             print "Volume '$label' is writeable.\n";
219         }
220
221         $steps->{'release'}->();
222     };
223
224     step release => sub {
225         $res->release(finished_cb => $steps->{'released'});
226     };
227
228     step released => sub {
229         my ($err) = @_;
230         return failure($err, $finished_cb) if $err;
231
232         $finished_cb->();
233     };
234 }
235
236 Amanda::MainLoop::call_later(\&do_check, \&Amanda::MainLoop::quit);
237 Amanda::MainLoop::run();
238 Amanda::Util::finish_application();
239 exit($exit_status);