Linux 启动顺序

第一步启动:BIOS/UEFI

BIOS (Basic Input Output System) 即基本输入输出系统。

UEFI (Unified Extensible Firmware Interface) 即可扩展固件接口。

它们的主要作用是为计算机提供直接、底层的硬件控制和设置。UEFT 是 BIOS 的升级替代方案。

第二步启动:MBR/GPT

MBR (Master Boot Record) 即主引导记录。MBR 通常和 BIOS 搭配,最大分区容量不能超过 2T,最多可以有 3 个主分区,1 个扩展分区,不过扩展分区里可以有多个逻辑分区。

GPT (GUID Partition Table) 即全局唯一标识分区表。GPT 通常和 UEFI 搭配。

它们都是读取硬盘时最先读取的分区,里面有各自的启动代码。

第三步启动:GRUB2

GRUB2 (GRand Unified Bootloader version 2) 即多操作系统启动程序,可以选择系统分区上不同的系统内核,同时也可以向启动的内核传递参数。

第四步启动:initrd/Kernel

initrd (init ramdisk) 即初始化内存盘,作用是:1. 提供开机必备的单 kernel 文件 (vmlinuz) 没有提供的驱动模块。2. 通过引导加载程序加载内存,将内存视为临时根目录,从中运行程序,之后再将根目录转移到硬盘上真正的根目录上。

第五步启动:systemdprocess

systemdprocess,即启动各类程序和进程。

[命令] Linux 命令 uname (显示系统信息)

内容一:uname 命令的格式

# uname <option>

内容二:uname 命令的选项

1) -a 或者 –all,显示所有系统信息

(补充:如果系统处理器类型和系统硬件平台处于未知状态则不显示)

2) -s 或者 –kernel-name,显示系统内核
3) -n 或者 –nodename,显示系统节点名称
4) -r 或者 –kernel-release,显示系统内核发布版本
5) -m 或者 –machine,显示系统硬件名称
6) -p 或者 –processor,显示系统处理器类型
7) -i 或者 –hardware-platform,显示系统硬件平台
8) –help,显示帮助信息
9) –version,显示 uname 命令版本

[内容] Linux 生命周期

RHEL

English:

https://access.redhat.com/support/policy/updates/errata/#Extended_Life_Cycle_Phase

Chinese:

https://access.redhat.com/zh_CN/support/policy/updates/errata#Extended_Life_Cycle_Phase

openSUSE

English:

https://en.opensuse.org/Lifetime

Chinese:

https://zh.opensuse.org/%E4%BD%BF%E7%94%A8%E6%9C%9F%E9%99%90

SLE

https://www.suse.com/lifecycle

[步骤] PXE 新系统模板的添加

步骤一:准备安装镜像

1.1 从官网上下载安装镜像

(步骤略)

1.2 挂载安装镜像

1.2.1 创建用于挂载安装镜像的目录
# mkdir <directory for mounting the image>
1.2.2 挂载安装镜像
# mount -t iso9660 <image> <directory for mounting the image>

步骤二:准备用于进行 PXE 安装的数据

2.1 准备系统安装数据

2.1.1 创建用于存放系统安装数据的目录
# mkdir <directory of data for installing the system>

(注意:用于存放系统安装数据的目录必须要放在能够实现 PXE 安装时网络共享的目录里(例如:通过 httpd 服务进行网络共享))

2.1.2 拷贝安装镜像里的数据到用于存放系统安装数据的目录
2.1.2.1 拷贝安装镜像里的普通数据到用于存放系统安装数据的目录
# cp -rp <directory for mounting the image>/* <directory of data for installing the system>
2.1.2.2 拷贝安装镜像里的 .treeinfo 文件到用于存放系统安装数据的目录
# cp -rp <directory for mounting the image>/.treeinfo <directory of data for installing the system>

2.2 准备安装引导文件

