CentOS7.6制作升级内核后ISO镜像文件
LiuSw Lv6

CentOS7.6制作升级内核后ISO镜像文件

1.安装官方OS

下载Centos官方包,我这里下载的是CentOS-7.4-x86_64-DVD-1810.iso
然后安装在服务器上(用来定制ISO的服务器)
根据自己的需求选择安装,我这里选择的是minimal安装 。

注:制作出来的ISO会沿用现在的选择,也就是说制作出来的ISO只要没问题,就会在选择”install centos”后自动检查软件依赖并安装和设置用户(是的,账户&密码也会和现在安装所选择/设置的一样)。

2.安装制作ISO的工具

1
yum  -y install createrepo mkisofs isomd5sum rsync

3.建立 image-making-directory

(1)创建 ISO制作目录

1
mkdir -p /root/iso/CentOS7.6-5.16

(2)挂载官方ISO启动盘

1
2
3
4
5
6
7
# 方式1 软盘挂载(假设是/dev.sr1)
mkdir /mnt/usb
mount /dev/sr1 /mnt/usb

# 方式2 上传iso后挂载
mkdir /mnt/usb
mount -o loop /mnt/CentOS-7-x86_64-DVD-1810.iso /mnt/usb

(3)把官方镜像里的文件同步到image-making-directory

1
2
3
rsync -a /mnt/usb/ /root/iso/CentOS7.6-5.16/

scp -r /mnt/usb/ /root/iso/CentOS7.6-5.16/

可以使用 ll -a查看目录,其中

1
2
3
4
5
6
7
8
9
10
11
12
13
total 336
-rw-rw-r--. 1 root root 14 Nov 26 2018 CentOS_BuildTag
drwxr-xr-x. 3 root root 35 Nov 26 2018 EFI
-rw-rw-r--. 1 root root 227 Aug 30 2017 EULA
-rw-rw-r--. 1 root root 18009 Dec 10 2015 GPL
drwxr-xr-x. 3 root root 57 Nov 26 2018 images
drwxr-xr-x. 2 root root 212 Jan 17 19:00 isolinux
drwxr-xr-x. 2 root root 43 Nov 26 2018 LiveOS
drwxrwxr-x. 2 root root 233472 Jan 14 22:56 Packages
drwxr-xr-x. 2 root root 4096 Jan 17 16:43 repodata
-rw-rw-r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 Nov 26 2018 TRANS.TBL

4.内核升级与选择自己想安装的工具包

(1)配置yum

主要是为了保存rpm包(要放进我们定制的ISO里面)。

1
2
3
vi  /etc/yum.conf

# keepcache=0改为keepcache=1

保存缓存的rpm包

(2)安装下载工具包

1
2
3
4
5
6
7
# 方式1 yum想要安装的包,例如:
yum install -y net-tools.x86_64 vim i2c-tools gcc

# 方式2 也可以手动安装自己制作下载的rpm离线包,例如:
rpm -ivh myPackage-1.0.0-1.x86_64.rpm
# 或
yum install -y myPackage-1.0.0-1.x86_64.rpm

(3)内核升级

使用rpm命令添加新的ELRepo存储库。

1
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

(从ELRepo信息库安装一个新的最新内核)

1
2
# --enablerepo是在CentOS系统上启用特定存储库的选项。默认情况下,启 用“ elrepo”存储库,但不启用“ elrepo-kernel”存储库
yum --enablerepo=elrepo-kernel install kernel-ml

5.替换Packages/里的文件

官方包有4G多大,主要是里面存放了很多rpm包,这里可以选择删除掉也可以不删除。

(1)生成当前的rpm列表

1
rpm -qa >> /root/install.log

(2)选择性删除包(不删除此步骤略过)

需要其他rpm包不删除,建议不进行删除,可以用作本地源, 删除后的依赖问题也是比较头疼

添加的包不要太多,包太多ISO太大DVD可能刻录不下

