/usr/sbin/tgt-admin is in tgt 1:1.0.72-1ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 | #!/usr/bin/perl
#
# This tools parses /etc/tgt/targets.conf file and configures tgt
#
# Author: Tomasz Chmielewski
# License: GPLv2
#
use strict;
use Config::General qw(ParseConfig);
use Getopt::Long;
# Our config file
my $configfile = "/etc/tgt/targets.conf";
sub usage {
print <<EOF;
Usage:
tgt-admin [OPTION]...
This tool configures tgt targets.
-e, --execute read $configfile and execute tgtadm commands
--delete <value> delete all or selected targets
(see "--delete help" for more info)
--offline <value> put all or selected targets in offline state
(see "--offline help" for more info)
--ready <value> put all or selected targets in ready state
(see "--ready help" for more info)
--update <value> update configuration for all or selected targets
(see "--update help" for more info)
-s, --show show all the targets
-C, --control-port <NNNN> specify the control port to connect to
-c, --conf <conf file> specify an alternative configuration file
--ignore-errors continue even if tgtadm exits with non-zero code
-f, --force force some operations even if the target is in use
-p, --pretend only print tgtadm options
--dump dump current tgtd configuration (note: does not
include detailed parameters, like write caching)
-v, --verbose increase verbosity (show tgtadm commands)
-h, --help show this help
EOF
exit;
}
my %conf;
my $param = $ARGV[0];
my $execute = 0;
my $delete = 0;
my $offline = 0;
my $ready = 0;
my $update = 0;
my $show = 0;
my $alternate_conf="0";
my $ignore_errors = 0;
my $force = 0;
my $pretend = 0;
my $dump = 0;
my $verbose = 0;
my $help = 0;
my $control_port=0;
Getopt::Long::Configure("no_ignore_case");
my $result = GetOptions (
"e|execute" => \$execute,
"delete=s" => \$delete,
"offline=s" => \$offline,
"ready=s" => \$ready,
"update=s" => \$update,
"s|show" => \$show,
"c|conf=s" => \$alternate_conf,
"ignore-errors" => \$ignore_errors,
"f|force" => \$force,
"p|pretend" => \$pretend,
"dump" => \$dump,
"v|verbose" => \$verbose,
"h|help" => \$help,
"C|control-port=s" => \$control_port,
);
if (($help == 1) || ($param eq undef)) {
usage;
}
# Show all the targets and exit
if ($show == 1) {
execute("tgtadm -C $control_port --op show --mode target");
exit;
}
# Some variables/arrays/hashes we will use globally
my %tgtadm_output;
my %tgtadm_output_tid;
my %tgtadm_output_name;
my @largest_tid;
my $next_tid;
my %existing_accounts;
# Look up if iSNS is already on
sub check_isns {
# We need to run as root
if ( $> ) {
die("You must be root to run this program.\n");
}
my @show_sys = `tgtadm -C $control_port --op show --mode sys`;
foreach my $sys_line (@show_sys) {
if ($sys_line =~ m/iSNS=On/) {
return 1;
}
}
# iSNS is not enabled if we're here
return 0;
}
# Look up which targets are configured
sub process_targets {
# We need to run as root
if ( $> ) {
die("You must be root to run this program.\n");
}
my @show_target = `tgtadm -C $control_port --op show --mode target`;
my $tid;
my $targetname;
# Here, we create hashes of target names (all target data) and target tids
foreach my $show_target_line (@show_target) {
if ( $show_target_line =~ m/^Target (\d*): (.+)/ ) {
$tid = $1;
$targetname = $2;
$tgtadm_output{$targetname} = $show_target_line;
$tgtadm_output_tid{$targetname} = $tid;
$tgtadm_output_name{$tid} = $targetname;
} else {
$tgtadm_output{$targetname} .= $show_target_line;
}
}
# What is the largest tid?
my @tids = values %tgtadm_output_tid;
@largest_tid = sort { $a <=> $b } @tids;
$next_tid = $largest_tid[$#largest_tid];
}
sub process_accounts {
# We need to run as root
if ( $> ) {
die("You must be root to run this program.\n");
}
my @show_account = `tgtadm -C $control_port --op show --mode account`;
# Here, we create a hash of accounts
foreach my $show_account_line (@show_account) {
if ( $show_account_line =~ m/^\s+(.*?)$/ ) {
$existing_accounts{$1} = 1;
}
}
}
# Parse config file(s)
sub parse_configs {
# Parse the config
if ($alternate_conf ne 0) {
# Check if alternative configuration file exists
if (-e "$alternate_conf") {
execute("# Using $alternate_conf as configuration file\n");
%conf = ParseConfig(-ConfigFile => "$alternate_conf", -UseApacheInclude => 1, -IncludeDirectories => 1, -IncludeGlob => 1, -MergeDuplicateBlocks => 1);
}
else {
die("Config file $alternate_conf not found. Exiting...\n");
}
} else {
# Parse the config file with Config::General
if (-e "$configfile") {
%conf = ParseConfig(-ConfigFile => "$configfile", -UseApacheInclude => 1, -IncludeDirectories => 1, -IncludeGlob => 1, -MergeDuplicateBlocks => 1);
} else {
die("Config file $configfile not found. Exiting...\n");
}
}
}
# Add targets, if they are not configured already
my $default_driver;
my $target;
my $option;
my $value;
my $lun;
sub add_targets {
my $single_target = $_[0];
my $configured = $_[1];
my $connected = $_[2];
my $in_configfile = $_[3];
my $isns_enabled = check_isns;
foreach my $k (sort keys %conf) {
if ($k eq "default-driver") {
if (not length ref($conf{$k})) {
$default_driver = $conf{$k};
} else {
print "Multiple default-driver definitions are not allowed!\n";
print "Check your config file for errors.\n";
exit 1;
}
} elsif ($k eq "ignore-errors") {
if ($conf{$k} eq "yes") {
$ignore_errors = 1;
}
} elsif (($k eq "iSNSAccessControl") || ($k eq "iSNSServerIP") ||
($k eq "iSNSServerPort")) {
if ($isns_enabled eq 0) {
check_if_hash_array($conf{$k}, $k);
execute("tgtadm -C $control_port --op update --mode sys --name $k -v $conf{$k}");
}
} elsif (($k eq "incomingdiscoveryuser") || ($k eq "outgoingdiscoveryuser")) {
my @userpass = split(/ /, $conf{$k});
check_value($userpass[1]);
# Only delete or create account if it doesn't already exist
if (! exists $existing_accounts{$userpass[0]} ) {
execute("tgtadm -C $control_port --mode account --op new --user=$userpass[0] --password=$userpass[1]");
$existing_accounts{$userpass[0]} = 1;
}
my $extra_str = "";
if ($k eq "outgoingdiscoveryuser") {
$extra_str = " --outgoing";
}
execute("tgtadm -C $control_port --mode account --op bind --user=$userpass[0] $extra_str");
}
}
foreach my $k (sort keys %conf) {
if ($k eq "iSNS") {
if ($isns_enabled eq 0) {
check_if_hash_array($conf{$k}, $k);
execute("tgtadm -C $control_port --op update --mode sys --name $k -v $conf{$k}");
} else {
execute("# iSNS already enabled");
}
} elsif ($k eq "control-port") {
if ($control_port eq 0) {
$control_port = $conf{$k};
} else {
print "Multiple control-port definitions are not allowed!\n";
print "Check your config file for errors.\n";
exit 1;
}
}
}
# If $default_driver is empty, default to iscsi
if (not defined $default_driver) {
execute("# default-driver not defined, defaulting to iscsi.\n");
$default_driver = "iscsi";
}
foreach my $k (sort keys %conf) {
if ($k eq "target") {
foreach my $k2 (sort keys %{$conf{$k}}) {
# Do we run update or execute?
if (length $single_target) {
if ($single_target ne $k2) {
next;
} else {
$target = $single_target;
}
} else {
$target = $k2;
}
my $in_use = 0;
if (length $single_target) {
$in_use = main_delete($target);
}
my $allowall = 1;
if ((not defined $tgtadm_output{$k2}) ||
($update ne 0 && $in_use == 0) ||
($update ne 0 && $in_use == 1 && $pretend == 1 && $force == 1))
{
# We have to find available tid
if ($in_configfile == 1 && $configured == 0 && $pretend == 0) {
my $maxtid = find_max_tid();
$next_tid = $maxtid + 1;
} elsif (length $single_target && $configured == 1) {
$next_tid = $tgtadm_output_tid{$target};
} else {
$next_tid = $next_tid + 1;
}
# Before we add a target, we need to know its type
# and other parameters which can be specified globally
my %target_options;
my $target_options_ref;
my $data_key;
foreach my $k3 (sort keys %{$conf{$k}{$k2}}) {
$lun = 1;
$option = $k3;
$value = $conf{$k}{$k2}{$k3};
check_value($value);
$target_options{$option} = $value;
$target_options_ref = \%target_options;
$data_key = make_key($target_options_ref, "lun", "allow-in-use");
}
if (defined $target_options{"controller_tid"}) {
$next_tid = $target_options{"controller_tid"};
}
if (not defined $target_options{"driver"}) {
$target_options{"driver"} = $default_driver;
}
my $driver = $target_options{"driver"};
execute("# Adding target: $target");
execute("tgtadm -C $control_port --lld $driver --op new --mode target --tid $next_tid -T $target");
foreach my $k3 (sort keys %{$conf{$k}{$k2}}) {
$option = $k3;
$value = $conf{$k}{$k2}{$k3};
check_value($value);
process_options($target_options_ref,$data_key);
# If there was no option called "initiator-address", it means
# we want to allow ALL initiators for this target
if ($option eq "initiator-address") {
$allowall = 0;
}
}
if ($allowall == 1) {
execute("tgtadm -C $control_port --lld $driver --op bind --mode target --tid $next_tid -I ALL");
}
} else {
if (not length $configured || $in_use eq 1) {
execute("# Target $target already exists!");
}
}
$tgtadm_output_tid{$target} = $next_tid;
}
if (length $single_target && $in_configfile == 0 && $configured == 0) {
print "Target $single_target is currently not configured\n";
print "and does not exist in the config file - can't continue!\n";
exit 1;
}
execute();
}
}
}
# Pre-parse the config and get some values we need
sub make_key {
my $target_options_ref = shift;
my @actions = @_;
my %data_key;
foreach my $action (@actions) {
if (ref $$target_options_ref{'backing-store'} eq "HASH") {
foreach my $testlun (keys %{$$target_options_ref{'backing-store'}}) {
$data_key{$testlun}{$action} = $$target_options_ref{'backing-store'}{$testlun}{$action};
}
}
if (ref $$target_options_ref{'direct-store'} eq "HASH") {
foreach my $testlun (keys %{$$target_options_ref{'direct-store'}}) {
$data_key{$testlun}{$action} = $$target_options_ref{'direct-store'}{$testlun}{$action};
}
}
}
return \%data_key;
}
# Some options can be specified only once
sub check_if_hash_array {
my $check = $_[0];
my $definition = $_[1];
if (ref($check) eq 'ARRAY' || ref($check) eq "HASH") {
if ($option) {
print "Multiple '$definition' definitions in '$option' not allowed!\n";
print "Check your config file for errors (target: $target).\n";
} else {
print "Multiple '$definition' definitions not allowed!\n";
print "Check your config file for errors.\n";
}
exit 1;
}
}
# Force an array if we just have one command
sub force_array {
unless (ref($value) eq 'ARRAY') {
$value = [ $value ];
}
}
# If we start any external command, we want to know if it exists
sub check_exe {
my $command = $_[0];
my $option = $_[1];
my @path = split(":", $ENV{PATH});
my $exists = 0;
foreach my $path (@path) {
if ( -x "$path/$command" && -f "$path/$command" ) { $exists = 1 }
}
if ($exists == 0) {
if ($command eq "sg_inq") {
print "Command '$command' (needed by '$option') is not in your path - can't continue!\n";
exit 1;
} elsif ($command eq "lsof") {
execute("# Command '$command' is not in your path.");
execute("# Can't reliably check if device is not in use.");
return 1;
}
}
}
# Apply additional parameters
sub add_params {
my $param = shift;
my $param_value = shift;
my $lun = shift;
my $driver = shift;
if ($param eq "write-cache") {
if ($param_value eq "off") {
return("tgtadm -C $control_port --lld $driver --op update --mode logicalunit --tid $next_tid --lun=$lun --params mode_page=8:0:18:0x10:0:0xff:0xff:0:0:0xff:0xff:0xff:0xff:0x80:0x14:0:0:0:0:0:0");
} elsif ($param_value eq "on" || not length $param_value) {
return("# Write cache is enabled (default) for lun $lun.");
} else {
return("# WARNING! Unknown value ($param_value) to write-cache! Accepted values are \"on\" and \"off\".");
}
}
if ($param eq "scsi_id" ||
$param eq "scsi_sn" ||
$param eq "vendor_id" ||
$param eq "product_id" ||
$param eq "product_rev" ||
$param eq "sense_format" ||
$param eq "removable" ||
$param eq "online" ||
$param eq "path" ||
$param eq "mode_page" ||
$param eq "lbppbe" ||
$param eq "la_lba" ||
$param eq "optimal_xfer_gran" ||
$param eq "optimal_xfer_len" ||
$param eq "readonly") {
return("tgtadm -C $control_port --lld $driver --op update --mode logicalunit --tid $next_tid --lun=$lun --params $param=\"$param_value\"");
}
if ($param eq "params") {
return("tgtadm -C $control_port --lld $driver --op update --mode logicalunit --tid $next_tid --lun=$lun --params \"$param_value\"");
}
}
# Find next available LUN
sub find_next_lun {
my $backing_store = $_[0];
my $data_key_ref = $_[1];
my $lun_collision = 0;
my $lun_is_free = 0;
my $found_lun = 1;
while ($lun_is_free == 0) {
foreach my $testlun (keys %$data_key_ref) {
foreach my $testlun2 (values %{$$data_key_ref{$testlun}}) {
if ($found_lun eq $testlun2) {
$lun_collision = 1;
}
}
}
if ($lun_collision == 0) {
$lun_is_free = 1;
} else {
$found_lun += 1;
}
$lun_collision = 0;
}
$$data_key_ref{$backing_store}{'lun'} = $found_lun;
return $found_lun;
}
# Add backing or direct store
sub add_backing_direct {
my $backing_store = $_[0];
my $target_options_ref = $_[1];
my $data_key_ref = $_[2];
my $direct_store = $_[3];
my $driver = $$target_options_ref{"driver"};
my $bstype = $$target_options_ref{"bs-type"};
# Is the device in use?
my $can_alloc = 1;
if ($bstype !~ "rbd" && $force != 1 && $$target_options_ref{'allow-in-use'} ne "yes") {
$can_alloc = check_device($backing_store,$data_key_ref);
}
if ($can_alloc == 1 &&
($bstype =~ "glfs" || $bstype =~ "rbd" || (-e $backing_store && ! -d $backing_store))) {
my @exec_commands;
my $device_type;
my $bs_type;
my $bsopts;
my $bsoflags;
my $block_size;
my %luns;
my @added_luns;
# Find out LUNs which are "reserved" in the config file
if (ref $value eq "HASH") {
if (length $$data_key_ref{$backing_store}{'lun'}) {
$lun = $$data_key_ref{$backing_store}{'lun'};
} else {
# Find an available lun if it wasn't specified
$lun = find_next_lun($backing_store,$data_key_ref);
}
}
# Process parameters for each lun / backing store
if (ref $value eq "HASH") {
my %params_added;
my @mode_page;
my @params;
foreach my $store (keys %$value) {
# lun
if (length $$target_options_ref{"lun"}) {
check_if_hash_array($$target_options_ref{"lun"}, "lun");
$lun = $$target_options_ref{"lun"};
}
if (ref $$value{$store} eq "HASH" && $store eq $backing_store) {
foreach my $store_option (keys %{$$value{$store}}) {
my $result = $$value{$store}{$store_option};
check_value($result);
if ($store_option ne "mode_page" && $store_option ne "params")
{ check_if_hash_array($result,$store_option) }
# write-cache can be set globally per target and overridden per lun,
# so we treat it differently
if ($store_option ne "mode_page" && $store_option ne "write-cache"
&& $store_option ne "params") {
my $exec_command = add_params($store_option, $result, $lun, $driver);
push(@exec_commands, $exec_command);
$params_added{$store_option} = 1;
}
if ($store_option eq "write-cache") {
my $exec_command = add_params($store_option, $result, $lun, $driver);
$params_added{write_cache} = 1;
push(@exec_commands, $exec_command);
}
if ($store_option eq "device-type") {
$device_type = $result;
$params_added{$store_option} = 1;
}
if ($store_option eq "bs-type") {
$bs_type = $result;
$params_added{$store_option} = 1;
}
if ($store_option eq "bsopts") {
$bsopts = $result;
$params_added{$store_option} = 1;
}
if ($store_option eq "bsoflags") {
$bsoflags = $result;
$params_added{$store_option} = 1;
}
if ($store_option eq "block-size") {
$block_size = $result;
$params_added{$store_option} = 1;
}
if ($store_option eq "mode_page") {
unless (ref($result) eq 'ARRAY') {
$result = [ $result ];
}
@mode_page = @$result;
foreach my $mode_page (@mode_page) {
my $exec_command = add_params($store_option, $mode_page, $lun, $driver);
push(@exec_commands, $exec_command);
}
}
if ($store_option eq "params") {
unless (ref($result) eq 'ARRAY') {
$result = [ $result ];
}
@params = @$result;
foreach my $param (@params) {
my $exec_command = add_params($store_option, $param, $lun, $driver);
push(@exec_commands, $exec_command);
}
}
}
}
}
# Used only if lun is a direct-store
my $sg_inq;
my $sg_readcap;
my %direct_params;
if ($direct_store == 1) {
$sg_inq=`sg_inq $backing_store`;
if ($sg_inq=~m {
Vendor\ identification:\s+?(.*?)\n
\s+Product\ identification:\s+(.*?)\n
\s+Product\ revision\ level:\s+(.*?)\n
(?:\s+Unit\ serial\ number:\s+(.*?)\n)?
}xs ) {
# If they were not defined globally for a target,
# add them now
if (not length $$target_options_ref{vendor_id}) {
$direct_params{vendor_id} = $1;
}
if (not length $$target_options_ref{product_id}) {
$direct_params{product_id} = $2;
}
if (not length $$target_options_ref{product_rev}) {
$direct_params{product_rev} = $3;
}
if (not length $$target_options_ref{scsi_sn}) {
$direct_params{scsi_sn} = $4;
}
}
$sg_inq=`sg_inq -p 0xb0 $backing_store`;
if ($sg_inq=~m {
Optimal\ transfer\ length\ granularity:\s+?(\d+).*?\n
\s+Maximum\ transfer\ length:\s+?(\d+).*?\n
\s+Optimal\ transfer\ length:\s+?(\d+).*?\n
}xs ) {
if (not length $$target_options_ref{optimal_xfer_gran}) {
$direct_params{optimal_xfer_gran} = $1;
}
# $2 unused
if (not length $$target_options_ref{optimal_xfer_len}) {
$direct_params{optimal_xfer_len} = $3;
}
}
$sg_readcap=`sg_readcap -16 $backing_store`;
if ($sg_readcap=~ m {
Logical\ block\ length=(\d+).*?\n
\s+Logical\ blocks\ per\ physical\ block\ exponent=(\d+).*?\n
\s+Lowest\ aligned\ logical\ block\ address=(\d+).*?\n
}xs) {
if (not length $block_size) {
$block_size = $1;
}
if (not length $$target_options_ref{lbppbe}) {
$direct_params{lbppbe} = $2;
}
if (not length $$target_options_ref{la_lba}) {
$direct_params{la_lba} = $3;
}
}
}
# Add these parameters if they were not overwritten in the config file
my @opts = ("scsi_id", "sense_format", "removable", "online", "path", "readonly");
foreach my $single_opt (@opts) {
check_if_hash_array($$target_options_ref{$single_opt},$single_opt);
if ($params_added{$single_opt} ne 1 && length $$target_options_ref{$single_opt}) {
my $exec_command = add_params($single_opt, $$target_options_ref{$single_opt}, $lun, $driver);
push(@exec_commands, $exec_command);
$params_added{$single_opt} = 1;
}
}
# These options can be fetched by sg_inq for direct-store
my @opts = ("vendor_id", "product_id", "product_rev", "scsi_sn",
"lbppbe", "la_lba", "optimal_xfer_gran", "optimal_xfer_len");
foreach my $single_opt (@opts) {
check_if_hash_array($$target_options_ref{$single_opt},$single_opt);
my $this_opt;
if (length $$target_options_ref{$single_opt}) {
$this_opt = $$target_options_ref{$single_opt};
} elsif (length $direct_params{$single_opt}) {
$this_opt = $direct_params{$single_opt};
}
if ($params_added{$single_opt} ne 1 && length $this_opt) {
my $exec_command = add_params($single_opt, $this_opt, $lun, $driver);
push(@exec_commands, $exec_command);
$params_added{$single_opt} = 1;
}
}
# write-cache
if ($params_added{write_cache} ne 1) {
my $exec_command = add_params("write-cache", $$target_options_ref{"write-cache"}, $lun, $driver);
push(@exec_commands, $exec_command);
$params_added{write_cache} = 1;
}
# mode_page
unless (ref($$target_options_ref{mode_page}) eq 'ARRAY') {
$$target_options_ref{mode_page} = [ $$target_options_ref{mode_page} ];
}
foreach my $mode_page (@{$$target_options_ref{"mode_page"}}) {
if (length $mode_page) {
my $exec_command = add_params("mode_page", $mode_page, $lun, $driver);
push(@exec_commands, $exec_command);
}
}
# params
unless (ref($$target_options_ref{params}) eq 'ARRAY') {
$$target_options_ref{params} = [ $$target_options_ref{params} ];
}
foreach my $param (@{$$target_options_ref{"params"}}) {
if (length $param) {
my $exec_command = add_params("params", $param, $lun, $driver);
push(@exec_commands, $exec_command);
}
}
# device-type
if ($params_added{"device-type"} ne 1) {
check_if_hash_array($$target_options_ref{"device-type"}, "device-type");
$device_type = $$target_options_ref{"device-type"};
}
# bs-type
if ($params_added{"bs-type"} ne 1) {
check_if_hash_array($$target_options_ref{"bs-type"}, "bs-type");
$bs_type = $$target_options_ref{"bs-type"};
}
# bsopts
if ($params_added{"bsopts"} ne 1) {
check_if_hash_array($$target_options_ref{"bsopts"}, "bsopts");
$bsopts = $$target_options_ref{"bsopts"};
}
# bsoflags
if ($params_added{"bsoflags"} ne 1) {
check_if_hash_array($$target_options_ref{"bsoflags"}, "bsoflags");
$bsoflags = $$target_options_ref{"bsoflags"};
}
} else {
print "If you got here, this means your config file is not supported.\n";
print "Please report it to stgt mailing list and attach your config files.\n";
exit 1;
}
# Execute commands for a given LUN
if (length $device_type) { $device_type = "--device-type $device_type" };
if (length $bs_type) { $bs_type = "--bstype $bs_type" };
if (length $bsopts) { $bsopts = "--bsopts $bsopts" };
if (length $bsoflags) { $bsoflags = "--bsoflags $bsoflags" };
if (length $block_size) { $block_size = "--blocksize $block_size" };
execute("tgtadm -C $control_port --lld $driver --op new --mode logicalunit --tid $next_tid --lun $lun -b $backing_store $device_type $bs_type $bsopts $bsoflags $block_size");
# Commands should be executed in order
my @execute_last;
foreach my $exec_command (@exec_commands) {
if (length $exec_command) {
if ($exec_command =~ m/params mode_page/) {
execute($exec_command);
} else {
push(@execute_last, $exec_command);
}
}
}
foreach my $exec_command (@execute_last) {
execute($exec_command);
}
$lun += 1;
return $lun;
} elsif ($can_alloc == 0) {
execute("# Skipping device $backing_store - it is in use.");
execute("# You can override it with --force or 'allow-in-use yes' config option.");
execute("# Note - do so only if you know what you're doing, you may damage your data.");
} else {
execute("# Skipping device: $backing_store");
execute("# $backing_store does not exist - please check the configuration file");
}
}
# Process options from the config file
sub process_options {
my $target_options_ref = $_[0];
my $data_key_ref = $_[1];
my $driver = $$target_options_ref{"driver"};
if ($option eq "backing-store" || $option eq "direct-store") {
my $direct_store = 0;
if ($option eq "direct-store") {
check_exe("sg_inq", "option direct-store");
$direct_store = 1;
}
# We want to make everything a hash to use it
# in the same way later on
unless (ref($value)) {
$value = { $value }
}
my %arrvalue;
if (ref($value) eq "ARRAY") {
foreach my $backing_store (@$value) {
$arrvalue{$backing_store} = 1;
}
$value = \%arrvalue;
}
if (ref($value) eq "HASH") {
foreach my $backing_store (sort keys %$value) {
if ($backing_store =~ m/HASH/) {
print "\nYour config file is not supported. See targets.conf.example for details.\n";
exit 1;
}
}
foreach my $backing_store (sort keys %$value) {
add_backing_direct($backing_store,$target_options_ref,$data_key_ref,$direct_store);
}
}
}
if ($option eq "incominguser") {
# if we have one command, force it to be an array anyway
force_array();
my @value_arr = @$value;
foreach my $incominguser (@value_arr) {
my @userpass = split(/ /, $incominguser);
check_value($userpass[1]);
# Only delete or create account if it doesn't already exist
if (! exists $existing_accounts{$userpass[0]} ) {
execute("tgtadm -C $control_port --lld $driver --mode account --op new --user=$userpass[0] --password=$userpass[1]");
$existing_accounts{$userpass[0]} = 1;
}
execute("tgtadm -C $control_port --lld $driver --mode account --op bind --tid=$next_tid --user=$userpass[0]");
}
}
if ($option eq "outgoinguser") {
# if we have one command, force it to be an array anyway
force_array();
if (length @$value[1]) {
execute("# Warning: only one $option is allowed. Will only use the first one.");
}
my @userpass = split(/ /, @$value[0]);
check_value($userpass[1]);
# Only delete or create account if it doesn't already exist
if (! exists $existing_accounts{$userpass[0]} ) {
execute("tgtadm -C $control_port --lld $driver --mode account --op new --user=$userpass[0] --password=$userpass[1]");
$existing_accounts{$userpass[0]} = 1;
}
execute("tgtadm -C $control_port --lld $driver --mode account --op bind --tid=$next_tid --user=$userpass[0] --outgoing");
}
if ($option eq "initiator-address") {
# if we have one command, force it to be an array anyway
force_array();
my @value_arr = @$value;
foreach my $initiator_address (@value_arr) {
check_value($initiator_address);
execute("tgtadm -C $control_port --lld $driver --op bind --mode target --tid $next_tid -I $initiator_address");
}
}
if ($option eq "initiator-name") {
# if we have one command, force it to be an array anyway
force_array();
my @value_arr = @$value;
foreach my $initiator_name (@value_arr) {
check_value($initiator_name);
execute("tgtadm -C $control_port --lld $driver --op bind --mode target --tid $next_tid -Q $initiator_name");
}
}
if ($option =~ m/^MaxRecvDataSegmentLength$|
^MaxXmitDataSegmentLength$|
^HeaderDigest$|
^DataDigest$|
^InitialR2T$|
^MaxOutstandingR2T$|
^ImmediateData$|
^FirstBurstLength$|
^MaxBurstLength$|
^DataPDUInOrder$|
^DataSequenceInOrder$|
^ErrorRecoveryLevel$|
^IFMarker$|
^OFMarker$|
^DefaultTime2Wait$|
^DefaultTime2Retain$|
^OFMarkInt$|
^IFMarkInt$|
^MaxConnections$|
^state/x) {
# if we have one command, force it to be an array anyway
force_array();
if (length @$value[1]) {
execute("# Warning: only one $option is allowed. Will only use the first one.");
}
check_value($option);
execute("tgtadm -C $control_port --lld $driver --mode target --op update --tid $next_tid --name $option --value $$value[0]");
}
}
# If the target is configured, but not present in the config file,
# try to remove it
sub remove_targets {
process_targets;
my @all_targets = keys %tgtadm_output_tid;
foreach my $existing_target (@all_targets) {
my $dontremove = 0;
my $k2;
foreach my $k (sort keys %conf) {
if ( $k eq "target" ) {
foreach $k2 (sort keys %{$conf{$k}}) {
if ( $k2 eq $existing_target ) {
$dontremove = 1;
}
}
if ( $dontremove == 0 ) {
# Remove the target
main_delete($existing_target);
}
}
}
}
}
# Dump current tgtd configuration
sub dump_config {
process_targets;
my @all_targets = keys %tgtadm_output_tid;
# If all targets use the same driver, use it only once in the config
my $skip_driver = 0;
my @drivers_combined;
foreach my $current_target (@all_targets) {
my $driver = show_target_info($current_target, "driver");
push (@drivers_combined, $driver);
}
my %drivers_uniq;
@drivers_uniq{@drivers_combined} = ();
my @drivers_combined_uniq = sort keys %drivers_uniq;
if (scalar @drivers_combined_uniq == 1) {
print "default-driver $drivers_combined_uniq[0]\n\n";
}
# Print everything else in the config
foreach my $current_target (@all_targets) {
my $target_name = show_target_info($current_target, "target_name");
print "<target $target_name>\n";
if (scalar @drivers_combined_uniq gt 1) {
my $driver = show_target_info($current_target, "driver");
print "\tdriver $driver\n";
}
my @backing_stores = show_target_info($current_target, "backing_stores");
foreach my $backing_store (@backing_stores) {
print "\tbacking-store $backing_store\n";
}
my @account_information = show_target_info($current_target, "account_information");
foreach my $account (@account_information) {
if ($account =~ /(.+)\ \(outgoing\)/) {
print "\toutgoinguser $1 PLEASE_CORRECT_THE_PASSWORD\n";
} elsif (length $account) {
print "\tincominguser $account PLEASE_CORRECT_THE_PASSWORD\n";
}
}
my @acl_information = show_target_info($current_target, "acl_information");
if (scalar(@acl_information) != 1 || $acl_information[0] ne "ALL") {
foreach my $ini_address (@acl_information) {
print "\tinitiator-address $ini_address\n";
}
}
print "</target>\n\n";
}
}
# Offline or ready targets
sub ready_offline_targets {
my $var = $_[0]; # This variable is either "offline" or "ready"
my $off_ready;
if ($ready eq 0) {
$off_ready = $offline
} elsif ($offline eq 0) {
$off_ready = $ready
} else {
print "Invalid value (you can't use both ready and offline)!\n";
exit 1;
}
if ($off_ready eq "help") {
print <<EOF;
--$var <value> $var all or selected targets
Example usage:
--$var help - display this help
--$var ALL - $var all targets
--$var tid=4 - $var target 4 (target with tid 4)
--$var iqn.2008-08.com.example:some.target - $var this target
EOF
} elsif ($off_ready eq "ALL") {
process_targets;
# Run over all targets and offline/ready them
my @all_targets = keys %tgtadm_output_tid;
foreach my $existing_target (@all_targets) {
execute("tgtadm -C $control_port --op update --mode target --tid=$tgtadm_output_tid{$existing_target} -n state -v $var");
}
} elsif ($off_ready =~ m/tid=(.+)/) {
process_targets;
execute("tgtadm -C $control_port --op update --mode target --tid=$1 -n state -v $var");
} else {
process_targets;
if (length $tgtadm_output_tid{$off_ready}) {
execute("tgtadm -C $control_port --op update --mode target --tid=$tgtadm_output_tid{$off_ready} --name=\"$off_ready\" -n state -v $var");
} else {
print "There is no target with name \"$off_ready\", can't $var it!\n";
exit 1;
}
}
}
# Show info for a given target
sub show_target_info {
my $existing_target = $_[0];
my $task = $_[1];
# Returns target information
if ($task eq "target_name") {
if ($tgtadm_output{$existing_target} =~ m/^Target (\d*): (.+)/ ) {
return $2;
}
# Returns driver information
} elsif ($task eq "driver") {
if ($tgtadm_output{$existing_target} =~ m/\s+Driver: (.+)/ ) {
return $1;
}
# Returns backing store
} elsif ($task eq "backing_stores") {
if ($tgtadm_output{$existing_target} =~ m/\s+Backing store path: (?!None)(.+)/ ) {
my @backing_stores = $tgtadm_output{$existing_target} =~ m{\s+Backing store path: (?!None\n)(.+)}g;
return @backing_stores;
}
return;
# Returns account information:
} elsif ($task eq "account_information") {
if ($tgtadm_output{$existing_target} =~ m{
\s+Account\ information:\n(.*)\n\s+ACL\ information:
}xs
) {
my @accounts = split(/\n/, $1);
my @account_information;
foreach my $user (@accounts) {
my @var = split(/^\s+/, $user);
push(@account_information, $var[1]);
}
return @account_information;
}
# Returns ACL information
} elsif ($task eq "acl_information") {
if ($tgtadm_output{$existing_target} =~ m{
\s+ACL\ information:\n(.*)
}xs
) {
my @ini_addresses = split(/\n/, $1);
my @acls;
foreach my $ini_address (@ini_addresses) {
my @var = split(/^\s+/, $ini_address);
push(@acls, $var[1]);
}
return @acls;
}
# Returns sessions
} elsif ($task eq "sessions") {
my %sessions;
if ($tgtadm_output{$existing_target} =~ m{
\s+I_T\ nexus\ information:\n(.*)LUN\ information:
}xs
) {
my @var = split(/\n/, $1);
my $sid;
my $cid;
foreach my $line (@var) {
if ($line =~ m/\s+I_T nexus:\ (.+)/) {
$sid = $1;
} else {
if ($line =~ m/\s+Connection:\ (.+)/) {
$cid = $1;
$sessions{$sid} = $cid;
}
}
}
}
return %sessions;
}
}
# Main subroutine for deleting targets
sub main_delete {
my $current_target = $_[0];
my $current_tid = $_[1];
my $configured = check_configured($current_target);
my $del_upd_text;
# Check if the target has initiators connected
if ($tgtadm_output{$current_target} =~ m/\s+Connection:/) {
if ($force == 1) {
execute("# Removing target: $current_target");
# Remove ACLs first
my @acl_info = show_target_info($current_target, "acl_information");
foreach my $acl (@acl_info) {
execute("tgtadm -C $control_port --op unbind --mode target --tid $tgtadm_output_tid{$current_target} -I $acl");
}
# Now, remove all sessions / connections from that tid
my %sessions = show_target_info($current_target, "sessions");
foreach my $sid (keys %sessions) {
foreach my $cid ($sessions{$sid}) {
execute("tgtadm -C $control_port --op delete --mode conn --tid $tgtadm_output_tid{$current_target} --sid $sid --cid $cid");
}
}
execute("tgtadm -C $control_port --mode target --op delete --tid=$tgtadm_output_tid{$current_target}");
} else {
if ($update ne 0) {
$del_upd_text = "updated";
} else {
$del_upd_text = "deleted";
}
execute("# Target with tid $tgtadm_output_tid{$current_target} ($current_target) is in use, it won't be $del_upd_text.");
return 1;
}
} elsif (length $tgtadm_output_tid{$current_target}) {
execute("# Removing target: $current_target");
execute("tgtadm -C $control_port --mode target --op delete --tid=$tgtadm_output_tid{$current_target}");
} else {
if (length $current_tid) {
execute("# Target with tid $current_tid does not exist!");
} else {
execute("# Target with name $current_target does not exist!");
}
}
if ($configured ne 0) {
execute();
}
return 0;
}
# Delete the targets
sub delete_targets {
if ($delete eq "help") {
print <<EOF;
--delete <value> delete all or selected targets
The target will be deleted only if it's not used
(no initiator is connected to it).
If you want to delete targets which are in use,
you have to add "--force" flag.
Example usage:
--delete help - display this help
--delete ALL - delete all targets
--delete tid=4 - delete target 4 (target with tid 4)
--delete iqn.2008-08.com.example:some.target - delete this target
EOF
exit;
} elsif ($delete eq "ALL") {
process_targets;
# Run over all targets and delete them
my @all_targets = keys %tgtadm_output_tid;
foreach my $current_target (@all_targets) {
main_delete($current_target);
}
} elsif ($delete =~ m/^tid=(.+)/) {
# Delete by tid
process_targets;
my $current_target = $tgtadm_output_name{$1};
main_delete($current_target, $1);
} else {
# Delete by name
process_targets;
my $current_target = $delete;
main_delete($current_target);
}
}
# Update targets
sub update_targets {
if ($update eq "help") {
print <<EOF;
--update <value> update all or selected targets
The target will be updated only if it's not used
(no initiator is connected to it).
If you want to update targets which are in use,
you have to add "--force" flag.
Example usage:
--update help - display this help
--update ALL - update all targets
--update tid=4 - update target 4 (target with tid 4)
--update iqn.2008-08.com.example:some.target - update this target
EOF
exit;
} elsif ($update eq "ALL") {
# Run over all targets and delete them if they are not in use
parse_configs;
process_targets;
my @targets_combined = combine_targets();
foreach my $current_target (@targets_combined) {
my $configured = check_configured($current_target);
my $connected = check_connected($current_target);
my $in_configfile = check_in_configfile($current_target);
combine_targets();
if (($in_configfile == 0) && ($configured == 1)) {
# Delete the target if it's not in the config file
main_delete($current_target);
} else {
add_targets($current_target, $configured, $connected, $in_configfile);
}
}
} elsif ($update =~ m/^tid=(.+)/) {
# Update by tid
parse_configs;
process_targets;
my $current_target = $tgtadm_output_name{$1};
my $configured = check_configured($current_target);
my $connected = check_connected($current_target);
my $in_configfile = check_in_configfile($current_target);
if (($in_configfile == 0) && ($configured == 1)) {
# Delete the target if it's not in the config file
main_delete($current_target);
} elsif ($configured == 1) {
add_targets($current_target, $configured, $connected, $in_configfile);
} else {
print "There is no target with tid $1, can't continue!\n";
exit 1;
}
} else {
# Update by name
parse_configs;
process_targets;
my $current_target = $update;
my $configured = check_configured($current_target);
my $connected = check_connected($current_target);
my $in_configfile = check_in_configfile($current_target);
if ($in_configfile == 0 && $configured == 1) {
# Delete the target if it's not in the config file
main_delete($current_target);
} else {
add_targets($current_target, $configured, $connected, $in_configfile);
}
}
}
# Find the biggest tid
sub find_max_tid {
my @all_targets = keys %tgtadm_output_tid;
my $maxtid = 0;
foreach my $var (@all_targets) {
if ($tgtadm_output_tid{$var} > $maxtid) {
$maxtid = $tgtadm_output_tid{$var};
}
}
return $maxtid;
}
# Combine targets from the config file and currently configured targets
sub combine_targets {
my @targets_in_configfile;
my @all_targets = keys %tgtadm_output_tid;
my @targets_combined;
# Make an array of targets in the config file
foreach my $k (sort keys %conf) {
if ( $k eq "target" ) {
foreach my $k2 (sort keys %{$conf{$k}}) {
push(@targets_in_configfile, $k2)
}
}
}
# Use only unique elements from both arrays
foreach my $current_target (@all_targets) {
push (@targets_combined, $current_target) unless grep { $_ eq $current_target } @targets_in_configfile;
}
@targets_combined = (@targets_combined, @targets_in_configfile);
return @targets_combined;
}
# Check if a value is correct
sub check_value {
if ( not defined $_[0] or not length $_[0] ) {
print "\nOption $option has a missing value!\n";
print "Check your config file for errors (target: $target)\n";
exit 1;
}
}
# Check if the target is in the config file
sub check_in_configfile {
my $current_target = $_[0];
my $result;
foreach my $k (sort keys %conf) {
if ( $k eq "target" ) {
foreach my $k2 (sort keys %{$conf{$k}}) {
if ($k2 eq $current_target) {
return 1;
}
}
# If we're here, we didn't find a match
return 0;
}
}
}
# Check if the target is configured in tgtd
sub check_configured {
my $current_target = $_[0];
if (length $tgtadm_output_tid{$current_target}) {
return 1;
} else {
return 0;
}
}
# Check if any initiators are connected to the target
sub check_connected {
my $current_target = $_[0];
if ($tgtadm_output{$current_target} =~ m/\s+Connection:/) {
return 1;
} else {
return 0;
}
}
# Check if a device can be allocated
# Device can be used "by system" (i.e. mounted, used as swap, as a part of
# a RAID array etc.) or "by user" - i.e., already by tgtd, or someone doing:
# dd if=/dev/1st_device of=/dev/2nd_device
# We shouldn't allow a device to be used more than one time, as it could
# cause corruption when written several times. Unless the user really wants to.
sub check_device {
my $backing_store = $_[0];
my $data_key_ref = $_[1];
# If allow-in-use is "yes", there is no need to do
# farther tests
if ($$data_key_ref{$backing_store}{'allow-in-use'} eq "yes") {
return 1;
}
if (! -e $backing_store) {
return 1;
}
# Check if the system uses this device
use Fcntl qw(O_RDONLY O_EXCL);
use Errno;
sysopen(FH, $backing_store, O_RDONLY | O_EXCL);
if ($!{EBUSY}) {
execute("# Device $backing_store is used by the system (mounted, used by swap?).");
return 0;
}
close(FH);
# Check if userspace uses this device
my $lsof_check = check_exe("lsof");
if ($lsof_check ne 1) {
system("lsof $backing_store >/dev/null 2>&1");
my $exit_value = $? >> 8;
if ($exit_value eq 0) {
execute("# Device $backing_store is used (already tgtd target?).");
execute("# Run 'lsof $backing_store' to see the details.");
return 0;
}
}
return 1;
}
# Execute or just print (or both) everything we start or would start
sub execute {
if ($pretend == 0) {
my $args = "@_";
if ($verbose == 1) {
print "$args\n";
}
# Don't try to execute if it's a comment
my @execargs = split(/#/, $args);
if ($execargs[0] ne undef) {
system($args);
# If non-zero exit code was return, exit
my $exit_value = $? >> 8;
if (($exit_value != 0) && ($ignore_errors == 0)) {
print "Command:\n\t$args\nexited with code: $exit_value.\n";
exit $exit_value;
}
}
} elsif ($pretend == 1) {
print "@_\n";
}
}
if ($execute == 1) {
parse_configs;
process_targets;
process_accounts;
add_targets;
remove_targets;
} elsif ($delete ne 0) {
delete_targets;
} elsif ($update ne 0) {
process_accounts;
update_targets;
} elsif ($dump == 1) {
dump_config;
} elsif ($offline ne 0) {
ready_offline_targets("offline");
} elsif ($ready ne 0) {
ready_offline_targets("ready");
} else {
print "No action specified.\n";
}
|