2.2.1 创建用于存放安装引导文件的目录
2.2.1.1 创建用于存放 BIOS 安装引导文件的目录
# mkdir <directory of BIOS boot file for installing the system>

(注意:用于存放 BIOS 安装引导文件的目录必须要放在能够实现 TFPT 网络共享的目录里)

2.2.1.2 创建用于存放 EFI 安装引导文件的目录
# mkdir <directory of EFI boot file for installing the system>

(注意:用于存放 EFI 安装引导文件的目录必须要放在能够实现 TFPT 网络共享的目录里)

2.2.2 拷贝安装镜像里的安装引导文件到存放安装引导文件的目录
2.2.2.1 拷贝安装镜像里的 BIOS 安装引导文件到存放 BIOS 安装引导文件的目录

如果是 Rocky Linux & RHEL 则拷贝 initrd.img 文件、TRANS.TBL 文件和 vmlinuz 文件:

# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/initrd.img -O <directory of BIOS boot file for installing the system>/initrd.img
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/TRANS.TBL -O <directory of BIOS boot file for installing the system>/TRANS.TBL
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/vmlinuz -O <directory of BIOS boot file for installing the system>/vmlinuz

如果是 openSUSE & SLE 则拷贝 linux 文件和 initrd 文件:

# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/boot/x86_64/loader/linux -O <directory of BIOS boot file for installing the system>/linux
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/boot/x86_64/loader/initrd -O <directory of BIOS boot file for installing the system>/initrd
2.2.2.2 拷贝安装镜像里的 EFI 安装引导文件到存放 EFI 安装引导文件的目录

如果是 Rocky Linux & RHEL 则拷贝 initrd.img 文件、TRANS.TBL 文件和 vmlinuz 文件:

# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/initrd.img -O <directory of EFI boot file for installing the system>/initrd.img
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/TRANS.TBL -O <directory of EFI boot file for installing the system>/TRANS.TBL
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/images/pxeboot/vmlinuz -O <directory of EFI boot file for installing the system>/vmlinuz

如果是 openSUSE & SLE 则拷贝 linux 文件和 initrd 文件:

# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/boot/x86_64/loader/linux -O <directory of EFI boot file for installing the system>/linux
# curl <The URL of the network share when PXE installing>/<directory of data for installing the system>/boot/x86_64/loader/initrd -O <directory of EFI boot file for installing the system>/initrd

2.3 准备系统安装配置文件

2.3.1 进入到用于存放系统安装配置文件的目录
# cd <directory of profile for installing the system>

(注意:进入到用于存放系统安装配置文件的目录必须要放在能够实现 PXE 安装时网络共享的目录里(例如:通过 httpd 服务进行网络共享))

2.3.2 创建系统安装配置文件
2.3.2.1 创建 BIOS 系统安装配置文件

如果是 Rocky Linux & RHEL 的话

# vim <BIOS system installation profile>

(步骤略)


补充:
1) 如果是 Rocky Linux & RHEL 的话系统安装配置文件是 CFG 文件,文件名最好以 .cfg 后缀结尾
2) 如果是 openSUSE & SLE 的话系统安装配置文件是 XML 文件,文件名最好以 .xml 后缀结尾

2.3.2.2 创建 EFI 系统安装配置文件
# vim <EFI system installation profile>

(步骤略)


补充:
1) 如果是 Rocky Linux & RHEL 的话系统安装配置文件是 CFG 文件,文件名最好以 .cfg 后缀结尾
2) 如果是 openSUSE & SLE 的话系统安装配置文件是 XML 文件,文件名最好以 .xml 后缀结尾

2.3.3 设置系统安装配置文件的权限
2.3.3.1 设置 BIOS 系统安装配置文件的权限
# chmod 755 <BIOS system installation profile>
2.3.3.2 设置 EFI 系统安装配置文件的权限
# chmod 755 <EFI system installation profile>

2.4 修改系统安装菜单文件 pxelinux.cfg

