为什么我们要自动化安装系统呢?管理一些重复性的操作呢,这样可以减轻自己的操作,并且提高了工作效率,假如公司中又有十几台甚至更多的服务器,不可能自己一台一台的安装,这样就需要自动化安装。

一、光盘内容简介

1、CentOS系统的启动流程

POST-->boot Sequence-->bootloader(MBR)-->kernel(initramdisk)-->rootfs-->/sbin/init/

2、光盘内容

(1)[root@centos7/media/cdrom/isolinux]#ls

内容 解释
boot.cat 相当于启动菜单显示内容
grub.conf 启动系统启动与boot.cat显示内容配置文件
isolinux.bin 相当于系统安装的stage2阶段
memtest 内存检测,这是个独立程序
vmlinuz 内核映像
boot.msg
initrd.img 虚拟文件系统
isolinux.cfg isolinux.cat的配置文件
splash.png 背景图片
vesamenu.c32 显示背景界面

(2)isolinux.cfg详解

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 CentOS Linux 7   #显示titlemenu vshift 8   menu rows 18menu margin 8#menu hidden    menu helpmsgrow 15# Border Areamenu color border * #00000000 #00000000 none# Selected itemmenu color sel 0 #ffffffff #00000000 none# Title barmenu color title 0 #ff7ba3d0 #00000000 none# Press [Tab] messagemenu color tabmsg 0 #ff3a6496 #00000000 none# Unselected menu itemmenu color unsel 0 #84b8ffff #00000000 none# Unselected hotkeymenu color hotkey 0 #ffffffff #00000000 none# Help textmenu color help 0 #ffffffff #00000000 none# A scrollbar of some type? Not sure.menu color scrollbar 0 #ffffffff #ff355594 none# Timeout msgmenu color timeout 0 #ffffffff #00000000 nonemenu color timeout_msg 0 #ffffffff #00000000 none# Command prompt textmenu tabmsg Press Tab for full configuration options on menu items.     #键入TAB进入配置模式menu separator # insert an empty linemenu separator # insert an empty linelabel linux     #相当于标签  menu label ^Install CentOS Linux 7        #安装linux  kernel vmlinuz    #内核文件  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet     #向内核传递参数label check  menu label Test this ^media & install CentOS Linux 7  menu default      #默认启动项  kernel vmlinuzmenu separator # insert an empty line# utilities submenumenu begin ^Troubleshooting  menu title Troubleshootinglabel vesa  menu indent count 5  menu label Install CentOS Linux 7 in ^basic graphics mode  text help        Try this option out if you're having trouble installing        CentOS Linux 7.  endtext  kernel vmlinuz  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quietlabel rescue  menu indent count 5  menu label ^Rescue a CentOS Linux 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 quietlabel memtest  menu label Run a ^memory test  text help        If your system is having issues, a problem with your        system's memory may be the cause. Use this utility to        see if the memory is working correctly.  endtext  kernel memtestmenu separator # insert an empty linelabel local  menu label Boot from ^local drive  localboot 0xffffmenu separator # insert an empty linemenu separator # insert an empty linelabel returntomain  menu label Return to ^main menu  menu exitmenu end

配置文件中有“^”相当于快捷键。

3、anaconda程序

(1)概念

anaconda是系统安装程序。启动系统安装过程。

(2)anaconda安装过程

anaconda安装分为三个阶段:

(a)安装前配置阶段

安装过程使用的语言键盘类型安装目标存储设备    basic storage 本地磁盘    特殊设备 iscsI设定主机名配置网络接口时区设置管理员密码设定分区方式及MBR的安装位置创建一个普通用户选定要安装的程序包

(b)安装阶段在目标磁盘

创建分区,执行格式化操作等选定的程序包安装至目标位置安装BootLoader和initramfs

(c)图形模式首次启动

iptablesselinuxcore dump

(3)安装程序包的选择

(a)本地光盘(b)本地硬盘(c)NFS(d)URL    ftp server :yum repository    http server:yum repository(e)手动指定安装源    在系统启动选项菜单界面进入TAB键,进入boot命令行模式输入`linux askmethod`,如果需要网络后续会跳出来一个对话框填入IP即可。

