Using shell script one click deployment service under RHEL7(Centos7)

Keywords: yum DNS network firewall

course

Using shell script one click deployment service under RHEL7(Centos7)
Using shell script to deploy service one click under RHEL7(Centos7) (2)
Using shell script to deploy service one click under RHEL7(Centos7) (3)

Update content: one click deployment of PXE+kickstart unattended installation service is added. After testing, there is no problem. You are welcome to test and put forward your opinions. Writing is not easy. I hope you can support me.
Functions that can be completed at present
1. One click configuration yum source
2. Configure IP address
3. Set up NFS service
4. Set up DHCP service
5. Set up DNS Service
6. One click deployment of PXE+kickstart unattended installation service
The script is as follows:

#author:Roya
#script name:ROne-deploy
#creation time:2020-01-23
#update time:2020-01-26
#version:0.20
#!/bin/bash
echo "------------------------------"
echo "|input 1:Configure Yum source|"
echo "------------------------------"
echo "|input 2:Configure IP address|"
echo "------------------------------"
echo "|input 3:Configure NFS server|"
echo "------------------------------"
echo "|input 4:Configure DHCP server|" 
echo "------------------------------"
echo "|input 5:Configure DNS server|"
echo "-------------------------------------------------------"
echo "|input 6:Configure PXE unattended installation service|"
echo "-------------------------------------------------------"
read -p "|please input 1,2,3,4,5,6|: " a 
case $a in #Use case statement to judge input
1)
echo "-------------------------------------"
echo "|Prepare to configure Yum source...|"
echo "-------------------------------------"
sleep 1  #Wait a second to execute the following command
echo "|One moment...please|"
echo "---------------------"
if [ -e /media/cdrom ]
then sleep 0.7
else mkdir /media/cdrom
fi
echo "/dev/cdrom /media/cdrom iso9660 defaults 0 0" >> /etc/fstab && mount -a &> /dev/null  # Write mount information to fstab
sleep 0.7
echo '[rhel]
name=rhel
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1' >> /etc/yum.repos.d/rhel.repo #Profile of yum source
sleep 2.7
yum makecache &> /dev/null
if [ $? -eq 0 ]
then
sleep 0.7
echo "----------------------------------------"
echo "|Yum source configuration successfully!|"
echo "----------------------------------"
else echo "|Yum source configuration failed!|"
echo "----------------------------------"
fi
yum repolist | grep repolist
;;
2) #Configure IP address
echo "----------------------------------------------------------------"
read -p "|please enter Please enter the network card name(Tips:ens33,eth0)|: " wlanname #Please enter the network card name
if [ -e /etc/sysconfig/network-scripts/ifcfg-$wlanname ]
then 
continue &> /dev/null
else echo "input error!"
exit 0
fi
sleep 0.7 
echo "----------------------------------------------------------------"
read -p "|Please enter IP address(Tips:192.168.1.1/24)|: " IPad #Please enter IP address
sleep 0.7
echo "-------------------------------------------"
read -p "|Please enter GATEWAY(Tips:192.168.1.254)|: " gate #Please enter gateway
sleep 0.7
echo "-----------------------------------------------"
read -p "|please enter DNS server(Tips:114.114.114.114)|: " DNS #Please enter gateway
sleep 0.7
echo "-----------------------------------------------------------"
nmcli connection modify $wlanname ipv4.addresses $IPad
nmcli connection modify $wlanname ipv4.method manual 
nmcli connection modify $wlanname ipv4.dns $DNS ipv4.gateway $gate &>/dev/null
nmcli connection modify $wlanname connection.autoconnect yes &>/dev/null
systemctl restart network 
if [ $? -eq 0 ]
then
echo "|Network configuration successful!|" #Network configuration successful
echo "----------------------------------"
ifconfig | awk 'NR==2'
else echo "Network configuration failed!" 
fi
;;
3) #Configure services for NFS
echo "-------------------------------------"
yum install -y nfs-utils &> /dev/null #Install nfs server
if [ $? -eq 0 ]
then echo '|NFS service installed successfully!|'  #nfs service installed successfully
echo "-------------------------------------------------------------"
read -p '|Please enter NFS directory path you want to share(Tips:/nfsdir)|: ' nfsdir #Enter the directory of the nfs share
echo "------------------------------------------------------------"
sleep 0.7 
read -p '|Please enter the IP address of the host allowed to be shared(Tips:192.168.1.1 or 192.168.1.*)|: ' nfsIP #Enter the host IP address or network segment that can be shared
echo "---------------------------------------------------------------"
sleep 0.7
read -p '|Please enter the permission of the shared host(Tips:sync,rw,ro)|: ' nfspwr #Enter permissions for hosts that are allowed to share
echo "-------------------------------"
sleep 0.7
echo "|Configuring..... Please wait.|"
echo "-------------------------------"
sleep 3
if [ -e $nfsdir ] #Determine whether the nfs share directory exists
then echo '|File Exists|' 
echo "------------"
sleep 0.7
else mkdir $nfsdir
chmod -Rf 777 $nfsdir
fi
echo "$nfsdir $nfsIP($nfspwr)" > /etc/exports 
systemctl restart nfs-server 
if [ $? -eq 0 ] 
then 
IP=`ifconfig | awk -F ' ' 'NR==2{print$2}'` #Use the awk command to extract the IP address
exportfs -r 
showmount -e $IP
if [ $? -eq 0 ] #Determine whether the service is configured correctly
then echo "NFS server has been configured successfully!"
else echo "NFS server has been configured failed!"
fi
fi
systemctl restart rpcbind  
systemctl enable rpcbind &> /dev/null
systemctl enable nfs-server &> /dev/null #Add to startup
firewall-cmd --add-service=nfs --permanent &> /dev/null
firewall-cmd --add-service=rpc-bind --permanent &> /dev/null
firewall-cmd --reload &> /dev/null
else echo 'NFS service installion failed!'
fi
;;
4) #Configure DHCP service
echo "-------------------"
echo '|one momnet...please|' 
yum install -y dhcp &> /dev/null 
if [ $? -eq 0 ]
then 
echo "DHCP service installed successfully!"
echo "----------------------------------------------------------------------------"
read -p "|Please enter the type of DNS service dynamic update(Tips:none,interim,ad-hoc)|: " style  #Please enter the type of DNS service dynamic update
case $style in 
none|interim|ad-hoc)
continue
;;
*)
echo "input error!"
exit 0
;;
esac
echo "---------------------------------------------------------"
sleep 0.7
read -p "|Allow/ignore client update DNS records(Tips:allow/ignore)|: " judge #Allow / ignore client update DNS records
case $judge in
allow|ignore)
continue
;;
*)
echo "input error!"
exit 0
;;
esac
echo "------------------------------------------"
sleep 0.7
read -p "|Please enter a DNS domain(Tips:roya.com)|: " domain #Please enter DNS domain
echo "------------------------------------------"
sleep 0.7
IP=`ifconfig | awk -F ' ' 'NR==2{print$2}'`  #IP address
NETMASK=`ifconfig | awk -F ' ' 'NR==2{print$4}'` #Subnet mask
IP0="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.0""  #Take the top three IP addresses
IP1="expr `ifconfig| awk -F ' ' 'NR==2{print$2}'  | awk -F '.' '{print$4}'` + 20" #Add 20 to the last bit of IP address
IP2="expr `ifconfig| awk -F ' ' 'NR==2{print$2}'  | awk -F '.' '{print$4}'` + 100" #Add 100 to the last bit of IP address
IP3="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.`$IP1`"" #IP address after adding
IP4="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.`$IP2`""
cat >> /etc/dhcp/dhcpd.conf << EOF #Write profile
ddns-update-style $style;
$judge client-updates;
subnet `$IP0` netmask $NETMASK {
range `$IP3` `$IP4`;
option subnet-mask $NETMASK;
option routers $IP;
option domain-name "$domain";
option domain-name-servers $IP;
default-lease-time 21600;
max-lease-time 43200;
}
EOF
systemctl restart dhcpd 
if [ $? -eq 0 ]
then echo "DHCP service configuration succeeded!"
systemctl enable dhcpd &> /dev/null
firewall --add-service=dhcp --permanent &> /dev/null
firewall --reload &> /dev/null
systemctl status dhcpd
else echo "DHCP service configuration failed!"
fi
else echo "DHCP service not installed successfully!"
fi
;;
5) #Configure DNS Service
echo "---------------------"
echo "|one moment...please|"
yum install -y bind* &> /dev/null 
if [ $? -eq 0 ]
then
sed -i "s/listen-on port 53 { 127.0.0.1; };/listen-on port 53 { any; };/g" /etc/named.conf 
sed -i "s/listen-on-v6 port 53 { ::1; };/\/\/listen-on-v6 port 53 { ::1; };/g" /etc/named.conf 
sed -i "s/allow-query     { localhost; };/allow-query     { any; };/g" /etc/named.conf
read -p "Please enter the website you want to analyze(Tips:runtime.com): " local1 #Please enter the domain name you want to resolve
sleep 1.7 
IP_0="`ifconfig | awk -F ' ' 'NR==2{print$2}'`"
IP_1="`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1`"
IP_2="`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 2`"
IP_3="`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 3`"
IP_4="`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 4`"
cat > /etc/named.rfc1912.zones << EOF
zone "$local1" IN {
        type master;
        file "$local1.local";
        allow-update { none; };
};
zone "$IP_3.$IP_2.$IP_1.in-addr.arpa" IN {
        type master;
        file "$local1.zone";
        allow-update { none; };
};
EOF
cp -a /var/named/named.localhost /var/named/$local1.local
cp -a /var/named/named.loopback /var/named/$local1.zone
cat > /var/named/$local1.local << EOF
\$TTL 1D
@       IN SOA  root.$local1. $local1. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      ns.$local1.
ns      A       $IP_0
        A       $IP_0