2.4.1 修改 BIOS 系统安装菜单文件 pxelinux.cfg
# vim <directory of file for BIOS system installation menu>/pxelinux.cfg

如果是 Rocky Linux & RHEL 的话,添加以下内容:

......
label Rocky Linux or RHEL
  menu label ^Installation Rocky Linux or RHEL
  kernel <relative directory of pxelinux.cfg of BIOS boot file for installing the system>/vmlinuz
  append initrd=/<relative directory of pxelinux.cfg of BIOS boot file for installing the system>/initrd.img ks=<The URL of the network share when PXE installing>/<BIOS system installation profile>

(注意:这里的 vmlinuz 文件和 initrd.im 文件的位置要写 pxelinux.cfg 文件的相对路径)

如果是 openSUSE & SLE 的话,添加以下内容:

......
label openSUSE or SLE
  menu label ^Installation openSUSE or SLE
  kernel <relative directory of pxelinux.cfg of BIOS boot file for installing the system>/linux
  append initrd=<relative directory of pxelinux.cfg of BIOS boot file for installing the system>/initrd splash=silent showopts install=<The URL of the network share when PXE installing>/<directory of data for installing the system>/ autoyast=<The URL of the network share when PXE installing>/<BIOS system installation profile>

(注意:这里的 linux 文件和 initrd 文件的位置要写 pxelinux.cfg 文件的相对路径)

(注意:用于存放 BIOS 系统安装菜单文件的目录必须要放在能够实现 TFPT 网络共享的目录里)

2.4.2 修改 EFI 系统安装菜单文件 grub.cfg
# vim <directory of file for EFI system installation menu>/grub.cfg

如果是 Rocky Linux & RHEL 的话,添加以下内容:

......
label Rocky Linux or RHEL
  menu label ^Installation Rocky Linux or RHEL
  kernel <relative directory of pxelinux.cfg of EFI boot file for installing the system>/vmlinuz
  append initrd=/<relative directory of pxelinux.cfg of EFI boot file for installing the system>/initrd.img ks=<The URL of the network share when PXE installing>/<EFI system installation profile>

(注意:这里的 vmlinuz 文件和 initrd.im 文件的位置要写 pxelinux.cfg 文件的相对路径)

如果是 openSUSE & SLE 的话,添加以下内容:

......
label openSUSE or SLE
  menu label ^Installation openSUSE or SLE
  kernel <relative directory of pxelinux.cfg of EFI boot file for installing the system>/linux
  append initrd=<relative directory of pxelinux.cfg of EFI boot file for installing the system>/initrd splash=silent showopts install=<The URL of the network share when PXE installing>/<directory of data for installing the system>/ autoyast=<The URL of the network share when PXE installing>/<EFI system installation profile>

(注意:这里的 linux 文件和 initrd 文件的位置要写 pxelinux.cfg 文件的相对路径)

(注意:用于存放 EFI 系统安装菜单文件的目录必须要放在能够实现 TFPT 网络共享的目录里)

步骤三:取消挂载安装镜像

# umount <directory for mounting the image>

[步骤] KVM 虚拟机模板的创建 (Rocky Linux 8 版)

注意:

在创建 KVM 虚拟机之前要先安装 KVM 并创建 KVM 虚拟网络

软件准备:

在 Rocky Linux 官网上下载安装系统所需要的镜像:

https://rockylinux.org/download

正文:

步骤一:理解创建 KVM 虚拟机模板的目的

主要用于批量克隆出新的 KVM 机器,节约创建新虚拟机的时间

步骤二:为这个虚拟机创建硬盘文件

2.1 创建硬盘文件

(只在真机上执行以下步骤)

# qemu-img create -f qcow2 /var/lib/libvirt/images/rockylinux8.qcow2 10G

(补充:这里以创建 10G 大小的 rockylinux8.qcow2 硬盘文件为例)

