Path: blob/main/Db2-L3-Tech-Lab/db2_env_setup.sh
1928 views
#!/bin/bash1#-------------------------------------------------------------------------------------------------#2# NAME: db2_env_setup.sh #3# #4# PURPOSE: This program is designed to install the libraries that are needed to set up and run #5# IBM Db2 Community Edition, and then install the IBM Db2 software, on an Ubuntu #6# Linux server. #7# #8# USAGE: 1) Locate the following variables in the main() function of this file and assign #9# them the appropriate values: #10# #11# Db2CodeFile #12# Db2InstallDir #13# #14# 2) Log in as the root user and issue the following command from a Linux terminal #15# window (after moving to the directory where the .tar file and this file are #16# stored - typically the Downloads directory): #17# #18# ./db2_env_setup.sh #19# #20#-------------------------------------------------------------------------------------------------#21# DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY #22# #23# (C) COPYRIGHT International Business Machines Corp. 2017, 2018, & 2022. All Rights Reserved #24# Licensed Materials - Property of IBM #25# #26# US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP #27# Schedule Contract with IBM Corp. #28# #29# The following source code ("Sample") is owned by International Business Machines Corporation #30# or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, #31# copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of #32# assisting you with the installation of Db2 on a Ubuntu Linux server. #33# #34# The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM #35# HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT #36# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #37# Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the #38# above limitations or exclusions may not apply to you. IBM shall not be liable for any damages #39# you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM #40# has been advised of the possibility of such damages. #41#-------------------------------------------------------------------------------------------------#42# HISTORY: 16MAY2018 - Initial Coding Roger E. Sanders #43# 07JUN2018 - Added Error Checking and Db2 Install Shruthi Subbaiah Machimada #44# 02SEP2022 - Added Modifications for Installing Db2 V11.5 Roger E. Sanders #45#-------------------------------------------------------------------------------------------------#464748#-------------------------------------------------------------------------------------------------#49# NAME: init_log_file() #50# #51# PURPOSE: This function writes the appropriate header information to the Db2 environment #52# setup up script log file (db2_env_setup.log). #53# #54# PARAMETERS: $1 - Log file name #55# #56# RETURNS: None #57#-------------------------------------------------------------------------------------------------#58function init_log_file59{60# Create A New Log File And Write The Appropriate Header Information To It61echo "#--------------------------------------------------------------------#" > ${1} 2>&162echo "# NAME: db2_env_setup.log #" >> ${1} 2>&163echo "# CREATED: ${timeStamp} #" >> ${1} 2>&164echo "# #" >> ${1} 2>&165echo "# CONTENTS: Log data produced by db2_env_setup.sh #" >> ${1} 2>&166echo "#--------------------------------------------------------------------#" >> ${1} 2>&167echo "" >> ${1} 2>&16869# Change Permissions On The Log File So Anyone Can Access It70chmod 777 ${1}71}727374#-------------------------------------------------------------------------------------------------#75# NAME: install_db2_prereq_libraries() #76# #77# PURPOSE: This function installs the Db2 prerequisite libraries needed (for Ubuntu Linux). #78# #79# PARAMETERS: $1 - Log file name #80# #81# RETURNS: TRUE (1) - Packages and libraries needed were installed #82# FALSE (0) - One or more packages or libraries were not installed #83#-------------------------------------------------------------------------------------------------#84function install_db2_prereq_libraries85{86# Define All Local Variables Used87local returnCode=${returnCode:=0} # Function Return Code88local errorFlag=${errorFlag:=0} # Error Encountered Flag89local outputMsg=${outputMsg:=''} # Command Output Message Text9091# Initialize The Appropriate Memory Variables92returnCode=${FALSE}93errorFlag=${FALSE}9495# Upgrade All Packages Installed On The System96echo "Installing newer versions of all packages currently installed." | tee -a ${1}97echo "(This may take a few minutes)."98outputMsg=$(apt-get -y upgrade 2>&1)99echo "${outputMsg}" >> ${1} 2>&1100if [[ ${outputMsg} =~ "E:" ]]; then101echo "ERROR: Could not upgrade packages."102errorFlag=${TRUE}103fi104echo "" >> ${1}105echo "" | tee -a ${1}106107# Fix Any Broken Packages Or Package Dependencies Found108echo "Fixing any broken packages and package dependencies found." | tee -a ${1}109echo "" >> ${1}110outputMsg=$(apt-get --fix-broken install)111echo "${outputMsg}" >> ${1} 2>&1112if [[ ${outputMsg} =~ "E:" ]]; then113echo "ERROR: Could not fix broken packages and package dependencies."114errorFlag=${TRUE}115fi116echo "" >> ${1}117echo "" | tee -a ${1}118119# Install The LIBAIO1 Linux Library120echo "Installing the libaio1 package." | tee -a ${1}121echo "" >> ${1}122outputMsg=$(apt-get -y install libaio1 2>&1)123echo "${outputMsg}" >> ${1} 2>&1124if [[ ${outputMsg} =~ "E:" ]]; then125echo "ERROR: Could not install libaio1."126errorFlag=${TRUE}127fi128echo "" >> ${1}129echo "" | tee -a ${1}130131# Install The BINUTILS Linux Library132echo "Installing the binutils package." | tee -a ${1}133echo "" >> ${1}134outputMsg=$(apt-get -y install binutils 2>&1)135echo "${outputMsg}" >> ${1} 2>&1136if [[ ${outputMsg} =~ "E:" ]]; then137echo "ERROR: Could not install binutils."138errorFlag=${TRUE}139fi140echo "" >> ${1}141echo "" | tee -a ${1}142143# Install The ZLIB1G Linux Library144echo "Installing the zlib1g-dev package." | tee -a ${1}145echo "" >> ${1}146outputMsg=$(apt-get -y install zlib1g-dev 2>&1)147echo "${outputMsg}" >> ${1} 2>&1148if [[ ${outputMsg} =~ "E:" ]]; then149echo "ERROR: Could not install zlib1g-dev."150errorFlag=${TRUE}151fi152echo "" >> ${1}153echo "" | tee -a ${1}154155# Install The LIBPAM0G:I386 Linux Library156echo "Installing the libpam0g:i386 package." | tee -a ${1}157echo "" >> ${1}158outputMsg=$(apt-get -y install libpam0g:i386 2>&1)159echo "${outputMsg}" >> ${1} 2>&1160if [[ ${outputMsg} =~ "E:" ]]; then161echo "ERROR: Could not install libpam0g:i386."162errorFlag=${TRUE}163fi164echo "" >> ${1}165echo "" | tee -a ${1}166167# Install The LIBSTDC++:I386 Linux Library168echo "Installing the libstdc++6:i386 package." | tee -a ${1}169echo "" >> ${1}170outputMsg=$(apt-get -y install libstdc++6:i386 2>&1)171echo "${outputMsg}" >> ${1} 2>&1172if [[ ${outputMsg} =~ "E:" ]]; then173echo "ERROR: Could not install libstdc++6:i386."174errorFlag=${TRUE}175fi176echo "" >> ${1}177echo "" | tee -a ${1}178179# Install The LIBLOGGER-SYSLOG-PERL Linux Library (For TSA MP)180echo "Installing the liblogger-syslog-perl package." | tee -a ${1}181echo "" >> ${1}182outputMsg=$(apt-get -y install liblogger-syslog-perl 2>&1)183echo "${outputMsg}" >> ${1} 2>&1184if [[ ${outputMsg} =~ "E:" ]]; then185echo "ERROR: Could not install liblogger-syslog-perl."186errorFlag=${TRUE}187fi188echo "" >> ${1}189echo "" | tee -a ${1}190191# Install The KSH Linux Library (For TSA MP)192echo "Installing the ksh package." | tee -a ${1}193echo "" >> ${1}194outputMsg=$(apt-get -y install ksh 2>&1)195echo "${outputMsg}" >> ${1} 2>&1196if [[ ${outputMsg} =~ "E:" ]]; then197echo "ERROR: Could not install ksh."198errorFlag=${TRUE}199fi200echo "" >> ${1}201echo "" | tee -a ${1}202203# Install The OPENSSH-SERVER Linux Library (For Data Server Manager)204echo "Installing the openssh-server package." | tee -a ${1}205echo "" >> ${1}206outputMsg=$(apt-get -y install openssh-server 2>&1)207echo "${outputMsg}" >> ${1} 2>&1208if [[ ${outputMsg} =~ "E:" ]]; then209echo "ERROR: Could not install openssh-server."210errorFlag=${TRUE}211fi212echo "" >> ${1}213echo "" | tee -a ${1}214215# Install The NET-TOOLS Linux Library (For Data Server Manager)216echo "Installing the net-tools package." | tee -a ${1}217echo "" >> ${1}218outputMsg=$(apt-get -y install net-tools 2>&1)219echo "${outputMsg}" >> ${1} 2>&1220if [[ ${outputMsg} =~ "E:" ]]; then221echo "ERROR: Could not install net-tools."222errorFlag=${TRUE}223fi224echo "" >> ${1}225echo "" | tee -a ${1}226227# Remove Packages That Are No Longer Needed228echo "Removing dependency packages that are no longer needed." | tee -a ${1}229echo "" >> ${1}230outputMsg=$(apt-get -y autoremove 2>&1)231echo "${outputMsg}" >> ${1} 2>&1232if [[ ${outputMsg} =~ "E:" ]]; then233echo "ERROR: Could not remove unused packages."234errorFlag=${TRUE}235fi236echo "" >> ${1}237echo "" | tee -a ${1}238239# Generate A New List Of All Packages Installed On The System240echo "Updating the installed package list." | tee -a ${1}241echo "" >> ${1}242outputMsg=$(apt-get update 2>&1)243echo "${outputMsg} Done." >> ${1} 2>&1244if [[ ${outputMsg} =~ "E:" ]]; then245echo "ERROR: Could not update the installed package list."246errorFlag=${TRUE}247fi248echo "" >> ${1}249echo "" | tee -a ${1}250251# Write An Appropriate Message To The Log File252if [[ ${errorFlag} -eq ${TRUE} ]]; then253echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."254returnCode=${FALSE}255else256date >> ${1} 2>&1257echo "Finished installing the libraries needed for Db2." | tee -a ${1}258echo "" | tee -a ${1}259returnCode=${TRUE}260fi261262# Return The Appropriate Value To The Calling Function263return ${returnCode}264}265266267#-------------------------------------------------------------------------------------------------#268# NAME: create_db2_users() #269# #270# PURPOSE: This function creates the users and groups that are needed by Db2. #271# #272# PARAMETERS: $1 - Log file name #273# #274# RETURNS: TRUE (1) - The users and groups needed were successfully created #275# FALSE (0) - The users and groups needed were not created #276#-------------------------------------------------------------------------------------------------#277function create_db2_users278{279# Define All Local Variables Used280local returnCode=${returnCode:=0} # Function Return Code281local errorFlag=${errorFlag:=0} # Error Encountered Flag282local outputMsg=${outputMsg:=''} # Command Output Message Text283284# Initialize The Appropriate Memory Variables285returnCode=${FALSE}286errorFlag=${FALSE}287288# Create A User Group Named "db2iadm1"289echo "Creating a group named \"db2iadm1\"." | tee -a ${1}290outputMsg=$(groupadd db2iadm1 2>&1)291returnCode=$?292if [ ${returnCode} != 0 ] && [ ${returnCode} != 9 ]; then293echo "ERROR: Could not create the \"db2iadm1\" group." | tee -a ${1}294echo " ${outputMsg}" >> ${1} 2>&1295errorFlag=${TRUE}296fi297echo "" | tee -a ${1}298299# Create A User Group Named "db2fadm1"300echo "Creating a group named \"db2fadm1\"." | tee -a ${1}301outputMsg=$(groupadd db2fadm1 2>&1)302returnCode=$?303if [ ${returnCode} != 0 ] && [ ${returnCode} != 9 ]; then304echo "ERROR: Could not create the \"db2fadm1\" group." | tee -a ${1}305echo " ${outputMsg}" >> ${1} 2>&1306errorFlag=${TRUE}307fi308echo "" | tee -a ${1}309310# Create A User Named "db2inst1"311echo "Creating a user named \"db2inst1\" (with a home directory)." | tee -a ${1}312outputMsg=$(useradd -g db2iadm1 -m -d /home/db2inst1 -s /bin/bash db2inst1 2>&1)313returnCode=$?314if [ ${returnCode} != 0 ] && [ ${returnCode} != 9 ]; then315echo "ERROR: Could not create a user named \"db2inst1\"." | tee -a ${1}316echo " ${outputMsg}" >> ${1} 2>&1317errorFlag=${TRUE}318fi319320# Set The Password For The User Just Created To "ibmdb2"321outputMsg=$(echo -e "ibmdb2\nibmdb2" | passwd db2inst1 2>&1)322returnCode=$?323if [ ${returnCode} != 0 ]; then324echo "ERROR: Could not assign a password to the user named \"db2inst1\"." | tee -a ${1}325echo " ${outputMsg}" >> ${1} 2>&1326errorFlag=${TRUE}327else328echo " Password for user \"db2inst1\" has been set to \"ibmdb2\"." | tee -a ${1}329fi330echo "" | tee -a ${1}331332# Create A User Named "db2fenc1"333echo "Creating a user named \"db2fenc1\" (with a home directory)." | tee -a ${1}334outputMsg=$(useradd -g db2fadm1 -m -d /home/db2fenc1 -s /bin/bash db2fenc1 2>&1)335returnCode=$?336if [ ${returnCode} != 0 ] && [ ${returnCode} != 9 ]; then337echo "ERROR: Could not create a user named \"db2fenc1\"." | tee -a ${1}338echo " ${outputMsg}" >> ${1} 2>&1339errorFlag=${TRUE}340fi341342# Set The Password For The User Just Created To "ibmdb2"343outputMsg=$(echo -e "ibmdb2\nibmdb2" | passwd db2fenc1 2>&1)344returnCode=$?345if [ ${returnCode} != 0 ]; then346echo "ERROR: Could not create a user named \"db2fenc1\"." | tee -a ${1}347echo " ${outputMsg}" >> ${1} 2>&1348errorFlag=${TRUE}349else350echo " Password for user \"db2fenc1\" has been set to \"ibmdb2\"." | tee -a ${1}351fi352echo "" | tee -a ${1}353354# Write An Appropriate Message To The Log File355if [[ ${errorFlag} -eq ${TRUE} ]]; then356echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."357returnCode=${FALSE}358else359date >> ${1} 2>&1360echo "Finished creating the users and groups needed for Db2." | tee -a ${1}361echo "" | tee -a ${1}362returnCode=${TRUE}363fi364365# Return The Appropriate Value To The Calling Function366return ${returnCode}367}368369370#-------------------------------------------------------------------------------------------------#371# NAME: install_db2() #372# #373# PURPOSE: This function installs the Db2 software and creates an instance and instance #374# user. #375# #376# PARAMETERS: $1 - Log file name #377# $2 - Db2 tarball file name #378# $3 - Db2 installation software location #379# $4 - Db2 software installation directory #380# #381# RETURNS: TRUE (1) - Db2 was successfully installed #382# FALSE (0) - Db2 was not installed #383#-------------------------------------------------------------------------------------------------#384function install_db2385{386# Define All Local Variables Used387local returnCode=${returnCode:=0} # Function Return Code388local errorFlag=${errorFlag:=0} # Error Encountered Flag389local outputMsg=${outputMsg:=''} # Command Output Message Text390local fileName=${fileName:=''} # Current Working File Name391local softwareDir=${softwareDir:=''} # Db2 Installation Software Location (Directory)392local installDir=${installDir:=''} # Db2 Software Installation Directory393local TivoliDir=${TivoliDir:=''} # Tivoli Installation Software Location (Directory)394395# Initialize The Appropriate Memory Variables396returnCode=${FALSE}397errorFlag=${FALSE}398fileName=${2}399softwareDir=${3}400installDir="${4}/instance"401402# Untar The File That Contains The Db2 Software, Then Delete It403if [ -f ${fileName} ]; then404echo "Untaring the file \"${fileName}\"." | tee -a ${1}405outputMsg=$(tar -xvf ${fileName} 2>&1)406returnCode=$?407if [ ${returnCode} != 0 ]; then408echo "ERROR: Could not untar the file \"${fileName}\"." | tee -a ${1}409echo " ${outputMsg}" >> ${1} 2>&1410returnCode=${FALSE}411return ${returnCode}412else413echo "${outputMsg}" >> ${1} 2>&1414rm ${fileName} 2>&1415fi416echo "" | tee -a ${1}417fi418419# Create A Software Storage Directory420outputMsg=$(mkdir ${softwareDir} 2>&1)421returnCode=$?422if [ ${returnCode} != 0 ]; then423echo "ERROR: Could not create the \"${softwareDir}\" directory." | tee -a ${1}424echo " ${outputMsg}" >> ${1} 2>&1425returnCode=${FALSE}426return ${returnCode}427fi428429# Change The Permissions Of The Software Storage Directory So Anyone Can Access It430outputMsg=$(chmod 777 ${softwareDir} 2>&1)431returnCode=$?432if [ ${returnCode} != 0 ]; then433echo -e "ERROR: Could not "434echo "change permissions for the \"${software_Dir}\" directory." | tee -a ${1}435echo " ${outputMsg}" >> ${1} 2>&1436returnCode=${FALSE}437return ${returnCode}438fi439440# Move The Db2 Installation Software To The Software Storage Directory441softwareDir="${softwareDir}/ibm-db2"442outputMsg=$(mv server_dec ${softwareDir} 2>&1)443returnCode=$?444if [ ${returnCode} != 0 ]; then445echo -e "ERROR: Could not "446echo "move the Db2 software to the \"${softwareDir}\" directory." | tee -a ${1}447echo " ${outputMsg}" >> ${1} 2>&1448returnCode=${FALSE}449return ${returnCode}450fi451452# Change The Permissions Of The Db2 Installation Software Storage Directory So Anyone453# Can Access It454outputMsg=$(chmod 777 -R ${softwareDir} 2>&1)455returnCode=$?456if [ ${returnCode} != 0 ]; then457echo "ERROR: Could not change permissions for the \"${softwareDir}\" directory." | tee -a ${1}458echo " ${outputMsg}" >> ${1} 2>&1459returnCode=${FALSE}460return ${returnCode}461fi462463# Move To The Directory Where The Db2 Installation Software Resides464cd ${softwareDir}465466# Install The Db2 Software467echo "Running the program \"db2_install\"." | tee -a ${1}468outputMsg=$(echo -e "yes\nSERVER" | ./db2_install -y -f NOTSAMP 2>&1)469returnCode=$?470if [ ${returnCode} != 0 ]; then471echo "ERROR: Problem encountered running the \"db2_install\" program." | tee -a ${1}472echo " ${outputMsg}" >> ${1} 2>&1473returnCode=${FALSE}474return ${returnCode}475fi476echo "${outputMsg}" >> ${1} 2>&1477echo "" | tee -a ${1}478479# Move To The Directory Where The Tivoli System Automation for Multiplatforms (SA MP)480# Software Resides481TivoliDir="${softwareDir}/db2/linuxamd64/tsamp"482cd ${TivoliDir}483484# Install The Tivoli SA MP Software485echo "Running the program \"installSAM\"." | tee -a ${1}486outputMsg=$(./installSAM --noprereqcheck --noliccheck --silent 2>&1)487returnCode=$?488if [ ${returnCode} != 0 ]; then489echo "ERROR: Problem encountered running the \"installSAM\" program." | tee -a ${1}490echo " ${outputMsg}" >> ${1} 2>&1491returnCode=${FALSE}492return ${returnCode}493fi494echo "${outputMsg}" >> ${1} 2>&1495echo "" | tee -a ${1}496497# Move To The Directory Where The Software Needed To Create A Db2 Instance Resides498cd ${installDir}499500# Create A Db2 Instance501echo "Creating a Db2 instance named \"db2inst1\"." | tee -a ${1}502outputMsg=$(./db2icrt -u db2fenc1 db2inst1 2>&1)503returnCode=$?504if [ ${returnCode} != 0 ]; then505echo "ERROR: Problem encountered creating a Db2 instance." | tee -a ${1}506echo " ${outputMsg}" >> ${1} 2>&1507returnCode=${FALSE}508return ${returnCode}509fi510echo "${outputMsg}" >> ${1} 2>&1511echo "" | tee -a ${1}512513# Return The Appropriate Value To The Calling Function514returnCode=${TRUE}515return ${returnCode}516}517518519#=================================================================================================#520# NAME: main() #521# #522# PURPOSE: This is the main body of the script. #523# #524# PARAMETERS: $1 - Script key word #525# $2 - Option #526# #527# RETURNS: 0 - The script executed successfully #528# 1 - The script did NOT execute successfully #529#=================================================================================================#530531# Define All Variables Used532TRUE=${TRUE:=1} # Boolean TRUE533FALSE=${FALSE:=0} # Boolean FALSE534535currentDir=${currentDir:=''} # Current Working Directory536logFile=${logFile:=''} # Log File Name537Db2CodeFile=${Db2CodeFile:=''} # Db2 Install Code TAR File Name538timeStamp=${timeStamp:=''} # Current Date And Time Value539540# Initialize The Appropriate Memory Variables541currentDir=$(pwd)542logFile="${currentDir}/db2_env_setup.log"543Db2CodeFile="${currentDir}/v11.5.7_linuxx64_server_dec.tar"544Db2SoftwareDir="/home/Software"545Db2InstallDir="/opt/ibm/db2/V11.5/"546timeStamp=$(date '+%m-%d-%Y:%H-%M-%S')547548# Clear The Screen549clear550551# If The File Containing The Db2 Developer Edition Software Does Not Exist, Display An Error Message552# And Return Control To The Operating System553if [ ! -f ${Db2CodeFile} ]; then554echo "ERROR: Unable to locate the file \"${Db2CodeFile}\"." | tee -a ${logFile}555echo "" | tee -a ${logFile}556exit 1557fi558559# Create A Log File In The Current Directory And Write Header Information To It560init_log_file ${logFile}561562# Display A "Setting Up" Message563date >> ${logFile} 2>&1564echo "Setting up the Db2 prerequisite software needed. Please wait ..." | tee -a ${logFile}565echo "" | tee -a ${logFile}566567# Install The Db2 Prerequisite Libraries Needed For Ubuntu Linux568install_db2_prereq_libraries ${logFile}569returnCode=$?570if [ ${returnCode} == ${FALSE} ]; then571exit 1572fi573574# Create The Users Needed For Db2575date >> ${logFile} 2>&1576echo "Creating the appropriate Db2 instance users and groups. Please wait ..." | tee -a ${logFile}577echo "" | tee -a ${logFile}578create_db2_users ${logFile}579returnCode=$?580if [ ${returnCode} == ${FALSE} ]; then581exit 1582fi583584# Install The Db2 Software And Create A Default Db2 Instance585date >> ${logFile} 2>&1586echo "Installing the Db2 software and creating a Db2 instance. Please wait ..." | tee -a ${logFile}587echo "" | tee -a ${logFile}588install_db2 ${logFile} ${Db2CodeFile} ${Db2SoftwareDir} ${Db2InstallDir}589returnCode=$?590if [ ${returnCode} == ${FALSE} ]; then591exit 1592fi593594# Write The End Timestamp To the Log File595date >> ${logFile} 2>&1596echo "Finished installing db2." >> ${logFile}597598# Display A "Finished" Message And Write A "Finished" Message To The Log File599echo "Finished! Refer to the file \"${logFile}\" for more information."600echo ""601602# Exit And Return Control To The Operating System603exit 0604605606