(4)anaconda配置方式

(a)交互式配置(b)通过读取事先给定的配置文件自动完成配置(kickstart文件)

4、kickstart文件

(1)配置文件详解

#version=DEVEL  #指明版本# System authorization information   auth --enableshadow --passalgo=sha512 # Install OS instead of upgradeinstall# Use text installtext# Firewall configurationfirewall --disabledfirstboot --disableignoredisk --only-use=sda# Keyboard layoutskeyboard --vckeymap=cn --xlayouts='cn'# System languagelang en_US.UTF-8# Use network installation#url --url="http://172.16.0.1/cblr/links/CentOS-7-1511-x86_64"#repo --name="source-1" --baseurl=http://172.16.0.1/cobbler/ks_mirror/CentOS-7-1511-x86_64#url --url="http://172.16.0.1/cobbler/ks_mirror/7/"url --url=$tree# Network informationnetwork  --bootproto=dhcp --device=ens33 --onboot=yes  --ipv6=autonetwork  --hostname=localhost.localdomain# Reboot after installationreboot# SELinux configurationselinux --disabled# Do not configure the X Window Systemskipx# Root passwordrootpw --iscrypted $6$dGARWYghrvhD9W7P$4af2uw8A4tHvNLe2F6bDrk0J69dt.uYoV4SneKG4kzIsc/nF3JpfnuHg7D5lVE.jxC3p6.K29FCjwtom9VXWf.# System timezonetimezone Asia/Shanghaiuser --groups=wheel --name=wang --password=$6$S0t9SxXkvSI0Npz5$YDVonz69YGGGkGyZDy/KesO4CI1HUBLNThcHj6PXDntQGS0Cl0SNpN6zGASuzYE.z5veu1XAzMvO4xLWbKE.n0 --iscrypted --gecos="123456"# System bootloader configurationbootloader --location=mbr --boot-drive=sda# Partition clearing informationclearpart --all --initlabel# Disk partitioning informationpart /boot --fstype="xfs" --ondisk=sda --size=500part swap --fstype="swap" --ondisk=sda --size=2049part / --fstype="xfs" --ondisk=sda --size=40960part /app --fstype="xfs" --ondisk=sda --size=20480%postsystemctl set-default multi-user.target[ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.sshcat >> /root/.ssh/authorized_keys << EOFssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlmq1z0G/7wbGuSUewfXlFnwzqCg/myqTi/AwP8LP+JJ49xzIKMzpeWXHD8RWIf5RlDzo+6N7uPK5O22x/QtMosi0egz4shavEJeUkO0EH+KygXXgBIGuMWmAsL+yzbgWXT9H3zdzXi/qWcrBeBv2nYB5mpYSf7o0xqdhCst1MTfcYLD8qxvkwC8RiqBA/1u9N6jeDFbHO+UzZYYCr9zgk9uz4Rrhb9BU7c1GhjUCgRwBDAuo47IHw/OT6KS9lb8lT2R/ujVoDARy/eOhw8cAFXo+QcvzNSW2qKf/Qo21uR/wz2u9SRV0lvUDNSvC2PYtR+iPlDwHY81md430yiNf9w== root@172.16.0.1EOFClientName=`ifconfig enoens33 | awk '/inet\>/{print $2}' | awk -F. '{print $NF}'`hostnamectl set-hostname "stu$ClientName.wang.com"#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak#wget http://172.18.0.1/repos/CentOS-Base.repo -O /etc/yum.repos.d/CentOS-Base.repo%end%packages@base@core@fonts@input-methods@kde-apps@kde-desktop@network-file-system-client@networkmanager-submodules@x11lftpscreen%end%addon com_redhat_kdump --disable --reserve-mb='auto'%end

(2)kickstart文件格式

(a)命令端:指明各种安装前配置,如果键盘类型,时区等 必备命令 authconfig:认证方式配置 authconfig --useshadow --passalgo=sha512 bootloader:bootloader的安装位置及相关配置 bootloader --location=mbr --driverorder=sda --append=“crashkernel=auto rhgb quiet” keyboard:设定键盘类型 lang:语言类型 

