【Linux】Linux环境下制作ISO镜像与刻录U盘


文章目录

  • 前言
  • 一、工具包安装
  • 二、自定义镜像文件
    • 2.1 拷贝
    • 2.2 更新软件包
    • 2.3 ks文件与启动项修改
    • 2.4 制作ISO文件
  • 三、制作USB启动盘(刻录U盘)
    • 3.1 插入U盘
    • 3.2 格式化U盘分区
    • 3.3 写入镜像
  • 四、总结


前言

本文是记录下在Linux环境下制作系统镜像文件,以及将该镜像文件刻录到U盘中 这两个场景。


一、工具包安装

[root@server ~]# yum install -y createrepo mkisofs dosfstools syslinux  

createrepo:用于对软件包生成分组索引信息
mkisofs:制作镜像文件
dosfstools:主要利用该工具包中的mkfs.vfat对U盘进行格式化
syslinux:使用该包中的isohybrid工具,将标准ISO文件转换成混合ISO,以便写入USB存储设备

二、自定义镜像文件

2.1 拷贝

找一个现有的镜像,挂载到本地,并将其所有内容,拷贝到自己即将打包的自建目录中。

[root@server ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@server ~]# ls /mnt/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@server ~]# mkdir customISO    
[root@server ~]# cp -ar /mnt/* customISO/

[root@server ~]# ll customISO/
总用量 732
-rw-rw-r--. 1 root root     14 910 2019 CentOS_BuildTag
drwxr-xr-x. 3 root root     35 96 2019 EFI
-rw-rw-r--. 1 root root    227 830 2017 EULA
-rw-rw-r--. 1 root root  18009 1210 2015 GPL
drwxr-xr-x. 3 root root     57 910 2019 images
drwxr-xr-x. 2 root root    198 910 2019 isolinux
drwxr-xr-x. 2 root root     43 96 2019 LiveOS
drwxrwxr-x. 2 root root 561152 910 2019 Packages
drwxrwxr-x. 2 root root   4096 910 2019 repodata
-rw-rw-r--. 1 root root   1690 1210 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 1 root root   1690 1210 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root   2883 910 2019 TRANS.TBL

2.2 更新软件包

当有自己的实际需求时,将目标RPM包下载到customISO/Packages中,下载方式可以由YUM联网源直接下载,或者就自己Windows下载后并上传。

[root@server ~]# yum install docker -y --downloadonly --downloaddir="/root/customISO/Packages"      
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* centos-sclo-rh: mirrors.ustc.edu.cn
* centos-sclo-sclo: ftp.sjtu.edu.cn
* development: ftp.sjtu.edu.cn
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
file:///media/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /media/repodata/repomd.xml"
正在尝试其它镜像。
正在解决依赖关系
--> 正在检查事务
...
...
--> 解决依赖关系完成


依赖关系解决


==================================================================================================================
Package                                   架构      版本                                    源              大小
==================================================================================================================
正在安装:
docker                                    x86_64    2:1.13.1-209.git7d71120.el7.centos      extras          17 M
为依赖而安装:
atomic-registries                         x86_64    1:1.22.1-33.gitb507039.el7_8            extras          36 k
container-selinux                         noarch    2:2.119.2-1.911c772.el7_8               extras          40 k
container-storage-setup                   noarch    0.11.0-2.git5eaf76c.el7                 extras          35 k
containers-common                         x86_64    1:0.1.40-11.el7_8                       extras          43 k
docker-client                             x86_64    2:1.13.1-209.git7d71120.el7.centos      extras         3.9 M
docker-common                             x86_64    2:1.13.1-209.git7d71120.el7.centos      extras         101 k
fuse-overlayfs                            x86_64    0.7.2-6.el7_8                           extras          54 k
fuse3-libs                                x86_64    3.6.1-4.el7                             extras          82 k
oci-register-machine                      x86_64    1:0-6.git2b44233.el7                    extras         1.1 M
oci-systemd-hook                          x86_64    1:0.2.0-1.git05e6923.el7_6              extras          34 k
oci-umount                                x86_64    2:2.5-3.el7                             extras          33 k
python-dateutil                           noarch    1.5-7.el7                               development     85 k
python-dmidecode                          x86_64    3.12.2-4.el7                            development     83 k
python-pytoml                             noarch    0.1.14-1.git7dea353.el7                 extras          18 k
python-syspurpose                         x86_64    1.24.51-1.el7.centos                    updates        275 k
slirp4netns                               x86_64    0.4.3-4.el7_8                           extras          81 k
subscription-manager                      x86_64    1.24.51-1.el7.centos                    updates        1.1 M
subscription-manager-rhsm                 x86_64    1.24.51-1.el7.centos                    updates        334 k
subscription-manager-rhsm-certificates    x86_64    1.24.51-1.el7.centos                    updates        237 k


事务概要
==================================================================================================================
安装  1 软件包 (+19 依赖软件包)


