第十二周作业

一、简述linux操作系统启动流程

centos6启动流程:

  1. 加载BIOS的硬件信息,获取第一个启动设备;
  2. 读取第一个启动设备MBR的引导加载程序(grub)的启动信息;
  3. 加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备;
  4. 核心执行init程序,并获取默认的运行信息;
  5. init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统;
  6. 启动核心的外挂模块;
  7. init执行运行的各个批处理文件(scripts);
  8. init执行/etc/rc.d/rc.local;
  9. 执行/bin/login程序,等待用户登录;
  10. 登录之后开始以Shell控制主机。

CentOS 7之后版本引导顺序

  1. UEFi或BIOS初始化,运行POST开机自检;
  2. 选择启动设备;
  3. 引导装载程序, centos7是grub2,加载装载程序的配置文件:
    /etc/grub.d/
    /etc/default/grub
    /boot/grub2/grub.cfg
  4. 加载initramfs驱动模块;
  5. 加载内核选项;
  6. 内核初始化,centos7使用systemd代替init;
  7. 执行initrd.target所有单元,包括挂载/etc/fstab;
  8. 从initramfs根文件系统切换到磁盘根目录;
  9. systemd执行默认target配置,配置文件/etc/systemd/system/default.target;
  10. systemd执行sysinit.target初始化系统及basic.target准备操作系统;
  11. systemd启动multi-user.target下的本机与服务器服务;
  12. systemd执行multi-user.target下的/etc/rc.d/rc.local;
  13. Systemd执行multi-user.target下的getty.target及登录服务;
  14. systemd执行graphical需要的服务。

二、制作一个只运行shell的linux

1、分区并创建文件系统,分两个必要的分区,/dev/sdb1对应/boot /dev/sdb2对应根 /

[root@centos6 ~]#echo -e 'n\np\n1\n\n+1G\nw\n' | fdisk /dev/sdb
[root@centos6 ~]#echo -e 'n\np\n2\n\n\n\nw\n' | fdisk /dev/sdb
[root@centos6 ~]#mkfs.ext4 /dev/sdb1
[root@centos6 ~]#mkfs.ext4 /dev/sdb2

2、挂载boot,子目录必须为boot

[root@centos6 ~]#mkdir /mnt/boot
[root@centos6 ~]#mount /dev/sdb1 /mnt/boot

3、安装grub

[root@centos6 ~]#grub-install --root-directory=/mnt/ /dev/sdb

4、准备内核和initramfs文件

[root@centos6 ~]#cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/vmlinuz
[root@centos6 ~]#cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot/initramfs.img

5、建立grub.conf

[root@centos6 ~]#cat /mnt/boot/grub/grub.conf
default=0
timeout=6
title wang linux
root (hd0,0)
kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
[root@centos6 ~]#tree /mnt/boot
/mnt/boot
├── grub
│ ├── device.map
│ ├── e2fs_stage1_5
│ ├── fat_stage1_5
│ ├── ffs_stage1_5
│ ├── grub.conf
│ ├── iso9660_stage1_5
│ ├── jfs_stage1_5
│ ├── minix_stage1_5
│ ├── reiserfs_stage1_5
│ ├── stage1
│ ├── stage2
│ ├── ufs2_stage1_5
│ ├── vstafs_stage1_5
│ └── xfs_stage1_5
├── initramfs.img
├── lost+found
└── vmlinuz
2 directories, 16 files

6、准备根下面相关程序和库

[root@centos6 ~]#mkdir /mnt/sysroot
[root@centos6 ~]#mount /dev/sdb2 /mnt/sysroot
[root@centos6 ~]#mkdir –pv
/mnt/sysroot/{boot,dev,sys,proc,etc,bin,sbin,tmp,var,usr,,home,root,lib,lib64,op
tmnt,media}
#busybox:bin,dev,etc,home,proc,root,sys,tmp,usr,var

复制bash等命令和相关库文件,如:

bash,ps,pstree,ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid,tree,fdisk
[root@centos6 ~]#mkdir /mnt/sysroot/{dev,proc,etc,sys,lib,home,root}

准备网卡驱动

[root@centos6 ~]#ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version:
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@centos6 ~]#modinfo -n e1000
/lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
[root@centos6 ~]#cp /lib/modules/2.6.32-
754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
[root@centos6 ~]#chroot /mnt/sysroot

3、总结systemctl管理命令及system unit文件格式

systemd 特性:

Systemd:从 CentOS 7 版本之后开始用 systemd 实现init进程,系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程。
1.系统引导时实现服务并行启动
2.按需启动守护进程
3.自动化的服务依赖关系管理
4.同时采用socket式与D-Bus总线式激活服务
5.socket与服务程序分离
6.向后兼容sysv init脚本
7.使用systemctl 命令管理,systemctl命令固定不变,不可扩展,非由systemd启动的服务,
8.systemctl无法与之通信和控制
9.系统状态快照

system unit文件格式:

systemd核心概念:unit,unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听
查看unit类型

[root@centos8 ~]#systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope
socket、保存的系统快照以及其它与init相关的信息

service unit: 文件扩展名为.service, 用于定义系统服务
Socket unit: .socket, 定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现
按需启动
Target unit: 文件扩展名为.target,用于模拟实现运行级别
Device unit: .device, 用于定义内核识别的设备
Mount unit: .mount, 定义文件系统挂载点
Snapshot unit: .snapshot, 管理系统快照
Swap unit: .swap, 用于标识swap设备
Automount unit: .automount,文件系统的自动挂载点
Path unit: .path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激
活服务,如:spool 目录

unit的配置文件

/usr/lib/systemd/system #每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/lib/systemd/system #ubutun的对应目录
/run/systemd/system #系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system #管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目
录优先运行

4、破解centos7 密码

方法一:

启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux 开始的行,添加内核参数rd.break
按ctrl-x启动
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
#如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
exit
reboot

方法二:

启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux 开始的行,改为rw init=/sysroot/bin/sh
按ctrl-x启动
chroot /sysroot
passwd root
#如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
exit
reboot
posted @ 2021-07-20 10:13  开心学习0806  阅读(28)  评论(0编辑  收藏  举报