EOF
cat > /var/named/$local1.zone << EOF
\$TTL 1D
@       IN SOA  root.$local1. $local1. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS        ns.$local1.
$IP_4   PTR       $local1.
$IP_4   PTR       ns.$local1.      
EOF
systemctl restart named
if [ $? -eq 0 ]
then
echo "--------------------------------------"
echo "|DNS service configuration succeeded!|"
systemctl enable named &> /dev/null
firewall-cmd --add-service=dns --permanent &> /dev/null
firewall-cmd --reload &> /dev/null
echo "--------------------------------------"
nslookup $IP_0
echo "--------------------------------------"
else echo "|DNS service is not configured successfully!|"
fi
else echo "|DNS service not installed succesfully!|"
echo "--------------------------------------"
fi
;;
6)
echo "------------------"
echo "|Just a minute...|"
yum install -y xinetd &> /dev/null
yum install -y dhcp &> /dev/null
IP_a=`ifconfig | awk -F ' ' 'NR==2{print$2}'`
netmask=`ifconfig | awk -F ' ' 'NR==2{print$4}'`
IP_b="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.0""
IP_c="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.0""  #Take the top three IP addresses
IP_d="expr `ifconfig| awk -F ' ' 'NR==2{print$2}'  | awk -F '.' '{print$4}'` + 100" #Add 100 to the last bit of IP address
IP_e="expr `ifconfig| awk -F ' ' 'NR==2{print$2}'  | awk -F '.' '{print$4}'` + 200" #Add 200 to the last bit of IP address
IP_f="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.`$IP_d`"" #IP address after adding
IP_g="echo "`ifconfig | awk -F ' ' 'NR==2{print$2}' | cut -d '.' -f 1,2,3`.`$IP_e`""
cat > /etc/dhcp/dhcpd.conf << EOF
allow booting;
allow bootp;
ddns-update-style interim;
ignore client-updates;
subnet `$IP_b`  netmask $netmask {
        option subnet-mask      $netmask;
        option domain-name-servers  $IP_a;
        range dynamic-bootp `$IP_f` `$IP_g`;
        default-lease-time      21600;
        max-lease-time          43200;
        next-server             $IP_a;
        filename                "pxelinux.0";
}
EOF
systemctl restart dhcpd 
if [ $? -eq 0 ]
then systemctl enable dhcpd &> /dev/null
else echo " DHCP Configured error!"
exit 0
fi
yum install -y tftp-server &> /dev/null
cat > /etc/xinetd.d/tftp <<EOF
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
EOF
systemctl restart xinetd
systemctl restart tftp
if [ $? -eq 0 ]
then systemctl enable tftp &> /dev/null
else echo "TFTP service Configured error!"
exit 0
fi
systemctl enable xinetd &> /dev/null
firewall-cmd --permanent --add-service=tftp &> /dev/null
firewall-cmd --reload &> /dev/null
yum install -y syslinux &> /dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
cp /media/cdrom/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot
cp /media/cdrom/isolinux/{vesamenu.c32,boot.msg} /var/lib/tftpboot
if [ -e /var/lib/tftpboot/pxelinux.cfg ]
then echo "File Exists!"
else mkdir /var/lib/tftpboot/pxelinux.cfg &> /dev/null
fi
cp /media/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
version_0=`cat /etc/redhat-release | awk -F ' ' '{print$7}'`
cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF
default linux
timeout 600

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Red Hat Enterprise Linux $version_0
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label linux
  menu label ^Install Red Hat Enterprise Linux $version_0
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=ftp://$IP_a ks=ftp://$IP_a/pub/ks.cfg quiet

