1e6d8b4c12ae3816ed1e44fc7d49fef68c8f6320
[debian/amanda] / server-src / amdevcheck.pl
1 #! @PERL@
2 use lib '@amperldir@';
3 use strict;
4 use Amanda::Device qw( :constants );
5 use Amanda::Config qw( :getconf :init );
6 use Amanda::Debug qw( :logging );
7 use Amanda::Util qw( :running_as_flags );
8
9 # try to open the device and read its label, returning the device_read_label
10 # result (one or more of ReadLabelStatusFlags)
11 sub try_read_label {
12     my ($device_name) = @_;
13
14     if ( !$device_name ) {
15         return $READ_LABEL_STATUS_DEVICE_MISSING;
16     }
17
18     my $device = Amanda::Device->new($device_name);
19     if ( !$device ) {
20         return $READ_LABEL_STATUS_DEVICE_MISSING
21              | $READ_LABEL_STATUS_DEVICE_ERROR;
22     }
23
24     $device->set_startup_properties_from_config();
25
26     return $device->read_label();
27 }
28
29 # print the results, one flag per line
30 sub print_result {
31     my ($flags) = @_;
32
33     print join( "\n", ReadLabelStatusFlags_to_strings($flags) ), "\n";
34 }
35
36 sub usage {
37     print <<EOF;
38 Usage: amdevcheck <config> [ <device name> ]
39 EOF
40     exit(1);
41 }
42
43 ## Application initialization
44
45 Amanda::Util::setup_application("amdevcheck", "server", "cmdline");
46
47 usage() if ( @ARGV < 1 || @ARGV > 2 );
48 my $config_name = $ARGV[0];
49 if (!config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name)) {
50     critical('errors processing config file "' .
51                Amanda::Config::get_config_filename() . '"');
52 }
53
54 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
55
56 ## Check the device
57
58 my $device_name;
59 if ( $#ARGV == 1 ) {
60     $device_name = $ARGV[1];
61 } else {
62     $device_name = getconf($CNF_TAPEDEV);
63 }
64
65 print_result( try_read_label($device_name) );