2010-07-18 15:18:59 +00:00
#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#
#-----------------------------------------------------------------------------
=head1 db2setup
This script is called after the installation of DB2
on xCAT. It will automate the setup of the DB2 to run
xCAT on the IBM DB2 database on AIX and Linux.
On AIX ( AIX61J - AIX61 TL5(AIX 6.1.5.0)) or later and Linux.
2010-07-29 17:31:28 +00:00
As an option it will also setup the ODBC for C/C++ applications
to be able to use the database.
2010-07-18 15:18:59 +00:00
It will setup an xcatdb
database ,a db2instance id, name xcatdb ( same as database),
and instance password to be used in the /etc/xcat/cfgloc file
to access the database.
It will interact for the password to assign to the xcatdb
for the Unix id and DB2 instance id,
unless the XCATDB2PW env var is set to the password for the instance id.
2010-07-19 13:23:49 +00:00
For Client setup, it will interact for the hostname or ip address
of the DB2 Server as known by the Service Node, unless the
XCATDB2SERVER environement variable is set.
2010-07-18 15:18:59 +00:00
See man db2setup for more information and the xCAT2SetupDB2.pdf
documentation.
2010-07-19 12:04:51 +00:00
The script will assume the default install paths of /opt/IBM/db2 for AIX and
2010-07-18 15:18:59 +00:00
/opt/ibm/db2 for Linux, unless overridden with the XCATDB2INSPATH
environment variable.
=cut
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
$::XCATDIR = $ENV{'XCATDIR'} ? $ENV{'XCATDIR'} : '/etc/xcat';
}
# if AIX - make sure we include perl 5.8.2 in INC path.
# Needed to find perl dependencies shipped in deps tarball.
# TODO: Remove during FVT should not need because DBD is compiled with
# 5.8.8 and we require 61J or later which runs with 5.8.8 Perl.
if ($^O =~ /^aix/i)
{
use lib "/usr/opt/perl5/lib/5.8.2/aix-thread-multi";
use lib "/usr/opt/perl5/lib/5.8.2";
use lib "/usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-multi";
use lib "/usr/opt/perl5/lib/site_perl/5.8.2";
}
use lib "$::XCATROOT/lib/perl";
use DBI;
use xCAT::Utils;
use xCAT::NetworkUtils;
use Getopt::Long;
use xCAT::MsgUtils;
use xCAT::Table;
use Expect;
use Socket;
use strict;
#-----------------------------------------------------------------------------
# Main
$::progname = "db2sqlsetup";
my $args = join ' ', @ARGV;
$::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
$::databaseloc = "/var/lib/db2"; # database loccation
# parse the options
if (
!GetOptions(
'i|init' => \$::INIT,
'S|Server' => \$::SERVER,
'C|Client' => \$::CLIENT,
'o|odbc' => \$::SETUPODBC,
'h|help' => \$::HELP,
'v|version' => \$::VERSION,
'V|verbose' => \$::VERBOSE,
)
)
{
&usage;
exit(1);
}
# display the usage if -h or --help is specified
if ($::HELP)
{
&usage;
exit(0);
}
# display the version statement if -v or --version is specified
if ($::VERSION)
{
my $version = xCAT::Utils->Version();
xCAT::MsgUtils->message("I", $version);
exit 0;
}
if ((!($::INIT)) && (!($::SETUPODBC)))
{
xCAT::MsgUtils->message("I", "Either -i or -o flag must be chosen");
&usage;
exit(1);
}
if ((!($::SERVER)) && (!($::CLIENT)))
{
xCAT::MsgUtils->message("I", "Either -S or -C flag must be chosen");
&usage;
exit(1);
}
if (($::SERVER) && ($::CLIENT))
{
xCAT::MsgUtils->message("I", "Either -S or -C flag must be chosen");
&usage;
exit(1);
}
2010-07-29 17:31:28 +00:00
if ((!($::SERVER)) && (!($::CLIENT)) && ($::SETUPODBC))
{
xCAT::MsgUtils->message("I",
"Either -S or -C flag must be chosen with the -o flag");
&usage;
exit(1);
}
2010-07-18 15:18:59 +00:00
# check to see if only odbc update, no passwords needed
my $odbconly = 0;
if ((!($::INIT)) && ($::SETUPODBC))
{
$odbconly = 1;
}
#
# Get OS
#
if (xCAT::Utils->isAIX())
{
$::osname = 'AIX';
}
else
{
$::osname = 'Linux';
# determine whether redhat or sles
$::linuxos = xCAT::Utils->osver();
}
# overide the default install path with environment variable
if ($ENV{'XCATDB2INSPATH'}) # input where DB2 is installed
{
$::installdb2dir = $ENV{'XCATDB2INSPATH'};
}
else
{
if ($::osname eq 'AIX')
{
$::installdb2dir = "/opt/IBM/db2/V9.7"; # default
}
else
{ # linux
$::installdb2dir = "/opt/ibm/db2/V9.7"; # default
}
}
#
# check to see if DB2 is installed, in given location
#
if (!(-e ($::installdb2dir)))
{
my $message =
"\nDB2 is not installed. It should be first obtained and installed into $::installdb2dir.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
# If setting up the Server, check to see if DB2 is running
$::db2running = 0;
$::xcatrunningdb2 = 0;
if ($::SERVER)
{
my $cmd = "ps -ef | grep db2";
my @output = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC == 0) # already running
{
my $db2check = "db2agent"; # see if really running
if (grep(/$db2check/, @output))
{
if ($::INIT)
{
my $message =
"DB2 is running. Database initialization will not take place.";
xCAT::MsgUtils->message("I", "$message");
}
$::db2running = 1;
}
}
if (-e ("/etc/xcat/cfgloc")) # check to see if xcat is using DB2 already
{ # cfgloc exists
$cmd = "fgrep DB2 /etc/xcat/cfgloc";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC == 0)
{
if ($::INIT)
{
my $message =
"The /etc/xcat/cfgloc file is already configured for DB2. xCAT appears to be already running on DB2. xcat database configuration will not take place.";
xCAT::MsgUtils->message("I", "$message");
}
$::xcatrunningdb2 = 1;
}
}
}
# if not just odbc update and xcat MN is not already running on DB2
2010-07-29 17:31:28 +00:00
# or if we are setting up the Client and not just the ODBC
# then Get instance password
2010-07-18 15:18:59 +00:00
#
2010-07-29 17:31:28 +00:00
if ((($odbconly == 0) && ($::xcatrunningdb2 == 0)) || (($::CLIENT) && ($odbconly == 0)))
2010-07-18 15:18:59 +00:00
{
if ($ENV{'XCATDB2PW'}) # passwd supplied
{
$::adminpassword = $ENV{'XCATDB2PW'};
}
else
{ #prompt for password
my $msg = "Input the password for xcatdb instance id: ";
xCAT::MsgUtils->message('I', "$msg");
`stty -echo`;
chop($::adminpassword = <STDIN>);
`stty echo`;
}
}
# initial setup request and not already running db2
if (($::INIT) && ($::xcatrunningdb2 == 0))
{
if ($::SERVER)
{ # setting up server
#
# Backup current database
#
my $homedir = xCAT::Utils->getHomeDir();
$::backupdir = $homedir;
if ($::osname eq 'AIX')
{
$::backupdir .= "xcat-dbback";
}
else
{
$::backupdir .= "/xcat-dbback";
}
&backupxcatdb;
2010-08-19 13:27:40 +00:00
# shutdown the xcatd daemon while migrating
&shutdownxcatd;
2010-07-18 15:18:59 +00:00
#
# Get MN name from site.master in backed up database
# if that does not exist use resolved hostname
# double check site.master for resolution
my $sitefile = "$::backupdir/site.csv";
my $cmd = "grep master $sitefile";
my @output = xCAT::Utils->runcmd($cmd, -1);
my $hname;
if ($::RUNCMD_RC != 0) # no entry in site table
{
$hname = `hostname`;
chomp $hname;
}
else # from site.master
{
(my $attr, my $master) = split(",", $output[0]);
(my $q, $hname) = split("\"", $master);
chomp $hname;
}
my $ipaddr = xCAT::NetworkUtils->getipaddr($hname);
if ($ipaddr)
{
$::MN = $ipaddr;
}
else
{
xCAT::MsgUtils->message("E",
"Hostname resolution for $hname failed.");
exit(1);
}
} # end setting up SERVER
# DB2 not running, or setting up Client, continue setting up DB2
if (($::db2running == 0) || ($::CLIENT))
{
# Add DB2 user and group and xcatdb instance for AIX/Linux
&mkdb2user;
# Setup /etc/services
&setupservices;
# Setup the xcatdb instance
&setupinstance;
#
# Setup DB2 env variables
#
&setupdb2env;
#
# Start DB2 server
#
if ($::SERVER)
{
&db2start;
2010-08-25 15:30:21 +00:00
#
# Setup DB2 to restart on reboot
#
&db2reboot;
}
2010-07-18 15:18:59 +00:00
}
# On server, xcat not already configured to run DB2 add xCAT database
if (($::SERVER) && ($::xcatrunningdb2 == 0))
{
2010-08-11 13:29:46 +00:00
#
# update DBM config
#
&updateDBM;
2010-07-18 15:18:59 +00:00
#
# Create the xcatdb database
#
&createxcatdb;
#
# create cfgloc file
#
&createcfgloc;
}
# if setting up Client catalog the MN (db2 server address)
2010-07-19 13:23:49 +00:00
# and create /etc/xcat/cfgloc
2010-07-18 15:18:59 +00:00
if ($::CLIENT)
{
&catalogServer;
2010-07-19 13:23:49 +00:00
&createcfgloc;
2010-07-18 15:18:59 +00:00
}
#
# Restore backed up database into DB2
#
if ($::SERVER)
{
&restorexcatdb;
xCAT::MsgUtils->message("I",
"xCAT is now running on the DB2 database.\n");
}
else
{
xCAT::MsgUtils->message("I", "xCAT DB2 Client setup is complete.\n");
}
} # end initialization
if ($::SETUPODBC)
{
#
# set up the ODBC on the Management Node
#
2010-07-29 17:31:28 +00:00
&setupODBC;
2010-07-18 15:18:59 +00:00
}
exit;
#####################################
# subroutines
#####################################
#-----------------------------------------------------------------------------
=head3 usage
Displays message for -h option
=cut
#-----------------------------------------------------------------------------
sub usage
{
xCAT::MsgUtils->message(
'I',
"Usage:\ndb2sqlsetup - Performs the setup of IBM DB2 for xCAT to use as its database. See man db2sqlsetup for more information."
);
my $msg =
2010-07-29 17:31:28 +00:00
"db2sqlsetup <-h|--help>\n <-v|--version>\n <-i|--init> <-S|-C> [-o|--setupODBC] [-V|--verbose]\n <-o|--setupODBC> <-S|-C> [-V|--verbose]";
2010-07-18 15:18:59 +00:00
xCAT::MsgUtils->message('I', "$msg");
}
#-----------------------------------------------------------------------------
=head3 shutdownxcatd
shutdown the daemon
=cut
#-----------------------------------------------------------------------------
sub shutdownxcatd
{
my $msg = "Shutting down the xcatd daemon during database migration.";
xCAT::MsgUtils->message('I', "$msg");
my $xcmd;
if ($::osname eq 'AIX')
{
$xcmd = "stopsrc -s xcatd";
}
else
{
$xcmd = "service xcatd stop";
}
system($xcmd);
}
#-----------------------------------------------------------------------------
=head3 backupxcatdb
Backup xCATdb
=cut
#-----------------------------------------------------------------------------
sub backupxcatdb
{
# If there is no backup or the /etc/xcat/cfgloc file does not point to
# pgsql, then we backup the database
my $sitefile = "$::backupdir/site.csv";
if ((!(-e $sitefile)) || ($::xcatrunningdb2 == 0))
{
xCAT::MsgUtils->message(
"I",
"Backing up xCAT Database to $::backupdir.\nThis could take several minutes."
);
if (!(-e $::backupdir))
{ # does not exist, make it
my $cmd = "mkdir -p $::backupdir";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
else
{ # remove contents
my $cmd = "rm -f $::backupdir/*";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
# back it up
2010-08-19 13:27:40 +00:00
my $cmd = " dumpxCATdb -p $::backupdir";
2010-07-18 15:18:59 +00:00
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
}
#-----------------------------------------------------------------------------
=head3 mkdb2user
adds db2instance user and group and xcatdb instance id
=cut
#-----------------------------------------------------------------------------
sub mkdb2user
{
#
# mk db2 instance group and user
#
my $cmd;
if ($::osname eq 'AIX')
{
$cmd = "lsgroup xcatdb";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{
#group does not exist, need to make it
$cmd = "mkgroup xcatdb";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
$cmd = "lsuser xcatdb";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{
#user does not exist, need to make it
$cmd = "mkuser pgrp=xcatdb home=/var/lib/db2 shell=/bin/ksh xcatdb";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2010-08-02 17:09:52 +00:00
# touch .profile to makesure it stays owned by xcatd
$cmd = "touch /var/lib/db2/.profile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
$cmd = "chmod 777 /var/lib/db2/.profile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2010-07-18 15:18:59 +00:00
}
# set xcatdb id password
$cmd = qq~echo "xcatdb:$::adminpassword" | /bin/chpasswd -c~;
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
else
{ # Linux
$cmd = "egrep -i \"^xcatdb:\" /etc/group";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{
#group does not exist, need to make it
$cmd = "groupadd xcatdb";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
$cmd = "egrep -i \"^xcatdb:\" /etc/passwd";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{
#user does not exist, need to make it
$cmd = "useradd -d /var/lib/db2 -g xcatdb -m -s /bin/bash xcatdb";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
# set xcatdb id password
$cmd = qq~echo $::adminpassword | passwd xcatdb --stdin~;
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
}
}
#-----------------------------------------------------------------------------
=head3 setupservices
add db2 xcatdb instance entries into /etc/services on Server or Client
=cut
#-----------------------------------------------------------------------------
sub setupservices
{
my $cmd;
$cmd = "egrep -i \"^db2c_xcatdb\" /etc/services";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{ # if not already there
open(FILE, ">>/etc/services")
or die "cannot open file /etc/services\n";
if ($::SERVER)
{ # on the server
print FILE "# xcatdb db2 entries
DB2_xcatdb 60000/tcp
DB2_xcatdb_1 60001/tcp
DB2_xcatdb_2 60002/tcp
DB2_xcatdb_END 60003/tcp
db2c_xcatdb 50001/tcp # Port for server connection";
}
else
{ # on the client
print FILE "# xcatdb db2 entries
db2c_xcatdb 50001/tcp # Port for server connection";
}
close FILE;
}
}
#-----------------------------------------------------------------------------
=head3 setupinstance
Create the db2 server or client instance
For server
run <db2installpath>/instance/db2icrt -a server -p db2c_xcatdb -s ese -u xcatdb xcatdb
For Client
run <db2installpath>/instance/db2icrt -s client xcatdb
=cut
#-----------------------------------------------------------------------------
sub setupinstance
{
my $rc = 0;
#check if instance already exists
my $cmd = $::installdb2dir;
$cmd .= "\/instance\/db2ilist ";
my @output = xCAT::Utils->runcmd($cmd, 0);
my $check = "xcatdb"; # see if instance exists
if (!(grep(/$check/, @output)))
{
# if xcatdb instance does not exist, setup
$cmd = $::installdb2dir; # set db2 path to the create instance cmd
$cmd .= "\/instance\/db2icrt ";
if ($::SERVER)
{
$cmd .= "-a server -p db2c_xcatdb -s ese -u xcatdb xcatdb ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{ # if error exit
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
# setup instance
$cmd = $::installdb2dir; #
$cmd .= "\/instance\/db2iset ";
$cmd .= "-g DB2_PARALLEL_IO=* ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
}
$cmd = $::installdb2dir;
$cmd .= "\/instance\/db2iset ";
$cmd .= "-g DB2AUTOSTART=yes ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
}
$cmd = $::installdb2dir;
$cmd .= "\/instance\/db2iset ";
$cmd .= "-g DB2_STRIPED_CONTAINERS=ON ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
}
# su to xcatdb and run more setup
2010-08-11 13:29:46 +00:00
$cmd = "echo \"EXTSHM=ON\" >> /var/lib/db2/sqllib/db2profile ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
$cmd = "echo \"export EXTSHM\" >>/var/lib/db2/sqllib/db2profile ";
2010-07-18 15:18:59 +00:00
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
}
else
{ # client setup
$cmd .= "-s client xcatdb ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{ # if error exit
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2010-08-11 13:29:46 +00:00
# su to xcatdb and run more setup
$cmd = "echo \"EXTSHM=ON\" >> /var/lib/db2/sqllib/db2profile ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
$cmd = "echo \"export EXTSHM\" >>/var/lib/db2/sqllib/db2profile ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
2010-07-18 15:18:59 +00:00
}
}
}
#-----------------------------------------------------------------------------
=head3 rundb2cmd
Run a commmand as the xcatdb instance id
Input: command
=cut
#-----------------------------------------------------------------------------
sub rundb2cmd
{
my $orgcmd = shift;
my $rc = 0;
my $cmd = "\'";
$cmd .= $orgcmd;
$cmd .= ' 2>&1';
$cmd .= "\'";
system("su - xcatdb -c $cmd");
if ($? > 0) # error
{
$rc = $? >> 8;
}
return ($rc);
}
#-----------------------------------------------------------------------------
=head3 setupdb2env
Setup the db2 environment variables for root
=cut
#-----------------------------------------------------------------------------
sub setupdb2env
{
if ($::osname eq 'AIX')
{
my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{ # if not already there
open(FILE, ">>/etc/profile")
or die "cannot open file /etc/profile\n";
print FILE "export DB2INSTANCE=xcatdb \n";
2010-08-11 13:29:46 +00:00
print FILE "export EXTSHM=ON \n";
2010-07-18 15:18:59 +00:00
close FILE;
}
}
else
{ # LInux
my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile.d/xcat.sh";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{ # if not already there
open(FILE, ">>/etc/profile.d/xcat.sh")
or die "cannot open file /etc/profile.d/xcat.sh\n";
print FILE "export DB2INSTANCE=xcatdb \n";
2010-08-11 13:29:46 +00:00
print FILE "export EXTSHM=ON \n";
2010-07-18 15:18:59 +00:00
close FILE;
}
my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile.d/xcat.csh";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{ # if not already there
open(FILE, ">>/etc/profile.d/xcat.csh")
or die "cannot open file /etc/profile.d/xcat.csh\n";
print FILE "setenv DB2INSTANCE \"xcatdb\" \n";
2010-08-11 13:29:46 +00:00
print FILE "setenv EXTSHM \"ON\" \n";
2010-07-18 15:18:59 +00:00
close FILE;
}
}
}
#-----------------------------------------------------------------------------
=head3 db2start
Start the db2 server
=cut
#-----------------------------------------------------------------------------
sub db2start
{
xCAT::MsgUtils->message("I", "Starting the DB2 Server");
my $cmd;
# su to xcatdb to start the server
$cmd = "db2start ";
my $rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("W", " $cmd failed.");
}
}
#-----------------------------------------------------------------------------
=head3 catalogServer
Catalog the location of the db2 server on the Service Node
=cut
#-----------------------------------------------------------------------------
sub catalogServer
{
xCAT::MsgUtils->message("I", "Cataloging the DB2 Server");
my $MN;
2010-07-29 17:31:28 +00:00
if ($ENV{'XCATDB2SERVER'})
{ # hostname or ip address supplied
$MN = $ENV{'XCATDB2SERVER'};
}
else
{
my $msg =
"Input the hostname or ip address of the Management Node as known by this Service node. Should be the same as the site.master attribute: ";
xCAT::MsgUtils->message('I', "$msg");
chop($MN = <STDIN>);
}
my $cmd;
2010-07-18 15:18:59 +00:00
my $rc = 0;
# su to xcatdb and catalog the database server and database
$cmd = "db2 catalog tcpip node mn remote $MN server db2c_xcatdb ";
$rc = &rundb2cmd($cmd);
2010-07-29 17:31:28 +00:00
$cmd = "db2 terminate"; # refresh the cache
2010-07-18 15:18:59 +00:00
$rc = &rundb2cmd($cmd);
$cmd = "db2 catalog db xcatdb as xcatdb at node mn ";
$rc = &rundb2cmd($cmd);
2010-07-29 17:31:28 +00:00
$cmd = "db2 terminate"; # refresh the cache
2010-07-18 15:18:59 +00:00
$rc = &rundb2cmd($cmd);
}
2010-08-11 13:29:46 +00:00
#-----------------------------------------------------------------------------
=head3 updateDBM
Setup configuration requirements for the DBM
=cut
2010-07-18 15:18:59 +00:00
#-----------------------------------------------------------------------------
2010-08-11 13:29:46 +00:00
sub updateDBM
{
xCAT::MsgUtils->message("I",
"Updating the DBM.");
my $cmd;
my $rc = 0;
# su to xcatdb to update the DBM
$cmd = "db2 -tvf /opt/xcat/share/xcat/tools/updateDBM.sql ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
}
2010-07-18 15:18:59 +00:00
#-----------------------------------------------------------------------------
=head3 createxcatdb
Create the xcat database
=cut
#-----------------------------------------------------------------------------
sub createxcatdb
{
xCAT::MsgUtils->message("I",
2010-08-10 17:28:19 +00:00
"Creating the xCAT database. This can take several minutes.");
2010-07-18 15:18:59 +00:00
my $cmd;
my $rc = 0;
# su to xcatdb to create the database
$cmd = "db2 -tvf /opt/xcat/share/xcat/tools/createdb.sql ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
# restart the instance to apply the change
xCAT::MsgUtils->message("I",
"Database created. Restarting the instance to apply the change. ");
$cmd = "db2 connect reset ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
$cmd = "db2 force applications all; db2 terminate; ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("I", " $cmd warning.");
}
$cmd = "db2stop ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("E", " $cmd warning.");
exit(1);
}
$cmd = "db2start ";
$rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("E", " $cmd warning.");
exit(1);
}
}
#-----------------------------------------------------------------------------
=head3 db2reboot
2010-08-25 15:30:21 +00:00
Setup for db2 to start on reboot
2010-07-18 15:18:59 +00:00
=cut
#-----------------------------------------------------------------------------
sub db2reboot
{
2010-08-25 15:30:21 +00:00
xCAT::MsgUtils->message("I", "Setting up DB2 to start on reboot");
2010-07-18 15:18:59 +00:00
my $cmd;
2010-08-25 15:30:21 +00:00
# su to xcatdb
$cmd = "db2iauto -on xcatdb ";
my $rc = &rundb2cmd($cmd);
if ($rc != 0)
{
xCAT::MsgUtils->message("W", " $cmd failed.");
}
2010-07-18 15:18:59 +00:00
}
#-----------------------------------------------------------------------------
=head3 setupODBC
2010-07-29 17:31:28 +00:00
Will setup the ODBC. C++ applications are running
2010-07-18 15:18:59 +00:00
that need access to the DB2 database for example LoadLeveler.
=cut
#-----------------------------------------------------------------------------
sub setupODBC
{
#
# check to see if correct rpms are installed
#
# for all OS need unixODBC rpm
my $cmd = "rpm -qa | grep unixODBC";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
my $message =
"\nunixODBC is not installed. If on AIX, it should be first obtained from the xcat dependency tarballs and installed before we can setup the ODBC. If on Linux, install from the OS CDs.";
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
my @rpmoutput;
my $odbcinstfile;
my $odbcfile;
my $message;
2010-07-29 17:31:28 +00:00
my $homedir = xCAT::Utils->getHomeDir();
2010-07-18 15:18:59 +00:00
my $xcatconfig = "/etc/xcat/cfgloc";
2010-07-29 17:31:28 +00:00
my $xcatconfigbackup = "/etc/xcat/cfgloc.db2";
2010-07-18 15:18:59 +00:00
if (!(-e ($xcatconfig)) && (!(-e ($xcatconfigbackup))))
{
$message =
2010-07-29 17:31:28 +00:00
"The $xcatconfig and $xcatconfigbackup files are missing. You need to configure xCAT for DB2 before setting up the ODBC.";
2010-07-18 15:18:59 +00:00
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
2010-07-29 17:31:28 +00:00
$cmd = "fgrep -i DB2 $xcatconfig";
2010-07-18 15:18:59 +00:00
my @output;
@output = xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then try backup
{
2010-07-29 17:31:28 +00:00
$cmd = "fgrep -i DB2 $xcatconfigbackup";
2010-07-18 15:18:59 +00:00
@output = xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then try backup
{
$message =
2010-07-29 17:31:28 +00:00
"Cannot find DB2 info in the cfgloc or cfgloc.db2 file. Configuration of ODBC cannot continue.";
2010-07-18 15:18:59 +00:00
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
}
2010-07-29 17:31:28 +00:00
# instance, database and password from cfgloc
(my $databasehdr, my $instance, my $passwd) = split(/\|/, $output[0]);
(my $header, my $database) = split(/:/, $databasehdr);
# The unixODBC Driver Manager loads the DB2 Driver dynamically
# so the shared object must be extracted from the driver
#if it does not already exist
if (!(-e "/var/lib/db2/sqllib/lib/libdb2.so"))
{
if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX'))
{
$cmd = "ar -x /var/lib/db2/sqllib/lib/libdb2.a";
@rpmoutput = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
my $message = "Cannot configure the ODBC.";
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
$cmd = "mv shr.o /var/lib/db2/sqllib/lib/libdb2.so";
@rpmoutput = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
my $message = "Cannot configure the ODBC.";
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
}
else
{ #sles TBD
xCAT::MsgUtils->message("E",
" SLES ODBC support not implemented yet");
exit(1);
}
}
2010-07-18 15:18:59 +00:00
if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX'))
{
$odbcinstfile = "/etc/odbcinst.ini";
$odbcfile = "/etc/odbc.ini";
}
else
{ #sles
2010-07-29 17:31:28 +00:00
$odbcinstfile = "/etc/unixODBC/odbcinst.ini";
$odbcfile = "/etc/unixODBC/odbc.ini";
xCAT::MsgUtils->message("E", " SLES ODBC support not implemented yet");
2010-07-18 15:18:59 +00:00
exit(1);
}
# setup the odbcinst.ini file
2010-07-29 17:31:28 +00:00
my $sharedlib = "/var/lib/db2/sqllib/lib/libdb2.so";
2010-07-18 15:18:59 +00:00
$cmd = "fgrep -i driver $odbcinstfile ";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then driver entry not there
{
my $entry =
2010-07-29 17:31:28 +00:00
"[DB2]\nDescription = ODBC for DB2\nDriver = $sharedlib";
2010-07-18 15:18:59 +00:00
$cmd = "echo \"$entry\" >> $odbcinstfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup ODBC.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
2010-07-29 17:31:28 +00:00
$entry = "FileUsage = 1";
$cmd = "echo \"$entry\" >> $odbcinstfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup ODBC.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
$entry = "DontDLClose = 1";
$cmd = "echo \"$entry\" >> $odbcinstfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup $odbcinstfile.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
2010-07-18 15:18:59 +00:00
}
else
{ # entry already there
$message = "$odbcinstfile already configured, will not change.";
xCAT::MsgUtils->message("I", "$message");
}
2010-07-29 17:31:28 +00:00
# setup the Database and instance Name in the odbc.ini file
2010-07-18 15:18:59 +00:00
$cmd = "fgrep -i XCATDB $odbcfile";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then xcat entry not there
{
2010-07-29 17:31:28 +00:00
my $entry = "[$instance]\nDriver = DB2\nDATABASE = xcatdb";
2010-07-18 15:18:59 +00:00
$cmd = "echo \"$entry\" >> $odbcfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2010-07-29 17:31:28 +00:00
$message = "$cmd failed. Could not setup $odbcfile.";
2010-07-18 15:18:59 +00:00
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
}
else
{ # entry already there
$message = "$odbcfile already configured, will not change.";
xCAT::MsgUtils->message("I", "$message");
}
# setup $roothome/.odbc.ini so root will not have to specify password
# when accessing through ODBC
my $rootodbcfile = $homedir;
if ($::osname eq "AIX")
{
$rootodbcfile .= ".odbc.ini";
}
else
{
$rootodbcfile .= "/.odbc.ini";
}
# setup the DSN odbc.ini file
$cmd = "fgrep -i XCATDB $rootodbcfile";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then xcat entry not there
{
my $entry =
2010-07-29 17:31:28 +00:00
"[$instance]\nDATABASE = $database\nUSER = $instance\nPASSWORD = $passwd";
2010-07-18 15:18:59 +00:00
$cmd = "echo \"$entry\" >> $rootodbcfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2010-07-29 17:31:28 +00:00
$message = "$cmd failed. Could not setup $rootodbcfile.";
2010-07-18 15:18:59 +00:00
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
}
else
{ # entry already there
$message = "$rootodbcfile already configured, will not change.";
xCAT::MsgUtils->message("I", "$message");
}
# allow readonly by root
chmod 0600, $rootodbcfile;
2010-07-29 17:31:28 +00:00
# if on the client, must setup /var/lib/db2/sqllib/cfg/db2cli.ini
# this is a db2 support bug workaround
if ($::CLIENT)
{
my $db2clifile = "/var/lib/db2/sqllib/cfg/db2cli.ini";
my $db2clifilebackup = "/var/lib/db2/sqllib/cfg/db2cli.ini.org";
$cmd = "fgrep -i XCATDB $db2clifile";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then xcat entry not there
{
$cmd = "cp $db2clifile $db2clifilebackup ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup ODBC.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
my $entry = "\n[$instance]\nuid=$instance\npasswd=$passwd";
$cmd = "echo \"$entry\" >> $db2clifile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup $db2clifile.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
}
else
{ # entry already there
$message = "$db2clifile already configured, will not change.";
xCAT::MsgUtils->message("I", "$message");
}
}
2010-07-18 15:18:59 +00:00
}
#-----------------------------------------------------------------------------
=head3 createcfgloc
Creates the cfgloc file
to run xCAT on DB2
=cut
#-----------------------------------------------------------------------------
sub createcfgloc
{
my $cfgloc = "/etc/xcat/cfgloc";
my $cfglocbackup = "/etc/xcat/cfgloc.xcat.backup";
my $cmd;
my $message;
# if they had an old cfgloc , save it
if ((-e ($cfgloc)) && (!(-e ($cfglocbackup))))
{
$cmd = "mv $cfgloc $cfglocbackup";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
}
}
my $db2entry = "DB2:xcatdb|xcatdb|$::adminpassword";
$cmd = "echo \"$db2entry\" >> $cfgloc";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup cfgloc";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
# allow readonly by root
chmod 0600, $cfgloc;
}
#-----------------------------------------------------------------------------
=head3 restorexcatdb
Restores the database from ~/xcat-dbback and restarts the xcatd using
2010-07-29 17:31:28 +00:00
DB2
2010-07-18 15:18:59 +00:00
=cut
#-----------------------------------------------------------------------------
sub restorexcatdb
{
# restore the database
xCAT::MsgUtils->message(
"I",
2010-08-09 20:13:56 +00:00
"Restoring the xCat Database with $::backupdir to DB2 database.\nThis could take several minutes."
2010-07-18 15:18:59 +00:00
);
if (!(-d $::backupdir))
{ # does not exist, error
xCAT::MsgUtils->message("E",
" $::backupdir is missing. Cannot retore the database.");
exit(1);
}
# restore it
my $cmd = "XCATBYPASS=1 restorexCATdb -p $::backupdir";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
#
# start the daemon
#
my $xcmd;
if ($::osname eq 'AIX')
{
$xcmd = "startsrc -s xcatd";
}
else
{
$xcmd = "service xcatd start";
}
system($xcmd);
}