Linux安装-kickstart无人值守安装

                      Linux安装-kickstart无人值守安装

                                        作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  大家做运维估计都避免不了时不时会去机房安装一台linux服务器,但是服务器启动的特别慢,有的要2分钟,有的要5分钟,甚至时间刚才,我个人比较喜欢的服务器是Dell,相对来说启动的还算是快的,像华为服务器,IBM服务器启动的到加载bootloader是特别慢的一个时间。然后等到这些流程走完了还要安装操作系统,格式化又得等时间,想了想要是能把系统的服务器都提前指定好,让它自己安装就好了,查阅了相关资料,发现/root目录下有“anaconda-ks.cfg”这么一个配置文件,凡是你安装操作系统都会有这个文件。它到底是干啥用的呢?我会在下面的内容中一一揭晓,请带着你的好奇心往下看。

   在我们切入正式话题之前,我们也得先了解一些基本原理,因为原理是一通百通的,以下我只是用centos环境为实验,大家也可以用ubantu,windows这些用户常用的系统来做,其原理都大同小异,这只是自动化安装系统的冰山一角,详情请参考redhat官网,那里面有700多页的关于这个的介绍,感兴趣的朋友可以去官网看下,下面跟着我一起来学习如何配置一个无人值守安装的OS(Operating System)吧。

一.linux系统安装光盘目录结构
1.images:存放内核镜像;
2.isolinux:启动安装界面时,所用到的内核,应用程序,背景图片文件等都由该目录提供;
3.Packages:存放安装所需要的软件包(二进制的rpm包)用的,里面存放着anaconda程序
anaconda:fedora,Linux的安装程序
anaconda:将安装分成两个阶段
安装前配置阶段
键盘类型
安装过程中的语言
支持的语言
时区
选择要使用的磁盘设备
分区格式化
选择要安装的程序包
管理员密码
是否使用iptables
是否启用selinux
安装阶段
在目标磁盘上根文件系统
将选定的程序包安装至目标磁盘
 
二.如何启动安装过程
MBR:bootloader,本身可启动的安装光盘
网络启动安装过程
可移动设备,便携式设备
 
anaconda的配置文件称为kickstat
 
安装前配置阶段:
配置的选项:
必选项:
可选项:
 
三.kickstart文件的组成部分:
1.命令段:用于配置系统
install
text #文本式安装
firewall
part
lvm
2.软件包:指定要安装的程序包(程序包名)及程序包组(@组名)
%packages 标识
@Base:使用@指定包组
lftp:直接写程序包名
tree:
每行一个
注意:在程序包前加-,不安装相关包
%end :软件包结束
3.脚本段:
%pre:安装过程开始前的预备脚本,所能执行的操作较小,它是一个受限的环境,因为其是仅有简装版的shell环境。
%post:所有的软件完成之后执行的脚本,此时,具有完整意义上的shell环境,但并非所有命令都安装,先确保所有的程序包已经安装
 1 [root@yinzhengjie ~]# cat anaconda-ks.cfg 
 2 # Kickstart file automatically generated by anaconda.
 3 
 4 #version=DEVEL
 5 install #命令段:用于配置系统,install就类似于标识符。
 6 cdrom  #指定安装树
 7 lang en_US.UTF-8 #执行语言选项的
 8 keyboard us #键盘类型
 9 network --onboot yes --device eth0 --bootproto dhcp  #定义网络属性
