#! /bin/bash # Run this script on a freshly installed compute-node to automatically establish an intranet connection to the frontend via eth0-switch, as well as to the internet via eth1 (if eth1 is present). The number $ipaddlastnumber will be assigned to the new node automatically by reading information from /etc/hosts from the frontend. # 1 set temporary ip for eth0 to 192.168.255.255 ################################################ # Check the identities of the network cards. Select one as the 'eth0' (to guess) and then modify the corresponding /etc/sysconfig/network-scripts/ifcfg-$nc nc0=$(lshw -class network | grep -E 'Ethernet interface|logical name' | grep 'logical name' | awk '{print $3}' | awk '!/vir/' | tail -n1) echo $nc0 'is chosen as the eth0, internal network port. To modify /etc/sysconfig/network-scripts/ifcfg-"$nc".' ls /etc/sysconfig/network-scripts/ifcfg-$nc0 cp /etc/sysconfig/network-scripts/ifcfg-$nc0 /etc/sysconfig/network-scripts/orig0.ifcfg-$nc0 ifdown ifcfg-$nc0 # replacement: sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/IPV6INIT="yes"/IPV6INIT="no"/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/DHCPCLASS=//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/ONBOOT=no/ONBOOT="yes"/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/NM_CONTROLLED="yes"//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ### removal if present sed -i 's/PREFIX=16//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line PREFIX=16 if present sed -i 's/IPADDR=192.168.1.999//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line IPADDR=192.168.1.26 if present # append echo 'IPADDR=192.168.250.250' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'NETMASK=255.255.0.0' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'DNS1=192.168.1.10' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'DOMAIN=local' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'GATEWAY=192.168.1.10' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 cat /etc/sysconfig/network-scripts/ifcfg-$nc0 | sort | uniq > temp.ifcfg mv temp.ifcfg /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'cat /etc/sysconfig/network-scripts/ifcfg-$nc0:' cat /etc/sysconfig/network-scripts/ifcfg-$nc0 echo ' ' service network restart ifup ifcfg-$nc0 checkping=$(ping -q -c1 -W 5 192.168.1.10 | awk 'NR==4 {print $4}') if [ $checkping -eq 0 ] then echo $i is not responding to ping after 5 second fi if [ $checkping -eq 1 ] then echo 192.168.1.10 is responding to ping within 1 second. cat > temp.dat << __EOF__ #cat /etc/hosts | sort | uniq | grep '192.168' | awk '{print $1}' | awk -F "." '{print $4}'| awk 'END {print}' cat /etc/hosts | sort | uniq | grep '192.168' | awk '{print $1}' | awk 'END {print}' __EOF__ command=$(cat temp.dat) lastindex0=$(ssh -X -Y 192.168.1.10 "$command") echo 'lastindex0=' $lastindex0 lastindex=$(echo $lastindex0 | awk '{print $1}' | awk -F "." '{print $4}'| awk 'END {print}') echo '$lastindex=' $lastindex ipaddlastnumber=$(( $lastindex + 1 )) echo 'ipaddlastnumber=' $ipaddlastnumber rm -rf temp.dat fi # end of 1 set temporary ip for eth0 to 192.168.255.255 ####################################################### ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ # 2 configure "eth1" to auto dhcp, if present ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ # Check the identities of the network cards. Select one as the 'eth1' (to guess) and then modify the corresponding /etc/sysconfig/network-scripts/ifcfg-$nc1 nc1=$(lshw -class network | grep -E 'Ethernet interface|logical name' | grep 'logical name' | awk '{print $3}' | awk '!/vir/' | awk 'NR==1{print}') echo $nc1 'is chosen as the eth1, external network port. To modify /etc/sysconfig/network-scripts/ifcfg-"$nc1" in the node.' echo ' ' ls /etc/sysconfig/network-scripts/ifcfg-$nc1 # The following code will replace the ifcfg-$nc1 with the desired content. The original ifcfg-$nc1 will be saved as ifcfg-$nc1.orig. cp /etc/sysconfig/network-scripts/ifcfg-$nc1 /etc/sysconfig/network-scripts/orig1.ifcfg-$nc1 ifdown $nc1 # replacement: sed -i 's/static/dhcp/g' /etc/sysconfig/network-scripts/ifcfg-$nc1 sed -i 's/BOOTPROTO=static/BOOTPROTO=dhcp/g' /etc/sysconfig/network-scripts/ifcfg-$nc1 sed -i 's/ONBOOT=no/ONBOOT="yes"/g' /etc/sysconfig/network-scripts/ifcfg-$nc1 sed -i 's/NM_CONTROLLED="yes"//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ### remove if present sed -i 's/DHCPCLASS=//g' /etc/sysconfig/network-scripts/ifcfg-$nc1 ### remove if present #echo 'NM_CONTROLLED=yes' >> /etc/sysconfig/network-scripts/ifcfg-$nc1 #sed -i 's/IPV6INIT="yes"/IPV6INIT="no"/g' /etc/sysconfig/network-scripts/ifcfg-$nc1 cat /etc/sysconfig/network-scripts/ifcfg-$nc1 | sort | uniq > temp.ifcfg mv temp.ifcfg /etc/sysconfig/network-scripts/ifcfg-$nc1 echo 'cat /etc/sysconfig/network-scripts/ifcfg-$nc1:' cat /etc/sysconfig/network-scripts/ifcfg-$nc1 echo ' ' ifup $nc1 ############################################################ ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ # 3 configure "eth0" by using ipaddlastnumber abstracted from step 1 ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ifdown $nc0 # replacement: sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/IPV6INIT="yes"/IPV6INIT="no"/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/DHCPCLASS=//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-$nc0 sed -i 's/NM_CONTROLLED="yes"//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ### removal if present sed -i 's/PREFIX=16//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line PREFIX=16 if present sed -i 's/IPADDR=192.168.1.999//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line IPADDR=192.168.1.26 if present sed -i 's/IPADDR=192.168.250.250//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line IPADDR=192.168.259.250 if present sed -i 's/IPADDR=192.168.1.200//g' /etc/sysconfig/network-scripts/ifcfg-$nc0 ## remove the line IPADDR=192.168.259.250 if present # append if [ -z "$ipaddlastnumber" ]; then echo '$ipaddlastnumber is empty'; ipaddlastnumber=205 fi echo 'IPADDR=192.168.1.'$ipaddlastnumber >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'NETMASK=255.255.0.0' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'DNS1=192.168.1.10' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'DOMAIN=local' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'GATEWAY=192.168.1.10' >> /etc/sysconfig/network-scripts/ifcfg-$nc0 cat /etc/sysconfig/network-scripts/ifcfg-$nc0 | sort | uniq > temp.ifcfg mv temp.ifcfg /etc/sysconfig/network-scripts/ifcfg-$nc0 echo 'cat /etc/sysconfig/network-scripts/ifcfg-$nc0:' cat /etc/sysconfig/network-scripts/ifcfg-$nc0 echo ' ' ifup $nc0 checkping=$(ping -q -c1 -W 5 192.168.1.10 | awk 'NR==4 {print $4}') if [ $checkping -eq 0 ] then echo $i is not responding to ping after 5 second fi if [ $checkping -eq 1 ] then echo 192.168.1.10 is responding to ping within 1 second. fi ############################################################ ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ # 4 Configure the default gateway (the IP of the switch) and hostname in /etc/sysconfig/network file in the node ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ # The following code will replace the /etc/sysconfig/network file with the desired content. The original netowork file will be saved as network.orig mv /etc/sysconfig/network /etc/sysconfig/network.orig touch /etc/sysconfig/network echo 'NETWORKING=yes' >> /etc/sysconfig/network echo 'HOSTNAME='compute-0-$ipaddlastnumber >> /etc/sysconfig/network echo 'GATEWAY='192.168.1.10 >> /etc/sysconfig/network ############################################################ ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ## 5 Create /etc/systemd/system/network-service-restart.sh echo '#!/bin/bash' > /usr/local/bin/network-service-restart.sh echo 'service NetworkManager restart' >> /usr/local/bin/network-service-restart.sh echo 'service network restart' >> /usr/local/bin/network-service-restart.sh chmod 744 /usr/local/bin/network-service-restart.sh ## create /etc/systemd/system/service-network-restart.service cat << EOF > /etc/systemd/system/service-network-restart.service [Unit] After=mysql.service [Service] ExecStart=/usr/local/bin/network-service-restart.sh [Install] WantedBy=default.target EOF # Next, install systemd service unit and enable it so it will be executed at the boot time: chmod 664 /etc/systemd/system/service-network-restart.service systemctl daemon-reload systemctl enable service-network-restart.service # If you wish to test your script before you reboot run: # systemctl start service-network-restart.service ##### $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ## 6 ### nfs_node.txt ### Configuring compute nodes (NFS clients) ### reference: https://www.itzgeek.com/how-tos/linux/centos-how-tos/how-to-setup-nfs-server-on-centos-7-rhel-7-fedora-22.html #### default values. dont change ### HOSTNAME=$(cat /etc/sysconfig/network | grep HOSTNAME | awk -F "=" '{print $2}') hostnamectl set-hostname $HOSTNAME #### default values. dont change ### ## 7. Doing NFS configuration in current node # Check NFS Share # Before mounting the NFS share, check the NFS shares available on the NFS server by running the following command on the NFS client. showmount -e 192.168.1.10 # Mount the shared directory in the NFS clients (the share directory in the frontend is supposed to be /export/share mkdir /share mount 192.168.1.10:/export/share /share #Verify the mounted share on the NFS client using mount command. mount | grep nfs #Also, you can use the df -hT command to check the mounted NFS share. df -hT #Create a file on the mounted directory to verify the read and write access on NFS share. touch /share/test_$HOSTNAME #If the above command returns no error, you have working NFS setup # To mount the shares automatically on every reboot, you would need to modify /etc/fstab file of your NFS client. cp /etc/fstab /etc/fstab.orig echo '192.168.1.10:/export/share /share nfs nosuid,rw,sync,hard,intr 0 0' >> /etc/fstab ### replace /etc/hosts file by that shared in /share/cluster/hosts, then add to hosts the identify of current node mv /etc/hosts /etc/hosts.orig ln -s /share/cluster/hosts /etc/ ipaddlastnumber=$(cat /etc/sysconfig/network | grep HOSTNAME | awk -F "=" '{print $2}' | awk -F "-" '{print $3}') echo 192.168.1.$ipaddlastnumber $HOSTNAME.local $HOSTNAME c$ipaddlastnumber >> /share/cluster/hosts cat /share/cluster/hosts | sort | uniq > temp mv temp /share/cluster/hosts # do auto exec network service restart in current noe # see https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux for How to automatically execute shell script at startup boot on systemd Linux # execute as su ## create /etc/systemd/system/network-service-restart.sh echo '#!/bin/bash' > /usr/local/bin/network-service-restart.sh echo 'service NetworkManager restart' >> /usr/local/bin/network-service-restart.sh echo 'service network restart' >> /usr/local/bin/network-service-restart.sh chmod 744 /usr/local/bin/network-service-restart.sh ## create /etc/systemd/system/service-network-restart.service cat << EOF > /etc/systemd/system/service-network-restart.service [Unit] After=mysql.service [Service] ExecStart=/usr/local/bin/network-service-restart.sh [Install] WantedBy=default.target EOF # Next, install systemd service unit and enable it so it will be executed at the boot time: chmod 664 /etc/systemd/system/service-network-restart.service systemctl daemon-reload systemctl enable service-network-restart.service # If you wish to test your script before you reboot, run: #systemctl start service-network-restart.service #service network restart ### customize the root for the present node mkdir -p /state/partition1 chmod -R 755 /state/partition1 if [[ -e $HOME/.ssh/id_rsa.pub ]]; then echo "pub rsa exists. Will not create any."; else yes '' | ssh-keygen -t rsa; fi ### if the nfs mount has been successful at this stage, the node should be able to see the shared directory /share/apps/local/bin/ sh /share/apps/local/bin/gen_bashrc-root source ~/.bashrc ## Check if the frontend is ssh-reachable checkping=$(ping -q -c1 -W 1 192.168.1.10 | awk 'NR==4 {print $4}') if [ $checkping -eq 0 ] then echo The frontend is not responding to ping after 1 second. ssh to frontend is not yet established. fi if [ $checkping -eq 1 ] then echo $i is responding to ping within 1 second echo 'To establish passwordless ssh into frontend from current node' ssh-copy-id -i $HOME/.ssh/id_rsa.pub $USER@192.168.1.10 fi #### customize /etc/ssh in a fresh node, so that ssh can work seamlessly # edit /etc/ssh/ssh_config, so that it reads 'GSSAPIAuthentication no' # edit /etc/ssh/sshd_config, so that it reads 'GSSAPIAuthentication no \ UseDNS no' # edit /etc/resolv.conf, so that it reads 'search local \ nameserver 192.168.1.10'. This line could be overwritten when setting the eth0 using NetworkManager. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config sed -i 's/UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/ssh_config sed -i 's/UseDNS yes/UseDNS no/g' /etc/ssh/ssh_config #### end of customize /etc/ssh in a fresh node # need sshpass # yum install -y epel-release yum install -y sshpass reboot