第五周

1、破坏mbr表并修复

1.1 备份

1.1.1 使用dd工具备份MBR
[root@centos7 ~]# dd if=/dev/sda of=/data/dpt.img bs=1 count=64 skip=446
1.1.2 将备份文件保存在另一台主机
[root@centos7 ~]# scp /data/dpt.img  192.168.31.185:/data/dpt.img

1.2 破坏:索引部分任意地方破坏都启动不了,前面因为只备份446开始的64字节

[root@centos7 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446

注:skip是跳过if中的前多少,seek是跳过of中的前多少

1.3 救援

1.3.1 reboot
1.3.2 按ESC进入启动菜单,选择3进入光盘启动

image.png

1.3.3 然后选择Troubleshooting

image.png

1.3.4 再选择Rescue a CentOS system

image.png

1.3.5 然后选择3,进入shell模式

image.png

1.3.6 配置IP地址
ip addr add 192.168.31.175/24 dev ens33

image.png

1.3.7 拷贝备份文件到当前目录
scp 192.168.31.185:/data/dpt.img .

image.png

1.3.8 使用dd工具恢复MBR分区
dd if=dpt.img of=/dev/sda bs=1 seek=446

2、总结RAID的各个级别及其组合方式和性能的不同。

image.png


3、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项。

3.1 查看块设备信息

[root@centos7 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
└─sda3   8:3    0   50G  0 part /data
sdb      8:16   0   50G  0 disk
sr0     11:0    1 1024M  0 rom

3.2 在sdb磁盘上创建大小为2G的分区

[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xf2dabdc7.

Command (m for help): n
Partition type:
  p   primary (0 primary, 0 extended, 4 free)
  e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

3.3 创建文件系统

[root@centos7 ~]# mkfs.ext4 -b 2048 -m 1 -L TEST /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=TEST
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 1048576 blocks
10485 blocks (1.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=269484032
64 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
    	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

3.4 查看UUID信息

[root@centos7 ~]# blkid
/dev/sdb1: LABEL="TEST" UUID="12da7d7c-5813-4e19-b234-49dca7c60cf0" TYPE="ext4"

3.5 将分区挂载到指定文件夹,在/etc/fstab文件中添加以下内容

UUID=12da7d7c-5813-4e19-b234-49dca7c60cf0 /test    ext4    acl    0 0

3.6 创建目标文件夹,使配置文件生效

[root@centos7 ~]# mkdir /test
[root@centos7 ~]# mount -a

3.7 查看最后结果

[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        900M     0  900M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M  9.6M  901M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda2       100G  1.8G   99G   2% /
/dev/sda3        50G   33M   50G   1% /data
/dev/sda1      1014M  142M  873M  14% /boot
tmpfs           182M     0  182M   0% /run/user/0
/dev/sdb1       2.0G  9.1M  1.9G   1% /test

4、创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录。

4.1 查看块设备信息

[root@centos7 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
└─sda3   8:3    0   50G  0 part /data
sdb      8:16   0   50G  0 disk
└─sdb1   8:17   0    2G  0 part /test
sr0     11:0    1 1024M  0 rom

4.2 创建分区

4.2.1 在sda上创建大小为5G的分区
[root@centos7 ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
 p   primary (3 primary, 0 extended, 1 free)
 e   extended
Select (default e):
Using default response e
Selected partition 4
First sector (316672000-419430399, default 316672000):
Using default value 316672000
Last sector, +sectors or +size{K,M,G} (316672000-419430399, default 419430399):
Using default value 419430399
Partition 4 of type Extended and of size 49 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (316674048-419430399, default 316674048):
Using default value 316674048
Last sector, +sectors or +size{K,M,G} (316674048-419430399, default 419430399): +5G
Partition 5 of type Linux and of size 5 GiB is set

Command (m for help): t
Partition number (1-5, default 5):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009d5d8

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   211814399   104857600   83  Linux
/dev/sda3       211814400   316671999    52428800   83  Linux
/dev/sda4       316672000   419430399    51379200    5  Extended
/dev/sda5       316674048   327159807     5242880   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
4.2.2 在sdb上创建大小为15G的分区
[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
  p   primary (1 primary, 0 extended, 3 free)
  e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (4196352-104857599, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-104857599, default 104857599): +15G
Partition 2 of type Linux and of size 15 GiB is set

Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xf2dabdc7

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    35653631    15728640   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos7 ~]# partprobe
[root@centos7 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part
└─sda5   8:5    0    5G  0 part
sdb      8:16   0   50G  0 disk
├─sdb1   8:17   0    2G  0 part /test
└─sdb2   8:18   0   15G  0 part
sr0     11:0    1 1024M  0 rom

4.3 创建物理卷(pv)

4.3.1 创建物理卷
[root@centos7 ~]# pvcreate /dev/sda5 /dev/sdb2
Physical volume "/dev/sda5" successfully created.
Physical volume "/dev/sdb2" successfully created.
4.3.2 查看物理卷
[root@centos7 ~]# pvs
 PV         VG Fmt  Attr PSize  PFree
 /dev/sda5     lvm2 ---   5.00g  5.00g
 /dev/sdb2     lvm2 ---  15.00g 15.00g
[root@centos7 ~]# pvdisplay
 "/dev/sdb2" is a new physical volume of "15.00 GiB"
  --- NEW Physical volume ---
 PV Name               /dev/sdb2
 VG Name
PV Size               15.00 GiB
 Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
 PV UUID               6IBD3k-oHa8-ddBq-ugYj-bQxf-vcw5-d1tpbn

 "/dev/sda5" is a new physical volume of "5.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sda5
 VG Name
 PV Size               5.00 GiB
 Allocatable           NO
 PE Size               0
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               yDwMO0-LRLy-BJlR-Ol8U-9u6g-J6Gk-bCRrNU

4.4 创建卷组(vg)

4.4.1 创建卷组
[root@centos7 ~]# vgcreate -s 16M testvg /dev/sda5 /dev/sdb2
Volume group "testvg" successfully created
4.4.2 查看卷组
[root@centos7 ~]# vgs
VG     #PV #LV #SN Attr   VSize   VFree
testvg   2   0   0 wz--n- <19.97g <19.97g
[root@centos7 ~]# vgdisplay
--- Volume group ---
 VG Name               testvg
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                2
Act PV                2
 VG Size               <19.97 GiB
PE Size               16.00 MiB
Total PE              1278
Alloc PE / Size       0 / 0
Free  PE / Size       1278 / <19.97 GiB
VG UUID               VZp5Sl-XPww-sNHs-CiKV-nsjx-EVCR-Rebd1A

4.5 创建逻辑卷(lv)

4.5.1 创建逻辑卷
[root@centos7 ~]# lvcreate -n testlv -L 5G testvg
Logical volume "testlv" created.
4.5.2 查看逻辑卷
[root@centos7 ~]# lvs
LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
testlv testvg -wi-a----- 5.00g
[root@centos7 ~]# lvdisplay
 --- Logical volume ---
 LV Path                /dev/testvg/testlv
 LV Name                testlv
 VG Name                testvg
 LV UUID                WjHFtk-kXet-VA8y-SAW5-pV63-FvuN-Hvf4IF
 LV Write Access        read/write
 LV Creation host, time centos7, 2021-07-11 16:12:32 +0800
 LV Status              available
 # open                 0
 LV Size                5.00 GiB
 Current LE             320
 Segments               1
 Allocation             inherit
  Read ahead sectors     auto
 - currently set to     8192
 Block device           253:0

4.6 创建文件系统

[root@centos7 ~]# mkfs.xfs /dev/testvg/testlv
meta-data=/dev/testvg/testlv     isize=512    agcount=4, agsize=327680 blks
   	 =                       sectsz=512   attr=2, projid32bit=1
     	 =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
     	 =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
     	 =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

4.7 创建挂载点

4.7.1 创建挂载目录
[root@centos7 ~]# mkdir /users
4.7.2 查看UUID
[root@centos7 ~]# blkid
/dev/mapper/testvg-testlv: UUID="ebe260a4-5f2e-43e9-a96c-4d94610c0281" TYPE="xfs"
4.7.3 在/etc/fstab文件中添加以下内容
UUID=ebe260a4-5f2e-43e9-a96c-4d94610c0281 /users   xfs     defaults        0 0
4.7.4 使配置文件生效
[root@centos7 ~]# mount -a

4.8 查看最终结果

[root@centos7 ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   900M     0  900M   0% /dev
tmpfs                      910M     0  910M   0% /dev/shm
tmpfs                      910M  9.6M  901M   2% /run
tmpfs                      910M     0  910M   0% /sys/fs/cgroup
/dev/sda2                  100G  1.8G   99G   2% /
/dev/sda3                   50G   33M   50G   1% /data
/dev/sda1                 1014M  142M  873M  14% /boot
tmpfs                      182M     0  182M   0% /run/user/0
/dev/sdb1                  2.0G  9.1M  1.9G   1% /test
/dev/mapper/testvg-testlv  5.0G   33M  5.0G   1% /users

5、简述OSI七层模型和TCP/IP五层模型

image.png