2 # Copyright (c) 2009, 2010 Zmanda Inc. All Rights Reserved.
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.
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
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
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
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 );
28 use Amanda::Device qw( :constants );
31 use Amanda::Taper::Scan;
32 use Amanda::Interactivity;
35 Amanda::Util::setup_application("amcheck-device", "server", $CONTEXT_CMDLINE);
37 my $config_overrides = new_config_overrides($#ARGV+1);
39 Getopt::Long::Configure(qw{bundling});
41 'version' => \&Amanda::Util::version_opt,
42 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
47 print STDERR "USAGE: amcheck-device <config> [-w] <config-overwrites>";
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";
66 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
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'}) {
75 if ($params{'expected_label'}) {
76 push @exp, "volume '$params{expected_label}'";
78 if ($params{'expected_new'}) {
79 push @exp, "a new volume";
81 my $exp = join(" or ", @exp);
82 print STDERR " (expecting $exp)\n";
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'}:";
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'}:";
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"
113 print STDERR " volume '$volume_label'\n";
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";
127 my $errmsg = $res->{device}->error_or_status();
128 print STDERR " $errmsg\n";
132 print STDERR "UNKNOWN\n";
137 my ($msg, $finished_cb) = @_;
138 print STDERR "ERROR: $msg\n";
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,
155 interactivity => $interactivity,
158 my $steps = define_steps
159 cb_ref => \$finished_cb,
160 finalize => sub { $taperscan->quit(); };
164 result_cb => $steps->{'result_cb'},
165 user_msg_fn => \&_user_msg_fn
169 step result_cb => sub {
170 (my $err, $res, $label, $mode) = @_;
173 $res->release(finished_cb => sub {
174 return failure($err, $finished_cb);
178 return failure($err, $finished_cb);
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";
187 print "Will $modestr label '$label' to new volume in slot $slot.\n";
190 $steps->{'check_access_type'}->();
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";
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";
206 $steps->{'check_overwrite'}->();
209 step check_overwrite => sub {
210 # if we're not overwriting, just release the reservation
212 print "NOTE: skipping tape-writable test\n";
213 return $steps->{'release'}->();
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'}->();
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";
227 print "Volume '$label' is writeable.\n";
230 $steps->{'release'}->();
233 step release => sub {
234 $res->release(finished_cb => $steps->{'released'});
237 step released => sub {
239 return failure($err, $finished_cb) if $err;
245 Amanda::MainLoop::call_later(\&do_check, \&Amanda::MainLoop::quit);
246 Amanda::MainLoop::run();
247 Amanda::Util::finish_application();