3a72bbe0053a0296889163fc22edb45fc589dc91
[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 Amanda::Interactivity;
33 use Getopt::Long;
34
35 Amanda::Util::setup_application("amcheck-device", "server", $CONTEXT_CMDLINE);
36
37 my $config_overrides = new_config_overrides($#ARGV+1);
38 my $overwrite = 0;
39 Getopt::Long::Configure(qw{bundling});
40 GetOptions(
41     'version' => \&Amanda::Util::version_opt,
42     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
43     'w' => \$overwrite,
44 ) or usage();
45
46 sub usage {
47     print STDERR "USAGE: amcheck-device <config> [-w] <config-overwrites>";
48     exit 1;
49 }
50
51 if (@ARGV != 1) {
52     usage();
53 }
54
55 set_config_overrides($config_overrides);
56 config_init($CONFIG_INIT_EXPLICIT_NAME, $ARGV[0]);
57 my ($cfgerr_level, @cfgerr_errors) = config_errors();
58 if ($cfgerr_level >= $CFGERR_WARNINGS) {
59     config_print_errors();
60     if ($cfgerr_level >= $CFGERR_ERRORS) {
61         print STDERR "Errors processing config file";
62         exit 1;
63     }
64 }
65
66 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
67 my $exit_status = 0;
68
69 sub _user_msg_fn {
70         my %params = @_;
71         if (exists $params{'scan_failed'}) {
72             print STDERR "Taper scan algorithm did not find an acceptable volume.\n";
73             if ($params{'expected_label'} or $params{'expected_new'}) {
74                 my @exp;
75                 if ($params{'expected_label'}) {
76                     push @exp, "volume '$params{expected_label}'";
77                 }
78                 if ($params{'expected_new'}) {
79                     push @exp, "a new volume";
80                 }
81                 my $exp = join(" or ", @exp);
82                 print STDERR "    (expecting $exp)\n";
83             }
84         } elsif (exists($params{'text'})) {
85             print STDERR "$params{'text'}\n";
86         } elsif (exists($params{'scan_slot'})) {
87             print STDERR "slot $params{'slot'}:";
88         } elsif (exists($params{'search_label'})) {
89             print STDERR "Searching for label '$params{'label'}':";
90         } elsif (exists($params{'slot_result'}) ||
91                  exists($params{'search_result'})) {
92             if (defined($params{'err'})) {
93                 if (exists($params{'search_result'}) &&
94                     defined($params{'err'}->{'this_slot'})) {
95                     print STDERR "slot $params{'err'}->{'this_slot'}:";
96                 }
97                 print STDERR " $params{'err'}\n";
98             } else { # res must be defined
99                 my $res = $params{'res'};
100                 my $dev = $res->{'device'};
101                 if (exists($params{'search_result'})) {
102                     print STDERR "found in slot $res->{'this_slot'}:";
103                 }
104                 if ($dev->status == $DEVICE_STATUS_SUCCESS) {
105                     my $volume_label = $res->{device}->volume_label;
106                     if ($params{'active'}) {
107                         print STDERR " volume '$volume_label' is still active and cannot be overwritten\n";
108                     } elsif ($params{'does_not_match_labelstr'}) {
109                         print STDERR " volume '$volume_label' does not match labelstr '$params{'labelstr'}'\n";
110                     } elsif ($params{'not_in_tapelist'}) {
111                         print STDERR " volume '$volume_label' is not in the tapelist\n"
112                     } else {
113                         print STDERR " volume '$volume_label'\n";
114                     }
115                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED and
116                          $dev->volume_header and
117                          $dev->volume_header->{'type'} == $Amanda::Header::F_EMPTY) {
118                     print STDERR " contains an empty volume\n";
119                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED and
120                          $dev->volume_header and
121                          $dev->volume_header->{'type'} == $Amanda::Header::F_WEIRD) {
122                     print STDERR " contains a non-Amanda volume; check and relabel it with 'amlabel -f'\n";
123                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_ERROR) {
124                     my $message = $dev->error_or_status();
125                     print STDERR " Can't read label: $message\n";
126                 } else {
127                     my $errmsg = $res->{device}->error_or_status();
128                     print STDERR " $errmsg\n";
129                 }
130             }
131         } else {
132             print STDERR "UNKNOWN\n";
133         }
134 }
135
136 sub failure {
137     my ($msg, $finished_cb) = @_;
138     print STDERR "ERROR: $msg\n";
139     $exit_status = 1;
140     $finished_cb->();
141 }
142
143 sub do_check {
144     my ($finished_cb) = @_;
145     my ($res, $label, $mode);
146     my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
147     my $tl = Amanda::Tapelist->new($tlf);
148     my $chg = Amanda::Changer->new(undef, tapelist => $tl);
149     return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
150     my $interactivity = Amanda::Interactivity->new(
151                                         name => getconf($CNF_INTERACTIVITY));
152     my $scan_name = getconf($CNF_TAPERSCAN);
153     my $taperscan = Amanda::Taper::Scan->new(algorithm => $scan_name,
154                                              changer => $chg,
155                                              interactivity => $interactivity,
156                                              tapelist => $tl);
157
158     my $steps = define_steps
159         cb_ref => \$finished_cb,
160         finalize => sub { $taperscan->quit(); };
161
162     step start => sub {
163         $taperscan->scan(
164             result_cb => $steps->{'result_cb'},
165             user_msg_fn => \&_user_msg_fn
166         );
167     };
168
169     step result_cb => sub {
170         (my $err, $res, $label, $mode) = @_;
171         if ($err) {
172             if ($res) {
173                 $res->release(finished_cb => sub {
174                     return failure($err, $finished_cb);
175                 });
176                 return;
177             } else {
178                 return failure($err, $finished_cb);
179             }
180         }
181
182         my $modestr = ($mode == $ACCESS_APPEND)? "append" : "write";
183         my $slot = $res->{'this_slot'};
184         if (defined $res->{'device'} and defined $res->{'device'}->volume_label()) {
185             print "Will $modestr to volume '$label' in slot $slot.\n";
186         } else {
187             print "Will $modestr label '$label' to new volume in slot $slot.\n";
188         }
189
190         $steps->{'check_access_type'}->();
191     };
192
193     step check_access_type => sub {
194         my $mat = $res->{'device'}->property_get('medium_access_type');
195         if (defined $mat and $mat == $MEDIA_ACCESS_MODE_WRITE_ONLY) {
196             print "WARNING: Media access mode is WRITE_ONLY; dumps may not be recoverable\n";
197         }
198
199         if (getconf_seen($CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
200             my $dobs = getconf($CNF_DEVICE_OUTPUT_BUFFER_SIZE);
201             my $block_size = $res->{'device'}->property_get("BLOCK_SIZE");
202             if ($block_size * 2 > $dobs) {
203                 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";
204             }
205         }
206         $steps->{'check_overwrite'}->();
207     };
208
209     step check_overwrite => sub {
210         # if we're not overwriting, just release the reservation
211         if (!$overwrite) {
212             print "NOTE: skipping tape-writable test\n";
213             return $steps->{'release'}->();
214         }
215
216         if ($mode != $ACCESS_WRITE) {
217             my $modestr = Amanda::Device::DeviceAccessMode_to_string($mode);
218             print "NOTE: taperscan specified access mode $modestr; skipping volume-writeable test\n";
219             return $steps->{'release'}->();
220         }
221
222         print "Writing label '$label' to check writablility\n";
223         if (!$res->{'device'}->start($ACCESS_WRITE, $label, "X")) {
224             print "ERROR: writing to volume: " . $res->{'device'}->error_or_status(), "\n";
225             $exit_status = 1;
226         } else {
227             print "Volume '$label' is writeable.\n";
228         }
229
230         $steps->{'release'}->();
231     };
232
233     step release => sub {
234         $res->release(finished_cb => $steps->{'released'});
235     };
236
237     step released => sub {
238         my ($err) = @_;
239         return failure($err, $finished_cb) if $err;
240
241         $finished_cb->();
242     };
243 }
244
245 Amanda::MainLoop::call_later(\&do_check, \&Amanda::MainLoop::quit);
246 Amanda::MainLoop::run();
247 Amanda::Util::finish_application();
248 exit($exit_status);