1
rm -f /root/iso/CentOS7.6-5.16/Packages/*

执行以下会提示一些包没有,这些是我们用yum安装的以及自己本地的rpm包。

1
cat /root/install.log|awk '{print $0}' |xargs -i cp /mnt/usb/Packages/{}.rpm /root/iso/CentOS7.6-5.16/Packages/

原包Packages内可能是其他包的依赖包,所以此次都复制过去,避免依赖问题。

1
2
3
\cp /mnt/usb/Packages/lib* /root/iso/CentOS7.6-5.16/Packages/
\cp /mnt/usb/Packages/kernel* /root/iso/CentOS7.6-5.16/Packages/
\cp /mnt/usb/Packages/python* /root/iso/CentOS7.6-5.16/Packages/

(3)拷贝安装下载后的包文件到image-making-directory目录下

1
2
3
4
5
\cp /var/cache/yum/x86_64/7/base/packages/* /root/iso/CentOS7.6-5.16/Packages/
\cp /var/cache/yum/x86_64/7/updates/packages/* /root/iso/CentOS7.6-5.16/Packages/

# 升级内核的包不在base目录下,在其他源elrepo-kernel的目录下
\cp /var/cache/yum/x86_64/7/elrepo-kernel/packages/* /root/iso/CentOS7.6-5.16/Packages/

当然不是所有yum的包都在这里,比如centos-release-gcc和devtoolset-7-gcc*这些包是在其他目录的,使用find命令查找,别忘了把自己制作的rpm也放进去(如果有的话)。

1
find / -name devtoolset-7*.rpm

6.重新生成依赖文件(非常重要)

*comps.xml文件包含所有与RPM相关的内容。 它检查“软件包”下的RPM软件包依赖性。 安装时如果依赖包丢失,它将提示哪个RPM包需要哪个依赖。

1
2
ISO_DIR=/root/iso/CentOS7.6-5.16
createrepo -g ${ISO_DIR}/repodata/*-comps.xml ${ISO_DIR}

*-coms.xml只有一个

1
aced7d22b338fdf7c0a71ffcf32614e058f4422c42476d1f4b9e9364d567702f-c7-x86_64-comps.xml

注:如果后面加了RPM包一定要重新生成

7.编辑 ks.cfg文件

通常,安装系统后,/ root目录中将存在anaconda-ks.cfg文件,
记录系统安装过程中的配置。 将其复制到镜像下的isolinux目录中 。

1
cp /root/anaconda-ks.cfg /root/iso/CentOS7.6-5.16/isolinux/ks.cfg

可以根据这个ks.cfg来定制安装需求,与rpmbuild配置类似。

%pre表示系统安装前运行的内容

%post表示系统安装后执行的脚本
%packages表示要安装的包,我们主要是在这里加上需要安装的rpm

ks.cfg文件内容:

(此文件root密码默认为Root@123)

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
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# 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=enp1s0 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$/cak3hUuBmFNqaMn$0Ug37Q5q5l8Jx/aegQ7G/Ksz6pe8jrSyBthtmuD5xBA1X7G4kjqVh0ABYilcteihLt4x3HVGFHIPVUkPyv34W.
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel --list=sda

%packages
@^minimal
@core
kexec-tools
wget
net-tools
curl
telnet
expect
unzip
bash-completion
vim-enhanced
vim
gcc
kernel-ml

%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

注1:加上的是RPM的NAME而不是rpm文件的名字,如我定制的rpm包,文件名叫evglow-1.0.0-1.x86_64.rpm,但是RPM的NAME是evglow-tool,所以%packages下面写的是evglow-tool,

即yum包时你写什么文件内就写什么

8.编辑启动文件

(1)修改isolinux.cfg文件

修改在label linux这个item下的配置

  1. 如果有“menu default”这一行,删掉。
  2. 把” append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet”这一行改为
    ”append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg“
    这个“CentOS7”可以任意命名,记得保持一致就好(后面刻盘时候,USB启动盘的名字要改成这个。不然找不到U盘的话需要先在install centos那里按”E”,然后使用linux dd 查看U盘设备名,假如是/dev/sda4,确认设备名后重启,再次按E编辑,把”LABEL=CentOS7”改为/dev/sda4,两边都要改,然后ctrl+X进入安装界面)。
1
vim isolinux/isolinux.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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 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 #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 linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg quiet

label check
menu label Test this ^media & install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg 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
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

(2)修改 grub.cfg

把menuentry ‘Install CentOS 7’ –class fedora –class gnu-linux –class gnu –class os {undefined
下面那行的后半部分也改为:

inst.stage2=hd:LABEL=CentOS7 inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg
效果如下 :

1
vim /root/iso/CentOS7.6-5.16/EFI/BOOT/grub.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
set default="1"

function load_video {
insmod efi_gop
insmod efi_uga
insmod video_bochs
insmod video_cirrus
insmod all_video
}

load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2

set timeout=60
### END /etc/grub.d/00_header ###

search --no-floppy --set=root -l 'CentOS 7 x86_64'

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Install CentOS 7' --class fedora --class gnu-linux --class gnu --class os {
linuxefi /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS7 inst.stage2=hd:LABEL=CentOS7:/isolinux/ks.cfg quiet
initrdefi /images/pxeboot/initrd.img
}
menuentry 'Test this media & install CentOS 7' --class fedora --class gnu-linux --class gnu --class os {
linuxefi /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS7 inst.stage2=hd:LABEL=CentOS7:/isolinux/ks.cfg rd.live.check quiet
initrdefi /images/pxeboot/initrd.img
}
submenu 'Troubleshooting -->' {
menuentry 'Install CentOS 7 in basic graphics mode' --class fedora --class gnu-linux --class gnu --class os {
linuxefi /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet
initrdefi /images/pxeboot/initrd.img
}
menuentry 'Rescue a CentOS system' --class fedora --class gnu-linux --class gnu --class os {
linuxefi /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet
initrdefi /images/pxeboot/initrd.img
}
}

9.制作ISO

(1)制作镜像

1
mkisofs -o CentOS7.6-5.16-V1.2.iso -input-charset utf-8 -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -T -joliet-long -V CentOS7 /root/iso/CentOS7.6-5.16/

此时会生成CentOS7.6-5.16-V1.2.iso文件

(2) 生产Md5(可以略过)

1
implantisomd5 CentOS7.4-wedge400.iso

10.目录展示

1
2
3
4
5
6
[root@localhost ~]# ll /root
total 4639200
-rw-------. 1 root root 1592 Jan 14 23:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 4750512128 Jan 17 17:46 CentOS7.6-boer-v1.2.iso
-rw-r--r--. 1 root root 21153 Jan 14 22:31 install.log
drwxr-xr-x. 3 root root 28 Jan 17 22:09 iso

本文借鉴 evglow

 评论