RHEL6 无人值守搭建脚本


RHEL6 无人值守搭建脚本:

  1. #!/bin/bash   
  2. # by kumu 2012-07-28   
  3. # RHEL6 unattended deployment,用于RHEL6无人值守搭建.v0.1    
  4. # 有不足的地方还请见谅   
  5.   
  6. #搭建yum   
  7. Yum_build()  
  8. {  
  9.     yum clean all &>/dev/null  
  10.     num_list=$(yum list | wc -l)  
  11.     yum clean all &>/dev/null  
  12.   
  13.     if [[ $num_list < 2000 ]];then  
  14.   
  15.     mount /dev/cdrom /mnt  
  16.         if [ -d /etc/yum.repos.d/bak ];then  
  17.             mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak  
  18.         else  
  19.             mkdir /etc/yum.repos.d/bak  
  20.             mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak  
  21.         fi    
  22.   
  23. cat << EOF > /etc/yum.repos.d/base.repo  
  24. [base]  
  25. name=base  
  26. baseurl=file:///mnt  
  27. enabled=1  
  28. gpgcheck=0  
  29. EOF  
  30.     else  
  31.         echo "yum is ok!"  
  32.     fi  
  33. }  
  34.   
  35. Check_rpm()  
  36. {  
  37.     Packages=(dhcp tftp-server vsftpd syslinux)  
  38.     for i in ${Packages[@]}  
  39.     do  
  40.         if ! rpm -q $i &> /dev/null;then  
  41.              yum install $i -y &>/dev/null  
  42.         fi  
  43.     done  
  44. }  
  45.   
  46. #dhcp服务搭建以及网络安装引导文件   
  47. dhcp_build()  
  48. {  
  49.     read -p "输入所在subnet、netmask、broadcast、gateway: " subnet netmask broadcast gateway   
  50.     read -p "输入next-server地址: " next_server   
  51.     read -p "提供IP的范围start_ip end_ip: " start_ip end_ip  
  52.   
  53. cat << EOF >/etc/dhcp/dhcpd.conf  
  54. subnet $subnet netmask $netmask {  
  55.   range $start_ip $end_ip;  
  56.   option routers $gateway;  
  57.   option broadcast-address $broadcast;  
  58.   default-lease-time 600;  
  59.   max-lease-time 7200;  
  60.   filename "pxelinux.0";  
  61.   next-server $next_server;  
  62. }  
  63. EOF  
  64.   
  65. cat << EOF >/var/lib/tftpboot/pxelinux.cfg/default  
  66. default vesamenu.c32  
  67. #prompt 1   
  68. timeout 600  
  69.   
  70. display boot.msg  
  71.   
  72. menu background splash.jpg  
  73. menu title Welcome to Red Hat Enterprise Linux 6.2!  
  74. menu color border 0 #ffffffff #00000000   
  75. menu color sel 7 #ffffffff #ff000000   
  76. menu color title 0 #ffffffff #00000000   
  77. menu color tabmsg 0 #ffffffff #00000000   
  78. menu color unsel 0 #ffffffff #00000000   
  79. menu color hotsel 0 #ff000000 #ffffffff   
  80. menu color hotkey 7 #ffffffff #ff000000   
  81. menu color scrollbar 0 #ffffffff #00000000   
  82.   
  83. label linux  
  84.   menu label ^Install or upgrade an existing system  
  85.   menu default  
  86.   kernel vmlinuz  
  87.   append ks=ftp://$next_server/ks.cfg initrd=initrd.img  
  88. EOF  
  89. }  
  90.   
  91. #无人值守配置文件   
  92. ks_cfg()  
  93. {  
  94. read -p "请输入无人值守ftp地址: " ftp  
  95. cat <<EOF >/var/ftp/ks.cfg  
  96. firewall --disabled  
  97. install  
  98. url --url="ftp://$ftp/iso"  
  99. rootpw --iscrypted $1$A9aGh0yl$ahDdo0eAvQFAplXTeBkX//  
  100. auth  --useshadow  --passalgo=sha512  
  101. text  
  102. firstboot --disable  
  103. keyboard us  
  104. lang zh_CN  
  105. selinux --disabled  
  106. logging --level=info  
  107. reboot  
  108. timezone  Asia/Shanghai  
  109. network --bootproto=dhcp --device=eth0 --onboot=on  
  110. bootloader --location=mbr  
  111. zerombr  
  112. clearpart --all --initlabel   
  113.   
  114. part /boot --asprimary --fstype="ext4" --size=200  
  115. part swap --asprimary --fstype="swap" --size=2048  
  116. part / --asprimary --fstype="ext4" --size=100000  
  117.   
  118. %packages  
  119. @additional-devel  
  120. @backup-client  
  121. @base 
  122. @basic-desktop  
  123. @chinese-support  
  124. @client-mgmt-tools  
  125. @compat-libraries  
  126. @console-internet  
  127. @debugging 
  128. @desktop-debugging  
  129. @desktop-platform  
  130. @desktop-platform-devel  
  131. @development 
  132. @dial-up  
  133. @directory-client  
  134. @eclipse 
  135. @emacs 
  136. @fonts 
  137. @general-desktop  
  138. @graphical-admin-tools  
  139. @graphics 
  140. @hardware-monitoring  
  141. @infiniband 
  142. @input-methods  
  143. @internet-browser  
  144. @java-platform  
  145. @kde-desktop  
  146. @large-systems  
  147. @legacy-unix  
  148. @legacy-x  
  149. @mainframe-access  
  150. @network-file-system-client  
  151. @network-tools  
  152. @performance 
  153. @perl-runtime  
  154. @remote-desktop-clients  
  155. @scientific 
  156. @security-tools  
  157. @server-platform-devel  
  158. @smart-card  
  159. @storage-client-fcoe  
  160. @storage-client-iscsi  
  161. @storage-client-multipath  
  162. @system-management  
  163. @system-management-messaging-client  
  164. @system-management-messaging-server  
  165. @system-management-snmp  
  166. @system-management-wbem  
  167. @technical-writing  
  168. @tex 
  169. @x11  
  170. hmaccalc  
  171. %end  
  172. EOF  
  173. }  
  174.   
  175. #调用函数   
  176. Yum_build  
  177. Check_rpm  
  178.   
  179. #拷贝系统引导文件   
  180. cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/  
  181. cp  -r /mnt/isolinux/* /var/lib/tftpboot/  
  182. mkdir /var/lib/tftpboot/pxelinux.cfg &>/dev/null  
  183. mv /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default  
  184.   
  185. #调用函数   
  186. dhcp_build  
  187. ks_cfg  
  188.   
  189. mkdir /var/ftp/iso &>/dev/null  
  190. mount /dev/cdrom /var/ftp/iso &>/dev/null  
  191.   
  192. #设置开机启动   
  193. chkconfig tftp on   
  194. chkconfig dhcpd on  
  195. chkconfig vsftpd on  
  196.   
  197. #刷新服务   
  198. service xinetd restart &>/dev/null  
  199. service dhcpd restart &>/dev/null  
  200. service vsftpd restart &>/dev/null  

相关内容