PXE+cobbler+kickstart全自动无人值守网络安装系统

简述:通过配置cobbler自动部署DHCP、TFTP、HTTP,在安装过程中加载kiskstart无人值守安装应答文件实现无人值守。从客户端使用PXE引导启动安装。

1. 安装相关软件:

1
2
3
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
yum -y install cobbler tftp-server dhcp httpd xinetd

注意 /var/www/cobbler 目录必须具有足够容纳 Linux 安装文件的空间(移动,建软链接)

#ln -s /cobbler /var/www

2. 检查 cobbler 配置:

1
cobbler check

按提示解决相关问题,把 /var/lib/cobbler/settings 中的 server 和 next_server 设为本服务器的 IP 地址,manage_dhcp 设为 1,以便管理 DHCP
将 /etc/xinetd.d/tftp 中 disable = no

3. 导入CentOS 安装 DVD ISO 中的文件:

1
2
3
4
5
6
7
mount -o loop /home/dtm/CentOS-5.5-x86_64-bin-DVD.iso /mnt/iso       # 将ISO文件挂载到 /mnt/iso 目录
cobbler import --mirror=/mnt/iso --name=centos5.5                              # 从 /mnt/iso目录导入所有安装文件,命名为 centos5.5
cobbler distro list                                                                                      # 查看导入结果

[root@elain www]# cobbler distro list
centos5.5-x86_64
centos5.5-xen-x86_64


cobbler目录及文件

cobbler相关配置文件: /etc/cobbler
cobbler数据存储目录: /var/www/cobbler
dhcp配置文件: /etc/dhcpd.conf
dhcp租期缓存文件: /var/lib/dhcpd/dhcpd.leases
pxe配置文件: /tftpboot/pxelinux.cfg/default

ks模板文件: /var/lib/cobbler/kickstarts

4. 修改 DHCP 和 Kickstart 配置模板:

vi /etc/cobbler/dhcp.template # DHCP 配置模板,根据提示修改下面模板
vi /etc/dhcpd.conf

################################

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ddns-update-style interim;

allow booting;
allow bootp;

ignore client-updates;
set vendorclass = option vendor-class-identifier;

subnet 192.168.8.0 netmask 255.255.255.0 {
option routers          192.168.8.92;
option subnet-mask      255.255.255.0;
range dynamic-bootp     192.168.8.10 192.168.8.254;
filename                "/pxelinux.0";
default-lease-time      21600;
max-lease-time          43200;
next-server             192.168.8.92;
}

###############################
vi /etc/cobbler/kickstart_fc6.ks # Kickstart 配置模板

#####################

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#platform=x86, AMD64, 或 Intel EM64T
#created by elain
#date:2010.06.27

# System authorization information
auth  --useshadow  --enablemd5

# System bootloader configuration
bootloader --location=mbr

# Partition clearing information
clearpart --all --initlabel

# Use text mode install
text

# Firewall configuration
firewall --disabled

# Run the Setup Agent on first boot
firstboot --disable

# System keyboard
keyboard us

# System language
lang zh_CN

# Installation logging level
logging --level=info

# Use network installation
url --url=http://192.168.8.92/cobbler/ks_mirror/centos5.5

# Network information
network --bootproto=bootp --device=eth0 --onboot=on

# Reboot after installation
reboot

#Root password
rootpw   elain

# SELinux configuration
selinux --disabled

# Do not configure the X Window System
skipx

# System timezone
timezone  Asia/Chongqing

# Install OS instead of upgrade
install

# Disk partitioning information(根据不同服务需求更改分区大小)
part /boot --asprimary --bytes-per-inode=4096 --fstype="ext3" --grow --maxsize=512 --size=1
part / --asprimary --bytes-per-inode=4096 --fstype="ext3" --grow --maxsize=81920 --size=1
part swap --bytes-per-inode=4096 --fstype="swap" --grow --maxsize=81920 --size=1
part /elain --bytes-per-inode=4096 --fstype="ext3" --grow --size=1

%packages
@ development-libs
@ development-tools
@ admin-tools
@ system-tools
@ x-software-development
@ base
@ chinese-support
@ editors
%post

