制做一张属于DIY的CnetOS 7安装光盘

为何我们要自动化安装系统呢?管理一些重复性的操做呢,这样能够减轻本身的操做,而且提升了工做效率,假如公司中又有十几台甚至更多的服务器,不可能本身一台一台的安装,这样就须要自动化安装。html

1、光盘内容简介

一、CentOS系统的启动流程

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

二、光盘内容

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

内容 解释
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详解redis

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   #显示title
menu vshift 8   
menu rows 18
menu margin 8
#menu hidden    
menu helpmsgrow 15
# 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


# 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 tabmsg Press Tab for full configuration options on menu items.     #键入TAB进入配置模式

menu separator # insert an empty line
menu separator # insert an empty line

label 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 vmlinuz

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label 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 quiet

label 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 quiet

label 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 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

配置文件中有“^”至关于快捷键。vim

三、anaconda程序

(1)概念

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

(2)anaconda安装过程

anaconda安装分为三个阶段:centos

(a)安装前配置阶段bash

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

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

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

(c)图形模式首次启动网络

iptables
selinux
core 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文件)

四、kickstart文件

(1)配置文件详解

#version=DEVEL  #指明版本
# System authorization information   
auth --enableshadow --passalgo=sha512 
# Install OS instead of upgrade
install
# Use text install
text
# Firewall configuration
firewall --disabled
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang 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 information
network  --bootproto=dhcp --device=ens33 --onboot=yes  --ipv6=auto
network  --hostname=localhost.localdomain
# Reboot after installation
reboot
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Root password
rootpw --iscrypted $6$dGARWYghrvhD9W7P$4af2uw8A4tHvNLe2F6bDrk0J69dt.uYoV4SneKG4kzIsc/nF3JpfnuHg7D5lVE.jxC3p6.K29FCjwtom9VXWf.
# System timezone
timezone Asia/Shanghai
user --groups=wheel --name=wang --password=$6$S0t9SxXkvSI0Npz5$YDVonz69YGGGkGyZDy/KesO4CI1HUBLNThcHj6PXDntQGS0Cl0SNpN6zGASuzYE.z5veu1XAzMvO4xLWbKE.n0 --iscrypted --gecos="123456"
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=500
part swap --fstype="swap" --ondisk=sda --size=2049
part / --fstype="xfs" --ondisk=sda --size=40960
part /app --fstype="xfs" --ondisk=sda --size=20480

%post
systemctl set-default multi-user.target

[ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.ssh

cat >> /root/.ssh/authorized_keys << EOF
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlmq1z0G/7wbGuSUewfXlFnwzqCg/myqTi/AwP8LP+JJ49xzIKMzpeWXHD8RWIf5RlDzo+6N7uPK5O22x/QtMosi0egz4shavEJeUkO0EH+KygXXgBIGuMWmAsL+yzbgWXT9H3zdzXi/qWcrBeBv2nYB5mpYSf7o0xqdhCst1MTfcYLD8qxvkwC8RiqBA/1u9N6jeDFbHO+UzZYYCr9zgk9uz4Rrhb9BU7c1GhjUCgRwBDAuo47IHw/OT6KS9lb8lT2R/ujVoDARy/eOhw8cAFXo+QcvzNSW2qKf/Qo21uR/wz2u9SRV0lvUDNSvC2PYtR+iPlDwHY81md430yiNf9w== root@172.16.0.1
EOF

ClientName=`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
@x11
lftp
screen

%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版本app

(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_FILE
Hard drive:ks=hd:/PATH/TO/KICKSTART_FILE
FTP server:ks=ftp://host:port/PATH/TO/KICKSTART_FILE
HTTP server:ks=http://host:port/PATH/TO/KICKSTART_FILE
HTTPS server:ks=https://host:port/PATH/TO/KICKSTART_FILE
NFS 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

2、制做光盘或U盘

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

一、如何制做光盘

(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 install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto 
network  --hostname=localhost.localdomain

# Root password
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
zerombr
reboot
# Partition clearing information
clearpart --all
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="xfs" --ondisk=sda --size=50000
part /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
@x11
kexec-tools
autofs
lftp
dhcp
vsftpd

%end
%post
#Create repo
mkdir /media/cdrom
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base <<EOF
[base]
name=centos 7
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1
EOF

cat >> /etc/fstab << EOF
/dev/sr0        /media/cdrom    iso9660 defaults 0 0

cat >> /root/.bashrc << EOF
alias cdnet="cd /etc/sysconfig/network-scripts/"
EOF

cat >> /etc/profile.d/env.sh
export PS1='\[\e[31m\][\u@\h\w]\$\[\e[0m\]'
EOF

sed -i 's/quiet/& net.ifnames=0/' /etc/default/grub
grub2-config -o /boot/grub2/grub.cfg

%end

(e)编辑isolinux.cfg文件[root@localhost fulliso]# vim isolinux/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
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
# 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 #84b8ffff #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 desktop
  menu label desktop Linux 7
  kernel vmlinuz
  append initrd=initrd.img ks=cdrom:/ks7.cfg

label mini
  menu label mini linux 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=cdrom:/ks7mini.cfg

menu 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出现上述界面,代表制做光盘已经成功。

相关文章
相关标签/搜索