总计:25 M
总下载量:25 M
安装大小:86 M
Background downloading packages, then exiting:
No Presto metadata available for development
No Presto metadata available for updates
(1/19): container-storage-setup-0.11.0-2.git5eaf76c.el7.noarch.rpm                         |  35 kB  00:00:00     
(2/19): atomic-registries-1.22.1-33.gitb507039.el7_8.x86_64.rpm                            |  36 kB  00:00:00     
(3/19): containers-common-0.1.40-11.el7_8.x86_64.rpm                                       |  43 kB  00:00:00     
(4/19): docker-client-1.13.1-209.git7d71120.el7.centos.x86_64.rpm                          | 3.9 MB  00:00:00     
(5/19): docker-common-1.13.1-209.git7d71120.el7.centos.x86_64.rpm                          | 101 kB  00:00:00     
(6/19): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm                                            |  54 kB  00:00:00     
(7/19): fuse3-libs-3.6.1-4.el7.x86_64.rpm                                                  |  82 kB  00:00:00     
(8/19): oci-register-machine-0-6.git2b44233.el7.x86_64.rpm                                 | 1.1 MB  00:00:00     
(9/19): oci-systemd-hook-0.2.0-1.git05e6923.el7_6.x86_64.rpm                               |  34 kB  00:00:00     
(10/19): python-dmidecode-3.12.2-4.el7.x86_64.rpm                                          |  83 kB  00:00:00     
(11/19): docker-1.13.1-209.git7d71120.el7.centos.x86_64.rpm                                |  17 MB  00:00:00     
(12/19): oci-umount-2.5-3.el7.x86_64.rpm                                                   |  33 kB  00:00:00     
(13/19): python-pytoml-0.1.14-1.git7dea353.el7.noarch.rpm                                  |  18 kB  00:00:00     
(14/19): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                                              |  81 kB  00:00:00     
(15/19): subscription-manager-1.24.51-1.el7.centos.x86_64.rpm                              | 1.1 MB  00:00:00     
(16/19): subscription-manager-rhsm-1.24.51-1.el7.centos.x86_64.rpm                         | 334 kB  00:00:00     
(17/19): subscription-manager-rhsm-certificates-1.24.51-1.el7.centos.x86_64.rpm            | 237 kB  00:00:00     
(18/19): python-syspurpose-1.24.51-1.el7.centos.x86_64.rpm                                 | 275 kB  00:00:00     
(19/19): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                              |  40 kB  00:00:05     
------------------------------------------------------------------------------------------------------------------
总计                                                                              4.4 MB/s |  25 MB  00:00:05     
exiting because "Download Only" specified

此时,相关RPM包就更新到了Packages目录中。
因为软件包出现了变化,所以就需要重新更新关联信息。

[root@server customISO]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@server customISO]# ls repodata/
04efe80d41ea3d94d36294f7107709d1c8f70db11e152d6ef562da344748581a-primary.sqlite.bz2
41232548001a78473ae0f2d4b92e1ec28f7a0593e0495056515887fe2a39b416-filelists.sqlite.bz2
4af1fba0c1d6175b7e3c862b4bddfef93fffb84c37f7d5f18cfbff08abc47f8a-c7-x86_64-comps.xml.gz
521f322f05f9802f2438d8bb7d97558c64ff3ff74c03322d77787ade9152d8bb-c7-x86_64-comps.xml
879e9b33c0ecf18a99bdac5b607b1ea65dc66423b99bdf4214129c5d5923e475-other.sqlite.bz2
afe9c5f88d9c365382c0553424b18192b79f3da7c1e3d120818203e9a5d1a70a-primary.xml.gz
c1977f4def8aa2f0addb2695b9cc6a391ece8bb5d5b53d601db15df6240261f1-filelists.xml.gz
dc871e4d16c013169e2e42f7a7a0659116f5b63f7e07d9a30dccbef2f71a3f0c-other.xml.gz
repomd.xml
repomd.xml.asc
TRANS.TBL
[root@server customISO]# mv repodata/*comps.xml repodata/comps.xml
[root@server customISO]# cd repodata/
[root@server repodata]# ls | grep -v  comps.xml | xargs rm
[root@server repodata]# ls
4af1fba0c1d6175b7e3c862b4bddfef93fffb84c37f7d5f18cfbff08abc47f8a-c7-x86_64-comps.xml.gz  comps.xml
[root@server repodata]# rm -f 4af1fba0c1d6175b7e3c862b4bddfef93fffb84c37f7d5f18cfbff08abc47f8a-c7-x86_64-comps.xml.gz 
[root@server repodata]# ls
comps.xml
[root@server repodata]# cd ..
# 根据指定组文件,创建所有RPM关联信息
[root@server customISO]# createrepo -g repodata/comps.xml .
Spawning worker 0 with 5058 pkgs
Spawning worker 1 with 5058 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

