1 # Copyright (c) 2008,2009,2010 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19 package Amanda::Changer::single;
24 @ISA = qw( Amanda::Changer );
26 use File::Glob qw( :glob );
28 use Amanda::Config qw( :getconf );
32 use Amanda::Device qw( :constants );
36 Amanda::Changer::single
40 This changer represents a single drive as a changer. It may eventually morph
41 into something similar to the old C<chg-manual>.
43 Whatever you load, you get the volume in the drive. The volume's either
44 reserved or not. All pretty straightforward.
46 See the amanda-changers(7) manpage for usage information.
52 my ($config, $tpchanger) = @_;
53 my ($device_name) = ($tpchanger =~ /chg-single:(.*)/);
55 # check that $device_name is an honest-to-goodness device
56 my $tmpdev = Amanda::Device->new($device_name);
57 if ($tmpdev->status() != $DEVICE_STATUS_SUCCESS) {
58 return Amanda::Changer->make_error("fatal", undef,
59 message => "chg-single: error opening device '$device_name': " .
60 $tmpdev->error_or_status());
65 device_name => $device_name,
69 bless ($self, $class);
76 $self->validate_params('load', \%params);
78 return if $self->check_error($params{'res_cb'});
80 die "no res_cb supplied" unless (exists $params{'res_cb'});
82 if ($self->{'reserved'}) {
83 return $self->make_error("failed", $params{'res_cb'},
85 message => "'$self->{device_name}' is already reserved");
88 if (keys %{$params{'except_slots'}} > 0) {
89 return $self->make_error("failed", $params{'res_cb'},
91 message => "all slots have been loaded");
94 my $device = Amanda::Device->new($self->{'device_name'});
95 if ($device->status() != $DEVICE_STATUS_SUCCESS) {
96 return $self->make_error("fatal", $params{'res_cb'},
97 message => "error opening device '$self->{device_name}': " . $device->error_or_status());
100 if (my $msg = $self->{'config'}->configure_device($device)) {
101 # a failure to configure a device is fatal, since it's probably
102 # a user configuration error (and thus unlikely to work for the
103 # next device, either)
104 return $self->make_error("fatal", $params{'res_cb'},
108 my $res = Amanda::Changer::single::Reservation->new($self, $device);
109 $device->read_label();
110 $params{'res_cb'}->(undef, $res);
120 device_status => undef,
125 $params{'inventory_cb'}->(undef, \@inventory);
130 my ($key, %params) = @_;
133 return if $self->check_error($params{'info_cb'});
135 if ($key eq 'num_slots') {
137 } elsif ($key eq 'fast_search') {
138 # (asking the user for a specific label is faster than asking
139 # for each "slot" in a sequential scan, so search is "fast")
143 $params{'info_cb'}->(undef, %results) if $params{'info_cb'};
146 package Amanda::Changer::single::Reservation;
148 @ISA = qw( Amanda::Changer::Reservation );
152 my ($chg, $device) = @_;
153 my $self = Amanda::Changer::Reservation::new($class);
155 $self->{'chg'} = $chg;
157 $self->{'device'} = $device;
159 $self->{'this_slot'} = '1';
160 $chg->{'reserved'} = 1;
169 $self->{'device'}->eject() if (exists $self->{'device'} and
170 exists $params{'eject'} and
173 $self->{'chg'}->{'reserved'} = 0;
175 # unref the device, for good measure
176 $self->{'device'} = undef;
178 $params{'finished_cb'}->(undef) if $params{'finished_cb'};