| 1 |
#! @PERL@ |
| 2 |
# Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved. |
| 3 |
# Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; either version 2 |
| 8 |
# of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
# for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License along |
| 16 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
# |
| 19 |
# Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 20 |
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com |
| 21 |
|
| 22 |
# This is a tool to examine a device and generate a reasonable tapetype |
| 23 |
# entry accordingly. |
| 24 |
|
| 25 |
use lib '@amperldir@'; |
| 26 |
use strict; |
| 27 |
use warnings; |
| 28 |
|
| 29 |
use File::Basename; |
| 30 |
use Getopt::Long; |
| 31 |
use Math::BigInt; |
| 32 |
use Amanda::BigIntCompat; |
| 33 |
|
| 34 |
use Amanda::Device qw( :constants ); |
| 35 |
use Amanda::Debug qw( :logging ); |
| 36 |
use Amanda::Util qw( :constants ); |
| 37 |
use Amanda::Config qw( :init :getconf config_dir_relative ); |
| 38 |
use Amanda::MainLoop; |
| 39 |
use Amanda::Xfer; |
| 40 |
use Amanda::Constants; |
| 41 |
use Amanda::Header; |
| 42 |
|
| 43 |
# command-line options |
| 44 |
my $opt_only_compression = 0; |
| 45 |
my $opt_blocksize; |
| 46 |
my $opt_tapetype_name = 'unknown-tapetype'; |
| 47 |
my $opt_force = 0; |
| 48 |
my $opt_label = "amtapetype-".(int rand 2**31); |
| 49 |
my $opt_device_name; |
| 50 |
my $opt_config; |
| 51 |
my $opt_property; |
| 52 |
|
| 53 |
# global "hint" from the compression heuristic as to how fast this |
| 54 |
# drive is. |
| 55 |
my $device_speed_estimate; |
| 56 |
|
| 57 |
# open up a device, optionally check its label on the first invocation, |
| 58 |
# and start it in ACCESS_WRITE. |
| 59 |
my $_label_checked = 0; |
| 60 |
sub open_device { |
| 61 |
my $device = Amanda::Device->new($opt_device_name); |
| 62 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 63 |
die("Could not open device $opt_device_name: ".$device->error()."\n"); |
| 64 |
} |
| 65 |
|
| 66 |
if (!$device->configure(0)) { |
| 67 |
die("Errors configuring $opt_device_name: " . $device->error_or_status()); |
| 68 |
} |
| 69 |
|
| 70 |
if (defined $opt_blocksize) { |
| 71 |
my $r = $device->property_set('BLOCK_SIZE', $opt_blocksize); |
| 72 |
if ($r) { |
| 73 |
die "Error setting blocksize: " . $device->error_or_status(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (!$opt_force and !$_label_checked) { |
| 78 |
my $read_label_status = $device->read_label(); |
| 79 |
if ($read_label_status & $DEVICE_STATUS_VOLUME_UNLABELED) { |
| 80 |
# unlabeled is OK |
| 81 |
} elsif ($read_label_status != $DEVICE_STATUS_SUCCESS) { |
| 82 |
die "Error reading label: " . $device->error_or_status(); |
| 83 |
} elsif ($device->volume_label) { |
| 84 |
die "Volume in device $opt_device_name has Amanda label '" . |
| 85 |
$device->volume_label . "'. Giving up."; |
| 86 |
} |
| 87 |
$_label_checked = 1; |
| 88 |
} |
| 89 |
|
| 90 |
my $start_time = time; |
| 91 |
my $retries = 0; |
| 92 |
my $backoff = 1; |
| 93 |
while (1) { |
| 94 |
last if ($device->start($ACCESS_WRITE, $opt_label, undef)); |
| 95 |
if (!($device->status & $DEVICE_STATUS_DEVICE_BUSY)) { |
| 96 |
die("Error writing label '$opt_label': ". $device->error_or_status()); |
| 97 |
} |
| 98 |
|
| 99 |
if ($retries == 0) { |
| 100 |
print STDERR "Device is busy. Amtapetype will retry forever; hit ctrl-C to quit.\n"; |
| 101 |
} |
| 102 |
|
| 103 |
sleep($backoff); |
| 104 |
$backoff *= 2; |
| 105 |
$backoff = 120 if $backoff > 120; |
| 106 |
$retries++; |
| 107 |
} |
| 108 |
|
| 109 |
if ($retries) { |
| 110 |
my $elapsed = time - $start_time; |
| 111 |
print STDERR "Drive was busy for $elapsed seconds.\n"; |
| 112 |
print STDERR "If this device is used in a changer, you may want to set timeouts appropriately.\n"; |
| 113 |
} |
| 114 |
|
| 115 |
return $device; |
| 116 |
} |
| 117 |
|
| 118 |
# Write a single file to the device, and record the results in STATS. |
| 119 |
# write_one_file( |
| 120 |
# STATS => $stats_hashref, (see below) |
| 121 |
# DEVICE => $dev, (device to write to) |
| 122 |
# PATTERN => RANDOM or FIXED, (data pattern to write) |
| 123 |
# BYTES => nn, (number of bytes; optional) |
| 124 |
# MAX_TIME => secs); (cancel write after this time; optional) |
| 125 |
# |
| 126 |
# Returns 0 on success (including EOM), "TIMEOUT" on timeout, or an error message |
| 127 |
# on failure. |
| 128 |
# |
| 129 |
# STATS is a multi-level hashref; write_one_file adds to any values |
| 130 |
# already in the data structure. |
| 131 |
# $stats->{$pattern}->{TIME} - number of seconds spent writing |
| 132 |
# $stats->{$pattern}->{FILES} - number of files written |
| 133 |
# $stats->{$pattern}->{BYTES} - number of bytes written (approximate) |
| 134 |
# |
| 135 |
sub write_one_file(%) { |
| 136 |
my %options = @_; |
| 137 |
my $stats = $options{'STATS'} || { }; |
| 138 |
my $device = $options{'DEVICE'}; |
| 139 |
my $bytes = $options{'MAX_BYTES'} || 0; |
| 140 |
my $pattern = $options{'PATTERN'} || 'FIXED'; |
| 141 |
my $max_time = $options{'MAX_TIME'} || 0; |
| 142 |
|
| 143 |
# get the block size now, while the device is still working |
| 144 |
my $block_size = $device->property_get("block_size"); |
| 145 |
|
| 146 |
# start the device |
| 147 |
my $hdr = Amanda::Header->new(); |
| 148 |
$hdr->{type} = $Amanda::Header::F_DUMPFILE; |
| 149 |
$hdr->{name} = "amtapetype"; |
| 150 |
$hdr->{disk} = "/test"; |
| 151 |
$hdr->{datestamp} = "X"; |
| 152 |
$hdr->{program} = "AMTAPETYPE"; |
| 153 |
$device->start_file($hdr) |
| 154 |
or return $device->error_or_status(); |
| 155 |
|
| 156 |
# set up the transfer |
| 157 |
my ($source, $dest, $xfer); |
| 158 |
if ($pattern eq 'FIXED') { |
| 159 |
# a simple 256-byte pattern to dodge run length encoding. |
| 160 |
my $non_random_pattern = pack("C*", 0..255); |
| 161 |
$source = Amanda::Xfer::Source::Pattern->new($bytes, $non_random_pattern); |
| 162 |
} elsif ($pattern eq 'RANDOM') { |
| 163 |
$source = Amanda::Xfer::Source::Random->new($bytes, 1 + int rand 100); |
| 164 |
} else { |
| 165 |
die "Unknown PATTERN $pattern"; |
| 166 |
} |
| 167 |
$dest = Amanda::Xfer::Dest::Device->new($device, 1); |
| 168 |
$xfer = Amanda::Xfer->new([$source, $dest]); |
| 169 |
if (!defined $stats->{$pattern}) { |
| 170 |
$stats->{$pattern} = { BYTES => 0, FILES => 0, TIME => 0 }; |
| 171 |
} |
| 172 |
|
| 173 |
# set up the relevant callbacks |
| 174 |
my ($timeout_src, $spinner_src); |
| 175 |
my $got_error = 0; |
| 176 |
my $got_timeout = 0; |
| 177 |
|
| 178 |
if ($max_time) { |
| 179 |
$timeout_src = Amanda::MainLoop::timeout_source($max_time * 1000); |
| 180 |
$timeout_src->set_callback(sub { |
| 181 |
my ($src) = @_; |
| 182 |
$got_timeout = 1; |
| 183 |
$xfer->cancel(); # will result in an XFER_DONE |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
$spinner_src = Amanda::MainLoop::timeout_source(1000); |
| 188 |
$spinner_src->set_callback(sub { |
| 189 |
my ($src) = @_; |
| 190 |
my ($file, $block) = ($device->file(), $device->block()); |
| 191 |
print STDERR "File $file, block $block \r"; |
| 192 |
}); |
| 193 |
|
| 194 |
my $start_time = time(); |
| 195 |
|
| 196 |
$xfer->start(sub { |
| 197 |
my ($src, $xmsg, $xfer) = @_; |
| 198 |
if ($xmsg->{type} == $Amanda::Xfer::XMSG_ERROR) { |
| 199 |
$got_error = $xmsg->{message}; |
| 200 |
} elsif ($xmsg->{'type'} == $Amanda::Xfer::XMSG_DONE) { |
| 201 |
Amanda::MainLoop::quit(); |
| 202 |
} |
| 203 |
}); |
| 204 |
|
| 205 |
Amanda::MainLoop::run(); |
| 206 |
$spinner_src->remove(); |
| 207 |
$timeout_src->remove() if ($timeout_src); |
| 208 |
print STDERR " " x 60, "\r"; |
| 209 |
|
| 210 |
my $duration = time() - $start_time; |
| 211 |
|
| 212 |
# OK, we finished, update statistics (even if we saw an error) |
| 213 |
my $blocks_written = $device->block(); |
| 214 |
$stats->{$pattern}->{BYTES} += $blocks_written * $block_size; |
| 215 |
$stats->{$pattern}->{FILES} += 1; |
| 216 |
$stats->{$pattern}->{TIME} += $duration; |
| 217 |
|
| 218 |
# make sure the time is nonzero |
| 219 |
if ($stats->{$pattern}->{TIME} == 0) { |
| 220 |
$stats->{$pattern}->{TIME}++; |
| 221 |
} |
| 222 |
|
| 223 |
if ($device->status() != $Amanda::Device::DEVICE_STATUS_SUCCESS) { |
| 224 |
return $device->error_or_status(); |
| 225 |
} |
| 226 |
|
| 227 |
if ($got_error && $got_error =~ /LEOM detected/) { |
| 228 |
return "LEOM"; |
| 229 |
} |
| 230 |
|
| 231 |
if ($got_error) { |
| 232 |
return $got_error; |
| 233 |
} |
| 234 |
|
| 235 |
if ($got_timeout) { |
| 236 |
return "TIMEOUT"; |
| 237 |
} |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
sub check_compression { |
| 243 |
my $device = open_device(); |
| 244 |
|
| 245 |
# Check compression status here by property query. If the device can answer |
| 246 |
# the question, there's no reason to investigate further. |
| 247 |
my $compression_enabled = $device->property_get("compression"); |
| 248 |
|
| 249 |
if (defined $compression_enabled) { |
| 250 |
return $compression_enabled; |
| 251 |
} |
| 252 |
|
| 253 |
# Need to use heuristic to find out if compression is enabled. Also, we |
| 254 |
# rewind between passes so that the second pass doesn't get some kind of |
| 255 |
# buffering advantage. |
| 256 |
|
| 257 |
print STDERR "Applying heuristic check for compression.\n"; |
| 258 |
|
| 259 |
# We base our determination on whether it's faster to write random data or |
| 260 |
# patterned data. That starts by writing random data for a short length of |
| 261 |
# time, then measuring the elapsed time and total data written. Due to |
| 262 |
# potential delay in cancelling a transfer, the elapsed time will be a bit |
| 263 |
# longer than the intended time. We then write the same amount of |
| 264 |
# patterned data, and again measure the elapsed time. We can then |
| 265 |
# calculate the speeds of the two operations. If the compressible speed |
| 266 |
# was faster by more than min_ratio, then we assume compression is enabled. |
| 267 |
|
| 268 |
my $compression_check_time = 60; |
| 269 |
my $compression_check_min_ratio = 1.2; |
| 270 |
|
| 271 |
my $stats = { }; |
| 272 |
|
| 273 |
my $err = write_one_file( |
| 274 |
DEVICE => $device, |
| 275 |
STATS => $stats, |
| 276 |
MAX_TIME => $compression_check_time, |
| 277 |
PATTERN => 'RANDOM'); |
| 278 |
|
| 279 |
if ($err != 'TIMEOUT') { |
| 280 |
die $err; |
| 281 |
} |
| 282 |
$device->finish(); |
| 283 |
|
| 284 |
# speed calculations are a little tricky: BigInt * float comes out to NaN, so we |
| 285 |
# cast the BigInts to float first |
| 286 |
my $random_speed = ($stats->{RANDOM}->{BYTES} . "") / $stats->{RANDOM}->{TIME}; |
| 287 |
print STDERR "Wrote random (uncompressible) data at $random_speed bytes/sec\n"; |
| 288 |
|
| 289 |
# sock this away for make_tapetype's use |
| 290 |
$device_speed_estimate = $random_speed; |
| 291 |
|
| 292 |
# restart the device to clear any errors and rewind it |
| 293 |
$device = open_device(); |
| 294 |
|
| 295 |
$err = write_one_file( |
| 296 |
DEVICE => $device, |
| 297 |
STATS => $stats, |
| 298 |
MAX_BYTES => $stats->{'RANDOM'}->{'BYTES'}, |
| 299 |
PATTERN => 'FIXED'); |
| 300 |
if ($err) { |
| 301 |
die $err; |
| 302 |
} |
| 303 |
$device->finish(); |
| 304 |
|
| 305 |
my $fixed_speed = ($stats->{FIXED}->{BYTES} . "") / $stats->{FIXED}->{TIME}; |
| 306 |
print STDERR "Wrote fixed (compressible) data at $fixed_speed bytes/sec\n"; |
| 307 |
|
| 308 |
$compression_enabled = |
| 309 |
($fixed_speed / $random_speed > $compression_check_min_ratio); |
| 310 |
return $compression_enabled; |
| 311 |
} |
| 312 |
|
| 313 |
sub data_to_null { |
| 314 |
my ($device) = @_; |
| 315 |
my $got_error = 0; |
| 316 |
|
| 317 |
my $xfer = Amanda::Xfer->new([ |
| 318 |
Amanda::Xfer::Source::Device->new($device), |
| 319 |
Amanda::Xfer::Dest::Null->new(0), |
| 320 |
]); |
| 321 |
|
| 322 |
$xfer->start(sub { |
| 323 |
my ($src, $xmsg, $xfer) = @_; |
| 324 |
if ($xmsg->{type} == $Amanda::Xfer::XMSG_ERROR) { |
| 325 |
$got_error = $xmsg->{message}; |
| 326 |
} elsif ($xmsg->{'type'} == $Amanda::Xfer::XMSG_DONE) { |
| 327 |
Amanda::MainLoop::quit(); |
| 328 |
} |
| 329 |
}); |
| 330 |
|
| 331 |
Amanda::MainLoop::run(); |
| 332 |
} |
| 333 |
|
| 334 |
sub check_property { |
| 335 |
my $device = open_device(); |
| 336 |
|
| 337 |
print STDERR "Checking for FSF_AFTER_FILEMARK requirement\n"; |
| 338 |
my $fsf_after_filemark = $device->property_get("FSF_AFTER_FILEMARK"); |
| 339 |
|
| 340 |
# not a 'tape:' device |
| 341 |
return if !defined $fsf_after_filemark; |
| 342 |
|
| 343 |
$device->start($ACCESS_WRITE, "TEST-001", "20080706050403"); |
| 344 |
|
| 345 |
my $hdr = new Amanda::Header; |
| 346 |
|
| 347 |
$hdr->{type} = $Amanda::Header::F_DUMPFILE; |
| 348 |
$hdr->{name} = "localhost"; |
| 349 |
$hdr->{disk} = "/test1"; |
| 350 |
$hdr->{datestamp} = "20080706050403"; |
| 351 |
$hdr->{program} = "AMTAPETYPE"; |
| 352 |
$device->start_file($hdr); |
| 353 |
$device->finish_file(); |
| 354 |
|
| 355 |
$hdr->{type} = $Amanda::Header::F_DUMPFILE; |
| 356 |
$hdr->{name} = "localhost"; |
| 357 |
$hdr->{disk} = "/test2"; |
| 358 |
$hdr->{datestamp} = "20080706050403"; |
| 359 |
$hdr->{program} = "AMTAPETYPE"; |
| 360 |
$device->start_file($hdr); |
| 361 |
$device->finish_file(); |
| 362 |
|
| 363 |
$hdr->{type} = $Amanda::Header::F_DUMPFILE; |
| 364 |
$hdr->{name} = "localhost"; |
| 365 |
$hdr->{disk} = "/test3"; |
| 366 |
$hdr->{datestamp} = "20080706050403"; |
| 367 |
$hdr->{program} = "AMTAPETYPE"; |
| 368 |
$device->start_file($hdr); |
| 369 |
$device->finish_file(); |
| 370 |
|
| 371 |
$device->finish(); |
| 372 |
|
| 373 |
#set fsf_after_filemark to false |
| 374 |
my $r = $device->property_set('FSF_AFTER_FILEMARK', 0); |
| 375 |
if ($r) { |
| 376 |
die "Error setting FSF_AFTER_FILEMARK: $r"; |
| 377 |
} |
| 378 |
|
| 379 |
my $need_fsf_after_filemark = 0; |
| 380 |
|
| 381 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 382 |
die ("Could not read label from: " . $device->error_or_status()); |
| 383 |
} |
| 384 |
if ($device->volume_label != "TEST-001") { |
| 385 |
die ("wrong label: ", $device->volume_label); |
| 386 |
} |
| 387 |
$device->start($ACCESS_READ, undef, undef) |
| 388 |
or die ("Could not start device: " . $device->error_or_status()); |
| 389 |
|
| 390 |
$hdr = $device->seek_file(1); |
| 391 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 392 |
die ("seek_file(1) failed"); |
| 393 |
} |
| 394 |
if ($hdr->{disk} ne "/test1") { |
| 395 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test1"); |
| 396 |
} |
| 397 |
data_to_null($device); |
| 398 |
|
| 399 |
$hdr = $device->seek_file(2); |
| 400 |
if ($device->status() == $DEVICE_STATUS_SUCCESS) { |
| 401 |
if ($hdr->{disk} ne "/test2") { |
| 402 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test2"); |
| 403 |
} |
| 404 |
data_to_null($device); |
| 405 |
|
| 406 |
$hdr = $device->seek_file(3); |
| 407 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 408 |
die("seek_file(3) failed"); |
| 409 |
} |
| 410 |
if ($hdr->{disk} ne "/test3") { |
| 411 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test3"); |
| 412 |
} |
| 413 |
data_to_null($device); |
| 414 |
|
| 415 |
$device->finish(); |
| 416 |
} else { |
| 417 |
$need_fsf_after_filemark = 1; |
| 418 |
|
| 419 |
# $device is in error, so open a new one |
| 420 |
$device->finish(); |
| 421 |
$device = open_device(); |
| 422 |
} |
| 423 |
|
| 424 |
#verify need_fsf_after_filemark |
| 425 |
my $fsf_after_filemark_works = 0; |
| 426 |
if ($need_fsf_after_filemark) { |
| 427 |
#set fsf_after_filemark to true |
| 428 |
$r = $device->property_set('FSF_AFTER_FILEMARK', 1); |
| 429 |
if ($r) { |
| 430 |
die "Error setting FSF_AFTER_FILEMARK: $r"; |
| 431 |
} |
| 432 |
|
| 433 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 434 |
die ("Could not read label from: " . $device->error_or_status()); |
| 435 |
} |
| 436 |
if ($device->volume_label != "TEST-001") { |
| 437 |
die ("wrong label: ", $device->volume_label); |
| 438 |
} |
| 439 |
$device->start($ACCESS_READ, undef, undef) |
| 440 |
or die ("Could not start device: " . $device->error_or_status()); |
| 441 |
|
| 442 |
$hdr = $device->seek_file(1); |
| 443 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 444 |
die ("seek_file(1) failed"); |
| 445 |
} |
| 446 |
if ($hdr->{disk} ne "/test1") { |
| 447 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test1"); |
| 448 |
} |
| 449 |
data_to_null($device); |
| 450 |
|
| 451 |
$hdr = $device->seek_file(2); |
| 452 |
if ($device->status() == $DEVICE_STATUS_SUCCESS) { |
| 453 |
if ($hdr->{disk} ne "/test2") { |
| 454 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test2"); |
| 455 |
} |
| 456 |
data_to_null($device); |
| 457 |
|
| 458 |
$hdr = $device->seek_file(3); |
| 459 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 460 |
die("seek_file(3) failed"); |
| 461 |
} |
| 462 |
if ($hdr->{disk} ne "/test3") { |
| 463 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test3"); |
| 464 |
} |
| 465 |
data_to_null($device); |
| 466 |
$fsf_after_filemark_works = 1; |
| 467 |
} else { |
| 468 |
die("seek_file(2) failed"); |
| 469 |
} |
| 470 |
$device->finish(); |
| 471 |
} |
| 472 |
|
| 473 |
if ($need_fsf_after_filemark == 0 && $fsf_after_filemark_works == 0) { |
| 474 |
if (defined $opt_property || $fsf_after_filemark) { |
| 475 |
print STDOUT "device-property \"FSF_AFTER_FILEMARK\" \"false\"\n"; |
| 476 |
} |
| 477 |
$r = $device->property_set('FSF_AFTER_FILEMARK', 0); |
| 478 |
if ($r) { |
| 479 |
die "Error setting FSF_AFTER_FILEMARK: $r"; |
| 480 |
} |
| 481 |
} elsif ($need_fsf_after_filemark == 1 && $fsf_after_filemark_works == 1) { |
| 482 |
if (defined $opt_property || !$fsf_after_filemark) { |
| 483 |
print STDOUT "device-property \"FSF_AFTER_FILEMARK\" \"true\"\n"; |
| 484 |
} |
| 485 |
$r = $device->property_set('FSF_AFTER_FILEMARK', 1); |
| 486 |
if ($r) { |
| 487 |
die "Error setting FSF_AFTER_FILEMARK: $r"; |
| 488 |
} |
| 489 |
} else { |
| 490 |
die ("Broken seek_file"); |
| 491 |
} |
| 492 |
|
| 493 |
#Check seek to file 1 from header |
| 494 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 495 |
die ("Could not read label from: " . $device->error_or_status()); |
| 496 |
} |
| 497 |
if ($device->volume_label != "TEST-001") { |
| 498 |
die ("wrong label: ", $device->volume_label); |
| 499 |
} |
| 500 |
$device->start($ACCESS_READ, undef, undef) |
| 501 |
or die ("Could not start device: " . $device->error_or_status()); |
| 502 |
|
| 503 |
$hdr = $device->seek_file(1); |
| 504 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 505 |
die ("seek_file(1) failed"); |
| 506 |
} |
| 507 |
if ($hdr->{disk} ne "/test1") { |
| 508 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test1"); |
| 509 |
} |
| 510 |
$device->finish(); |
| 511 |
|
| 512 |
#Check seek to file 2 from header |
| 513 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 514 |
die ("Could not read label from: " . $device->error_or_status()); |
| 515 |
} |
| 516 |
if ($device->volume_label != "TEST-001") { |
| 517 |
die ("wrong label: ", $device->volume_label); |
| 518 |
} |
| 519 |
$device->start($ACCESS_READ, undef, undef) |
| 520 |
or die ("Could not start device: " . $device->error_or_status()); |
| 521 |
|
| 522 |
$hdr = $device->seek_file(2); |
| 523 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 524 |
die ("seek_file(2) failed"); |
| 525 |
} |
| 526 |
if ($hdr->{disk} ne "/test2") { |
| 527 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test1"); |
| 528 |
} |
| 529 |
$device->finish(); |
| 530 |
|
| 531 |
#Check seek to file 3 from header |
| 532 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 533 |
die ("Could not read label from: " . $device->error_or_status()); |
| 534 |
} |
| 535 |
if ($device->volume_label != "TEST-001") { |
| 536 |
die ("wrong label: ", $device->volume_label); |
| 537 |
} |
| 538 |
$device->start($ACCESS_READ, undef, undef) |
| 539 |
or die ("Could not start device: " . $device->error_or_status()); |
| 540 |
|
| 541 |
$hdr = $device->seek_file(3); |
| 542 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 543 |
die ("seek_file(3) failed"); |
| 544 |
} |
| 545 |
if ($hdr->{disk} ne "/test3") { |
| 546 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test1"); |
| 547 |
} |
| 548 |
$device->finish(); |
| 549 |
|
| 550 |
#Check seek to file 3 from eof of 1 |
| 551 |
if ($device->read_label() != $DEVICE_STATUS_SUCCESS) { |
| 552 |
die ("Could not read label from: " . $device->error_or_status()); |
| 553 |
} |
| 554 |
if ($device->volume_label != "TEST-001") { |
| 555 |
die ("wrong label: ", $device->volume_label); |
| 556 |
} |
| 557 |
$device->start($ACCESS_READ, undef, undef) |
| 558 |
or die ("Could not start device: " . $device->error_or_status()); |
| 559 |
|
| 560 |
$hdr = $device->seek_file(1); |
| 561 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 562 |
die ("seek_file(1) failed"); |
| 563 |
} |
| 564 |
data_to_null($device); |
| 565 |
$hdr = $device->seek_file(3); |
| 566 |
if ($device->status() != $DEVICE_STATUS_SUCCESS) { |
| 567 |
die ("seek_file(3) failed"); |
| 568 |
} |
| 569 |
if ($hdr->{disk} ne "/test3") { |
| 570 |
die ("Wrong disk: " . $hdr->{disk} . " expected /test3"); |
| 571 |
} |
| 572 |
$device->finish(); |
| 573 |
} |
| 574 |
|
| 575 |
sub make_tapetype { |
| 576 |
my ($compression_enabled) = @_; |
| 577 |
|
| 578 |
my $device = open_device(); |
| 579 |
my $blocksize = $device->property_get("BLOCK_SIZE"); |
| 580 |
|
| 581 |
# First, write one very long file to get the total tape length |
| 582 |
print STDERR "Writing one file to fill the volume.\n"; |
| 583 |
my $stats = {}; |
| 584 |
my $err = write_one_file( |
| 585 |
DEVICE => $device, |
| 586 |
STATS => $stats, |
| 587 |
PATTERN => 'RANDOM'); |
| 588 |
|
| 589 |
# if we wrote almost no data, then there's probably a problem |
| 590 |
# with the device, so error out |
| 591 |
if ($stats->{RANDOM}->{BYTES} < 1024 * 1024) { |
| 592 |
die "Wrote less than 1MB to the device: $err\n"; |
| 593 |
} |
| 594 |
my $volume_size_estimate = $stats->{RANDOM}->{BYTES}; |
| 595 |
my $speed_estimate = (($stats->{RANDOM}->{BYTES}."") / 1024) |
| 596 |
/ $stats->{RANDOM}->{TIME}; |
| 597 |
$speed_estimate = int $speed_estimate; |
| 598 |
print STDERR "Wrote $volume_size_estimate bytes at $speed_estimate kb/sec\n"; |
| 599 |
|
| 600 |
my $leom = 0; |
| 601 |
if ($err eq 'LEOM') { |
| 602 |
print STDERR "Got LEOM indication, so drive and kernel together support LEOM\n"; |
| 603 |
$leom = 1; |
| 604 |
} |
| 605 |
|
| 606 |
# now we want to write about 100 filemarks; round down to the blocksize |
| 607 |
# to avoid counting padding as part of the filemark |
| 608 |
my $file_size = $volume_size_estimate / 100; |
| 609 |
$file_size -= $file_size % $blocksize; |
| 610 |
|
| 611 |
print STDERR "Writing smaller files ($file_size bytes) to determine filemark.\n"; |
| 612 |
$device->finish(); |
| 613 |
$device = open_device(); # re-open to rewind and clear errors |
| 614 |
$stats = {}; |
| 615 |
while (!write_one_file( |
| 616 |
DEVICE => $device, |
| 617 |
STATS => $stats, |
| 618 |
MAX_BYTES => $file_size, |
| 619 |
PATTERN => 'RANDOM')) { } |
| 620 |
|
| 621 |
my $filemark_estimate = ($volume_size_estimate - $stats->{RANDOM}->{BYTES}) |
| 622 |
/ ($stats->{RANDOM}->{FILES} - 1); |
| 623 |
if ($filemark_estimate < 0) { |
| 624 |
$filemark_estimate = 0; |
| 625 |
} |
| 626 |
|
| 627 |
my $comment = "Created by amtapetype; compression " |
| 628 |
. ($compression_enabled? "enabled" : "disabled"); |
| 629 |
|
| 630 |
# round these parameters to the nearest kb, since the parameters' units |
| 631 |
# are kb, not bytes |
| 632 |
my $volume_size_estimate_kb = $volume_size_estimate/1024; |
| 633 |
my $filemark_kb = $filemark_estimate/1024; |
| 634 |
|
| 635 |
# and suggest using device-property for blocksize if it's not an even multiple |
| 636 |
# of 1kb |
| 637 |
my $blocksize_line; |
| 638 |
if ($blocksize % 1024 == 0) { |
| 639 |
$blocksize_line = "blocksize " . $blocksize/1024 . " kbytes"; |
| 640 |
} else { |
| 641 |
$blocksize_line = "# add device-property \"BLOCK_SIZE\" \"$blocksize\" to the device"; |
| 642 |
} |
| 643 |
|
| 644 |
print <<EOF; |
| 645 |
define tapetype $opt_tapetype_name { |
| 646 |
comment "$comment" |
| 647 |
length $volume_size_estimate_kb kbytes |
| 648 |
filemark $filemark_kb kbytes |
| 649 |
speed $speed_estimate kps |
| 650 |
$blocksize_line |
| 651 |
} |
| 652 |
EOF |
| 653 |
|
| 654 |
if ($leom) { |
| 655 |
print "# for this drive and kernel, LEOM is supported; add\n"; |
| 656 |
print "# device-property \"LEOM\" \"TRUE\"\n"; |
| 657 |
print "# for this device.\n"; |
| 658 |
} else { |
| 659 |
print "# LEOM is not supported for this drive and kernel\n"; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
sub usage { |
| 664 |
print STDERR <<EOF; |
| 665 |
Usage: amtapetype [-h] [-c] [-f] [-b blocksize] [-t typename] [-l label] |
| 666 |
[ [-o config_override] ... ] [config] device |
| 667 |
-h Display this message |
| 668 |
-c Only check hardware compression state |
| 669 |
-f Run amtapetype even if the loaded volume is already in use |
| 670 |
or compression is enabled. |
| 671 |
-b Blocksize to use (default 32k) |
| 672 |
-t Name to give to the new tapetype definition |
| 673 |
-l Label to write to the tape (default is randomly generated) |
| 674 |
-p Check property of the device. |
| 675 |
-o Overwrite configuration parameter (such as device properties) |
| 676 |
Blocksize can include an optional suffix (k, m, or g) |
| 677 |
|
| 678 |
If CONFIG is specified, the device and its configuration are loaded |
| 679 |
from the correspnding amanda.conf. |
| 680 |
EOF |
| 681 |
exit(1); |
| 682 |
} |
| 683 |
|
| 684 |
## Application initialization |
| 685 |
|
| 686 |
Amanda::Util::setup_application("amtapetype", "server", $CONTEXT_CMDLINE, "amanda", "amanda"); |
| 687 |
config_init(0, undef); |
| 688 |
|
| 689 |
my $config_overrides = new_config_overrides($#ARGV+1); |
| 690 |
|
| 691 |
debug("Arguments: " . join(' ', @ARGV)); |
| 692 |
Getopt::Long::Configure(qw(bundling)); |
| 693 |
GetOptions( |
| 694 |
'version' => \&Amanda::Util::version_opt, |
| 695 |
'help|usage|?|h' => \&usage, |
| 696 |
'c' => \$opt_only_compression, |
| 697 |
'b=s' => sub { |
| 698 |
my ($num, $suff) = ($_[1] =~ /^([0-9]+)\s*(.*)$/); |
| 699 |
die "Invalid blocksize '$_[1]'" unless (defined $num); |
| 700 |
my $mult = (defined $suff)? |
| 701 |
Amanda::Config::find_multiplier($suff) : 1; |
| 702 |
die "Invalid suffix '$suff'" unless ($mult); |
| 703 |
$opt_blocksize = $num * $mult; |
| 704 |
}, |
| 705 |
't=s' => \$opt_tapetype_name, |
| 706 |
'f' => \$opt_force, |
| 707 |
'l' => \$opt_label, |
| 708 |
'p' => \$opt_property, |
| 709 |
'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); }, |
| 710 |
) or usage(); |
| 711 |
usage() if (@ARGV < 1 or @ARGV > 2); |
| 712 |
|
| 713 |
set_config_overrides($config_overrides); |
| 714 |
if (@ARGV == 2) { |
| 715 |
$opt_config = shift @ARGV; |
| 716 |
config_init($CONFIG_INIT_EXPLICIT_NAME, $opt_config); |
| 717 |
} else { |
| 718 |
config_init(0, undef); |
| 719 |
} |
| 720 |
$opt_device_name= shift @ARGV; |
| 721 |
|
| 722 |
my ($cfgerr_level, @cfgerr_errors) = config_errors(); |
| 723 |
if ($cfgerr_level >= $CFGERR_WARNINGS) { |
| 724 |
config_print_errors(); |
| 725 |
if ($cfgerr_level >= $CFGERR_ERRORS) { |
| 726 |
die("errors processing config file"); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
Amanda::Util::finish_setup($RUNNING_AS_ANY); |
| 731 |
|
| 732 |
# Find property of the device. |
| 733 |
check_property(); |
| 734 |
|
| 735 |
if (!defined $opt_property) { |
| 736 |
my $compression_enabled = check_compression(); |
| 737 |
print STDERR "Compression: ", |
| 738 |
$compression_enabled? "enabled" : "disabled", |
| 739 |
"\n"; |
| 740 |
|
| 741 |
if ($compression_enabled and !$opt_force) { |
| 742 |
print STDERR "Turn off compression or run amtapetype with the -f option\n"; |
| 743 |
exit(1); |
| 744 |
} |
| 745 |
|
| 746 |
if (!$opt_only_compression) { |
| 747 |
make_tapetype($compression_enabled); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
Amanda::Util::finish_application(); |