2 $Version = 'SCST Configurator v1.0.9';
6 # Author: Mark R. Buechler
8 # Copyright (c) 2005-2009 Mark R. Buechler
17 -config <config> : Configure SCST given the specified configuration file.
18 -ClearConfig : Clear all SCST configuration.
19 -WriteConfig <file> : Writes the current configuration out to the specified file.
20 -checkConfig <file> : Checks the saved configuration in the specified file.
21 -sessions : List current initiator sessions.
23 Target Driver Operations
24 -enable <wwn|host> : Enable target mode for driver at specified WWN or host.
25 -disable <wwn|host> : Disable target mode for driver at specified WWN or host.
28 -adddev <device> : Adds a device to a handler.
33 -resyncdev <device> : Resync the size of a device with the initiator(s).
35 -RemoveDev <device> : Remove a device from a handler.
39 -adduser <user> : Adds a user to a security group.
41 -MoveUser <user> : Moves a user from one security group to another.
44 -RemoveUser <user> : Delete a user from a security group.
46 -ClearUsers : Clear all users from a given security group.
50 -addgroup <group> : Add a given group to available security groups.
51 -renamegroup <group> : Renames a give group to a new name.
53 -RemoveGroup <group> : Remove a give group from available security groups.
56 -assigndev <device> : Assign a given device to a security group.
60 -ReplaceDev <new dev>: Replaces a device assigned to a give LUN and group.
64 -ReleaseDev <device> : Remove a given device from a security group.
66 -ClearDevs : Clear all device assignments for a security group.
70 -ForceConfig : Force all configuration changes, even deletions (DANGER!).
72 Debugging (limited support)
73 -debug : Debug mode - don\'t do anything destructive.
76 disk, vdisk, disk_perf, cdrom, vcdrom, changer, modisk, modisk_perf, tape, tape_perf
78 Available Options for create and open:
79 WRITE_THROUGH, READ_ONLY, O_DIRECT, NULLIO, NV_CACHE, BLOCK_IO, REMOVABLE
81 Available Options for assign and replace:
85 Enable target mode for fibre card specifying its WWN
86 scstadmin -enable 50:06:0B:00:00:39:71:78
88 Disable target mode for SCSI host specifying host number
89 scstadmin -disable host4
91 Create a new security group:
92 scstadmin -addgroup HOST01
94 Create a device given an already existing disk file:
95 scstadmin -adddev DISK01 -handler vdisk -path /vdisks/disk01.dsk -options READ_ONLY,WRITE_THROUGH
97 Assign a device to a security group:
98 scstadmin -assigndev DISK01 -group HOST01 -lun 1
100 Rename a security group:
101 scstadmin -RenameGroup HOST01 -to SERVER01
113 my $_DEF_CONFIG_ = '/etc/scst.conf';
118 my $_MAX_LUNS_ = 255;
119 my $_DEFAULT_GROUP_ = 'Default';
121 my $_SCSI_CLASS_ = '/sys/class/scsi_host';
122 my $_FC_CLASS_ = '/sys/class/fc_host';
123 my $_SCSI_ISP_ = '/proc/scsi/isp';
124 my $_SCSITGT_QLAISP_ = '/proc/scsi_tgt/qla_isp';
135 my %_HANDLER_MAP_ = ('cdrom' => $SCST::SCST::CDROM_TYPE,
136 'changer' => $SCST::SCST::CHANGER_TYPE,
137 'disk' => $SCST::SCST::DISK_TYPE,
138 'vdisk' => $SCST::SCST::VDISK_TYPE,
139 'vcdrom' => $SCST::SCST::VCDROM_TYPE,
140 'disk_perf' => $SCST::SCST::DISKPERF_TYPE,
141 'modisk' => $SCST::SCST::MODISK_TYPE,
142 'modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
143 'tape' => $SCST::SCST::TAPE_TYPE,
144 'tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
145 'processor' => $SCST::SCST::PROCESSOR_TYPE,
146 # Add in the dev_ names as well
147 'dev_cdrom' => $SCST::SCST::CDROM_TYPE,
148 'dev_changer' => $SCST::SCST::CHANGER_TYPE,
149 'dev_disk' => $SCST::SCST::DISK_TYPE,
150 'dev_disk_perf' => $SCST::SCST::DISKPERF_TYPE,
151 'dev_modisk' => $SCST::SCST::MODISK_TYPE,
152 'dev_modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
153 'dev_tape' => $SCST::SCST::TAPE_TYPE,
154 'dev_tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
155 'dev_processor' => $SCST::SCST::PROCESSOR_TYPE);
157 my %_REVERSE_MAP_ = ($SCST::SCST::CDROM_TYPE => 'cdrom',
158 $SCST::SCST::CHANGER_TYPE => 'changer',
159 $SCST::SCST::DISK_TYPE => 'disk',
160 $SCST::SCST::VDISK_TYPE => 'vdisk',
161 $SCST::SCST::VCDROM_TYPE => 'vcdrom',
162 $SCST::SCST::DISKPERF_TYPE => 'disk_perf',
163 $SCST::SCST::MODISK_TYPE => 'modisk',
164 $SCST::SCST::MODISKPERF_TYPE => 'modisk_perf',
165 $SCST::SCST::TAPE_TYPE => 'tape',
166 $SCST::SCST::TAPEPERF_TYPE => 'tape_perf',
167 $SCST::SCST::PROCESSOR_TYPE => 'processor');
169 my %_HANDLER_TYPE_MAP_ = ($SCST::SCST::IOTYPE_PHYSICAL => 'physical',
170 $SCST::SCST::IOTYPE_VIRTUAL => 'virtual',
171 $SCST::SCST::IOTYPE_PERFORMANCE => 'performance');
173 $SIG{INT} = \&commitSuicide;
175 use vars qw($Version);
213 my $p = new Getopt::Long::Parser;
215 if (!$p->getoptions('config:s' => \$applyConfig,
216 'ClearConfig' => \$clearConfig,
217 'ForceConfig' => \$forceConfig,
218 'WriteConfig=s' => \$writeConfig,
219 'checkConfig=s' => \$checkConfig,
220 'sessions' => \$showSessions,
221 'adddev=s' => \$addDev,
222 'path=s' => \$devPath,
223 'ReplaceDev=s' => \$replaceDev,
224 'RemoveDev=s' => \$removeDev,
226 'adduser=s' => \$addUser,
227 'MoveUser=s' => \$moveUser,
228 'RemoveUser=s' => \$removeUser,
229 'ClearUsers' => \$clearUsers,
230 'addgroup=s' => \$addGroup,
232 'RemoveGroup=s' => \$removeGroup,
233 'renamegroup=s' => \$renameGroup,
234 'assigndev=s' => \$assignDev,
235 'resyncdev=s' => \$resyncDev,
236 'ReleaseDev=s' => \$releaseDev,
237 'ClearDevs' => \$clearDevs,
238 'handler=s' => \$handler,
239 'group=s' => \$group,
240 'options=s' => \$options,
241 'blocksize=s' => \$blocksize,
242 'enable=s' => \$enable,
243 'disable=s' => \$disable,
244 'debug' => \$_DEBUG_)) {
248 if ((defined($enable) && !$enable) || (defined($disable) && !$disable)) {
249 print "Argument -enable/-disable requires a WWN or host.\n\n";
253 if ($handler && !$_HANDLER_MAP_{$handler}) {
254 print "Invalid handler '$handler' specified. Available handlers are:\n\n";
255 foreach my $_handler (keys %_HANDLER_MAP_) {
256 print "\t$_handler\n";
262 if ($addDev && !($handler && $devPath)) {
263 print "Please specify -handler and -path with -adddev.\n\n";
267 if (defined($blocksize) && !$blocksize) {
268 print "Please specify bytes with -blocksize.\n\n";
272 if ($blocksize && !$addDev) {
273 print "Please specify -adddev with -blocksize.\n";
277 if (defined($forceConfig) && !defined($applyConfig)) {
278 print "Please specify -config with -ForceConfig.\n\n";
282 if ($resyncDev && !$handler) {
283 print "Please specify -handler with -resyncdev.\n\n";
287 if ($removeDev && !$handler) {
288 print "Please specify -handler with -RemoveDev.\n\n";
292 if ($addUser && !defined($group)) {
293 print "Please specify -group with -adduser.\n\n";
297 if ($moveUser && (!defined($group) || !defined($toGroup))) {
298 print "Please specify -group and -to with -MoveUser.\n\n";
302 if ($removeUser && !defined($group)) {
303 print "Please specify -group with -RemoveUser.\n\n";
307 if ($clearUsers && !defined($group)) {
308 print "Please specify -group with -ClearUsers.\n\n";
312 if ($renameGroup && !defined($toGroup)) {
313 print "Please specify -to with -renamegroup.\n\n";
317 if ($assignDev && !defined($group)) {
318 print "Please specify -group with -assigndev.\n\n";
322 if ($replaceDev && (!defined($group) || !defined($devLun))) {
323 print "Please specify -group and -lun with -ReplaceDev.\n\n";
327 if ($releaseDev && !defined($group)) {
328 print "Please specify -group with -ReleaseDev.\n\n";
332 if ($clearDevs && !defined($group)) {
333 print "Please specify -group with -ClearDevs.\n\n";
337 if (defined($writeConfig) && !$writeConfig) {
338 print "Please specify a file name to write configuration to..\n\n";
342 $_DEBUG_ = $TRUE if (defined($_DEBUG_));
344 $forceConfig = $TRUE if (defined($forceConfig));
345 $showSessions = $TRUE if (defined($showSessions));
347 $enable =~ tr/A-Z/a-z/; $disable =~ tr/A-Z/a-z/;
348 $options =~ tr/a-z/A-Z/ if ($options);
350 if ((defined($showSessions) + defined($addDev) + defined($resyncDev) +
351 defined($removeDev)+ defined($addUser) + defined($enable) + defined($disable) +
352 defined($removeUser) + defined($clearUsers) + defined($assignDev) +
353 defined($releaseDev) + defined($clearDevs) + defined($applyConfig) +
354 defined($clearConfig) + defined($writeConfig) + defined($checkConfig)) > 1) {
355 print "Please specify only one operation at a time.\n";
359 $applyConfig = $_DEF_CONFIG_ if (defined($applyConfig) && !$applyConfig);
360 $checkConfig = $_DEF_CONFIG_ if (defined($checkConfig) && !$checkConfig);
362 return ($enable, $disable, $addDev, $devPath, $devLun, $resyncDev, $removeDev, $addUser,
363 $moveUser, $removeUser, $clearUsers, $addGroup, $renameGroup, $toGroup, $removeGroup,
364 $assignDev, $replaceDev, $releaseDev, $clearDevs, $handler, $group, $options, $blocksize,
365 $applyConfig, $forceConfig, $clearConfig, $writeConfig, $checkConfig, $showSessions);
371 STDOUT->autoflush(1);
373 # We need to run as root
374 if ( $> ) {die("This program must run as root.\n");}
376 my ($enable, $disable, $addDev, $devPath, $devLun, $resyncDev, $removeDev, $addUser,
377 $moveUser, $removeUser, $clearUsers, $addGroup, $renameGroup, $toGroup, $removeGroup,
378 $assignDev, $replaceDev, $releaseDev, $clearDevs, $handler, $group, $options, $blocksize,
379 $applyConfig, $forceConfig, $clearConfig, $writeConfig, $checkConfig, $showSessions) = getArgs();
381 $SCST = new SCST::SCST($_DEBUG_);
388 $rc = applyConfiguration($applyConfig, $TRUE, $TRUE);
389 die("Configuration errors found, aborting.\n") if ($rc);
391 print "\nConfiguration will apply in 10 seconds, type ctrl-c to abort..\n";
396 $rc = applyConfiguration($applyConfig, $forceConfig, $FALSE);
400 $rc = applyConfiguration($checkConfig, $FALSE, $TRUE);
404 $rc = writeConfiguration($writeConfig);
407 $showSessions && do {
408 $rc = showSessions();
411 defined($clearConfig) && do {
412 $rc = clearConfiguration();
416 $rc = addDevice($handler, $addDev, $devPath, $options, $blocksize);
420 $rc = resyncDevice($handler, $resyncDev);
424 $rc = removeDevice($handler, $removeDev);
428 $rc = addUser($group, $addUser);
432 $rc = moveUser($group, $moveUser, $toGroup);
436 $rc = removeUser($group, $removeUser);
439 defined($clearUsers) && do {
440 $rc = clearUsers($group);
444 $rc = addGroup($addGroup);
448 $rc = renameGroup($renameGroup, $toGroup);
452 $rc = removeGroup($removeGroup);
456 $rc = assignDevice($group, $assignDev, $devLun, $options);
460 $rc = replaceDevice($group, $replaceDev, $devLun, $options);
464 $rc = releaseDevice($group, $releaseDev);
467 defined($clearDevs) && do {
468 $rc = clearDevices($group);
472 $enable = unformatTarget($enable);
473 $rc = enableTarget($enable, $TRUE);
477 $disable = unformatTarget($disable);
478 $rc = enableTarget($disable, $FALSE);
482 print "No valid operations specified.\n";
492 sub readWorkingConfig {
495 print "Collecting current configuration.. ";
503 my $eHandlers = $SCST->handlers();
505 immediateExit($SCST->errorString());
507 foreach my $handler (@{$eHandlers}) {
508 $HANDLERS{$handler}++; # For quick lookups
511 $TARGETS = targets();
513 $DEVICES = $SCST->devices();
514 immediateExit($SCST->errorString());
516 my $_eGroups = $SCST->groups();
517 immediateExit($SCST->errorString());
519 foreach my $group (@{$_eGroups}) {
521 $ASSIGNMENTS{$group} = $SCST->groupDevices($group);
522 my $eUsers = $SCST->users($group);
524 foreach my $user (@{$eUsers}) {
525 $USERS{$group}->{$user}++; # For quick lookups
527 $USERS{$group} = \%empty if (!$USERS{$group});
533 sub writeConfiguration {
538 print "Failed to save current configuration, specified ".
539 "file exists and cannot be deleted.\n";
544 my $io = new IO::File $file, O_CREAT|O_WRONLY;
547 print "Failed to save configuration to file '$file': $!\n";
551 print "Writing current configuration to file '$file'.. ";
553 print $io "# Automatically generated by $Version.\n\n";
555 print $io "# NOTE: Options are pipe (|) seperated.\n\n";
558 foreach my $handler (sort keys %HANDLERS) {
559 if ($SCST->handlerType($handler) == $SCST::SCST::IOTYPE_VIRTUAL) {
560 print $io "[HANDLER ".$_REVERSE_MAP_{$handler}."]\n";
561 print $io "#DEVICE <vdisk name>,<device path>";
562 if ($handler == $SCST::SCST::VDISK_TYPE) {
563 print $io ",<options>,<block size>\n";
568 my $devices = $SCST->handlerDevices($handler);
570 immediateExit($SCST->errorString());
572 foreach my $device (sort keys %{$devices}) {
573 my $options = $$devices{$device}->{'OPTIONS'};
575 $options =~ s/\,/\|/g;
577 print $io "DEVICE $device,".$$devices{$device}->{'PATH'};
578 print $io ",$options";
579 print $io ",".$$devices{$device}->{'BLOCKSIZE'};
588 foreach my $group (sort keys %USERS) {
589 print $io "[GROUP $group]\n";
590 print $io "#USER <user wwn>\n";
592 foreach my $user (keys %{$USERS{$group}}) {
593 print $io "USER $user\n";
599 # Assignments configuration
600 foreach my $group (sort keys %ASSIGNMENTS) {
601 print $io "[ASSIGNMENT $group]\n";
602 print $io "#DEVICE <device name>,<lun>,<options>\n";
604 my $pointer = $ASSIGNMENTS{$group};
605 foreach my $device (sort keys %{$pointer}) {
606 print $io "DEVICE $device,".$$pointer{$device}."\n";
612 # Targets configuration
613 foreach my $type ('enable', 'disable') {
614 print $io "[TARGETS $type]\n";
615 print $io "#HOST <wwn identifier>\n";
617 foreach my $target (sort keys %{$TARGETS}) {
618 if ((($type eq 'enable') && $$TARGETS{$target}->{'enabled'}) ||
619 (($type eq 'disable') && !$$TARGETS{$target}->{'enabled'})) {
620 my $f_target = formatTarget($target);
621 print $io "HOST $f_target\n" if (!$$TARGETS{$target}->{'duplicate'});
635 sub applyConfiguration {
639 my $config = readConfig($confile);
645 my %used_assignments;
650 # Cache device/handler configuration
651 foreach my $entry (keys %{$$config{'HANDLER'}}) {
652 foreach my $device (@{$$config{'HANDLER'}->{$entry}->{'DEVICE'}}) {
653 my($vname, undef) = split(/\,/, $device, 2);
654 $vname = cleanupString($vname);
655 $used_devs{$vname} = $entry;
659 # Cache user/group configuration
660 foreach my $group (keys %{$$config{'GROUP'}}) {
661 foreach my $user (@{$$config{'GROUP'}->{$group}->{'USER'}}) {
662 if (defined($seen_users{$user})) {
663 print "\t-> FATAL: Configuration invalid. User '$user' is in more ".
667 $used_users{$group}->{$user}++;
668 $seen_users{$user}++;
670 $used_users{$group} = \%empty if (!$used_users{$group});
673 # Cache device association configuration
674 foreach my $group (keys %{$$config{'ASSIGNMENT'}}) {
675 foreach my $device (@{$$config{'ASSIGNMENT'}->{$group}->{'DEVICE'}}) {
676 my($vname, $arg) = split(/\,/, $device, 2);
677 $vname = cleanupString($vname);
678 $used_assignments{$group}->{$vname} = $arg;
682 # If -ForceConfig is used, check for configurations which we've deleted but are still active.
683 if ($force || $check) {
685 foreach my $group (sort keys %ASSIGNMENTS) {
686 if (!defined($used_assignments{$group}) && (keys %{$ASSIGNMENTS{$group}})) {
687 print "\t-> WARNING: Group '$group' has no associations in saved configuration";
690 print ", clearing all associations.\n";
691 if (clearDevices($group)) {
701 my $_assignments = $ASSIGNMENTS{$group};
703 foreach my $device (sort keys %{$_assignments}) {
704 if (!defined($used_assignments{$group}->{$device}) ||
705 ($$_assignments{$device} != $used_assignments{$group}->{$device})) {
706 if (defined($used_assignments{$group}->{$device}) &&
707 ($$_assignments{$device} != $used_assignments{$group}->{$device})) {
708 print "\t-> WARNING: Device '$device' assigned to group '$group' ".
709 "is at LUN ".$used_assignments{$group}->{$device}.
710 " whereas working configuration reflects LUN ".$$_assignments{$device};
712 print "\t-> WARNING: Device '$device' is not associated with group ".
713 "'$group' in saved configuration";
717 my $_lun = $$_assignments{$device};
719 my $replace_dev = findAssignedLun($used_assignments{$group}, $_lun);
721 if (defined($replace_dev) && ($replace_dev ne $device)) {
722 print ", replacing with device '$replace_dev'.\n";
724 if (replaceDevice($group, $replace_dev, $_lun)) {
730 print ", releasing.\n";
731 if (releaseDevice($group, $device)) {
747 foreach my $group (sort keys %USERS) {
748 next if ($group eq $_DEFAULT_GROUP_);
749 if (!defined($used_users{$group})) {
750 print "\t-> WARNING: Group '$group' does not exist in saved configuration";
753 print ", removing.\n";
754 if (clearUsers($group)) {
760 if (removeGroup($group)) {
770 foreach my $user (sort keys %{$USERS{$group}}) {
771 if (!defined($used_users{$group}->{$user})) {
772 print "\t-> WARNING: User '$user' is not defined as part of group '$group' ".
773 "in saved configuration";
776 # Are we moving this user to another group?
777 my $new_group = findUserGroup($user, $config);
778 if ($new_group && ($new_group ne $group)) {
779 print ", moving to group '$new_group'.\n";
780 if (moveUser($group, $user, $new_group)) {
786 print ", removing.\n";
787 if (removeUser($group, $user)) {
803 foreach my $device (sort keys %{$DEVICES}) {
804 if ($$DEVICES{$device} && !defined($used_devs{$device})) {
805 # Device gone, but is it still assigned to a group?
806 my $isAssigned = $FALSE;
807 foreach my $group (sort keys %used_assignments) {
808 if (defined($used_assignments{$group}->{$device})) {
809 print "\t-> WARNING: Device '$device' is not defined in saved configuration, ".
810 "however, it is still assigned to group '$group'! Ignoring removal.\n";
815 if (!$isAssigned && ($SCST->handlerType($$DEVICES{$device}) == $SCST::SCST::IOTYPE_VIRTUAL)) {
816 print "\t-> WARNING: Device '$device' is not defined in saved configuration";
819 print ", removing.\n";
820 if (removeDevice($_REVERSE_MAP_{$$DEVICES{$device}}, $device)) {
832 if ($_HANDLER_MAP_{$used_devs{$device}} != $$DEVICES{$device}) {
833 my $handler = $used_devs{$device};
835 if ($HANDLERS{$_HANDLER_MAP_{$handler}}) {
836 print "\t-> WARNING: Device '$device' changes handler to '$handler'";
839 print ", changing.\n";
840 if ($SCST->assignDeviceToHandler($device,
841 $_HANDLER_MAP_{$handler})) {
856 print "Applying configuration additions..\n" if (!$check);
859 readWorkingConfig() if ($force);
861 foreach my $_handler (sort keys %{$$config{'HANDLER'}}) {
862 if (!$HANDLERS{$_HANDLER_MAP_{$_handler}}) {
863 print "\t-> WARNING: Handler '$_handler' does not exist.\n";
868 foreach my $device (@{$$config{'HANDLER'}->{$_handler}->{'DEVICE'}}) {
869 my($vname, $path, $options, $blocksize) = split(/\,/, $device);
870 $path = cleanupString($path);
871 $options =~ s/\s+//g;
873 if (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} == $$DEVICES{$vname})) {
875 } elsif (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} != $$DEVICES{$vname})) {
876 if ($HANDLERS{$_HANDLER_MAP_{$_handler}}) {
877 print "\t-> WARNING: Device '$vname' changes handler from '".
878 $_REVERSE_MAP_{$$DEVICES{$vname}}."' to '$_handler'.\n".
879 "\t Use -ForceConfig to change device handler.\n" if (!$force && !$check);
885 print "\t-> New device '$_handler:$vname' at path '$path', options '$options', ".
886 "blocksize $blocksize.\n";
887 $$DEVICES{$vname} = $_HANDLER_MAP_{$_handler};
890 if (addDevice($_handler, $vname, $path, $options, $blocksize)) {
899 # Create new groups and add users..
900 foreach my $group (keys %used_users) {
901 if (!defined($USERS{$group})) {
903 print "\t-> New group definition '$group.'\n";
907 if (addGroup($group)) {
915 foreach my $user (keys %{$used_users{$group}}) {
916 if (!defined($USERS{$group}->{$user})) {
917 my $move_group = findUserGroupInCurrent($user);
919 print "\t-> WARNING: Use -ForceConfig to move user '$user' ".
920 "from group '$move_group' to group '$group'.\n" if (!$force);
925 print "\t-> New user definition '$user' for group '$group'.\n";
926 $USERS{$group}->{$user}++;
929 if (addUser($group, $user)) {
939 # Assign new devices to groups..
940 foreach my $group (keys %used_assignments) {
941 if (!defined($GROUPS{$group})) {
942 # Looks like we're lacking a group. We'll create an empty one
944 print "\t-> WARNING: Auto-creating an empty group '$group' since none was configured.\n";
946 if (addGroup($group)) {
953 if (!defined($GROUPS{$group})) {
954 print "\t-> WARNING: Unable to assign to non-existant group '$group'.\n";
959 foreach my $vname (keys %{$used_assignments{$group}}) {
960 my $arg = $used_assignments{$group}->{$vname};
961 my($lun, $options) = split(/\,/, $arg);
962 my $_assignments = $ASSIGNMENTS{$group};
964 if (defined($$_assignments{$vname}) && ($$_assignments{$vname} == $lun)) {
966 } elsif (defined($$_assignments{$vname}) && ($$_assignments{$vname} != $lun)) {
967 print "\t-> Device '$vname' assigned to group '$group' is at LUN ".$$_assignments{$vname}.
968 ", whereas the working configuration reflects LUN $lun.\n".
969 "\t Use -ForceConfig to force this LUN change.\n" if (!$force && !$check);
971 my $replace_dev = findAssignedLun($_assignments, $lun);
973 if (defined($replace_dev) && ($vname ne $replace_dev)) {
974 print "\t-> WARNING: Use -ForceConfig to replace device '$replace_dev' ".
975 "with device '$vname' for group '$group'.\n" if (!$force);
980 $lun = 'auto' if (!defined($lun));
981 print "\t-> New device assignment for '$vname' to group '$group' at LUN $lun.\n";
984 if (assignDevice($group, $vname, $lun, $options)) {
994 # Enable/Disable configured targets
995 foreach my $type (keys %{$$config{'TARGETS'}}) {
998 if ($type eq 'enable') {
1000 } elsif ($type eq 'disable') {
1003 print "\t-> WARNING: Ignoring invalid TARGETS specifier '$type'. ".
1004 "Should be one of enable,disable.\n";
1008 foreach my $target (@{$$config{'TARGETS'}->{$type}->{'HOST'}}) {
1009 my $i_target = unformatTarget($target);
1011 if (!defined($$TARGETS{$i_target})) {
1012 print "\t-> WARNING: Target '$target' not found on system.\n";
1017 next if ($enable == targetEnabled($i_target));
1019 if (!$enable && targetEnabled($target)) {
1020 if ($force || $check) {
1021 print "\t-> WARNING: Target mode for '$target' is currently enabled, ".
1022 "however configuration file wants it disabled";
1025 print ", disabling.\n";
1026 if (enableTarget($target, $enable)) {
1037 print "\t-> Target '$target' is enabled in configuration file, ".
1038 "however is currently disabled";
1041 print ", enabling.\n";
1042 if (enableTarget($target, $enable)) {
1055 print "\nEncountered $errs error(s) while processing.\n" if ($errs);
1058 print "Configuration checked, $changes difference(s) found with working configuration.\n";
1060 $changes = 0 if ($_DEBUG_);
1061 print "Configuration applied, $changes changes made.\n";
1064 return $TRUE if ($errs);
1068 sub clearConfiguration {
1071 print "WARNING: This removes ALL applied SCST configuration and may result in data loss!\n";
1072 print "If this is not what you intend, press ctrl-c now. Waiting 10 seconds.\n\n";
1075 print "\nRemoving all users and groups:\n\n";
1076 foreach my $group (keys %GROUPS) {
1077 $errs += removeGroup($group) if ($group ne $_DEFAULT_GROUP_);
1080 print "\nRemoving all handler devices:\n\n";
1081 foreach my $device (keys %{$DEVICES}) {
1082 next if (!$$DEVICES{$device});
1083 next if ($SCST->handlerType($$DEVICES{$device}) != $SCST::SCST::IOTYPE_VIRTUAL);
1084 $errs += removeDevice($_REVERSE_MAP_{$$DEVICES{$device}}, $device);
1087 print "\nEncountered $errs error(s) while processing.\n" if ($errs);
1088 print "\nConfiguration cleared.\n";
1090 return $TRUE if ($errs);
1095 my $sessions = $SCST->sessions();
1096 immediateExit($SCST->errorString());
1098 print "\n\tTarget Name\tInitiator Name\t\t\tGroup Name\t\tCommand Count\n";
1100 foreach my $target (keys %{$sessions}) {
1101 foreach my $group (keys %{$$sessions{$target}}) {
1102 foreach my $user (keys %{$$sessions{$target}->{$group}}) {
1103 my $commands = $$sessions{$target}->{$group}->{$user};
1105 print "\t$target\t$user\t\t$group\t\t$commands\n";
1116 my $handler = shift;
1119 my $options = shift;
1120 my $blocksize = shift;
1122 my $_handler = $_HANDLER_MAP_{$handler};
1123 my $htype = $SCST->handlerType($_handler);
1126 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1130 if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1131 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1132 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1133 print "WARNING: Handler $handler of type $typeString is incapable of ".
1134 "opening/closing devices. Valid handlers are:\n".
1135 validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1139 if (defined($$DEVICES{$device})) {
1140 print "WARNING: Device '$device' already defined.\n";
1144 print "\t-> Opening virtual device '$device' at path '$path' using handler '$handler'..\n";
1146 if ($SCST->openDevice($_handler, $device, $path, $options, $blocksize)) {
1147 print "WARNING: Failed to open virtual device '$device' at path '$path': ".
1148 $SCST->errorString()."\n";
1152 $$DEVICES{$device} = $_handler;
1158 my $handler = shift;
1161 my $_handler = $_HANDLER_MAP_{$handler};
1162 my $htype = $SCST->handlerType($_handler);
1164 if (!defined($$DEVICES{$device})) {
1165 print "WARNING: Device '$device' not defined.\n";
1169 print "\t-> Resync'ing virtual device '$device'..\n";
1171 if ($SCST->resyncDevice($_handler, $device)) {
1172 print "WARNING: Failed to resync virtual device '$device': ".
1173 $SCST->errorString()."\n";
1181 my $handler = shift;
1184 my $_handler = $_HANDLER_MAP_{$handler};
1185 my $htype = $SCST->handlerType($_handler);
1188 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1192 if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1193 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1194 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1195 print "WARNING: Handler $handler of type $typeString is incapable of ".
1196 "opening/closing devices. Valid handlers are:\n".
1197 validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1201 if (!defined($$DEVICES{$device})) {
1202 print "WARNING: Device '$device' not defined.\n";
1206 print "\t-> Closing virtual device '$device'..\n";
1208 if ($SCST->closeDevice($_handler, $device)) {
1209 print "WARNING: Failed to close virtual device '$device': ".
1210 $SCST->errorString()."\n";
1214 undef $$DEVICES{$device};
1222 if (defined($GROUPS{$group})) {
1223 print "WARNING: Group '$group' already exists.\n";
1227 print "\t-> Creating security group '$group'..\n";
1229 if ($SCST->addGroup($group)) {
1230 print "WARNING: Failed to create security group '$group': ".
1231 $SCST->errorString()."\n";
1242 my $toGroup = shift;
1244 if (defined($GROUPS{$toGroup})) {
1245 print "WARNING: Group '$toGroup' already exists.\n";
1249 print "\t-> Renaming security group '$group' to '$toGroup'..\n";
1251 if ($SCST->renameGroup($group, $toGroup)) {
1252 print "WARNING: Failed to rename security group '$group' to ".
1253 "'$toGroup': ".$SCST->errorString()."\n";
1257 delete $GROUPS{$group};
1258 $GROUPS{$toGroup}++;
1266 return $FALSE if ($group eq $_DEFAULT_GROUP_);
1268 if (!defined($GROUPS{$group})) {
1269 print "WARNING: Group '$group' does not exist.\n";
1273 print "\t-> Removing security group '$group'..\n";
1275 if ($SCST->removeGroup($group)) {
1276 print "WARNING: Failed to remove security group '$group': ".
1277 $SCST->errorString()."\n";
1281 undef $GROUPS{$group};
1290 if (!defined($GROUPS{$group})) {
1291 print "WARNING: Failed to add user '$user' to group '$group', group does not exist.\n";
1295 if (defined($USERS{$group}->{$user})) {
1296 print "WARNING: User '$user' already exists in security group '$group'.\n";
1300 print "\t-> Adding user '$user' to security group '$group'..\n";
1302 if ($SCST->addUser($user, $group)) {
1303 print "WARNING: Failed to add user '$user' to security group '$group': ".
1304 $SCST->errorString()."\n";
1308 $USERS{$group}->{$user}++;
1316 my $toGroup = shift;
1318 if (!defined($GROUPS{$group})) {
1319 print "WARNING: Failed to move user '$user' from group '$group', group does not exist.\n";
1323 if (defined($USERS{$toGroup}->{$user})) {
1324 print "WARNING: User '$user' already exists in security group '$toGroup'.\n";
1328 print "\t-> Moving user '$user' from security group '$group' to security group '$toGroup'..\n";
1330 if ($SCST->moveUser($user, $group, $toGroup)) {
1331 print "WARNING: Failed to move user '$user' from security group '$group' to ".
1332 "security group '$toGroup': ".$SCST->errorString()."\n";
1336 delete $USERS{$group}->{$user};
1337 $USERS{$toGroup}->{$user}++;
1346 if (!defined($GROUPS{$group})) {
1347 print "WARNING: Failed to remove user '$user' from group '$group', group does not exist.\n";
1351 if (!defined($USERS{$group}->{$user})) {
1352 print "WARNING: User '$user' doesn\'t exist in security group '$group'.\n";
1356 print "\t-> Removing user '$user' from security group '$group'..\n";
1358 if ($SCST->removeUser($user, $group)) {
1359 print "WARNING: Failed to remove user '$user' to security group '$group': ".
1360 $SCST->errorString()."\n";
1364 undef $USERS{$group}->{$user};
1372 if (!defined($GROUPS{$group})) {
1373 print "WARNING: Failed to clear users from group '$group', group does not exist.\n";
1377 print "\t-> Clearing users from security group '$group'..\n";
1379 if ($SCST->clearUsers($group)) {
1380 print "WARNING: Failed to clear users from security group '$group': ".
1381 $SCST->errorString()."\n";
1385 undef $USERS{$group};
1394 my $options = shift;
1397 # Put luns into something easier to parse..
1398 foreach my $_group (keys %ASSIGNMENTS) {
1399 my $_gAssigns = $ASSIGNMENTS{$_group};
1401 foreach my $_device (keys %{$_gAssigns}) {
1402 @{$allLuns{$_group}}[$$_gAssigns{$_device}] = $_device;
1406 # Use the next available LUN if none specified
1407 if ($lun !~ /\d+/) {
1408 $lun = ($#{$allLuns{$group}} + 1);
1409 if ($lun > $_MAX_LUNS_) {
1410 print "ERROR: Unable to use next available LUN of $lun, lun out of range.\n";
1414 print "\t-> Device '$device': Using next available LUN of $lun for group '$group'.\n";
1417 if (($lun < 0) || ($lun > $_MAX_LUNS_)) {
1418 print "ERROR: Unable to assign device '$device', lun '$lun' is out of range.\n";
1422 if (!defined($$DEVICES{$device})) {
1423 print "WARNING: Unable to assign non-existant device '$device' to group '$group'.\n";
1427 if (@{$allLuns{$group}}[$lun]) {
1428 print "ERROR: Device '$device': Lun '$lun' is already assigned to device '".@{$allLuns{$group}}[$lun]."'.\n";
1432 print "\t-> Assign virtual device '$device' to group '$group' at LUN '$lun'..\n";
1434 if ($SCST->assignDeviceToGroup($device, $group, $lun, $options)) {
1435 print "WARNING: Failed to assign device '$device' to group '$group': ".
1436 $SCST->errorString()."\n";
1440 if (!defined($ASSIGNMENTS{$group})) {
1442 $ASSIGNMENTS{$group} = \%assignments_t;
1445 my $_assignments = $ASSIGNMENTS{$group};
1447 $$_assignments{$device} = $lun;
1454 my $newDevice = shift;
1456 my $options = shift;
1459 # Put luns into something easier to parse..
1460 foreach my $_group (keys %ASSIGNMENTS) {
1461 my $_gAssigns = $ASSIGNMENTS{$_group};
1463 foreach my $_device (keys %{$_gAssigns}) {
1464 @{$allLuns{$_group}}[$$_gAssigns{$_device}] = $_device;
1468 if (!defined($$DEVICES{$newDevice})) {
1469 print "WARNING: Unable to assign non-existant device '$newDevice' to group '$group'.\n";
1473 if (${$allLuns{$group}}[$lun] eq $newDevice) {
1474 print "ERROR: Device '$newDevice': Lun '$lun' is already assigned to device '$newDevice'.\n";
1478 print "\t-> Replace device at LUN '$lun' in group '$group' with new device '$newDevice'..\n";
1480 if ($SCST->replaceDeviceInGroup($newDevice, $group, $lun, $options)) {
1481 print "WARNING: Failed to replace LUN '$lun' in group '$group' with new device '$newDevice': ".
1482 $SCST->errorString()."\n";
1486 if (!defined($ASSIGNMENTS{$group})) {
1488 $ASSIGNMENTS{$group} = \%assignments_t;
1491 my $_assignments = $ASSIGNMENTS{$group};
1493 delete $$_assignments{${$allLuns{$group}}[$lun]};
1494 $$_assignments{$newDevice} = $lun;
1503 if (!defined($GROUPS{$group})) {
1504 print "WARNING: Failed to release device '$device' from group '$group', group does not exist.\n";
1508 if (!defined($$DEVICES{$device})) {
1509 print "WARNING: Failed to release device '$device', device not defined.\n";
1513 print "\t-> Release virtual device '$device' from group '$group'..\n";
1515 if ($SCST->removeDeviceFromGroup($device, $group)) {
1516 print "WARNING: Failed to release device '$device' from group '$group': ".
1517 $SCST->errorString()."\n";
1521 my $_assignments = $ASSIGNMENTS{$group};
1523 undef $$_assignments{$device};
1531 if (!defined($GROUPS{$group})) {
1532 print "WARNING: Failed to clear devices from group '$group', group does not exist.\n";
1536 print "\t-> Clear virtual devices from group '$group'..\n";
1538 if ($SCST->clearGroupDevices($group)) {
1539 print "WARNING: Failed to clear devices from group '$group': ".
1540 $SCST->errorString()."\n";
1544 undef $ASSIGNMENTS{$group};
1553 my $root = new IO::Dir $_FC_CLASS_ if (-d $_FC_CLASS_);
1556 while (my $entry = $root->read()) {
1557 next if (($entry eq '.') || ($entry eq '..'));
1559 my $io = new IO::File "$_FC_CLASS_/$entry/port_name", O_RDONLY;
1566 $fcards{$entry} = $wwn;
1571 $root = new IO::Dir $_SCSI_CLASS_ if (-d $_SCSI_CLASS_);
1574 while (my $entry = $root->read()) {
1575 next if (($entry eq '.') || ($entry eq '..'));
1577 my $io = new IO::File "$_SCSI_CLASS_/$entry/target_mode_enabled", O_RDONLY;
1580 my $enabled = <$io>;
1584 $targets{$entry}->{'path'} = "$_SCSI_CLASS_/$entry/target_mode_enabled";
1585 $targets{$entry}->{'enabled'} = $enabled;
1586 $targets{$entry}->{'qla_isp'} = $FALSE;
1588 if ($fcards{$entry}) {
1589 $targets{$fcards{$entry}}->{'enabled'} = $enabled;
1590 $targets{$fcards{$entry}}->{'path'} =
1591 "$_SCSI_CLASS_/$entry/target_mode_enabled";
1592 $targets{$entry}->{'duplicate'} = $TRUE;
1594 $targets{$entry}->{'duplicate'} = $FALSE;
1600 $root = new IO::Dir $_SCSI_ISP_ if (-d $_SCSI_ISP_);
1603 while (my $entry = $root->read()) {
1604 next if (($entry eq '.') || ($entry eq '..'));
1607 my $io = new IO::File "$_SCSI_ISP_/$entry", O_RDONLY;
1617 ($wwn) = ($fstr =~ '.*?Port WWN +([^\ ]+) .*');
1618 $fcards{$entry} = $wwn;
1620 $io = new IO::File "$_SCSITGT_QLAISP_/$entry", O_RDONLY;
1625 ($enabled2) = ($fstr =~ '[^\n]+\n *\d *: *(\d)');
1626 $targets{$entry}->{'path'} = "$_SCSITGT_QLAISP_/$entry";
1627 $targets{$entry}->{'enabled'} = $enabled2;
1628 $targets{$entry}->{'qla_isp'} = $TRUE;
1630 if ($fcards{$entry}) {
1631 $targets{$fcards{$entry}}->{'enabled'} = $enabled2;
1632 $targets{$fcards{$entry}}->{'path'} = "$_SCSITGT_QLAISP_/$entry";
1633 $targets{$fcards{$entry}}->{'qla_isp'} = $TRUE;
1634 $targets{$entry}->{'duplicate'} = $TRUE;
1636 $targets{$entry}->{'duplicate'} = $FALSE;
1649 return undef if (!defined($$TARGETS{$target}));
1650 return $$TARGETS{$target}->{'enabled'};
1657 $target = unformatTarget($target);
1659 return undef if (!defined($$TARGETS{$target}));
1661 my $io = new IO::File $$TARGETS{$target}->{'path'}, O_WRONLY;
1662 return $TRUE if (!$io);
1664 print $enable ? "\t-> Enabling" : "\t-> Disabling";
1665 print " target mode for SCST host '$target'.\n";
1668 print "DBG($$): ".$$TARGETS{$target}->{'path'}." -> $enable\n\n";
1670 if ($$TARGETS{$target}->{'qla_isp'} == $FALSE) {
1673 print $io $enable ? "enable all" : "disable all";
1679 $$TARGETS{$target}->{'enabled'} = $enable;
1685 my $confile = shift;
1693 my $io = new IO::File $confile, O_RDONLY;
1695 die("FATAL: Unable to open specified configuration file $confile: $!\n") if (!$io);
1697 while (my $line = <$io>) {
1698 ($line, undef) = split(/\#/, $line, 2);
1699 $line = cleanupString($line);
1701 if ($line =~ /^\[(.*)\]$/) {
1702 ($section, $arg) = split(/\s+/, $1, 2);
1704 if ($last_arg && ($last_arg ne $arg) &&
1705 !defined($config{$last_section}->{$last_arg})) {
1706 $config{$last_section}->{$last_arg} = \%empty;
1710 $last_section = $section;
1711 } elsif ($section && $arg && $line) {
1712 my($parameter, $value) = split(/\s+/, $line, 2);
1714 push @{$config{$section}->{$arg}->{$parameter}}, $value;
1727 foreach my $group (keys %{$$config{'GROUP'}}) {
1728 foreach my $_user (@{$$config{'GROUP'}->{$group}->{'USER'}}) {
1729 return $group if ($_user eq $user);
1736 sub findUserGroupInCurrent {
1739 foreach my $group (keys %USERS) {
1740 foreach my $_user (keys %{$USERS{$group}}) {
1741 return $group if ($_user eq $user);
1748 sub findAssignedLun {
1749 my $associations = shift;
1752 return undef if (!defined($lun));
1754 foreach my $device (keys %{$associations}) {
1755 if ($$associations{$device} == $lun) {
1766 $string =~ s/^\s+//;
1767 $string =~ s/\s+$//;
1775 if ($target =~ /^0x/) {
1777 my($o1, $o2, $o3, $o4, $o5, $o6, $o7, $o8) = unpack("A2A2A2A2A2A2A2A2", $target);
1778 $target = "$o1:$o2:$o3:$o4:$o5:$o6:$o7:$o8";
1781 $target =~ tr/A-Z/a-z/;
1786 sub unformatTarget {
1789 if ($target =~ /^.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}/) {
1791 $target = "0x$target";
1794 $target =~ tr/A-Z/a-z/;
1799 sub validHandlerTypes {
1803 foreach my $handler (keys %_REVERSE_MAP_) {
1804 $buffer .= "\t".$_REVERSE_MAP_{$handler}."\n" if ($SCST->handlerType($handler) == $type);
1810 # If we have an unread error from SCST, exit immediately
1814 return if (!$error);
1816 print "\n\nFATAL: Received the following error:\n\n\t";
1824 print "\n\nAborting immediately.\n";