2.2 确认硬盘文件已创建

(只在真机上执行以下步骤)

# ls /var/lib/libvirt/images/ | grep rockylinux8.qcow2

(补充:这里以显示 rockylinux8.qcow2 硬盘文件为例)

步骤三:使用 KVM 和刚刚创建的硬盘文件新安装一台虚拟机

3.1 启动 KVM 的 virt-manager

(只在真机上执行以下步骤)

# virt-manager

3.2 在 virt-manager 上的左上角点击文件之后点击 “新建虚拟机”

(只在真机上执行以下步骤)

(步骤略)

3.2.1 选择以本地安装介质的方式安装系统

(只在真机上执行以下步骤)

(图:1)
3.2.2 选择安装系统的系统镜像

(只在真机上执行以下步骤)

(图:2)

(补充:这里以使用 Rocky-8.5-x86_6-dvd1.iso 系统镜像为例)

3.2.3 设置内存大小和 CPU 核心数

(只在真机上执行以下步骤)

(图:3)

(补充:这里以设置 2048 MiB 内容和 2 核 CPU 为例)

3.2.4 选择用刚刚创建的硬盘文件来安装系统

(只在真机上执行以下步骤)

(图:4)

(补充:这里以使用 rockylinux8.qcow2 硬盘文件为例)

3.2.5 给虚拟机命名并选择虚拟网络

(只在真机上执行以下步骤)

(注意:虚拟网络必须提前创建好)

(图:5)

(补充:这里以将虚拟机命名为 rockylinux8 并使用 0 网络为例)

3.2.6 开始安装系统

(只在真机上执行以下步骤)

(图:6)
3.2.7 选择系统语言

(只在真机上执行以下步骤)

(图:7)
3.2.8 之后进行系统配置界面

(只在真机上执行以下步骤)

需要手动配置的地方有四个:
1) “INSTALLATION DESTINATION”
2) “KDUMP”
3) “SOFTWARE SELECTION”
4) “Root Password”
分别点击以后就可以配置了

3.2.8.1 通过 “INSTALLATION DESTINATION” 对硬盘进行分区

(只在真机上执行以下步骤)

(补充:完成后点击左上角的 “DONE”)

(注意:只分一个分区,只设置一个挂载点挂载到根,使用标准硬盘类型,硬盘格式设置为 XFS)

(图:8)
(图:9)
3.2.8.2 取消 “KDUMP”

(只在真机上执行以下步骤)

(补充:完成后点击左上角的 “DONE”)

(图:10)
3.2.8.3 选择最小化安装系统

(只在真机上执行以下步骤)

(补充:完成后点击左上角的 “DONE”)

(图:11)
3.2.8.4 设置 root 密码

(只在真机上执行以下步骤)

(图:12)
3.2.9 之后点击右下角的 “Begin installation”

(只在真机上执行以下步骤)

(图:13)
3.2.10 安装完成后重启

(只在真机上执行以下步骤)

(图:14)
3.2.11 在安装系统的过程中需要注意的内容总结

(只在真机上执行以下步骤)

1) 一定要使用刚刚创建的 /var/lib/libvirt/images/rockylinux8.qcow2 作为安装虚拟机的硬件文件
2) 虚拟机网络 “0” 要提前创建好
3) 只分一个分区,只设置一个挂载点挂载到根,使用标准硬盘,硬盘格式是 XFS
4) 取消 “KDUMP”
5) 选择最小化安装系统
6) 设置 root 密码

步骤四:进入新创建虚拟机修改配置

4.1 修改网卡个性化设置

4.1.1 修改网卡配置文件

(只在虚拟机上执行以下步骤)

# vi /etc/sysconfig/network-scripts/ifcfg-enp1s0

将全部内容修改如下:

TYPE=Ethernet
BOOTPROTO=dhcp
NAME=enp1s0
DEVICE=enp1s0
ONBOOT=yes
4.1.2 使修改的网卡配置生效

