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;
34 Amanda::Util::setup_application("amcheck-device", "server", $CONTEXT_CMDLINE);
36 my $config_overrides = new_config_overrides($#ARGV+1);
38 Getopt::Long::Configure(qw{bundling});
40 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
45 print STDERR "USAGE: amcheck-device <config> [-w] <config-overwrites>";
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";
64 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
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'}) {
73 if ($params{'expected_label'}) {
74 push @exp, "volume '$params{expected_label}'";
76 if ($params{'expected_new'}) {
77 push @exp, "a new volume";
79 my $exp = join(" or ", @exp);
80 print STDERR " (expecting $exp)\n";
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'}:";
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'}:";
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"
111 print STDERR " volume '$volume_label'\n";
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";
125 my $errmsg = $res->{device}->error_or_status();
126 print STDERR " $errmsg\n";
130 print STDERR "UNKNOWN\n";
135 my ($msg, $finished_cb) = @_;
136 print STDERR "ERROR: $msg\n";
142 my ($finished_cb) = @_;
143 my ($res, $label, $mode);
145 my $steps = define_steps
146 cb_ref => \$finished_cb;
149 my $chg = Amanda::Changer->new();
151 return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
153 my $taperscan = Amanda::Taper::Scan->new(changer => $chg);
155 result_cb => $steps->{'result_cb'},
156 user_msg_fn => \&_user_msg_fn
160 step result_cb => sub {
161 (my $err, $res, $label, $mode) = @_;
162 return failure($err, $finished_cb) if $err;
164 if (defined $res->{'device'}->volume_label()) {
165 $res->set_label(label => $res->{'device'}->volume_label(),
166 finished_cb => $steps->{'set_labeled'});
168 $steps->{'set_labeled'}->(undef);
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";
178 print "Will $modestr label '$label' to new volume in slot $slot.\n";
181 $steps->{'check_access_type'}->();
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";
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";
197 $steps->{'check_overwrite'}->();
200 step check_overwrite => sub {
201 # if we're not overwriting, just release the reservation
203 print "NOTE: skipping tape-writable test\n";
204 return $steps->{'release'}->();
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'}->();
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";
218 print "Volume '$label' is writeable.\n";
221 $steps->{'release'}->();
224 step release => sub {
225 $res->release(finished_cb => $steps->{'released'});
228 step released => sub {
230 return failure($err, $finished_cb) if $err;
236 Amanda::MainLoop::call_later(\&do_check, \&Amanda::MainLoop::quit);
237 Amanda::MainLoop::run();
238 Amanda::Util::finish_application();