10 rootpw  --iscrypted $1$wFJh7rT6$omn0IhuHv7a95SYMx/K640  #管理员的密码
11 # Reboot after installation
12 reboot  #安装后会自动重启,如果不想其自动重启可以注释掉。
13 firewall --service=ssh  #防火墙开启SSH功能
14 authconfig --useshadow --enablemd5  #认证的相关配置(用户密码在shadow里去找,加密算法用md5)
15 selinux --enforcing  #selinux为开启模式
16 timezone --utc America/Los_Angeles  #选择时区
17 bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"  #指定你的MBR
18 # The following is the partition information you requested #注意:"#“后面没有空格说明是禁用功能,如果有空格说明是注释内容
19 # Note that any partitions you deleted are not expressed
20 # here so unless you clear all partitions first, this is
21 # not guaranteed to work
22 #clearpart --all --initlabel  #清楚所有分区,如果你是双系统的这个要注意用哟!
23 
24 #part /boot --fstype=ext4 --size=300  #指定分区/boot的分区信息,以下2行类似
25 #part / --fstype=ext4 --grow --size=3000
26 #part swap --grow --maxsize=1984 --size=1984
27 
28 
29 
30 repo --name="CentOS"  --baseurl=cdrom:sr1 --cost=100  #指定安装参考,明确指明安装树的路径
31 
32 %packages #软件包:指定要安装的程序包(程序包名)及程序包组(@组名)
33 @Base     #使用@指定包组,表示安装Base这个包组。
34 @Core
35 @Desktop
36 @Fonts
37 @General Purpose Desktop
38 @Internet Browser
39 @Printing client
40 @perl-runtime #在程序包前加-,不安装相关包,就说明这个包组不安装runtime这个软件包
41 @X Window System
42 binutils
43 gcc
44 kernel-devel #在程序包前加-,不安装相关包,就是这个程序包中不安装该包
45 make
46 patch
47 python
48 
49 %end #软件包结束
50 
51 %post #所有的软件完成之后执行的脚本
52 if [ -f /boot/grub/menu.lst -a ! -h /boot/grub/menu.lst ]; then cp /boot/grub/menu.lst /boot/grub/menu.lst.bak && sed -i 's/ rhgb//' /boot/grub/menu.lst; fi
53 if [ -f /boot/grub/grub.conf -a ! -h /boot/grub/grub.conf ]; then cp /boot/grub/grub.conf /boot/grub/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub/grub.conf; fi
54 if [ -f /boot/grub2/grub.conf -a ! -h /boot/grub2/grub.conf ]; then cp /boot/grub2/grub.conf /boot/grub2/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub2/grub.conf; fi
55 if [ -f /etc/rc.d/rc.local ]; then cp /etc/rc.d/rc.local /etc/rc.d/rc.local.backup; fi
56 cat >>/etc/rc.d/rc.local <<EOF
57 #!/bin/bash
58 echo
59 echo "Installing VMware Tools, please wait..."
60 if [ -x /usr/sbin/getenforce ]; then oldenforce=$(/usr/sbin/getenforce); /usr/sbin/setenforce permissive || true; fi
61 mkdir -p /tmp/vmware-toolsmnt0
62 for i in hda sr0 scd0; do mount -t iso9660 /dev/$i /tmp/vmware-toolsmnt0 && break; done
63 cp -a /tmp/vmware-toolsmnt0 /opt/vmware-tools-installer
64 chmod 755 /opt/vmware-tools-installer
65 cd /opt/vmware-tools-installer
66 mv upgra32 vmware-tools-upgrader-32
67 mv upgra64 vmware-tools-upgrader-64
68 mv upgrade.sh run_upgrader.sh
69 chmod +x /opt/vmware-tools-installer/*upgr*
70 umount /tmp/vmware-toolsmnt0
71 rmdir /tmp/vmware-toolsmnt0
72 if [ -x /usr/bin/rhgb-client ]; then /usr/bin/rhgb-client --quit; fi
73 cd /opt/vmware-tools-installer
74 for s in sr0 sr1; do eject -s /dev/$s; done
75 ./run_upgrader.sh
76 if [ -f /etc/rc.d/rc.local.backup ]; then mv /etc/rc.d/rc.local.backup /etc/rc.d/rc.local; else rm -f /etc/rc.d/rc.local; fi
77 rm -rf /opt/vmware-tools-installer
78 sed -i 's/3:initdefault/5:initdefault/' /etc/inittab
79 if [ -f /boot/grub/menu.lst.bak ]; then mv /boot/grub/menu.lst.bak /boot/grub/menu.lst; fi
80 if [ -f /boot/grub/grub.conf.bak ]; then mv /boot/grub/grub.conf.bak /boot/grub/grub.conf; fi
81 if [ -f /boot/grub2/grub.conf.bak ]; then mv /boot/grub2/grub.conf.bak /boot/grub2/grub.conf; fi
82 if [ -x /usr/sbin/getenforce ]; then /usr/sbin/setenforce $oldenforce || true; fi
83 if [ -x /bin/systemd ]; then systemctl restart prefdm.service; else telinit 5; fi
84 EOF
85 chmod 755 /etc/rc.d/rc.local
86 if [ -x /bin/systemd ]; then systemctl enable rc-local.service; fi
87 /usr/sbin/adduser yinzhengjie
88 /usr/sbin/usermod -p '$1$wFJh7rT6$omn0IhuHv7a95SYMx/K640' yinzhengjie
89 /usr/bin/chfn -f "yinzhengjie" yinzhengjie
90 /bin/echo done
91 %end #表示软件包结束
92 [root@yinzhengjie ~]# 
详解anaconda-ks.cfg配置文件
 1 #debug --graphics
 2 default=0
 3 splashimage=@SPLASHPATH@  #读取splash配镜图像的
 4 timeout 5
 5 hiddenmenu #隐藏菜单
 6 title @PRODUCT@ @VERSION@
 7     kernel @KERNELPATH@
 8     initrd @INITRDPATH@
 9 title Install system with basic video driver
10     kernel @KERNELPATH@ xdriver=vesa nomodeset askmethod
11     initrd @INITRDPATH@
12 title rescue
13     kernel @KERNELPATH@ rescue askmethod
14     initrd @INITRDPATH@
15 
16 '''
17 其实这个信息显示的并不完全准确,因为它是被“isolinux”和“isolinux.cfg”二次包装过
18 '''
iso镜像isolinux目录下grub.conf配置文件
 1 default vesamenu.c32  #加载“vesamenu.c32”这个应用程序
 2 #prompt 1
 3 timeout 600
 4 
 5 display boot.msg
 6 
 7 menu background splash.jpg  #设置菜单背景图
 8 menu title Welcome to CentOS 6.6! #这是标题信息
 9 menu color border 0 #ffffffff #00000000  #设置边框颜色
10 menu color sel 7 #ffffffff #ff000000
11 menu color title 0 #ffffffff #00000000
12 menu color tabmsg 0 #ffffffff #00000000
13 menu color unsel 0 #ffffffff #00000000
14 menu color hotsel 0 #ff000000 #ffffffff
15 menu color hotkey 7 #ffffffff #ff000000
16 menu color scrollbar 0 #ffffffff #00000000
17 
18 label linux #linux的标签名称,在启动过程中,你可以敲击Esc键,然后输入该标签名称,就会启动该选项,而且还可以在后面加一些参数,如你不想启用图像界面 可以输入:linux text
19   menu label ^Install or upgrade an existing system  #定义标题的名称,“^Install”其中这个“^”表示你敲击I就能选择这一项
20   menu default  #默认选择这项
21   kernel vmlinuz  #启动内核文件
22   append initrd=initrd.img  #启动rd文件,也是内核文件
23 label vesa
24   menu label Install system with ^basic video driver
25   kernel vmlinuz
26   append initrd=initrd.img xdriver=vesa nomodeset  #后面有参数,nomodeset表示不适用任何额外的模式
27 label rescue
28   menu label ^Rescue installed system
29   kernel vmlinuz
30   append initrd=initrd.img rescue #表示启动救援模式
31 label local
32   menu label Boot from ^local drive
33   localboot 0xffff
34 label memtest86
35   menu label ^Memory test
36   kernel memtest  #内核测试程序
37   append -
iso镜像isolinux目录下isolinux.cfg配置文件
 1 '''
 2 启动安装界面时,boot提示符后,可以向安装内核传递许多的配置参数,用于指定安装过程的特性
 3 boot:
 4    test:文本安装界面
 5    graphical:图形安装界面
 6    askmethod:提示用户指定安装方法,让用户选择使用的安装树
 7    asknetword:提示用户在安装过程中使用网络功能,并提示用户配置网络地址
 8    dd:提示用户指定一个驱动程序所在的设备
 9    ks=:指定一个安装过程使用的kickstart文件
10       ks={http|https}://<sevrer>/<path>
11       ks=cdrom:/<path>
12       ks=nfs:<server>:/<path>
13     repo=:指定安装树位置
14         repo=ftp://<path>
15         repo={http|https}://<path>
16         repo=nfs:<path>
17     ip:
18     netmask:
19     gateway:
20     dns:
21     noipv6:
22 '''
手动启动安装界面时,boot提示符后操作参数详解
1 '''
2 提示:
3       如果你的服务器是通过光盘安装的话,在启动的时候可以手动制动ks文件的所在路径,它就会去找那个路径,并根据文件的内容安装相应的软件包.
4 如:
5 '''

四.利用完整玩系统自带的kickstart文件配置自动安装系统

 1 [root@yinzhengjie ~]# mkdir /yinzhengjie/media
 2 [root@yinzhengjie ~]# mount /dev/sr0 /yinzhengjie/media/
 3 mount: block device /dev/sr0 is write-protected, mounting read-only
 4 [root@yinzhengjie ~]# mkdir /yinzhengjie/linux_iso
 5 [root@yinzhengjie ~]# cp -r /yinzhengjie/media/isolinux/  /yinzhengjie/linux_iso
 6 [root@yinzhengjie ~]# cat /root/anaconda-ks.cfg  > /yinzhengjie/linux_iso/ks.cf
 7 [root@yinzhengjie ~]# ll /yinzhengjie/linux_iso/
 8 total 8
 9 dr-xr-xr-x. 2 root root 4096 May 24 23:01 isolinux
10 -rw-r--r--. 1 root root 3350 May 24 23:03 ks.cfg
11 [root@yinzhengjie ~]# 
12 [root@yinzhengjie ~]# cd /yinzhengjie/linux_iso/isolinux/
13 [root@yinzhengjie isolinux]# more isolinux.cfg   #适当修改一些参数
14 default vesamenu.c32
15 #prompt 1
16 timeout 600
17 
18 display boot.msg
19 
20 menu background splash.jpg
21 menu title CentOS 6.6 made in china by yinzhengjie! #修改一个标题
22 menu color border 0 #ffffffff #00000000
23 menu color sel 7 #ffffffff #ff000000
24 menu color title 0 #ffffffff #00000000
25 menu color tabmsg 0 #ffffffff #00000000
26 menu color unsel 0 #ffffffff #00000000
27 menu color hotsel 0 #ff000000 #ffffffff
28 menu color hotkey 7 #ffffffff #ff000000
29 menu color scrollbar 0 #ffffffff #00000000
30 
31 label linux
32   menu label ^Install CentOs6.6 by yinzhengjie  #修改安装选项名称
33   menu default
34   kernel vmlinuz
35   append initrd=initrd.img ks=cdrom:/ks.cfg  #在这里指定kickstart的位置所在。
36 label vesa
37   menu label Install system with ^basic video driver
38   kernel vmlinuz
39   append initrd=initrd.img xdriver=vesa nomodeset
40 label rescue
41   menu label ^Rescue installed system
42   kernel vmlinuz
43   append initrd=initrd.img rescue
44 label local
45   menu label Boot from ^local drive
46   localboot 0xffff
47 label memtest86
48   menu label ^Memory test
49   kernel memtest
50   append -
51 
52 [root@yinzhengjie isolinux]# 
53 [root@yinzhengjie ~]# cd /yinzhengjie/ #修改
54 [root@yinzhengjie yinzhengjie]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CENTOS" -b isolinux/isolinux.bin  -c isolinux/boot.cat  -o /root/yinzhengjie.iso linux_iso/
55 I: -input-charset not specified, using utf-8 (detected in locale settings)
56 genisoimage 1.1.9 (Linux)
57 Scanning linux_iso/
58 Scanning linux_iso/isolinux
59 Excluded: linux_iso/isolinux/TRANS.TBL
60 Excluded by match: linux_iso/isolinux/boot.cat
61 Writing:   Initial Padblock                        Start Block 0
62 Done with: Initial Padblock                        Block(s)    16
63 Writing:   Primary Volume Descriptor               Start Block 16
64 Done with: Primary Volume Descriptor               Block(s)    1
65 Writing:   Eltorito Volume Descriptor              Start Block 17
66 Size of boot image is 4 sectors -> No emulation
67 Done with: Eltorito Volume Descriptor              Block(s)    1
68 Writing:   Joliet Volume Descriptor                Start Block 18
69 Done with: Joliet Volume Descriptor                Block(s)    1
70 Writing:   End Volume Descriptor                   Start Block 19
71 Done with: End Volume Descriptor                   Block(s)    1
72 Writing:   Version block                           Start Block 20
73 Done with: Version block                           Block(s)    1
74 Writing:   Path table                              Start Block 21
75 Done with: Path table                              Block(s)    4
76 Writing:   Joliet path table                       Start Block 25
77 Done with: Joliet path table                       Block(s)    4
78 Writing:   Directory tree                          Start Block 29
79 Done with: Directory tree                          Block(s)    2
80 Writing:   Joliet directory tree                   Start Block 31
81 Done with: Joliet directory tree                   Block(s)    2
82 Writing:   Directory tree cleanup                  Start Block 33
83 Done with: Directory tree cleanup                  Block(s)    0
84 Writing:   Extension record                        Start Block 33
85 Done with: Extension record                        Block(s)    1
86 Writing:   The File(s)                             Start Block 34
87  25.74% done, estimate finish Thu May 25 01:07:14 2017
88  51.39% done, estimate finish Thu May 25 01:07:14 2017
89  77.11% done, estimate finish Thu May 25 01:07:14 2017
90 Total translation table size: 4701
91 Total rockridge attributes bytes: 1438
92 Total directory bytes: 2654
93 Path table size(bytes): 26
94 Done with: The File(s)                             Block(s)    19284
95 Writing:   Ending Padblock                         Start Block 19318
96 Done with: Ending Padblock                         Block(s)    150
97 Max brk space used 0
98 19468 extents written (38 MB)
99 [root@yinzhengjie yinzhengjie]# 

 修改参数后,效果图如下:

1 [root@yinzhengjie yinzhengjie]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CENTOS(可改,这里只是个卷标名称)" -b isolinux/isolinux.bin(这里要写相对路径)  -c isolinux/boot.cat(这里要写相对路径)  -o /root/yinzhengjie.iso(这里只是个文件名称,定义生成映像的文件名) linux_iso/ (这里要写对那个目录进行源镜像文件)
2 
3 
4 注意:光盘中的背景图片也是可以更改的,但是要求大小:640*480
5 

 五.手动自作kickstart文件

A.创建kickstart文件的方式:
1.复制模板/root/anaconda-ks.cfg。而后使用vim编辑配置(得知道包租和包名,技术要求会高些),也可以用system-config-kickstart来打开文件进行编辑会更加方便。
2.使用system-config-kickstart来生成,建议使用/root/anaconda-ks.cfg 模板生成
 
B.安装HTTP服务,用于提供映像存储
1.安装http并挂载映像
[root@yinzhengjie ~]# yum -y install httpd #安装存储映像的服务
[root@yinzhengjie ~]# umount /dev/sr0 #卸载掉之前的挂载
[root@yinzhengjie ~]# mkdir /var/www/html/yinzhengjie #创建挂载点
[root@yinzhengjie ~]# mount /dev/sr0 /var/www/html/yinzhengjie/ #挂载目录
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@yinzhengjie ~]# ll /var/www/html/yinzhengjie/ #查看是否挂载成功
total 712
-r--r--r--. 2 root root 14 Oct 24 2014 CentOS_BuildTag
dr-xr-xr-x. 3 root root 2048 Oct 24 2014 EFI
-r--r--r--. 2 root root 212 Nov 28 2013 EULA
-r--r--r--. 2 root root 18009 Nov 28 2013 GPL
dr-xr-xr-x. 3 root root 2048 Oct 24 2014 images
dr-xr-xr-x. 2 root root 2048 Oct 24 2014 isolinux
dr-xr-xr-x. 2 root root 686080 Oct 24 2014 Packages
-r--r--r--. 2 root root 1354 Oct 20 2014 RELEASE-NOTES-en-US.html
dr-xr-xr-x. 2 root root 4096 Oct 24 2014 repodata
-r--r--r--. 2 root root 1706 Nov 28 2013 RPM-GPG-KEY-CentOS-6
-r--r--r--. 2 root root 1730 Nov 28 2013 RPM-GPG-KEY-CentOS-Debug-6
-r--r--r--. 2 root root 1730 Nov 28 2013 RPM-GPG-KEY-CentOS-Security-6
-r--r--r--. 2 root root 1734 Nov 28 2013 RPM-GPG-KEY-CentOS-Testing-6
-r--r--r--. 1 root root 3380 Oct 24 2014 TRANS.TBL
[root@yinzhengjie ~]#
2.启动http服务
[root@yinzhengjie ~]# /etc/init.d/httpd start #启动服务
Starting httpd: httpd: apr_sockaddr_info_get() failed for yinzhengjie
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@yinzhengjie ~]# ifconfig | head -2 | tail -1 |awk '{print $2}'|awk -F ":" '{print $2}' #获取server的IP
172.16.3.218
[root@yinzhengjie ~]#
3.验证服务是否启动成功

C.手动配置“kickstart”文件

1.安装图像配置工具:system-config-kickstart
[root@yinzhengjie ~]# yum -y install system-config-kickstart
2运行system-config-kickstart工具
[root@yinzhengjie ~]# system-config-kickstart &
3.基础配置
4.选择安装方法
5.引导选项默认即可
6.分区信息
7.网络配置
8.认证
9.安全配置
10.显示界面配置
11.安装包选择
12.安装前和安装后的脚本
13.保存文件

14.检测生成的kickstart文件是否有语法错误
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 [root@yinzhengjie ~]# cd Desktop
 8 [root@yinzhengjie Desktop]# ll
 9 total 4
10 -rw-r--r--. 1 root root 1394 May 25 03:33 ks.cfg
11 [root@yinzhengjie Desktop]# ksvalidator ks.cfg 
12 [root@yinzhengjie Desktop]# 
13 
14 '''
15 注意:
16      ksvalidator 检查kickstart文件的配置是否存在问题, centos 6和5的语法上并不完全兼容
17 
18 '''

六.系统故障排除之:bash程序文件损坏

[root@yinzhengjie ~]# mv /bin/bash /tmp/  #模拟bash被删除掉的场景,然后断电重启服务器

解决方案一:

1.将光盘加载进去,启动紧急救援模式

2.选择语言

3.选择键盘类别

4.不开启网络功能

5.点击继续

6.点击OK,

7.继续点击OK

8.启动bash,点击OK

9.安装bash

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 '''
 9 1.启动紧急救援模式
10 2.获取到bash的rpm包:
11    #mkdir -p /yinzhengjie/media  #创建挂载点
12    #mount -r /dev/dvd  /yinzhengjie/media  #以只读的方式挂载
13    #rpm -ivh bash-*.rpm --replacepkgs --root=/mnt/sysimage/  #安装bash,参数--root=/mnt/sysimage/ 表示你的操作系统在这个光盘启动shell的挂载点。
14  紧急救援模式:启动了一个工作于光盘上的linux,要注意的是你救援光盘要和你的光盘最好保持一致。
15 '''

原文地址:https://www.cnblogs.com/yinzhengjie/p/6894692.html