定制自己的CentOS,制作ISO镜像文件

您所在的位置:网站首页 如何替换iso内文件 定制自己的CentOS,制作ISO镜像文件

定制自己的CentOS,制作ISO镜像文件

2023-08-10 13:20| 来源: 网络整理| 查看: 265

简陋版:单纯替换,裁剪

先来个简陋版,目的:安装ISO后系统会自带我们需要的工具、驱动

(一):安装官方OS

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

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

(二):设置网络

因为是最小化安装,此时网络是没有自动起来的(记得插网线哦)。 可以使用“ip a”命令查看网络端口,使用dhclient命令自动获取ip。 在这里插入图片描述 最小化安装是没有ifconfig命令的,使用yum search ifconfig命令可以看到安装net-tools.x86_64就可以了(先不安装)

设置网络: vi /etc/sysconfig/network-scripts/ifcfg- 根据 ip a/addr/address 命令的结果

[root@localhost ~]# ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: wwp0s30u4i8: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 8e:53:3a:f6:95:4f brd ff:ff:ff:ff:ff:ff 3: eno1: mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:e0:ec:f4:43:9a brd ff:ff:ff:ff:ff:ff inet 192.168.1.110/24 brd 192.168.1.255 scope global dynamic eno1 valid_lft 6330sec preferred_lft 6330sec inet6 fe80::2e0:ecff:fef4:439a/64 scope link valid_lft forever preferred_lft forever

可以看到网口是eno1,所以vi /etc/sysconfig/network-scripts/ifcfg-eno1 把最后一行的ONBOOT=no改为ONBOOT=yes 这样每次开机就会自动获取ip了

(三)安装制作ISO的工具 yum -y install createrepo mkisofs isomd5sum rsync (四)建立 image-making-directory

(1)创建 ISO制作目录

mkdir -p /root/iso/CentOS7.4-evglow

(2)挂载官方ISO启动盘(假设是/dev.sdb4)

mkdir /mnt/usb mount /dev/sdb4 /mnt/usb

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

rsync -a /mnt/usb/ /root/iso/CentOS7.4-evglow/

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

total 216 drwxr-xr-x. 9 root root 8192 Dec 31 1969 . drwxr-xr-x. 3 root root 19 Jan 19 02:19 .. -rwxr-xr-x. 1 root root 14 Sep 6 2017 CentOS_BuildTag -rwxr-xr-x. 1 root root 29 Sep 6 2017 .discinfo drwxr-xr-x. 3 root root 8192 Sep 6 2017 EFI -rwxr-xr-x. 1 root root 227 Aug 31 2017 EULA -rwxr-xr-x. 1 root root 18009 Dec 10 2015 GPL drwxr-xr-x. 3 root root 8192 Sep 6 2017 images drwxr-xr-x. 2 root root 8192 Jan 19 2020 isolinux drwxr-xr-x. 2 root root 8192 Sep 6 2017 LiveOS drwxr-xr-x. 2 root root 81920 Jan 19 2020 Packages drwxr-xr-x. 2 root root 8192 Jan 19 2020 repodata -rwxr-xr-x. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7 -rwxr-xr-x. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7 drwxr-xr-x. 2 root root 8192 Jan 18 23:47 System Volume Information -rwxr-xr-x. 1 root root 3120 Jan 19 2020 TRANS.TBL -rwxr-xr-x. 1 root root 354 Sep 6 2017 .treeinfo

isolinux directory: store the installation interface information when the CD is booted; images directory: necessary boot image files; Packages directory: install packages and information; repodata directory: packages dependency information.

(五)安装自己想加进去的包/tool

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

vi /etc/yum.conf

keepcache=0改为keepcache=1 保存缓存的rpm包 (2)尽情安装 yum install -y net-tools.x86_64 vim i2c-tools gcc 等等 也可以安装自己制作的rpm包 rpm -ivh evglow-1.0.0-1.x86_64.rpm或yum install -y evglow-1.0.0-1.x86_64.rpm 可以把自己要安装的驱动写成一个rpm包,安装该rpm包时自动编译安装