#优化
echo "NETWORKING=yes" >/etc/sysconfig/network
echo "HOSTNAME=elain" >>/etc/sysconfig/network
echo "nameserver  172.168.32.18" >/etc/resolv.conf
echo "nameserver  202.106.0.20" >>/etc/resolv.conf

cd /root/

rm -rf install.log anaconda-ks.cfg install.log.syslog
mkdir {tools,scripts,bak,conf_bak}
mkdir -p /elain/{apps,data,logs,backup}
touch "请保持服务器整洁,操作后无用的文件及时清理"

#添加用户
groupadd -g 600 elainadmin
useradd -g elainadmin elainadmin -u 600 -p 'elainadmin!@#'

#允许sodu
echo "elainadmin ALL=(ALL)   PASSWD: ALL"  >>/etc/sudoers

#连接超时1小时
echo "TMOUT=3600" >>/etc/profile
source /etc/profile

#禁止提示接收邮件
echo "unset MAILCHECK" >> /etc/profile
source /etc/profile

#禁止按CTRL+ALT+DEL重启
sed -i s/^ca/\#ca/g /etc/inittab

#解决SSH登录慢的问题
echo "UseDNS no" >>/etc/ssh/sshd_config
/etc/init.d/sshd restart

#默认VI为VIM
echo "alias vi='vim'" >>/root/.bashrc
echo "alias vi='vim'" >>/home/elainadmin/.bashrc
. /root/.bashrc
. /home/elainadmin/.bashrc
#关闭不必要的服务
for service in `chkconfig --list|awk '{print $1}'|egrep -v "^$"|awk -F ":" '{print $1}'`; do chkconfig $service off;done
for service in crond  sshd syslog network; do chkconfig $service on;done

#################################
5. 生成并同步所有配置:

1
cobbler sync

注:每次更改Kickstart 配置文件,都需要使用cobbler sync重新同步所有配置

6. 启动相关服务:

1
2
3
4
/etc/init.d/httpd start
/etc/init.d/xinetd start
/etc/init.d/dhcpd start
/etc/init.d/cobblerd start

7.客户端启动PXE 安装

启动另一台新服务器,通过 PXE 启动进入,提示”boot:”时输入”menu”即可进入蓝色的 Cobbler 安装界面,选择安装项,自动完成系统安装。

http方式安装:服务器:192.168.9.101 目录:/cobbler/ks_mirror/centos/

8.重装系统:

在要重装的机器安装koan

1
yum install koan

查看cobbler服务器有哪些安装盘

1
koan --list-profiles --server=192.168.9.101

选择要重装的系统并重启

1
2
koan --replace-self --server=192.168.9.101 --profile=centos5.5
reboot

附上服务控制脚本:
[root@elain ~]# cat cobbler.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/sh
# created by dingtm
# date: 2010.12.31

case $1 in
start)
/etc/init.d/httpd start
/etc/init.d/xinetd start
/etc/init.d/dhcpd start
/etc/init.d/cobblerd start
;;
stop)
/etc/init.d/httpd stop
/etc/init.d/xinetd stop
/etc/init.d/dhcpd stop
/etc/init.d/cobblerd stop
;;
status)
/etc/init.d/httpd status
/etc/init.d/xinetd status
/etc/init.d/dhcpd status
/etc/init.d/cobblerd status
;;
sync)
cobbler sync
;;
*)
echo "Input error,please in put 'start|stop|status|sync'!";
exit 2>&1 >/dev/null &
;;
esac

注:如果不是同时安装,IP段比较少的话,会出现装一定数量后,无法获取IP现象,这是因为客户端IP都缓存在服务器上了,没有可用IP分配,解决方法:

清除客户端DHCP 缓存

1
rm /var/lib/dhcpd/dhcpd.leases

参考文档:

http://oss.org.cn/ossdocs/gnu_linux/redhat/rhl-cg-zh_CN-9/s1-kickstart2-options.html

文章目录
  1. 1. ks模板文件: /var/lib/cobbler/kickstarts
,