part:创建分区 rootpw:指明root的密码 timezone:时区 可选命令 install OR update text :文本界面安装 network 设定网络 firewall 设置防火墙 selinux 设置selinux halt 
poweroff reboot repo user:安装完成后为系统创建新用户 url:指明安装源 key -skip:跳过安装号码,适用于rhel版本

(b)程序包段:指明要安装的程序包或程序包组,不安装的包等

%packages #表明开始安装程序包或包组@group_name #安装包组package #安装程序包-package #不安装程序包%end 指明安装结束

(c)脚本段:指明运行的脚本

%pre #安装前脚本    运行环境:运行于安装介质上的微型linux环境%post   #安装后脚本    运行环境:运行于安装完成后的系统

注意:在每段中一定要有开始有结束%end表明结束安装。

(3)kickstart文件创建方式

(a)直接手动编辑,可以依赖模板进行自行修改

(b)可使用创建工具:system-config-kickstart命令,依赖某模板修改并生成新的配置文件 注意:在CentOS7中,如果要使用此命令必须把yum仓库光盘的名称修改为development。 检查ks文件的语法错误:ksvalidator 格式:ksvalidator /PATH/TO/SOMEFILE

(4)指明kickstart位置

DVD drive:ks=cdrom:/PATH/TO/KICKSTART_FILEHard drive:ks=hd:/PATH/TO/KICKSTART_FILEFTP server:ks=ftp://host:port/PATH/TO/KICKSTART_FILEHTTP server:ks=http://host:port/PATH/TO/KICKSTART_FILEHTTPS server:ks=https://host:port/PATH/TO/KICKSTART_FILENFS server:ks=nfs:host:/PATH/TO/KICKSTART_FILE

如果想理解更多,请查考详细内容《Installation Guide》。

(5)系统启动的不同方式

(a)text:文本安装模式

(b)askmethod:手动指定安装方法 在系统启动界面,键入ESC进入boot命令行模式:输入linux askmethod 与网络相关的引导选项: ip=IPADDR netmask=NETMAKS gateway=GATEWAY dns=DNS_SERVER_IP ifname=NAME:MAC_ADDR (c)与远程功能相关的引导选项 vns vncpassword=“PASSWORD”

实战

手动指定kickstart文件安装系统

方式一:

(1)在CentOS7上搭建ftp服务

[root@centos7~]#cd /var/ftp/pub/[root@centos7/var/ftp/pub]#cp /root/ks*.cfg .[root@centos7/var/ftp/pub]#ls

ks6.cfg  ks7.cfg

(2)创建一块虚拟机

(3)手动指定按启动,选定光盘启动

2017-09-15_183233.png当出现上面图示时,我们手动指定kickstart启动。

方法一: 按TAB键,输入ks=ftp://192.168.4.135/pub/ks6.cfg;回车启动 2017-09-15_183559.png方法二: 按ESC键进入boot命令行,输入linux ks=ftp://192.168.4.135/pub/ks6.cfg

方式二:

在CentOS7搭建http服务

[root@centos7/var/www/html]#mkdir ks[root@centos7/var/www/html]#mv ks*.cfg ks

其余步骤同上述方法类似。

注意:上述过程必须使ks文件权限为644.上述方法前提为桥接模式。

上述方法是没有办法安装包等后续工作的,我们可以搭建网络yum源进行安装。

方式一:创建网络yum源安装

(1)安装vsftpd服务,并启动服务

(2)将ks7.cfg文件复制到 /var/ftp/pub目录下

(3)在/var/ftp/pub目录下创建centos/7

[root@centos7/var/ftp/pub]#mkdir centos/7 -pv

(4)将光盘挂载到centos/7上

[root@centos7/var/ftp/pub]#mount /dev/sr0 /var/ftp/pub/centos/7/(5)测试是否能在网页中打开

输入地址ftp://192.168.4.135/pub 2017-09-15_192911.png

(6)新建一台虚拟机测试

按ESC键进入boot命令行,输入linux ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0