(六)替换Packages/里的文件

官方包有4G多大(这样vfat32格式的U盘就不能拷贝了),主要是里面存放了很多rpm包,我们用不到这么多,所以在服务器上安装好需要的rpm后,根据当前安装的rpm列表去裁剪Packages/目录 (1) 生成当前的rpm列表

rpm -qa >> /root/install.log

(2)根据该列表放置rpm包 先把原来的删了

rm -f /root/iso/CentOS7.4-evglow/Packages/*

然后

cat /root/instal.log|awk '{print $0}' |xargs -i cp /mnt/usb/Packages/{}.rpm /root/iso/CentOS7.4-evglow/Packages/

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

cp /var/cache/yum/x86_64/7/base/packages/* /root/iso/CentOS7.4-evglow/Packages/

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

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

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

ISO_DIR=/root/iso/CentOS7.4-evglow createrepo -g ${ISO_DIR}/repodata/*-comps.xml ${ISO_DIR}

这个*-comps.xml只有一个,不过名字太长了,我这里的叫做

38b60f66d52704cffb8696750b2b6552438c1ace283bc2cf22408b0ba0e4cbfa-c7-x86_64-comps.xml

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

(八)编辑 ks.cfg文件

为了方便,我们使用Kickstart安装。 参考文章:https://blog.csdn.net/Primeprime/article/details/80759635 通常,安装系统后,/ root目录中将存在anaconda-ks.cfg文件, 记录系统安装过程中的配置。 将其复制到镜像下的isolinux目录中 创建目录。

cp /root/anaconda-ks.cfg /root/iso/ CentOS7.4-evglow/isolinux/ks.cfg

可以根据这个ks.cfg来定制安装需求,与rpmbuild配置类似,%pre表示系统安装前运行的内容,%post表示系统安装后执行的脚本 %packages表示要安装的包,我们主要是在这里加上需要安装的rpm

注:每个item必须用%end结尾!!!

#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=sdb # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=eno1 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $6$i4EBS3qo2QQTq7j4$MhY/TUkw/IvV4lvVRXHSblSq7YdoihebZcifsqpdd4EQl5U6sTGmsGo07ht/ot7e6N/vWfOqbktCVVwZODONA0 # System services services --enabled="chronyd" # System timezone timezone America/New_York --isUtc user --name=test --password=$6$MPcNAvKOKkxI2C13$XBdEWCVV56shVfMcxN7zDMSXNdHzCodji2JKIApjsNq6EJHWfr.hnnThxwn7r4d/RIdDoZO8BvufjXbz/eiJK1 --iscrypted --gecos="test" # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sdb autopart --type=lvm # Partition clearing information clearpart --all --initlabel --drives=sdb %packages @^minimal @compat-libraries @core @debugging @development @security-tools @smart-card chrony kexec-tools i2c-tools vim net-tools.x86_64 tcl expect expect-devel evglow-tool centos-release-scl devtoolset-7-gcc* %end %post echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eno1 %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

注2:RPM的NAME可以使用rpm -qai 查询,如我之前使用yum install -y centos-release-scl,虽然下载的包叫centos-release-scl-2-3.el7.centos.src.rpm 但是NAEM为centos-release-scl,然后虽然有3个包,但是只用写centos-release-scl就好了(联网安装时yum -y install XXX写的是什么就写什么)

[root@localhost ~]# rpm -qai |grep scl Name : centos-release-scl-rh Source RPM : centos-release-scl-rh-2-3.el7.centos.src.rpm Summary : Software collections from the CentOS SCLo SIG (upstream scl only) URL : http://pcsclite.alioth.debian.org/ URL : http://pcsclite.alioth.debian.org/ Name : scl-utils Source RPM : scl-utils-20130529-19.el7.src.rpm URL : https://fedorahosted.org/released/scl-utils/ Name : centos-release-scl Source RPM : centos-release-scl-2-3.el7.centos.src.rpm URL : http://pcsclite.alioth.debian.org/ccid.html

注3:是可以使用通配符的,比如安装的时候使用了 yum install -y devtoolset-7-gcc,现在也可以用devtoolset-7-gcc*表示安装所有以devtoolset-7-gcc为前缀的包 注4:这些packages(所有的包及其依赖)一定要在/root/iso/CentOS7.4-evglow/Packages/目录下有,并且放在该目录下后运行了 createrepo -g ${ISO_DIR}/repodata/*-comps.xml ${ISO_DIR}命令,不然即使放了包,在刻录成ISO后安装时还是会提示包不存在

注5: 实测发现,制作好ISO后,如果想更改ks.cfg内容,即使更改的内容与rpm package无关,也许重新运行 createrepo -g ${ISO_DIR}/repodata/*-comps.xml ${ISO_DIR} 这个命令,不然进不去安装界面

(九)编辑启动文件

(1)Modify the isolinux.cfg file

vim isolinux/isolinux.cfg

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

如果有“menu default”这一行,删掉把" append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet"这一行改为 ”append initrd=initrd.img inst.stage2=hd:LABEL=EVGLOW inst.ks=hd:LABEL=EVGLOW:/isolinux/ks.cfg“ 这个“EVGLOW”可以任意命名,记得保持一致就好(后面刻盘时候,USB启动盘的名字要改成这个。不然找不到U盘的话需要先在install centos那里按"E",然后使用linux dd 查看U盘设备名,假如是/dev/sda4,确认设备名后重启,再次按E编辑,把"LABEL=EVGLOW"改为/dev/sda4,两边都要改,然后ctrl+X进入安装界面) label linux menu label ^Install CentOS 7 kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=MYISO inst.ks=hd:LABEL=MYISO:/isolinux/ks.cfg

注:是label linux下面的,不要改错了,然后EVGLOW :/isolinux/ks.cfg,EVGLOW后面的冒号别丢了

(2)Modify EFI/BOOT/grub.cfg vim /root/iso/CentOS7.4-evglow/EFI/BOOT/grub.cfg 把menuentry ‘Install CentOS 7’ --class fedora --class gnu-linux --class gnu --class os { 下面那行的后半部分也改为inst.stage2=hd:LABEL=EVGLOW inst.ks=hd:LABEL=MYISO:/isolinux/ks.cfg 效果如下

### 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=EVGLOW inst.ks=hd:LABEL=EVGLOW:/isolinux/ks.cfg 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=CentOS\x207\x20x86_64 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 } }

注:如果想配置console串口的Baud rate,可以在后面追加console=ttyS0,57600n8 效果如下(这是同一行,显示不下才成了两行)

append initrd=initrd.img inst.stage2=hd:LABEL=EVGLOW inst.ks=hd:LABEL=EVGLOW:/isolinux/ks.cfg console=ttyS0,57600n8

也可以设置console=ttyS3,115200n8 console=tty0(亲测有效,部分机器可能会乱码,需要改波特率。测试发现console=ttyS0和console=tty0都可以;但是console=ttyS3,115200n8里面ttyS3的字母S不可省略!)让串口和外接PCI-E显卡同时显示等等。其中ttyX表示虚拟终端,也就是ALT+F1~F6,tty0是前台虚拟终端(VGA),ttySX表示串口,ttyS0表示串口1,我们这里设置的ttyS3和tty0) isolinux/isolinus.cfg和EFI/BOOT/grub.cfg都要改,两个文件配置信息需保存一致 注:也可以在进入OS时,按E进入编辑,在"rhgb"与“quiet”之间加上console=ttyS3,115200n8 console=ttyS0

(十)制作ISO

(1) 运行如下命令(参数比较多,纯手打可能有点小错误)

mkisofs -o CentOS7.4-evglow.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 EVGLOW /root/iso/CentOS7.4-evglow/

此时会生成CentOS7.4-evglow.iso文件,需要注意的是,会把/root/iso/CentOS7.4-evglow/目录下的所有文件都包含,所以如果你之前生成过一个CentOS7.4-evglow.iso,再次运行这个命令会把上一次生成的ISO包含,导致新的CentOS7.4-evglow.iso有两倍大。 (2)生成MD5(虽然不做这一步ISO也能用)

implantisomd5 CentOS7.4-wedge400.iso DONE

关于安装过程要注意的点: (1) 我是在制作CentOS7.4-wedge400.iso后,拷贝到U盘里,然后windows下使用UltraISO软碟通刻盘,刻录完成后U盘名字可能是 “NO NAME”,这时要手动改成之前制作时设置的LABEL,如”EVGLOW“ 如果还是不行,则采用以下万能方法:

安装时,在"在install centos"那里按"E",然后删掉"linuxefi /image/pxeboot/vmlinuz"哪一行后面的内容(一般是到quiet结束),追加linux dd 命令查看U盘设备名,按Ctrl+X运行,然后可以看到挂载情况。假如是/dev/sda4,确认设备名后重启,再次按E编辑,把"LABEL=EVGLOW"改为/dev/sda4,前后都要改,然后ctrl+X进入安装界面 (2) 服务器本身没有VGA/HDMI显示器接口使用的是外接显卡,如果安装界面一直黑屏,换一张显卡试试。我遇见过安装好OS后,换一种显卡进不去(一直黑屏)的情况,但是使用SSH可以登录机器,说明OS起来了,再换回原来的显卡也是可以开机的。 可能是显卡生成了多个窗口等原因造成的。

0000000有图有真相00000000000

(1)方向键选择 “Install CentOS7” 然后按“e”进入编辑 在这里插入图片描述 (1)运行linux dd 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 可以看到是/dev/sda4。这里LABEL为空,所以识别不到

(3)把盘符改成/dev/sda4 在这里插入图片描述 (4)%packages要以%end结尾(%packages, %pre, %post以及%traceback这几项都需要) 在这里插入图片描述 (5)要creatrepo -g 不然找不到 当然这里也漏了通配符* 在这里插入图片描述 (6)rpm -Uvh不行就用yum install -y 安装 在这里插入图片描述 在这里插入图片描述 (7)改默认启动内核grub2-set-default “CentOS…(Core)” 在这里插入图片描述 (8)enable串口 修改/etc/default/grub, 追加(其实是给GRUB_CMDLINE_LINUX变量重新赋值) GRUB_CMDLINE_LINUX=“crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb console=ttyS3,115200n8 console=tty0 quiet” 修改后记得运行 grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg 命令 在这里插入图片描述 (9)编辑界面效果 在这里插入图片描述 (10) isolinux/ks.cfg 允许用户自己设置 这里把时区设置以及root密码配置去掉了,如此一来,安装的时候会检测到时区未设置,从而等待用户选择,而不是自行“Begian installation" P.s. 改完记得运行creatrepo -g XXXXXX . 命令

#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=sdb # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=eno1 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password #rootpw --iscrypted $6$i4EBS3qo2QQTq7j4$MhY/TUkw/IvV4lvVRXHSblSq7YdoihebZcifsqpdd4EQl5U6sTGmsGo07ht/ot7e6N/vWfOqbktCVVwZODONA0 # System services services --enabled="chronyd" # System timezone #timezone America/New_York --isUtc #user --name=test --password=$6$MPcNAvKOKkxI2C13$XBdEWCVV56shVfMcxN7zDMSXNdHzCodji2JKIApjsNq6EJHWfr.hnnThxwn7r4d/RIdDoZO8BvufjXbz/eiJK1 --iscrypted --gecos="test" # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sdb autopart --type=lvm # Partition clearing information clearpart --all --initlabel --drives=sdb %packages @^minimal @compat-libraries

在这里插入图片描述 后来又提出,希望当SSD中已有系统时,让用户自行选择处理方式。 查阅后发现需要更改"ignoredisk"项,

ignoredisk --drives=[disk1,disk2,…] 指定在分区、格式化、擦出时anaconda不应该访问的磁盘。 ignoredisk --only-use=[disk1,disk2,…] 与上面的选项相反。仅有列出的磁盘可以在安装过程中被使用。 ignoredisk --interactive 允许用户手动进行设置。 所以我们把ignoredisk --only-use=sdb 改为 ignoredisk --interactive,并把后面的clearpart --all --initlabel --drives=sdb 这一行注释掉(删掉也行) 最后结果: 在这里插入图片描述

P.s. rsync是可以实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时同步。 -r 即 recursive(递归的、循环的) -l 同步软链接文件(小写的L) -p 这个选项的全名是 perserve permissions,顾名思义,就是保持权限 -g -o 这两个选项是一对,用来保持文件的属组(group)和属主(owner)。改变属主和属组,往往要有管理员权限才可以。 -D 原文解释是 preserve devices (root only),从字面意思看,是保持设备文件的原始信息 -a 使用-a选项,就相当于使用了-rlptgoD选项, archive option,中文叫做归档选项。使用-a选项,就表明你希望采取递归方式来同步,且尽可能的保持各个方面的一致性 createrepo 命令用于创建yum源(软件仓库),即为存放于本地特定位置的众多rpm包建立索引,描述各包所需依赖信息,并形成元数据。

-g --groupfile 指定本地软件仓库的组划分,范例如下: createrepo -g comps.xml /path/to/rpms 注意:组文件需要和rpm包放置于同一路径下。 -u --baseurl 指定Base URL的地址 -o --outputdir 指定元数据的输出位置 -x --excludes 指定在形成元数据时需要排除的包 -i --pkglist 指定一个文件,该文件内的包信息将被包含在即将生成的元数据中,格式为每个包信息独占一行,不含通配符、正则,以及范围表达式。 -n --includepkg 通过命令行指定要纳入本地库中的包信息,需要提供URL或本地路径。 -q --quiet 安静模式执行操作,不输出任何信息。 -v --verbose 输出详细信息。 -c --cachedir 指定一个目录,用作存放软件仓库中软件包的校验和信息。 当createrepo在未发生明显改变的相同仓库文件上持续多次运行时,指定cachedir会明显提高其性能。 –update 如果元数据已经存在,且软件仓库中只有部分软件发生了改变或增减, 则可用update参数直接对原有元数据进行升级,效率比重新分析rpm包依赖并生成新的元数据要高很多。 -p --pretty 以整洁的格式输出xml文件。 -d --database 该选项指定使用SQLite来存储生成的元数据,默认项。

mkisofs可将指定的目录与文件做成ISO 9660格式的映像文件,以供刻录光盘

-D或-disable-deep-relocation ISO 9660最多只能处理8层的目录,超过8层的部分,RRIP会自动将它们设置成ISO 9660兼容的格式。使用-D参数可关闭此功能。 -o或-output 指定映像文件的名称 -input-charset utf8 -output-charset utf8,解决中文文件名乱码问题 -b或-eltorito-boot 指定在制作可开机光盘时所需的开机映像文件。 -c 制作可开机光盘时,mkisofs会将开机映像文件中的全-eltorito-catalog全部内容作成一个文件 -load-size 4:设置加载扇区数 -boot-info-table:修补启动映像与信息表 Patch boot image with info table -no-emul-boot Boot image is ‘no emulation’ image 无仿真 -R, -rock Generate Rock Ridge directory information -J或-joliet 使用Joliet格式的目录与文件名称 -v或-verbose 执行时显示详细的信息。 -V或-volid 指定光盘的卷册集ID -T或-translation-table 建立文件名的转换表,适用于不支持Rock Ridge Extensions的系统上

implantisomd5 – implant an MD5 checksum in an ISO9660 image

This manual page documents briefly the implantisomd5 command. implantisomd5 is a program that embeds an MD5 checksum in an unused section of and ISO9660 (.iso) image. This checksum can later be compared to the .iso, or a block device, using the corresponding checkisomd5 command.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3