2 # Copyright (c) 2009-2012 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 my $autolabel = getconf($CNF_AUTOLABEL);
123 if ($autolabel->{'non_amanda'}) {
124 print STDERR " contains a non-Amanda volume\n";
126 print STDERR " contains a non-Amanda volume; check and relabel it with 'amlabel -f'\n";
128 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_ERROR) {
129 my $message = $dev->error_or_status();
130 print STDERR " Can't read label: $message\n";
132 my $errmsg = $res->{device}->error_or_status();
133 print STDERR " $errmsg\n";
137 print STDERR "UNKNOWN\n";
142 my ($msg, $finished_cb) = @_;
143 print STDERR "ERROR: $msg\n";
149 my ($finished_cb) = @_;
150 my ($res, $label, $mode);
151 my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
152 my $tl = Amanda::Tapelist->new($tlf);
153 my $chg = Amanda::Changer->new(undef, tapelist => $tl);
154 return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
155 my $interactivity = Amanda::Interactivity->new(
156 name => getconf($CNF_INTERACTIVITY));
157 my $scan_name = getconf($CNF_TAPERSCAN);
158 my $taperscan = Amanda::Taper::Scan->new(algorithm => $scan_name,
160 interactivity => $interactivity,
163 my $steps = define_steps
164 cb_ref => \$finished_cb,
165 finalize => sub { $taperscan->quit(); };
169 result_cb => $steps->{'result_cb'},
170 user_msg_fn => \&_user_msg_fn
174 step result_cb => sub {
175 (my $err, $res, $label, $mode) = @_;
178 $res->release(finished_cb => sub {
179 return failure($err, $finished_cb);
183 return failure($err, $finished_cb);
187 my $modestr = ($mode == $ACCESS_APPEND)? "append" : "write";
188 my $slot = $res->{'this_slot'};
189 if (defined $res->{'device'} and defined $res->{'device'}->volume_label()) {
190 print "Will $modestr to volume '$label' in slot $slot.\n";
192 my $header = $res->{'device'}->volume_header();
193 if ($header->{'type'} == $Amanda::Header::F_WEIRD) {
194 print "Will $modestr label '$label' to non-Amanda volume in slot $slot.\n";
196 print "Will $modestr label '$label' to new volume in slot $slot.\n";
200 $steps->{'check_access_type'}->();
203 step check_access_type => sub {
204 my $mat = $res->{'device'}->property_get('medium_access_type');
205 if (defined $mat and $mat == $MEDIA_ACCESS_MODE_WRITE_ONLY) {
206 print "WARNING: Media access mode is WRITE_ONLY; dumps may not be recoverable\n";
209 if (getconf_seen($CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
210 my $dobs = getconf($CNF_DEVICE_OUTPUT_BUFFER_SIZE);
211 my $block_size = $res->{'device'}->property_get("BLOCK_SIZE");
212 if ($block_size * 2 > $dobs) {
213 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";
216 $steps->{'check_overwrite'}->();
219 step check_overwrite => sub {
220 # if we're not overwriting, just release the reservation
222 print "NOTE: skipping tape-writable test\n";
223 return $steps->{'release'}->();
226 if ($mode != $ACCESS_WRITE) {
227 my $modestr = Amanda::Device::DeviceAccessMode_to_string($mode);
228 print "NOTE: taperscan specified access mode $modestr; skipping volume-writeable test\n";
229 return $steps->{'release'}->();
232 print "Writing label '$label' to check writablility\n";
233 if (!$res->{'device'}->start($ACCESS_WRITE, $label, "X")) {
234 print "ERROR: writing to volume: " . $res->{'device'}->error_or_status(), "\n";
237 print "Volume '$label' is writeable.\n";
240 $steps->{'release'}->();
243 step release => sub {
244 $res->release(finished_cb => $steps->{'released'});
247 step released => sub {
249 return failure($err, $finished_cb) if $err;
255 Amanda::MainLoop::call_later(\&do_check, \&Amanda::MainLoop::quit);
256 Amanda::MainLoop::run();
257 Amanda::Util::finish_application();