# linuxAddPool.pl
#
# by Dean Giles 
# Version 1.0 
# June 11, 2007
#
# This Perl script creates an NSS pool on a specified partition.
# 
# as a snapshot data repository.
# This script creates an NSS pool on a Linux server.

# This script has been designed to be run on Linux. 
# The API set can be downloaded from  http://developer.novell.com/wiki/index.php/Virtual_File_Services_for_NetWare. 
# 
# The Perl script requires the name of an NSS pool, a storage pool, and the name of the new snapshot
# as input.  It requires that NSS be installed and an NSS pool be present.
#

{
	# User help function
	if (@ARGV < 3 || $ARGV[0] eq "/?" || $ARGV[0] eq "-?") 
	{
		print "USAGE: perl linuxAddPool.pl POOL_NAME DEVICE_NAME SIZE\n";
		print "Where POOL_NAME is the name of the new pool to add";
		print "Where DEVICE_NAME is the linux device name (ie. sda, sdb, hda, hdb...)";
		print "where SIZE is the size of the pool to add in bytes";
		print "Example: perl linuxAddPool.pl pool1 sda  1000000000\n";
		print "this will create an NSS pool named pool1 on device sda that is 1GB in size.\n";
		exit;
	}

	# Global Variables
	$poolName = $ARGV[0];  #Name of the NSS pool that will be added.
	$deviceName = $ARGV[1];	#Name of the linux device that the pool will be added to.
	$size = $ARGV[2]; #Size of the pool to add. 
	$file = "+</_admin/Manage_NSS/manage.cmd"; # This is the MSS management file we are accessing.
	$reply;	   # Variable used to read from file.
	$command; # This is the NSS xml command that will be the NSS request to the file.
	$error;  # Variable used to manage errors.
	
	# Open the file and check that it exists.
	open(NSSFILE, $file) 
  		or die "Error opening NSS management file ($!) on server";
	print "file is now open \n";
	$command = "<virtualIO><datastream name=\"command\"/></virtualIO>".
			"<nssRequest><pool><addPool2 state=\"active\"><poolName>$poolName</poolName>".
			"<ndsName/><context/><lssType>ZLSS</lssType>".
			"<devices><addPoolDeviceInfo><objectID>$deviceName</objectID>".
			"<size>$size</size></addPoolDeviceInfo></devices>".
			"</addPool2></pool></nssRequest>"; 
	# This is the command for initializing the virtual file for sending a command.
	print "File Name: $file \n";
	print "Request Sent: $command \n"; 

	
	if (!syswrite(NSSFILE, $command, length($command))) #Initialize the nss management file and write a command.
	{
		
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
		sysread (NSSFILE, $error, 10000);  #Read the error message.
		
		print "Error writing initialization to management file. \n";
		print "$error  \n\n";	 #Print the error message to the screen.
		close (NSSFILE);
	}
	else
	{
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
	
		sysread (NSSFILE, $reply, 10000);  #Read the reply.
	
		print "$reply  \n\n";	 #Print the reply to the screen.
		
		close (NSSFILE);
  	}
}		


