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@';
28 use Amanda::Device qw( :constants );
29 use Amanda::Debug qw( :logging );
30 use Amanda::Config qw( :init :getconf config_dir_relative );
31 use Amanda::Util qw( :constants );
33 use Amanda::Header qw( :constants );
45 print STDERR "Usage: amlabel [--barcode <barcode>] [--meta <meta>] [--assign] [--version]\n"
46 . " [-f] [-o configoption]* <conf> [<label>] [slot <slot-number>]\n";
50 Amanda::Util::setup_application("amlabel", "server", $CONTEXT_CMDLINE);
52 my $config_overrides = new_config_overrides($#ARGV+1);
53 my ($opt_force, $opt_config, $opt_slot, $opt_label);
54 my ($opt_barcode, $opt_meta, $opt_assign);
61 debug("Arguments: " . join(' ', @ARGV));
62 Getopt::Long::Configure(qw(bundling));
64 'version' => \&Amanda::Util::version_opt,
65 'help|usage|?' => \&usage,
66 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
68 'barcode=s' => \$opt_barcode,
69 'meta=s' => \$opt_meta,
70 'assign' => \$opt_assign,
71 'version' => \&Amanda::Util::version_opt,
74 if ($opt_assign && (!$opt_meta and !$opt_barcode)) {
75 print STDERR "--assign require --barcode or --meta\n";
79 usage() if @ARGV == 0;
80 $opt_config = $ARGV[0];
84 } elsif (@ARGV == 2) {
86 $opt_label = $ARGV[1];
87 } elsif (@ARGV == 3 and $ARGV[1] eq 'slot') {
90 } elsif (@ARGV == 4 and $ARGV[2] eq 'slot') {
92 $opt_label = $ARGV[1];
97 set_config_overrides($config_overrides);
98 config_init($CONFIG_INIT_EXPLICIT_NAME, $opt_config);
99 my ($cfgerr_level, @cfgerr_errors) = config_errors();
100 if ($cfgerr_level >= $CFGERR_WARNINGS) {
101 config_print_errors();
102 if ($cfgerr_level >= $CFGERR_ERRORS) {
103 print STDERR "errors processing config file";
108 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
110 my ($tlf, $tl, $res);
113 my ($msg, $finished_cb) = @_;
114 print STDERR "$msg\n";
117 $res->release(finished_cb => sub {
127 my ($finished_cb) = @_;
133 my $steps = define_steps
134 cb_ref => \$finished_cb,
135 finalize => sub { $chg->quit() if defined $chg };
138 my $labelstr = getconf($CNF_LABELSTR);
139 if (defined ($opt_label) && $opt_label !~ /$labelstr/) {
140 return failure("Label '$opt_label' doesn't match labelstr '$labelstr'.", $finished_cb);
143 $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
144 $tl = Amanda::Tapelist->new($tlf);
146 return failure("Can't load tapelist file ($tlf)", $finished_cb);
149 $chg = Amanda::Changer->new(undef, tapelist => $tl);
151 return failure($chg, $finished_cb)
152 if $chg->isa("Amanda::Changer::Error");
155 return $steps->{'assign'}->();
158 if (defined($opt_label) && !$opt_force) {
159 if ($tl->lookup_tapelabel($opt_label)) {
160 return failure("Label '$opt_label' already on a volume", $finished_cb);
164 $steps->{'load'}->();
168 print "Reading label...\n";
170 $chg->load(slot => $opt_slot, mode => "write",
171 res_cb => $steps->{'loaded'});
172 } elsif ($opt_barcode) {
173 $chg->inventory(inventory_cb => $steps->{'inventory'});
175 $chg->load(relative_slot => "current", mode => "write",
176 res_cb => $steps->{'loaded'});
180 step inventory => sub {
181 my ($err, $inv) = @_;
183 return failure($err, $finished_cb) if $err;
186 if ($sl->{'barcode'} eq $opt_barcode) {
187 return $chg->load(slot => $sl->{'slot'}, mode => "write",
188 res_cb => $steps->{'loaded'});
192 return failure("No volume with barcode '$opt_barcode' available", $finished_cb);
196 (my $err, $res) = @_;
198 return failure($err, $finished_cb) if $err;
200 if (defined $opt_slot && defined $opt_barcode &&
201 $opt_barcode ne $res->{'barcode'}) {
202 if (defined $res->{'barcode'}) {
203 return failure("Volume in slot $opt_slot have barcode '$res->{'barcode'}, it is not '$opt_barcode'", $finished_cb);
205 return failure("Volume in slot $opt_slot have no barcode", $finished_cb);
208 $dev = $res->{'device'};
210 if ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED) {
211 if (!$dev->volume_header or $dev->volume_header->{'type'} == $F_EMPTY) {
212 print "Found an empty tape.\n";
214 # force is required for non-Amanda tapes
215 print "Found a non-Amanda tape.\n";
216 $dev_ok = 0 unless ($opt_force);
218 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_ERROR) {
219 # it's OK to force through VOLUME_ERROR
220 print "Error reading volume label: " . $dev->error_or_status(), "\n";
221 $dev_ok = 0 unless ($opt_force);
222 } elsif ($dev->status != $DEVICE_STATUS_SUCCESS) {
223 # but anything else is fatal
224 print "Error reading volume label: " . $dev->error_or_status(), "\n";
227 # this is a labeled Amanda tape
228 my $label = $dev->volume_label;
229 my $labelstr = getconf($CNF_LABELSTR);
231 if ($label !~ /$labelstr/) {
232 print "Found label '$label', but it is not from configuration " .
233 "'" . Amanda::Config::get_config_name() . "'.\n";
234 $dev_ok = 0 unless ($opt_force);
235 } elsif ($tl->lookup_tapelabel($label)) {
236 print "Volume with label '$label' is active and contains data from this configuration.\n";
238 # if -f, then the user should clean things up..
239 print "Consider using 'amrmtape' to remove volume '$label' from the catalog.\n";
240 # note that we don't run amrmtape automatically, as it could result in data loss when
241 # multiple volumes have (perhaps accidentally) the same label
246 print "Found Amanda volume '$label'.\n";
250 $res->get_meta_label(finished_cb => $steps->{'got_meta'});
253 step got_meta => sub {
254 my ($err, $meta) = @_;
256 if (defined $meta && defined $opt_meta && $meta ne $opt_meta) {
259 $meta = $opt_meta if !defined $meta;
260 ($meta, my $merr) = $res->make_new_meta_label() if !defined $meta;
262 return failure($merr, $finished_cb);
266 my $label = $opt_label;
267 if (!defined($label)) {
268 ($label, my $lerr) = $res->make_new_tape_label(meta => $meta);
270 return failure($lerr, $finished_cb);
275 print "Writing label '$label'...\n";
277 if (!$dev->start($ACCESS_WRITE, $label, "X")) {
278 return failure("Error writing label: " . $dev->error_or_status(), $finished_cb);
279 } elsif (!$dev->finish()) {
280 return failure("Error finishing device: " . $dev->error_or_status(), $finished_cb);
283 print "Checking label...\n";
284 my $status = $dev->read_label();
285 if ($status != $DEVICE_STATUS_SUCCESS) {
286 return failure("Checking the tape label failed: " . $dev->error_or_status(),
288 } elsif (!$dev->volume_label) {
289 return failure("No label found.", $finished_cb);
290 } elsif ($dev->volume_label ne $label) {
291 my $got = $dev->volume_label;
292 return failure("Read back a different label: got '$got', but expected '$label'",
294 } elsif ($dev->volume_time ne "X") {
295 my $got = $dev->volume_time;
296 return failure("Read back a different timetstamp: got '$got', but expected 'X'",
300 # update the tapelist
302 $tl->remove_tapelabel($label);
303 $tl->add_tapelabel("0", $label, undef, 1, $meta, $res->{'barcode'}, $dev->block_size/1024);
309 $res->set_label(label => $label, finished_cb => $steps->{'set_meta_label'});
311 return failure("Not writing label.", $finished_cb);
315 step set_meta_label => sub {
319 return $res->set_meta_label(meta => $opt_meta,
320 finished_cb => $steps->{'labeled'});
322 return $steps->{'labeled'}->();
326 step labeled => sub {
328 $gerr = $err if !$gerr;
330 $res->release(finished_cb => $steps->{'released'});
333 step released => sub {
335 return failure($gerr, $finished_cb) if $gerr;
336 return failure($err, $finished_cb) if $err;
343 $tle = $tl->lookup_tapelabel($opt_label);
345 my $meta = $opt_meta;
347 if (defined($tle->{'meta'}) && $meta ne $tle->{'meta'} &&
349 return failure("Can't change meta-label with --force, old meta-label is '$tle->{'meta'}'");
352 $meta = $tle->{'meta'};
354 my $barcode = $opt_barcode;
355 if (defined $barcode) {
356 if (defined($tle->{'barcode'}) &&
357 $barcode ne $tle->{'barcode'} &&
359 return failure("Can't change barcode with --force, old barcode is '$tle->{'barcode'}'");
362 $barcode = $tle->{'barcode'};
366 $tl->remove_tapelabel($opt_label);
367 $tl->add_tapelabel($tle->{'datestamp'}, $tle->{'label'},
368 $tle->{'comment'}, $tle->{'reuse'}, $meta,
372 return failure("Label '$opt_label' is not in the tapelist file", $finished_cb);
375 $chg->inventory(inventory_cb => $steps->{'assign_inventory'});
378 step assign_inventory => sub {
379 my ($err, $inv) = @_;
382 return $finished_cb->() if $err->notimpl;
383 return failure($err, $finished_cb);
387 if (defined $sl->{'label'} && $sl->{'label'} eq $opt_label) {
388 return $chg->set_meta_label(meta => $opt_meta,
389 slot => $sl->{'slot'},
390 finished_cb => $steps->{'done'});
401 main(\&Amanda::MainLoop::quit);
402 Amanda::MainLoop::run();
403 Amanda::Util::finish_application();