# 创建完成之后,repodata目录中生成新的关联文件
[root@server customISO]# ls repodata/
11715cacff3b3d5b21d6bd6200acd61b105941f62a00073c9b8d17fd432b1a0d-other.sqlite.bz2
45334bca3ac8e67670079121ec4c65172e9ef5b8339e57dc81356e77a9705992-filelists.sqlite.bz2
4af1fba0c1d6175b7e3c862b4bddfef93fffb84c37f7d5f18cfbff08abc47f8a-comps.xml.gz
521f322f05f9802f2438d8bb7d97558c64ff3ff74c03322d77787ade9152d8bb-comps.xml
5395c62d22fd7fc07b1730de01d2ebcb93c5534b7c0e388294156666fa3a8062-primary.sqlite.bz2
9911d0091adceaeaaebf586a4124a148e4942f172ac07234bda2c81378d112e4-primary.xml.gz
9ee2831230f18fc5e01a34f8fcec44a5b1264a751aca70f6507d632906322bcc-other.xml.gz
9f5deebd7b239a11736d2115e161d1e4f1eb645bc5bfcd9e09b21401f36e4a7d-filelists.xml.gz
repomd.xml

2.3 ks文件与启动项修改

准备Kickstart文件,复制一份模板过来,并根据需要自定义修改,这边就不细说。

[root@server customISO]# cp ~/anaconda-ks.cfg ks.cfg
[root@server customISO]# ls
CentOS_BuildTag  EULA  images    ks.cfg  Packages  RPM-GPG-KEY-CentOS-7          TRANS.TBL
EFI              GPL   isolinux  LiveOS  repodata  RPM-GPG-KEY-CentOS-Testing-7

修改启动项文件,在其中添加单独的一个启动项,设置为默认,并指明ks文件路径。

[root@server customISO]# vi isolinux/isolinux.cfg 
...

label linux-ks
  menu label ^Kickstart Install CentOS 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/ks.cfg quiet

2.4 制作ISO文件

[root@server customISO]# mkisofs -o /tmp/CentOS-7-x86_64-demo.iso -b isolinux/isolinux.bin -c isolinux/boot.cat --no-emul-boot --boot-load-size 4 --boot-info-table -J -R -V "CentOS 7 x86_64" .

...
 99.69% done, estimate finish Sat Feb 25 22:51:15 2023
 99.79% done, estimate finish Sat Feb 25 22:51:16 2023
 99.88% done, estimate finish Sat Feb 25 22:51:16 2023
 99.97% done, estimate finish Sat Feb 25 22:51:16 2023
Total translation table size: 2048
Total rockridge attributes bytes: 1127937
Total directory bytes: 1689600
Path table size(bytes): 140
Max brk space used 9d2000
5426654 extents written (10598 MB)

# 若是将ISO文件刻录到U盘中,则需要进行转换
[root@server customISO]# isohybrid /tmp/CentOS-7-x86_64-demo.iso 
isohybrid: Warning: more than 1024 cylinders: 10599
isohybrid: Not all BIOSes will be able to boot this device

三、制作USB启动盘(刻录U盘)

3.1 插入U盘

将USB插入到物理机中,并确保虚机能识别到该设备。
【Linux】Linux环境下制作ISO镜像与刻录U盘_第1张图片

3.2 格式化U盘分区

[root@server ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda               8:0    0   40G  0 disk  
|-sda1            8:1    0    1G  0 part  /boot
`-sda2            8:2    0   39G  0 part  
  |-centos-root 253:0    0   37G  0 lvm   /
  `-centos-swap 253:1    0    2G  0 lvm   [SWAP]
...
...
sde               8:64   1 57.8G  0 disk  
`-sde4            8:68   1 57.8G  0 part  
sr0              11:0    1 10.3G  0 rom   
[root@server ~]# mkfs.vfat -I /dev/sde
mkfs.fat 3.0.20 (12 Jun 2013)
[root@server ~]# 

3.3 写入镜像

[root@server ~]# dd if=/tmp/CentOS-7-x86_64-demo.iso of=/dev/sde

注意,此时是看不了进度的。
可另开一个窗口,输入下面命令

[root@server ~]# kill -USR1 `pidof dd`

此时原窗口就会显示:

[root@server customISO]# dd if=/tmp/CentOS-7-x86_64-demo.iso of=/dev/sde
记录了728305+0 的读入
记录了728305+0 的写出
372892160字节(373 MB)已复制,1137.09 秒,328 kB/秒

由于虚机和U盘之间传输速率较慢,这个写入时间会比较长,我这边5GB大小镜像,刻录了近5h。

四、总结

U盘刻录完成后,便可以将其拿来在虚拟机上进行安装测试一番,至于如何以U盘作为启动盘来安装系统,这边可看一下【Linux】使用U盘自动化安装Linux(VMware虚拟机)


你可能感兴趣的:(Linux,#,Kickstart,linux)