近期,根据业务需要,我们需要为每个分支机构部署一台服务器,并要在服务器上部署一些业务系统,如MySQL、Nginx等。传统的方法是安装完操作系统,然后依次部署各项服务,显然,这么做的效率是非常低的,并且操作很繁锁,也容易出现操作上的误差。那么有没有一种简单高效且可靠的系统部署方法呢,于是我们想到了使用kickstart。利用kickstart制作无人值守的全自动安装ISO镜像,然后通过U盘进行安装。
使用kickstart制作可以自动安装的光盘是比较容易的,但是,由于现在光盘的使用越来越少,部分服务器上也不再提供光驱,于是,我们打算使用U盘来安装,但这个过程中,由于真实光盘和U盘的差异性,在适配U盘安装的过程中,遇到了很多坑,也学到了很多我东西,这里整理成你看到的这篇文章。
总体流程
总体的步骤还是比较清晰的,可以简单归纳为这几个部署:
复制标准安装ISO -> 集成rpm -> 编写ks.cfg文件 -> 加入自定义业务部署脚本 -> ISO封装 -> 写入U盘
任务目标
因为我们真实的任务中部署了非常多的服务,脚本也很长,不便于说明,所以这里假设一个目标,作以说明。
为了方便后文描述,这里假设我们需要制作一款自动部署镜像,安装完成后,会自动安装percona-xtrabackup(repo集成方式)和安装supervisor软件(tar包方式,无外网连接),系统启动后,通过初始化脚本,设置系统IP(静态)并设置主机名
构建repo软件仓库
这里使用CentOS-7-x86_64-Minimal-1804.iso作为基础镜像,之所以选择Minimal是因为镜像非常小,只有906M,并且没有无用的内容,可以保证最终生成的镜像大小也合适,需要的功能,根据需要添加即可。
挂载光盘镜像,并复制光盘内容到/tmp/iso目录下
mount /dev/sr0 /mnt
#创建工作目录
mkdir -p /tmp/iso
#复制光盘内容到iso目录
cp -r /mnt/* /tmp/iso/
cd /tmp/iso
#设置光盘作为yum本地源,方便分析哪些包在光盘中没有,需要添加
#安装yum源优先级插件,让光盘的源优先级更高
yum install yum-plugin-priorities
##priority=1 即优先级最高
cat >/etc/yum.repos.d/CentOS-Media.repo<<EOF
[c7-local]
name=CentOS-7-CD
baseurl=file:///tmp/iso/
gpgcheck=1
enabled=1
priority=1
gpgkey=file:///tmp/iso/RPM-GPG-KEY-CentOS-7
EOF
yum clean all #清除缓存
yum makecache #建立新缓存
percona-xtrabackup是Percona的一款非常出名且性能优秀的MySQL数据库在线备份工具,当然,CentOS的光盘中是没这个包的,我们希望把它集成到光盘里,并默认安装,首选,我们需要分析这个包的依赖关系
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
yum install percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
#如上图,可以看到,光盘中仅有rsync,所以我们需要先将其他包下载下来,并复制到/tmp/iso/Packages中
yum install yum-utils #yumdownloader命令在该包中,用于下载rpm包
cd /tmp/iso/Packages #直接下到这个目录
yumdownloader libev perl perl-Carp perl-Compress-Raw-Bzip2 perl-Compress-Raw-Zlib perl-DBD-MySQL perl-DBI perl-Data-Dumper perl-Digest perl-Digest-MD5 perl-Encode perl-Exporter perl-File-Path perl-File-Temp perl-Filter perl-Getopt-Long perl-HTTP-Tiny perl-IO-Compress perl-Net-Daemon perl-PathTools perl-PlRPC perl-Pod-Escapes perl-Pod-Perldoc perl-Pod-Simple perl-Pod-Usage perl-Scalar-List-Utils perl-Socket perl-Storable perl-Text-ParseWords perl-Time-HiRes perl-Time-Local perl-constant perl-libs perl-macros perl-parent perl-podlators perl-threads perl-threads-shared
#安装光盘制作工具集
yum install createrepo mkisofs isomd5sum syslinux
#重新生成repo数据库
cd /tmp/iso
createrepo -g repodata/83b61f9495b5f728989499479e928e09851199a8846ea37ce008a3eb79ad84a0-c7-minimal-x86_64-comps.xml .
#测试修改本地源位置到/tmp/iso
yum clean all
yum install percona-xtrabackup-24 #可以发现,已经可以通过yum安装xtrabackup
至此,已在光盘repo中集成了percona-xtrabackup-24,只需要ks.cfg中软件安装部分%packages添加percona-xtrabackup-24,即可实现自动安装该软件
编写ks.cfg文件
ks.cfg文件即kickstart,这个文件定义了所有在安装过程中需要写入的内容
如果系统安装了图形化界面,可以安装kickstart的图形化程序,当然,也可以不安装,这个工具可以用来生面ks.cfg文件,安装方法是yum install system-config-kickstart
这里直接附上我注释的ks.cfg文件。第一个示例文件可以全自动安装系统,并附带执行复制光盘文件到系统的任务,但是不足之处是,如果系统磁盘不为/dev/sda则会卡在分区位置,不能继续运行
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 |
# 密码选项 使用屏蔽口令 加密方式为sha512 auth --enableshadow --passalgo=sha512 # 安装源为cdrom 还可以是磁盘 NFS HTTP FTP等 cdrom # 文本界面 设置graphical则为图形化界面 text # 第一次启动 启用向导 X系统起作用 firstboot --enable # 忽略其他磁盘 仅使用sda ignoredisk --only-use=sda # 键盘部局设置 keyboard --vckeymap=us --xlayouts='us' # 系统语言支持 lang en_US.UTF-8 --addsupport=zh_CN.UTF-8 # 网络配置 这里是动态获取 启动时启用网卡 # 设置主机名 network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate network --hostname=example.domain # 设置root用户帐号 和/etc/shadow中密码字段内容一样 这里密码是123456 rootpw --iscrypted $6$SZ69HCyt$llZhDzWNhTGhvVwpRFlLLn3.2w22bZK6iF.A8QuC.4wlq3RDK3zHqhN24GAwtHzWAgXfaajVI.FwLHxKK7UKX1 # 禁用chronyd服务 services --disabled="chronyd" #firewall --disabled 禁用防火墙 #禁用selinux selinux --disabled # 系统时区 timezone Asia/Shanghai --isUtc --nontp # 系统引导配置 写入sda的mbr bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda #autopart --type=lvm 自动分区 若磁盘够大 /boot分区和/home分区将会分配较大空间 不推荐 # 清空mbr信息 zerombr # 清空原有分区 初始化磁盘 仅针对sda clearpart --all --initlabel --drives=sda # 分区设置 # 要使用全部空间 使用--size=1 --grow # 分区要使用所有剩余空间大小 --percent=100 part /boot --fstype="xfs" --ondisk=sda --size=200 part pv.364 --fstype="lvmpv" --ondisk=sda --size=1 --grow volgroup centos_school --pesize=4096 pv.364 logvol swap --fstype="swap" --size=4096 --name=swap --vgname=centos_school logvol /date --fstype="xfs" --percent=100 --name=date --vgname=centos_school logvol / --fstype="xfs" --size=40960 --name=root --vgname=centos_school # 完成后重启电脑 弹出光驱 reboot --eject # 指定要安装的软件包 # 默认 %packages @^minimal @core kexec-tools # 额外添加 net-tools openssl pciutils percona-xtrabackup-24 %end %addon com_redhat_kdump --enable --reserve-mb='auto' %end %anaconda # 密码策略 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty %end #%pre # 安装前执行的脚本 注意 此时磁盘尚未格式化 所以能执行的工作有限 # 常用于读取磁盘信息 生成分区脚本 然后供之后格式化使用 #%end # 安装后脚本 # %post --nochroot # 安装后脚本有两种模式 一种是在安装环境运行 此时系统挂载于/mnt/sysimage 光盘挂载于/mnt/install/repo # 可以从光盘复制文件到系统 # %post --log=/root/ks-post.log # 另一种方式是直接在新系统中运行 可以运行新系统中几乎所有命令 # 但是如果安装源不被识别为/dev/sr0 则不能通过挂载的方式把光盘中的文件复制到系统 # %post --nochroot # 复制安装光盘plus目录下所有内容到系统/mnt目录下 mkdir -p /mnt/sysimage/mnt/plus cp -fr /mnt/install/repo/plus/* /mnt/sysimage/mnt/plus/ # 复制安装光盘plus/init.sh文件到系统/root目录下 作为初始化执行脚本 cp /mnt/install/repo/plus/init.sh /mnt/sysimage/root/ %end |
下面这个是使用图形化界面,需要人工分区,其实就是启用图形界面,然后注释掉所有与磁盘相关的部分,这里命名为ks_m.cfg
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 |
# 密码选项 使用屏蔽口令 加密方式为sha512 auth --enableshadow --passalgo=sha512 # 安装源为cdrom 还可以是磁盘 NFS HTTP FTP等 cdrom # 文本界面test 设置graphical则为图形化界面 graphical # 第一次启动 启用向导 X系统起作用 firstboot --enable # 忽略其他磁盘 仅使用sda #ignoredisk --only-use=sda # 键盘部局设置 keyboard --vckeymap=us --xlayouts='us' # 系统语言支持 lang en_US.UTF-8 --addsupport=zh_CN.UTF-8 # 网络配置 这里是动态获取 启动时启用网卡 # 设置主机名 network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate network --hostname=example.domain # 设置root用户帐号 和/etc/shadow中密码字段内容一样 这里密码是123456 rootpw --iscrypted $6$SZ69HCyt$llZhDzWNhTGhvVwpRFlLLn3.2w22bZK6iF.A8QuC.4wlq3RDK3zHqhN24GAwtHzWAgXfaajVI.FwLHxKK7UKX1 # 禁用chronyd服务 services --disabled="chronyd" #firewall --disabled 禁用防火墙 #禁用selinux selinux --disabled # 系统时区 timezone Asia/Shanghai --isUtc --nontp # 系统引导配置 写入sda的mbr ##bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda #autopart --type=lvm 自动分区 若磁盘够大 /boot分区和/home分区将会分配较大空间 不推荐 # 清空mbr信息 ##zerombr # 清空原有分区 初始化磁盘 仅针对sda ##clearpart --all --initlabel --drives=sda # 分区设置 # 要使用全部空间 使用--size=1 --grow # 分区要使用所有剩余空间大小 --percent=100 ##part /boot --fstype="xfs" --ondisk=sda --size=200 ##part pv.364 --fstype="lvmpv" --ondisk=sda --size=1 --grow ##volgroup centos_school --pesize=4096 pv.364 ##logvol swap --fstype="swap" --size=4096 --name=swap --vgname=centos_school ##logvol /date --fstype="xfs" --percent=100 --name=date --vgname=centos_school ##logvol / --fstype="xfs" --size=40960 --name=root --vgname=centos_school # 完成后重启电脑 弹出光驱 reboot --eject # 指定要安装的软件包 # 默认 %packages @^minimal @core kexec-tools # 额外添加 net-tools openssl pciutils percona-xtrabackup-24 %end %addon com_redhat_kdump --enable --reserve-mb='auto' %end %anaconda # 密码策略 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty %end #%pre # 安装前执行的脚本 注意 此时磁盘尚未格式化 所以能执行的工作有限 # 常用于读取磁盘信息 生成分区脚本 然后供之后格式化使用 #%end # 安装后脚本 # %post --nochroot # 安装后脚本有两种模式 一种是在安装环境运行 此时系统挂载于/mnt/sysimage 光盘挂载于/mnt/install/repo # 可以从光盘复制文件到系统 # %post --log=/root/ks-post.log # 另一种方式是直接在新系统中运行 可以运行新系统中几乎所有命令 # 但是如果安装源不被识别为/dev/sr0 则不能通过挂载的方式把光盘中的文件复制到系统 # %post --nochroot # 复制安装光盘plus目录下所有内容到系统/mnt目录下 mkdir -p /mnt/sysimage/mnt/plus cp -fr /mnt/install/repo/plus/* /mnt/sysimage/mnt/plus # 复制安装光盘plus/init.sh文件到系统/root目录下 作为初始化执行脚本 cp /mnt/install/repo/plus/init.sh /mnt/sysimage/root/ %end |
这里有一点需要注意,在网络配置部分,我使用的网卡是ens33,所以只有在网卡名称为ens33时,才会生效,可以考虑获取网卡名称,从而自动判断,比如通过命令interface=$(ls /sys/class/net| grep -v "lo" | head -1) 然后使用变量$interface获取,不过这个未经测试,有兴趣的可以试试,我是放到安装后的初始化脚本中作判断的
编写完ks.cfg和ks_cfg两个文件后,将其放于/tmp/iso/isolinux目录下,供安装启动菜单调用
编写isolinux.cfg文件
isolinux.cfg是光盘启动后选择菜单的配置文件,位于/tmp/iso/isolinux下,可以在文件中添加修改启动项
先附上完整配置文件内容
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
default vesamenu.c32 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 Customer System based CentOS 7 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 #ff0000ff #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 local1 menu label ^x.Boot from local drive menu default localboot 0xffff label linux menu label ^a.Install Customer Server Automatic kernel vmlinuz #ks=cdrom:/isolinux/ks.cfg append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet label linux2 menu label ^b.Install Customer Server Manual kernel vmlinuz append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks_m.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet label linux3 menu label ^c.Install CentOS 7 Manual kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet label check menu label ^d.Test this media & install CentOS 7 kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_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 CentOS 7 in basic graphics mode text help Try this option out if you're having trouble installing CentOS 7 endtext kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet label rescue menu indent count 5 menu label ^Rescue a CentOS 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=CentOS\x207\x20x86_64 rescue quiet label memtest menu label Run a ^memory test text help If your system is having issues, a problem with your systems 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 |
menu title Customer System based CentOS 7 #设置标题栏
##默认从本地磁盘启动 不要将默认菜单设置为自动安装项目 最好设置默认菜单为从本地磁盘启动
label local1
menu label ^x.Boot from local drive
menu default
localboot 0xffff
##自动部署的菜单项配置
label linux
menu label ^a.Install Customer Server Automatic
kernel vmlinuz
#ks=cdrom:/isolinux/ks.cfg
append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
在菜单项配置中,重点看inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg这一项,即指定ks.cfg文件的位置,之前使用ks=cdrom:/isolinux/ks.cfg的方式,在使用光盘的时候运行得非常好,但换用U盘后,会找不到ks.cfg文件,很明显么,路径指定的是cdrom,后面使用基于LABEL的方式,这样可以正确找到ks.cfg文件的位置,使用卷标,这样不管这个U盘被识别成什么,只要卷标不变,那么,就能获取到正确位置。那么卷标在什么地方设置呢,在使用mkisofs打包成光盘ISO文件时,通过 -V参数指定
编写init.sh初始化脚本
ks.cfg文件中post脚本中定义了如下语句
mkdir -p /mnt/sysimage/mnt/plus
cp -fr /mnt/install/repo/plus/* /mnt/sysimage/mnt/plus
cp /mnt/install/repo/plus/init.sh /mnt/sysimage/root/
即将光盘中plus目录下所有文件复制到系统中/mnt/plus目录下,同时还复制了一个init.sh的脚本到/root目录下,这就是系统初始化脚本,用于在安装完系统后,登录系统,然后配置IP(因为系统网卡名可能不是ens33,且不是静态IP而是DHCP方式获取的,这我们不喜欢,换成静态方式),最后自动安装supervisor软件。
安装supervisor的最简单的方法是pip install supervisor,但是,我们假设服务器所在局域网不能联接到互联网,那么,这件事情就复杂一点了,因为不能使用pip,因为pip就没法安装,只能一个依赖包一个依赖包的安装,那就写到脚本里面。
supervisor可以通过python setup.py install命令从源码安装,但是先需要有python安装环境,即setuptools工具。
setuptools工具依赖于python-backports、python-ipaddress、python-backports-ssl_match_hostname,好在这些包都有现成的rpm,所以,安装python-setuptools命令如下:
rpm -ivh /mnt/plus/python-ipaddress-1.0.16-2.el7.noarch.rpm
rpm -ivh /mnt/plus/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
rpm -ivh /mnt/plus/python-setuptools-0.9.8-7.el7.noarch.rpm
supervisor依赖于meld3,meld3是一个tar包,supervisor也是tar包,解压后可以通过python setup.py install安装,如下
cp /mnt/plus/meld3-1.0.2.tar.gz /root
cd /root && tar zxf meld3-1.0.2.tar.gz
cd /root/meld3-1.0.2 && python setup.py install
rm -fr /root/meld3-1.0.2
rm -f /root/meld3-1.0.2.tar.gz
cp /mnt/plus/supervisor-3.3.4.tar.gz /root
cd /root && tar zxf supervisor-3.3.4.tar.gz
cd /root/supervisor-3.3.4 && python setup.py install
rm -fr /root/supervisor-3.3.4
rm -f /root/supervisor-3.3.4.tar.gz
此外,还需要supervisor.service文件、supervisord.conf、default.ini,(default.ini文件为空,用于添加需要加到supervisor上的服务)内容分别如下
## 注意,这里添加了LimitNOFILE=40960和LimitNPROC=40960
## 默认打开文件数为1024 所有使用supervisor管理的程序也会继承该值,这会导致对某应应用来讲,打开文件数严重不足的问题
## 因此需要添加该配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
cat /mnt/plus/supervisor.service [Unit] Description=supervisor After=network.target [Service] Type=forking LimitNOFILE=40960 LimitNPROC=40960 ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=2s [Install] WantedBy=multi-user.target |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
cat /mnt/plus/supervisord.conf ; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html ; ; Notes: ; - Shell expansion ("~" or "$HOME") is not supported. Environment ; variables can be expanded using this syntax: "%(ENV_HOME)s". ; - Quotes around values are not supported, except in the case of ; the environment= options as shown below. ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". ; - Command will be truncated if it looks like a config file comment, e.g. ; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server] file=/etc/supervisor/supervisor.sock ; the path to the socket file ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner username=admin ; default is no username (open server) password=admin ; default is no password (open server) ;[inet_http_server] ; inet (TCP) server disabled by default ;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server) [supervisord] logfile=/var/log/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/etc/supervisor/supervisord.pid ; supervisord pidfile; default supervisord.pid nodaemon=false ; start in foreground if true; default false minfds=1024 ; min. avail startup file descriptors; default 1024 minprocs=200 ; min. avail process descriptors;default 200 ;umask=022 ; process file creation umask; default 022 ;user=chrism ; default is current user, required if root ;identifier=supervisor ; supervisord identifier, default is 'supervisor' ;directory=/tmp ; default is not to cd during start ;nocleanup=true ; don't clean up tempfiles at start; default false ;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP ;environment=KEY="value" ; key value pairs to add to environment ;strip_ansi=false ; strip ansi escape codes in logs; def. false ; The rpcinterface:supervisor section must remain in the config file for ; RPC (supervisorctl/web interface) to work. Additional interfaces may be ; added by defining them in separate [rpcinterface:x] sections. [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface ; The supervisorctl section configures how supervisorctl will connect to ; supervisord. configure it match the settings in either the unix_http_server ; or inet_http_server section. [supervisorctl] serverurl=unix:///etc/supervisor/supervisor.sock ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket username=admin ; should be same as in [*_http_server] if set password=admin ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available ; The sample program section below shows all possible program subsection values. ; Create one or more 'real' program: sections to be able to control them under ; supervisor. ;[program:theprogramname] ;command=/bin/cat ; the program (relative uses PATH, can take args) ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) ;numprocs=1 ; number of processes copies to start (def 1) ;directory=/tmp ; directory to cwd to before exec (def no cwd) ;umask=022 ; umask for process (default None) ;priority=999 ; the relative start priority (default 999) ;autostart=true ; start at supervisord start (default: true) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;user=chrism ; setuid to this UNIX account to run the program ;redirect_stderr=true ; redirect proc stderr to stdout (default false) ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10) ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;environment=A="1",B="2" ; process environment additions (def no adds) ;serverurl=AUTO ; override serverurl computation (childutils) ; The sample eventlistener section below shows all possible eventlistener ; subsection values. Create one or more 'real' eventlistener: sections to be ; able to handle event notifications sent by supervisord. ;[eventlistener:theeventlistenername] ;command=/bin/eventlistener ; the program (relative uses PATH, can take args) ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) ;numprocs=1 ; number of processes copies to start (def 1) ;events=EVENT ; event notif. types to subscribe to (req'd) ;buffer_size=10 ; event buffer queue size (default 10) ;directory=/tmp ; directory to cwd to before exec (def no cwd) ;umask=022 ; umask for process (default None) ;priority=-1 ; the relative start priority (default -1) ;autostart=true ; start at supervisord start (default: true) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; autorestart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;user=chrism ; setuid to this UNIX account to run the program ;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;environment=A="1",B="2" ; process environment additions ;serverurl=AUTO ; override serverurl computation (childutils) ; The sample group section below shows all possible group values. Create one ; or more 'real' group: sections to create "heterogeneous" process groups. ;[group:thegroupname] ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions ;priority=999 ; the relative start priority (default 999) ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. [include] files = /etc/supervisor/config.d/*.ini |
所以,可以得取安装supervisor的脚本supervisor_install.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 32 33 |
#!/bin/sh echo "###########################################################" echo "开始部署superviosr" echo "###########################################################" rpm -ivh /mnt/plus/python-backports-1.0-8.el7.x86_64.rpm rpm -ivh /mnt/plus/python-ipaddress-1.0.16-2.el7.noarch.rpm rpm -ivh /mnt/plus/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm rpm -ivh /mnt/plus/python-setuptools-0.9.8-7.el7.noarch.rpm cp /mnt/plus/meld3-1.0.2.tar.gz /root cd /root && tar zxf meld3-1.0.2.tar.gz cd /root/meld3-1.0.2 && python setup.py install rm -fr /root/meld3-1.0.2 rm -f /root/meld3-1.0.2.tar.gz cp /mnt/plus/supervisor-3.3.4.tar.gz /root cd /root && tar zxf supervisor-3.3.4.tar.gz cd /root/supervisor-3.3.4 && python setup.py install rm -fr /root/supervisor-3.3.4 rm -f /root/supervisor-3.3.4.tar.gz echo "部署supervisor服务" #部署服务 mkdir -p /var/log/supervisor/ mkdir -p /etc/supervisor/config.d cp /mnt/plus/supervisor.service /lib/systemd/system/ cp /mnt/plus/supervisord.conf /etc/supervisor/ cp /mnt/plus/default.ini /etc/supervisor/config.d/ chmod 644 /lib/systemd/system/supervisor.service systemctl daemon-reload systemctl enable supervisor.service echo "supervisor服务部署完成" |
最后在init.sh中调用supervisor_install.sh即可完成supervisor的安装,那么init.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 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
#!/bin/sh #主动启动网卡 interface=$(ls /sys/class/net| grep -v "lo" | head -1) ifup $interface #获取当前网络信息 default_route=$(ip route show) default_interface=$(echo $default_route | sed -e 's/^.*dev \([^ ]*\).*$/\1/' | head -n 1) address=$(ip addr show label $default_interface scope global | awk '$1 == "inet" { print $2,$4}') ip=$(echo $address | awk '{print $1 }') ip=${ip%%/*} mask=$(route -n |grep 'U[ \t]' | head -n 1 | awk '{print $3}') gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}') dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}') #判断default_interface是否为空 if [ -z $default_interface ] then default_interface=$interface fi #显示网络信息 echo -e "The current net info [dynamic]" echo -e "------------------------------------------" echo -e " device: $default_interface" echo -e " ipaddr: $ip" echo -e "netmask: $mask" echo -e "gateway: $gateway" echo -e " dns: $dns" echo -e "------------------------------------------" echo -e "" #获取输入 echo -e "Please give static settings for the server" echo -e "please input ipaddr:[192.168.1.12]" read newip echo -e "please input netmask:[255.255.255.0]" read newnetmask echo -e "please input gateway:[192.168.1.1]" read newgateway echo -e "please input dns:[192.168.1.1]" read newdns echo -e "please input hostname:[MyServer]" read newhostname echo -e "" echo -e "Please check you input" echo -e "------------------------------------------" echo -e " ipaddr: $newip" echo -e " netmask: $newnetmask" echo -e " gateway: $newgateway" echo -e " dns: $newdns" echo -e "hostname: $newhostname" echo -e "------------------------------------------" echo -e "[yes] for submit echo -e "[ q ] for exit #确认输入 q="" while [ "$q" != "q" ] do read q case $q in q) break ;; yes) echo "begging..." if [ -e /mnt/plus/supervisor_install.sh ] ; then # 安装supervisor /usr/bin/sh /mnt/plus/supervisor_install.sh fi # 清空在安装阶段从光盘安装的所有文件 rm -fr /mnt/* #主机名配置 echo "$newhostname" > /etc/hostname #网络配置 cp /etc/sysconfig/network-scripts/ifcfg-$default_interface /etc/sysconfig/network-scripts/ifcfg-$default_interface.bak uuid=$(cat /etc/sysconfig/network-scripts/ifcfg-$default_interface |grep UUID|sed -e 's/"//g') echo "IPV6INIT=yes" > /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "IPV6_AUTOCONF=yes" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "DEVICE=$default_interface" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "$uuid" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "PROXY_METHOD=none" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "BROWSER_ONLY=no" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "IPADDR=$newip" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "NETMASK=$newnetmask" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "GATEWAY=$newgateway" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "DNS1=$newdns" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "DEFROUTE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "IPV4_FAILURE_FATAL=no" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "IPV6_DEFROUTE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface echo "IPV6_FAILURE_FATAL=no" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface #DNS配置 cp /etc/resolv.conf /etc/resolv.conf.bak echo "# Generated by NetworkManager" > /etc/resolv.conf echo "nameserver $newdns" >> /etc/resolv.conf #重启一下网络 使配置生效 systemctl restart network sleep 5 ping -c 4 www.baidu.com if [ $? != 0 ] then echo -e "Error! Cant link to Internet" #break fi #重启系统 echo "" echo "all settings finished" echo "now you system will restart" sleep 6 /usr/sbin/reboot now break ;; *) echo "please input 'yes' or 'q'" ;; esac done |
运行init.sh脚本,可以自动识别系统的网卡,并以DHCP方式启动网卡,如果能获取到IP地址,则会显示IP配置信息,可以参照动态获取的信息,再通过手动输入的方式,配置得到静态IP配置,同时,在这个脚本中,也会自动安装supervisor
生成ISO文件
完成上述工作后,则可以打包成ISO镜像文件了
# 打包ISO文件
mkisofs -o /tmp/My-Server-CentOS7-v1.0.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -joliet-long -R -J -v -V "CentOS 7 x86_64" -T /tmp/iso/
# 转换为ISOhybrid (该命令由syslinux提供)
isohybrid ../My-Server-CentOS7-v1.0.iso
# 嵌入md5校验码 (该命令由isomd5sum提供)
implantisomd5 ../My-Server-CentOS7-v1.0.iso
# 校验一下 (该命令由isomd5sum提供)
checkisomd5 ../My-Server-CentOS7-v1.0.iso
mkisofs参数说明:
-o /tmp/My-Server-CentOS7-v1.0.iso,设置输出文件名,-output
-b isolinux/isolinux.bin,指定开机映像文件
-c isolinux/boot.cat,制作启动光盘时,mkisofs会将开机映像文件中的全-eltorito-catalog*文件的全部内容作成一个文件
-no-emul-boot,非模拟模式启动
-boot-load-size 4,
-boot-info-table,
-joliet-long,
-R,使用Rock Ridge Extensions,是用于linux环境下的光盘,文件名区分大小写同时记录文件长度,-rock
-J,使用Joliet格式的目录与文件名称,Jolient是windows下使用的光盘,-joliet
-v,执行时显示详细的信息,-verbose
-V "CentOS 7 x86_64",设置卷标Volume ID,-volid
-T,建立文件名的转换表,适用于不支持Rock Ridge Extensions的系统,-translation-table
/tmp/iso/,光盘源文件目录
制作U盘版启动盘
推荐使用软件rufus,该软件为绿色小软件,小巧,功能完善
Rufus安装U盘制作工具:rufus
版本:v3.1
官网:http://rufus.akeo.ie/
下载地址:http://rufus.akeo.ie/downloads/rufus-3.1.exe
注:如果光盘镜像为ISOHybrid镜像,有ISO镜像模式和DD镜像模式两种写入方式,其中ISO镜像模式写入的U盘还可以写入其他文件,和一般U盘无区别,但兼容性较差,推荐DD镜像模式,但该模式写入后,windows下将不识别,仅可用于系统安装
参考连接
1、redhat系统启动项参数官方文档
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/installation_guide/chap-anaconda-boot-options
2、kickstart安装系统中的文件复制问题
https://www.centos.org/forums/viewtopic.php?t=11119
3、isohybrid(syslinux)官方wiki
https://www.syslinux.org/wiki/index.php?title=Isohybrid
4、基于Kickstart自动化安装CentOS实践
https://wsgzao.github.io/post/kickstart/
5、CSDN博客:kickstart自动应答脚本生成及虚拟机自动安装
https://blog.csdn.net/Lockey23/article/details/76376907