(只在虚拟机上执行以下步骤)

# reboot

4.2 禁用 SELinux

(只在虚拟机上执行以下步骤)

# vi /etc/selinux/config

将全部内容修改如下:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

4.3 禁用空路由

(只在虚拟机上执行以下步骤)

# vi /etc/sysconfig/network

将全部内容修改如下:

# Created by anaconda
NOZEROCONF="yes"

4.4 添加 Console 配置

4.4.1 修改 grub 内核配置文件

(只在虚拟机上执行以下步骤)

# vi /etc/default/grub

将全部内容修改如下:

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="serial console"
GRUB_SERIAL_COMMAND="serial --unit=1 --speed=115200"
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0 console=tty0 console=ttyS0,115200n8"
GRUB_DISABLE_LINUX_UUID="true"
GRUB_ENABLE_LINUX_LABEL="true"
GRUB_DISABLE_RECOVERY="true"
4.4.2 使修改的 grub 内核配置生效

(只在虚拟机上执行以下步骤)

# grub2-mkconfig -o grub

4.5 将系统自动挂载的硬盘从使用 UUID 换成硬件路径

4.5.1 显示根分区的 UUID

(只在虚拟机上执行以下步骤)

# blkid
/dev/sda1: UUID="e76ed189-6d0f-49d5-8586-c5aae4bdc9b5" TYPE="xfs" PARTUUID="3d8377ef-01"

(补充:这里的 UUID 是: e76ed189-6d0f-49d5-8586-c5aae4bdc9b5)

4.5.2 在自动挂载文件里将根分区的 UUID 换成硬件路径

(只在虚拟机上执行以下步骤)

# vi /etc/fstab

将以下内容:

......
UUID=e76ed189-6d0f-49d5-8586-c5aae4bdc9b5 /                   xfs     defaults        0 0

(补充:这里的 UUID 是: e76ed189-6d0f-49d5-8586-c5aae4bdc9b5)

修改为:

/dev/sda1 /                   xfs     defaults        0 0

4.6 删除不用的程序

(只在虚拟机上执行以下步骤)

# yum -y remove firewalld-* python-firewall

4.7 对虚拟系统进行升级

(只在虚拟机上执行以下步骤)

# yum -y update

4.8 进行分区扩展

4.8.1 安装分区扩展软件

(只在虚拟机上执行以下步骤)

# yum install -y cloud-utils-growpart
4.8.2 给开机自启配置文件相应的权限
# chmod 755 /etc/rc.local
4.8.3 设置开机自动扩容根目录

(只在虚拟机上执行以下步骤)

# vi /etc/rc.local

添加以下内容:

......
/usr/bin/growpart /dev/sda1
/usr/sbin/xfs_growfs /

4.9 修改虚拟机系统的名称

(只在虚拟机上执行以下步骤)

# vi /etc/hostname

将全部内容修改如下:

rockylinux8

4.10 启用 serial 服务实现通过 virsh console 命令控制虚拟机

(只在虚拟机上执行以下步骤)

# systemctl start serial-getty@ttyS0
# systemctl enable serial-getty@ttyS0

4.11 清除虚拟系统的历史命令

(只在虚拟机上执行以下步骤)

# history -c

4.12 关闭虚拟机

(只在虚拟机上执行以下步骤)

# poweroff

步骤五:在真机上对虚拟机进行清理优化

(只在真机上执行以下步骤)

# sudo virt-sysprep -d rockylinux8

(补充:这里以清理 rockylinux8 虚拟机为例)


注意:如果此命令不存在
1) Rocky Linux 系统的话需要安装 libguestfs-tools
2) openSUSE 系统的话需要安装 guestfs-tools

步骤六:此时就可以将此虚拟机的硬件文件作为模板进行批量克隆虚拟机了

(只在真机上执行以下步骤)

(步骤略)