或 按TAB键在行后追加 ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0

(7)出现下面说明成功 2017-09-15_193257.png

方式二:

(1)在CentOS7上搭建http服务

(2)在/var/www/html目录下创建ks目录和centos/7目录

[root@centos7/var/www/html]#mkdir testcentos/7 -pv

(3)将光盘挂载至centos/7目录下[root@centos7/var/www/html]#mount /dev/sr0 testcentos/7/

(4)重启服务测试 在网站输入http://192.168.4.135/test 2017-09-15_194000.png(5)新建一台虚拟机重新测试,出现下面内容说明成功

按ESC键进入boot命令行,输入linux ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0

按TAB键在行后追加 ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0 2017-09-15_193257.png

二、制作光盘或U盘

在日常的生产活动中,使用光驱的已经很少了,但是U盘自身的特点得到了大多数人的使用,这样让我掌握如何制作U盘变得特别重要,如果我们公司有的服务器没有光盘咋办,?U盘就很好的帮我们解决了这个问题。

1、如何制作光盘

(1)mkisofs命令有关包genisop_w_picpath 制作光盘需要使用命令才能执行。

选项 解释
-o 指定影响文件的名称
-b 指定在制作可开机光盘是所需要的开机映像文件
-c 制作可开机光盘时,会将开机映像文件中的no-eltorito-catalog全部内容做成一个文件
-no-emul-boot 非模拟模式启动
-boot-load-size 4 设置载入部分的数量
-boot-info-table 在启动的图像中显示信息
-R,-rock 使用Rock RidgeExtensions
-J,-joliet 使用Joliet格式的目录与文件名称
-v,-verbose 执行时显示过程
-T,-translation-table 建立文件名的装换表,适用于不支持Rock RidgeExtensions的系统上

(2)制作光盘过程

(a)在CentOS7主机上搭建yum源,并绑定到ftp服务上。

(b)创建光盘引导文件

[root@centos7~]#mkdir /tmp/testiso[root@centos7~]#mkdir /tmp/testiso/isolinux[root@centos7~]#cp /media/cdrom/isolinux/* /tmp/testiso/isolinux[root@centos7/tmp/testiso]#cp /var/ftp/pub/ks7.cfg .[root@centos7/tmp/testiso]#vim ks7.cfg #编辑配置文件 2017-09-15_200110.png[root@centos7/tmp/testiso]#tree

.├── isolinux│   ├── boot.cat│   ├── boot.msg│   ├── grub.conf│   ├── initrd.img│   ├── isolinux.bin│   ├── isolinux.cfg│   ├── memtest│   ├── splash.png│   ├── TRANS.TBL│   ├── vesamenu.c32│   └── vmlinuz└── ks7.cfg

注意:此处创建使用的是相对路径,而和工作目录无关。必须在isolinux下。

(c)创建光盘[root@centos7/tmp]#mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.9 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/

(d)将光盘下载至本机,安装到虚拟中尝试启动 2017-09-15_202146.png

说明我们制作的光盘成功

(3)制作U盘

(a)我们这里虚拟一块U盘,添加一块硬盘

(b)将光盘拷贝至硬盘中

[root@centos7~]#dd if=boot.iso of=/dev/sdb

(c)新建虚拟机挂载此光盘启动 2017-09-15_202146.png出现上述情况说明我们安装成功。但是这种方法还需要通过网络才能安装,如果我们去到一个地方没有网络,怎么办呢?有没有想过我们把rpm包到拷贝至一个U盘或光盘上呢?这样是不是很方便我们办公呢?

(4)制作完整的光盘和U盘

(a)创建一个临时目录并拷贝文件至此目录下[root@localhost ~]#mkdir /tmp/fulliso[root@localhost ~]# cp -a /media/cdrom/* /tmp/fulliso/[root@localhost ~]# cd /tmp/fulliso/[root@localhost fulliso]# cp /media/cdrom/.treeinfo .[root@localhost fulliso]# cp /media/cdrom/.discinfo .