label check
  menu label Test this ^media & install Red Hat Enterprise Linux $version_0
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.6\x20Server.x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install Red Hat Enterprise Linux $version_0 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	Red Hat Enterprise Linux $version_0.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-$version_0\x20Server.x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a Red Hat Enterprise Linux system
  text help
	If the system will not boot, this lets you access files
	and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-$version_0\x20Server.x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
	If your system is having issues, a problem with your
	system's memory may be the cause. Use this utility to
	see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end
EOF
yum install -y vsftpd &> /dev/null
systemctl restart vsftpd 
systemctl enable vsftpd &> /dev/null
cp -r /media/cdrom/* /var/ftp && echo "-----------------------" && echo "|Transmission complete|!"
echo "-----------------------"
firewall-cmd --add-service=ftp --permanent &> /dev/null
firewall-cmd --reload &> /dev/null
setsebool -P ftpd_connect_all_unreserved=on &> /dev/null
cp ~/anaconda-ks.cfg /var/ftp/pub/ks.cfg
chmod +r /var/ftp/pub/ks.cfg
echo "#version=RHEL$version_0
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# Use CDROM installation media
url --url=ftp://$IP_a
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

# Root password
rootpw --iscrypted \$6\$qRAoZkxh5SHa7N4X\$w2osf.ZFey1hPtFdOJVIMgVOzc8dygUol2JphmSNQB6MHb7vPL63D6s9hIfrT9ydduKFOlq0S5/kp6.zJzYMy.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
user --name=roya --password=\$6\$ZT/uZLv5GPvdSNr7\$caWMweAE4l9z93nmeRSttpiwHeJr9rjEGlAANrZBv5pRcZVkUfFzTAGQuQgNXEhKjI75sD9aVUmH.n55fUocX0 --iscrypted --gecos="roya"
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=5000
part /boot --fstype="xfs" --ondisk=sda --size=2000
part / --fstype="xfs" --ondisk=sda --size=13000

%packages
@^graphical-server-environment
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@hardware-monitoring
@input-methods
@internet-browser
@multimedia
@print-client
@x11
chrony

%end
" > /var/ftp/pub/ks.cfg
if [ $? -eq 0 ]
then echo "PXE service Configuration successed!"
else echo "PXE service Configuration failed!"
fi
;;
*) 
exit 0
;;
esac

Almost universal with CentOS 7 system (only need to modify a few small places)
The operation results of one click deployment of PXE+kickstart unattended installation service are as follows:

Recreate a virtual machine and test for availability.

Remember to change the network card mode to host only mode

After this page appears, you only need to prove that it has been successful. Just have a cup of tea, wait for a while, and the system is installed successfully.

You are not allowed to reprint without permission. You will continue to improve the function in the future. If you think it is OK, leave your praise and comments. The code is already in Github Open source, welcome to give suggestions, writing is not easy, hope to support

13 original articles published, 47 praised, 30000 visitors+
Private letter follow

Posted by sandrob57 on Sun, 26 Jan 2020 07:11:04 -0800