【kickstart】批量安装centos6.6脚本搭建kickstart

#!/bin/bash

#date 20150907

#auther xiangyan

#test by centos 6.6


#系统环境

#实验环境:VMware Workstation 10

#系统平台:CentOS release 6.6 (最小化安装)

#网络模式:NAT模式(共享主机的IP地址)

#DHCP / TFTP IP:192.168.100.236

#HTTP / FTP / NFS IP:192.168.100.236

#防火墙已关闭/iptables: Firewall is not running.

#SELINUX=disabled


#关闭selinux

sed -e "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

sed -e "s/SELINUXTYPE=targeted/#SELINUXTYPE=targeted/g" /etc/selinux/config

setenforce 0

chattr -i /etc/{passwd,group,shadow,gshadow}

iptables -F

#准备工作

#生成ks.cfg 文件需要system-config-kickstart 工具,而此工具依赖于X Windows,所以我们需要安装X Windows 和Desktop 并重启系统,操作如下:

yum groupinstall "X Window System"

yum groupinstall Desktop

# reboot

#重启等会再下面执行


#配置HTTP安装方式

#系统的安装方式可以选择HTTP、FTP、 NFS,我们这里介绍HTTP方式的安装,其他两种方式的安装,大家可以自行百度。

#安装并配置HTTP

yum install httpd �Cy

rpm -qa|grep httpd 


#开启服务并设置开机启动

/etc/init.d/httpd start

chkconfig --level 35 httpd on


#将iso文件挂载至/mnt/cdrom.

mkdir /mnt/cdrom

mount /dev/cdrom /mnt/cdrom


#复制光盘全部内容至http 的根目录/var/www/html/ 下

cp -r /mnt/cdrom/ /var/www/html/


#配置TFTP

#安装tftp-server

yum install tftp-server �Cy


#启用tftp 服务

#vim /etc/xinetd.d/tftp


cat > /etc/xinetd.d/tftp <<EOF

# default: off

# description: The tftp server serves files using the trivial file transfer \

#       protocol.  The tftp protocol is often used to boot diskless \

#       workstations, download configuration files to network-aware printers, \

#       and to start the installation process for some operating systems.

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




#启动tftp服务

#因为tftp服务是挂载在超级进程xinetd 下的,所以通过启动xinetd 来启动tftp服务。

/etc/init.d/xinetd restart

#设置开机启动xinetd

chkconfig xinetd on


#配置支持PXE的启动程序

#复制pxelinux.0 文件至/var/lib/tftpboot/ 文件夹中

yum -y install syslinux

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ #拷贝引导文件到tftp服务器根目录


#说明:syslinux是一个功能强大的引导加载程序,而且兼容各种介质。更加确切地说:SYSLINUX是一个小型的Linux操作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。

#复制iso 镜像中的/image/pxeboot/initrd.img 和vmlinux 至/var/lib/tftpboot/ 文件夹中

cp /var/www/html/cdrom/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/


#复制iso 镜像中的/isolinux/*.msg 至/var/lib/tftpboot/ 文件夹中