(b) 删除repodate目录非*comps.xml结尾的文件。[root@localhost fulliso]# find repodata/ -not -name *-comps.xml -delete[root@localhost fulliso]# find -name TRANS.TBL -delete

(c)以上述那个文件制作仓库[root@localhost fulliso]# ls repodata/ d918936f5019be3fb66e9981a28cb2a41477a2963d741d454f79377a22214f43-c7-x86_64-comps.xml

(d)编辑kickstart配置文件,并复制fulliso目录下[root@localhost fulliso]# vim ks7.cfg

auth --enableshadow --passalgo=sha512# Use graphical installgraphical# Run the Setup Agent on first bootfirstboot --enableignoredisk --only-use=sda# Keyboard layoutskeyboard --vckeymap=us --xlayouts='us'# System languagelang en_US.UTF-8# Network informationnetwork  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto network  --hostname=localhost.localdomain# Root password# System servicesservices --disabled="chronyd"# System timezonetimezone Asia/Shanghai# X Window System configuration informationxconfig  --startxonboot# System bootloader configurationbootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sdazerombrreboot# Partition clearing informationclearpart --allpart /boot --fstype="xfs" --ondisk=sda --size=1024part swap --fstype="swap" --ondisk=sda --size=2048part / --fstype="xfs" --ondisk=sda --size=50000part /app --fstype="xfs" --ondisk=sda --size=50000%packages@^gnome-desktop-environment@base@core@desktop-debugging@dial-up@directory-client@fonts@gnome-desktop@guest-agents@guest-desktop-agents@input-methods@internet-browser@java-platform@multimedia@network-file-system-client@networkmanager-submodules@print-client@x11kexec-toolsautofslftpdhcpvsftpd%end%post#Create repomkdir /media/cdrommkdir /etc/yum.repos.d/bakmv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bakcat > /etc/yum.repos.d/base <
> /etc/fstab << EOF/dev/sr0        /media/cdrom    iso9660 defaults 0 0cat >> /root/.bashrc << EOFalias cdnet="cd /etc/sysconfig/network-scripts/"EOFcat >> /etc/profile.d/env.shexport PS1='\[\e[31m\][\u@\h\w]\$\[\e[0m\]'EOFsed -i 's/quiet/& net.ifnames=0/' /etc/default/grubgrub2-config -o /boot/grub2/grub.cfg%end

(e)编辑isolinux.cfg文件[root@localhost fulliso]# vim isolinux/isolinux.cfg

default vesamenu.c32timeout 600display 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 clearmenu background splash.pngmenu title CentOS Linux 7menu vshift 8menu rows 18menu margin 8#menu hiddenmenu helpmsgrow 15menu tabmsgrow 13# Border Area# Selected itemmenu color sel 0 #ffffffff #00000000 none# Title barmenu color title 0 #ff7ba3d0 #00000000 none# Press [Tab] messagemenu color tabmsg 0 #ff3a6496 #00000000 none# Unselected menu itemmenu color unsel 0 #84b8ffff #00000000 none# Selected hotkeymenu color hotsel 0 #84b8ffff #00000000 none# Unselected hotkeymenu color hotkey 0 #ffffffff #00000000 none# Help textmenu color help 0 #ffffffff #00000000 none# A scrollbar of some type? Not sure.menu color scrollbar 0 #ffffffff #ff355594 none# Timeout msgmenu color timeout 0 #ffffffff #00000000 nonemenu color timeout_msg 0 #ffffffff #00000000 none# Command prompt textmenu color cmdmark 0 #84b8ffff #00000000 nonemenu 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 linemenu separator # insert an empty linelabel desktop  menu label desktop Linux 7  kernel vmlinuz  append initrd=initrd.img ks=cdrom:/ks7.cfglabel mini  menu label mini linux 7  menu default  kernel vmlinuz  append initrd=initrd.img ks=cdrom:/ks7mini.cfgmenu end

(f)制作光盘[root@localhost ~]# mkisofs -R -T -J -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 7 custom" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/centos7.iso /tmp/fulliso/

(g)测试是否可成功,新建一台虚拟机,测试 2017-09-17_171832.png出现上述界面,表明制作光盘已经成功。