cp /var/www/html/cdrom/isolinux/*.msg /var/lib/tftpboot/


#在/var/lib/tftpboot/ 中新建一个pxelinux.cfg目录

mkdir /var/lib/tftpboot/pxelinux.cfg


#将iso 镜像中的/isolinux 目录中的isolinux.cfg复制到pxelinux.cfg目录中,同时更改文件名称为default

cp /var/www/html/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default


#设置文件权限

chmod 644 /var/lib/tftpboot/pxelinux.cfg/default


#修改default文件

#vim /var/lib/tftpboot/pxelinux.cfg/default

cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF

default ks     #默认启动的是 'label ks' 中标记的启动内核

prompt 1           #显示 'boot: ' 提示符。为 '0' 时则不提示,将会直接启动 'default' 参数中指定的内容。

timeout 6      #在用户输入之前的超时时间,单位为 1/10 秒。

display boot.msg   #显示某个文件的内容,注意文件的路径。默认是在/var/lib/tftpboot/ 目录下。也可以指定位类似 '/install/boot.msg'这样的,路径+文件名。

F1 boot.msg     #按下 'F1' 这样的键后显示的文件。

F2 options.msg 

F3 general.msg 

F4 param.msg 

F5 rescue.msg 

label linux        #'label' 指定你在 'boot:' 提示符下输入的关键字,比如boot: linux[ENTER],这个会启动'label linux' 下标记的kernel 和initrd.img 文件。

  kernel vmlinuz   #kernel 参数指定要启动的内核。

  append initrd=initrd.img #append 指定追加给内核的参数,能够在grub 里使用的追加给内核的参数,在这里也都可以使用。

label text 

  kernel vmlinuz 

  append initrd=initrd.img text 

label ks 

  kernel vmlinuz 

  append ks=http://192.168.100.242/ks.cfg_6.6 initrd=initrd.img    #告诉系统,从哪里获取ks.cfg文件 

label local 

  localboot 1 

label memtest86 

  kernel memtest 

  append -

EOF


#配置DHCP

#安装DHCP服务

yum -y install dhcp

#复制配置模板文件到DHCP的配置目录中

cp -f /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample  /etc/dhcp/dhcpd.conf

#修改/etc/dhcp/dhcpd.conf 配置文件,内容如下:[直接复制一下内容]

cat > /etc/dhcp/dhcpd.conf <<EOF

ddns-update-style interim;  #设置DHCP服务器模式,这行必须有,否则启动失败。

ignore client-updates;      #禁止客户端更新

allow booting;

allow bootp;

ddns-update-style interim;

ignore client-updates;

allow booting;

allow bootp;

subnet 192.168.100.0 netmask 255.255.255.0{     #我所分配的IP地址所在网段

range 192.168.100.225 192.168.100.254;          #设置分配IP的地址池

option routers 192.168.100.225;                 #设置分配ip地址的网关

option subnet-mask 255.255.255.0;               #设置分配IP地址的子网掩码

option broadcast-address 192.168.100.255;       #广播地址

option domain-name-servers 202.106.0.20,8.8.8.8;   #设置dns服务器地址 

default-lease-time 21600;          #默认租用时间

max-lease-time 43200;              #最大租约时间

next-server 192.168.100.236;       #链接tftp服务器的IP,这个最重要

filename "pxelinux.0";             #tftp服务器根目录下面的文件名

}

#有时候我们需要为某个机器配置固定IP地址,配置选项如下

#Host server01 {

#Hardware ethernet b0:c0:12:f2:a3:a4;   #mac地址

#Fixed-address 192.168.100.253;         #绑定IP

#}

EOF

# 检测DHCP服务配置文件是否正确

service dhcpd configtest

#启动DHCP服务

/etc/init.d/dhcpd start


#生成ks.cfg 文件

#安装Kickstart

yum install system-config-kickstart


cat > /var/www/html/ks.cfg_VM <<EOF

#platform=x86, AMD64, 或 Intel EM64T

#version=DEVEL

# Firewall configuration

firewall --enabled --ssh

# Install OS instead of upgrade

install

# Use network installation

url --url="http://192.168.100.236/cdrom"

# Root password

rootpw --iscrypted $1$8HZ/4T8Y$M1yLywy/8nb9Cbtq072l3.

# System authorization information

auth  --useshadow  --passalgo=sha512

# Use graphical install

graphical

firstboot --disable

# System keyboard

keyboard us

# System language

lang zh_CN

# SELinux configuration

selinux --enforcing

# Installation logging level

logging --level=info

reboot

# System timezone

timezone  Africa/Abidjan

# System bootloader configuration

bootloader --location=mbr

# Clear the Master Boot Record

zerombr

# Partition clearing information

clearpart --all --initlabel 

# Disk partitioning information

#ignoredisk --only-use=sda

#ignoredisk --only-use=sdb

part / --fstype="ext4" --size=8000          #--ondisk=sdb

part /boot --fstype="ext4" --size=200        # --ondisk=sdb

part swap --fstype="swap" --size=1000        #--ondisk=sdb

part /opt --fstype="ext4" --grow --size=1  #--ondisk=sdb


%packages

@base

@chinese-support

@debugging

@dial-up

@performance

@security-tools

@system-management


%end

EOF



cat > /var/www/html/ks.cfg_xy <<EOF

#platform=x86, AMD64, 或 Intel EM64T

#version=DEVEL

# Firewall configuration

firewall --disabled

# Install OS instead of upgrade

install

# Use network installation

url --url="http://192.168.100.236/cdrom"

# Root password

rootpw --iscrypted $1$YD7IXDeW$Fd7P.UyZ.l.bKzamNm9QY1

# System authorization information

auth  --useshadow  --passalgo=sha512

# Use graphical install

graphical

firstboot --disable

# System keyboard

keyboard us

# System language

lang zh_CN

# SELinux configuration

selinux --disabled

# Installation logging level

logging --level=info

# Reboot after installation

reboot

# System timezone

timezone  Africa/Abidjan

# Network information

network  --bootproto=dhcp --device=eth0 --onboot=on

# System bootloader configuration

bootloader --location=mbr

# Clear the Master Boot Record

zerombr

# Partition clearing information

clearpart --all --initlabel 

# Disk partitioning information

part / --fstype="ext4" --size=10240

part /boot --fstype="ext4" --size=200

part swap --fstype="swap" --size=4096

part /opt --fstype="ext4" --grow --size=1


%packages

@additional-devel

@base

@desktop-platform-devel

@development

@directory-server

@internet-browser

@java-platform

@performance

@security-tools

@server-platform-devel

@system-admin-tools

@system-management

hmaccalc


%end


EOF


你可能感兴趣的:(pxe)