第三周作业

1、作业内容(略)

2、作业解答

2.1 图文并茂说明raid0,raid1, raid10, raid01, raid5等原理

**1、RAID0 工作原理**

RAID0 特点总结:

(1)最少需要2块磁盘组成;

(2)数据条带化分散存储到所有磁盘中;

(3)不需要进行数据镜像备份和数据校验运行,存储性能最佳;

(4)无数据冗余策略,磁盘组任一磁盘失效会导致整个逻辑磁盘的数据无法正常访问,数据安全保护能力不高。

(5)磁盘空间利用率:100%

**2、RAID1 工作原理**

 RAID1 特点总结:

(1)最少需要2块磁盘组成;

(2)通过磁盘数据镜像实现数据冗余,在成对的独立磁盘上产生互为备份的数据;

(3)提供了很高的数据安全性和可用性,当一个磁盘失效时,系统可以自动切换到镜像磁盘上读写,而不需要重组失效的数据;

(4)磁盘空间利用率:50%

**3、RAID10 工作原理**

  RAID10 特点总结:

(1)最少需要4块磁盘;

(2)先创建2个独立的RAID1,再把两个独立的RAID1组成一个RAID0;

(3)既提供数据镜像存储能力,又提供数据条带化分布,性能和安全均最优。

(4)磁盘空间利用率:50%

**4、RAID01 工作原理**

 RAID01 特点总结:

(1)最少需要4块磁盘;

(2)先创建2个独立的RAID0,再把两个独立的RAID0组成一个RAID1;

(3)任意一块磁盘故障将导致一组RAID0失效,安全性不如RAID10;

(4)磁盘空间利用率:50%

**5、RAID5 工作原理**

 RAID5 特点总结:

(1)最少需要3块磁盘;

(2)把业务数据和相对应的奇偶校验信息存储到组成RAID5的各个磁盘上,并且奇偶校验信息和相对应的数据分别存储于不同的磁盘上;

(3)当一个数据盘故障时,系统可以根据同一条带的其他数据块和对应的校验数据来重建损坏的数据;

(4)磁盘空间利用率:(N-1)/N

 

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

复制代码
#系统中有2块10G的磁盘,/dev/sdb和/dev/sdc
[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  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
sdc               8:32   0   10G  0 disk 
sr0              11:0    1  4.4G  0 rom 

#创建PV
[root@centos7 ~]#pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@centos7 ~]#pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
[root@centos7 ~]#

#创建testvg,指定PE大小为16M
[root@centos7 ~]#vgcreate -s 16M testvg /dev/sdb /dev/sdc
  Volume group "testvg" successfully created
[root@centos7 ~]#vgdisplay testvg
  --- 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               5f6y0U-uUNv-03dN-0Lkd-rs9C-NTMu-abt0jr
   
#创建逻辑卷testlv,大小为5G
[root@centos7 ~]#lvcreate -L 5G -n testlv testvg
  Logical volume "testlv" created.
[root@centos7 ~]#lvdisplay /dev/testvg/testlv 
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                Pp800W-Rjyv-3PTM-N2uF-SFIO-Jm9Q-0PbyH1
  LV Write Access        read/write
  LV Creation host, time centos7, 2022-11-30 23:32:16 +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:3

#格式化和挂载逻辑卷testlv,并挂载到/users目录
[root@centos7 ~]#mkfs.ext4 /dev/testvg/testlv 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

[root@centos7 ~]#mkdir /users
[root@centos7 ~]#mount /dev/testvg/testlv /users
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
/dev/mapper/testvg-testlv  4.8G   20M  4.6G   1% /users

#将挂载项写入/etc/fstab
[root@centos7 ~]#echo "/dev/mapper/testvg-testlv /users   ext4     defaults  0 0" >> /etc/fstab
复制代码

 

2.3 新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d/目录至自己的家目录。

复制代码
[root@centos7 ~]#useradd -m -d /users/archlinux archlinux
[root@centos7 ~]#su - archlinux
Last login: Wed Nov 30 23:40:42 CST 2022 on pts/0
[archlinux@centos7 ~]$pwd
/users/archlinux
[archlinux@centos7 ~]$cp -ra /etc/pam.d/ .
[archlinux@centos7 ~]$ls -la
total 36
drwx------ 5 archlinux archlinux 4096 Nov 30 23:41 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux   33 Nov 30 23:40 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config
drwxr-xr-x 2 archlinux archlinux 4096 Nov 16 23:45 pam.d
复制代码

 

2.4 扩展testlv至7G,要求archlinux用户的文件不能丢失

复制代码
#逻辑卷testlv从5G扩容至7G
[root@centos7 ~]#lvextend -L +2G /dev/testvg/testlv
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
  Logical volume testvg/testlv successfully resized.
[root@centos7 ~]#lvdisplay /dev/testvg/testlv 
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                Pp800W-Rjyv-3PTM-N2uF-SFIO-Jm9Q-0PbyH1
  LV Write Access        read/write
  LV Creation host, time centos7, 2022-11-30 23:32:16 +0800
  LV Status              available
  # open                 1
  LV Size                7.00 GiB
  Current LE             448
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
  
#扩容文件系统
[root@centos7 ~]#resize2fs /dev/testvg/testlv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.

[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/mapper/testvg-testlv  6.8G   23M  6.4G   1% /users
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0

#验证archlinux用户下的文件是否丢失
[root@centos7 users]#su - archlinux 
Last login: Thu Dec  1 14:24:18 CST 2022 on pts/0
[archlinux@centos7 ~]$ls -la
total 36
drwx------ 5 archlinux archlinux 4096 Nov 30 23:41 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux  100 Dec  1 14:24 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config
drwxr-xr-x 2 archlinux archlinux 4096 Nov 16 23:45 pam.d
复制代码

 

2.5 收缩testlv至3G,要求archlinux用户的文件不能丢失

复制代码
#卸载文件系统
[root@centos7 ~]#umount /dev/testvg/testlv
[root@centos7 ~]#df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.6M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  1.7G   49G   4% /
/dev/sda1               1014M  141M  874M  14% /boot
/dev/mapper/centos-home  147G   33M  147G   1% /home
tmpfs                    199M     0  199M   0% /run/user/0

#缩减逻辑卷testlv至3G,同时缩减文件系统
[root@centos7 ~]#lvreduce -L 3G -r /dev/testvg/testlv 
fsck from util-linux 2.23.2
/dev/mapper/testvg-testlv: 56/458752 files (0.0% non-contiguous), 67366/1835008 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 786432 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 786432 blocks long.

  Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume testvg/testlv successfully resized.
[root@centos7 ~]#mount /dev/testvg/testlv /users
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users

#验证archlinux用户下的文件是否丢失
[root@centos7 ~]#su - archlinux 
Last login: Thu Dec  1 14:31:10 CST 2022 on pts/0
[archlinux@centos7 ~]$ls -la
total 36
drwx------ 5 archlinux archlinux 4096 Nov 30 23:41 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux  132 Dec  1 14:32 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config
drwxr-xr-x 2 archlinux archlinux 4096 Nov 16 23:45 pam.d
复制代码

 

2.6 对testlv创建快照,并尝试基于快照备份数据,验正快照的功能

复制代码
#对testlv创建快照
[root@centos7 ~]# lvcreate -l 192 -p r -s -n testlv-snap /dev/testvg/testlv 
  Logical volume "testlv-snap" created.
[root@centos7 ~]#lvs
  LV          VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home        centos -wi-ao---- 146.99g                                                    
  root        centos -wi-ao----  50.00g                                                    
  swap        centos -wi-ao----   2.00g                                                    
  testlv      testvg owi-aos---   3.00g                                                    
  testlv-snap testvg sri-a-s---   3.00g      testlv 0.01                                   

#模拟用户数据丢失
[root@centos7 ~]#su - archlinux 
Last login: Thu Dec  1 14:37:48 CST 2022 on pts/0
[archlinux@centos7 ~]$pwd
/users/archlinux
[archlinux@centos7 ~]$ls -la
total 36
drwx------ 5 archlinux archlinux 4096 Nov 30 23:41 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux  172 Dec  1 14:39 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config
drwxr-xr-x 2 archlinux archlinux 4096 Nov 16 23:45 pam.d
[archlinux@centos7 ~]$rm -rf pam.d
[archlinux@centos7 ~]$ls -la
total 32
drwx------ 4 archlinux archlinux 4096 Dec  1 15:08 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux  172 Dec  1 14:39 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config

#基于快照备份进行恢复
[root@centos7 ~]#umount /dev/testvg/testlv
[root@centos7 ~]#df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.6M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  1.7G   49G   4% /
/dev/sda1               1014M  141M  874M  14% /boot
/dev/mapper/centos-home  147G   33M  147G   1% /home
tmpfs                    199M     0  199M   0% /run/user/0
[root@centos7 ~]#lvconvert --merge /dev/testvg/testlv-snap 
  Merging of volume testvg/testlv-snap started.
  testvg/testlv: Merged: 100.00%
[root@centos7 ~]#mount /dev/testvg/testlv /users

#验证用户已丢失数据是否恢复
[root@centos7 ~]#su - archlinux
Last login: Thu Dec  1 15:08:37 CST 2022 on pts/0
[archlinux@centos7 ~]$ls -la
total 36
drwx------ 5 archlinux archlinux 4096 Nov 30 23:41 .
drwxr-xr-x 4 root      root      4096 Nov 30 23:40 ..
-rw------- 1 archlinux archlinux  172 Dec  1 14:39 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 archlinux archlinux  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 archlinux archlinux  231 Apr  1  2020 .bashrc
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .cache
drwxrwxr-x 3 archlinux archlinux 4096 Nov 30 23:40 .config
drwxr-xr-x 2 archlinux archlinux 4096 Nov 16 23:45 pam.d
复制代码

 

2.7 创建10G的RAID1,要求CHUNK为128K,文件系统为ext4,有一个空闲盘,开机可自动挂载至/backup目录

复制代码
#系统中有3块空闲磁盘,/dev/sd{h,i,j}
[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  199G  0 part   
  ├─centos-root 253:0    0   50G  0 lvm    /
  ├─centos-swap 253:1    0    2G  0 lvm    [SWAP]
  └─centos-home 253:3    0  147G  0 lvm    /home
sdb               8:16   0   10G  0 disk   
└─testvg-testlv 253:2    0    3G  0 lvm    /users
sdc               8:32   0   10G  0 disk   
sdd               8:48   0    5G  0 disk   
└─md0             9:0    0   10G  0 raid10 /mydata
sde               8:64   0    5G  0 disk   
└─md0             9:0    0   10G  0 raid10 /mydata
sdf               8:80   0    5G  0 disk   
└─md0             9:0    0   10G  0 raid10 /mydata
sdg               8:96   0    5G  0 disk   
└─md0             9:0    0   10G  0 raid10 /mydata
sdh               8:112  0   10G  0 disk   
sdi               8:128  0   10G  0 disk   
sdj               8:144  0   10G  0 disk   
sr0              11:0    1  4.4G  0 rom  

#创建RAID1设备
[root@centos7 ~]#mdadm -Cv /dev/md1 -a yes -n 2 -l 1 -c 128 -x 1 /dev/sd{h,i,j}
mdadm: chunk size ignored for this level
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
mdadm: size set to 10476544K
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

#查看RAID1设备信息
[root@centos7 ~]#mdadm -D /dev/md1
/dev/md1:
           Version : 1.2
     Creation Time : Thu Dec  1 15:48:29 2022
        Raid Level : raid1
        Array Size : 10476544 (9.99 GiB 10.73 GB)
     Used Dev Size : 10476544 (9.99 GiB 10.73 GB)
      Raid Devices : 2
     Total Devices : 3
       Persistence : Superblock is persistent

       Update Time : Thu Dec  1 15:48:39 2022
             State : clean, resyncing 
    Active Devices : 2
   Working Devices : 3
    Failed Devices : 0
     Spare Devices : 1

Consistency Policy : resync

     Resync Status : 19% complete

              Name : centos7:1  (local to host centos7)
              UUID : 07ff9f48:51528179:f5406611:66a774b7
            Events : 3

    Number   Major   Minor   RaidDevice State
       0       8      112        0      active sync   /dev/sdh
       1       8      128        1      active sync   /dev/sdi

       2       8      144        -      spare   /dev/sdj
       
#格式化并挂载
[root@centos7 ~]#mkfs.ext4 /dev/md1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2619136 blocks
130956 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

[root@centos7 ~]#mkdir /backup
[root@centos7 ~]#mount /dev/md1 /backup
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/md0                   9.8G   37M  9.2G   1% /mydata
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
/dev/md1                   9.8G   37M  9.2G   1% /backup

#配置开机自动挂载
[root@centos7 ~]#echo "/dev/md1 /backup   ext4 defaults   0 0" >> /etc/fstab  
[root@centos7 ~]#reboot

Last login: Thu Dec  1 15:43:53 2022 from 192.168.100.1
welcome cloud sre
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/md1                   9.8G   37M  9.2G   1% /backup
/dev/md0                   9.8G   37M  9.2G   1% /mydata
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
复制代码

 

2.8 创建一个可用空间为10G的RAID10设备,要求CHUNK为256K,文件系统为ext4,开机可自动挂载至/mydata目录

复制代码
#系统中有4块空闲磁盘,/dev/sd{d,e,f,g}
[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  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:3    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─testvg-testlv 253:2    0    3G  0 lvm  /users
sdc               8:32   0   10G  0 disk 
sdd               8:48   0    5G  0 disk 
sde               8:64   0    5G  0 disk 
sdf               8:80   0    5G  0 disk 
sdg               8:96   0    5G  0 disk 
sr0              11:0    1  4.4G  0 rom 

#创建RAID10设备
[root@centos7 ~]#mdadm -Cv /dev/md0 -a yes -n 4 -l 10 -c 256 /dev/sd{d,e,f,g}
mdadm: layout defaults to n2
mdadm: layout defaults to n2
mdadm: size set to 5237760K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

#格式化并挂载
[root@centos7 ~]#mkfs.ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
655360 inodes, 2618880 blocks
130944 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

[root@centos7 ~]#mkdir /mydata
[root@centos7 ~]#mount /dev/md0 /mydata
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
/dev/md0                   9.8G   37M  9.2G   1% /mydata

#查看RAID10设备信息
[root@centos7 ~]#mdadm -D /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Thu Dec  1 15:32:07 2022
        Raid Level : raid10
        Array Size : 10475520 (9.99 GiB 10.73 GB)
     Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
      Raid Devices : 4
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Thu Dec  1 15:37:24 2022
             State : clean 
    Active Devices : 4
   Working Devices : 4
    Failed Devices : 0
     Spare Devices : 0

            Layout : near=2
        Chunk Size : 256K

Consistency Policy : resync

              Name : centos7:0  (local to host centos7)
              UUID : a410e0f0:3484e5a6:bee4b789:e48e8f18
            Events : 17

    Number   Major   Minor   RaidDevice State
       0       8       48        0      active sync set-A   /dev/sdd
       1       8       64        1      active sync set-B   /dev/sde
       2       8       80        2      active sync set-A   /dev/sdf
       3       8       96        3      active sync set-B   /dev/sdg
       
#配置开机自动挂载
[root@centos7 ~]#echo "/dev/md0 /mydata   ext4 defaults   0 0" >> /etc/fstab
[root@centos7 ~]#reboot

Last login: Thu Dec  1 15:19:51 2022 from 192.168.100.1
welcome cloud sre
[root@centos7 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   979M     0  979M   0% /dev
tmpfs                      991M     0  991M   0% /dev/shm
tmpfs                      991M  9.6M  981M   1% /run
tmpfs                      991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.7G   49G   4% /
/dev/sda1                 1014M  141M  874M  14% /boot
/dev/md0                   9.8G   37M  9.2G   1% /mydata
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users
/dev/mapper/centos-home    147G   33M  147G   1% /home
tmpfs                      199M     0  199M   0% /run/user/0
复制代码

 

2.9 静态配置网卡IP,centos/ubuntu实现

复制代码
#centos7.9网卡配置静态IP
[root@centos7 network-scripts]#cat ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.100.11
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
DNS2=8.8.8.8

#生效配置
[root@centos7 network-scripts]#systemctl restart netwokr

#查看网卡IP配置和默认路由
[root@centos7 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:3b:83:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.11/24 brd 192.168.100.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:50:56:2a:72:b1 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.2   0.0.0.0         UG    100    0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0

#ubuntu22.04网卡配置静态IP
[root@ubuntu2204:/etc/netplan]#cat 00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      addresses:
      - 192.168.100.13/24
      gateway4: 192.168.100.2
      nameservers:
        addresses:
        - 114.114.114.114
        - 8.8.8.8
        search: []
  version: 2
  
#生效配置
[root@ubuntu2204:/etc/netplan]#netplan apply

** (generate:1251): WARNING **: 04:13:53.821: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:1249): WARNING **: 04:13:54.147: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

#根据上述提示ubuntu22.04中gateway4已弃用,使用default routes代替。
[root@ubuntu2204:/etc/netplan]#cat 00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      addresses:
      - 192.168.100.12/24
      - 192.168.100.13/24
      routes:
        - to: default
          via: 192.168.100.2
      nameservers:
        addresses:
        - 114.114.114.114
        - 8.8.8.8
        search: []
  version: 2
  
#重新生效配置
[root@ubuntu2204:/etc/netplan]#netplan apply

#查看网卡IP配置和默认路由
[root@ubuntu2204:~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:75:ba:b4 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    altname ens33
    inet 192.168.100.12/24 brd 192.168.100.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.100.13/24 brd 192.168.100.255 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe75:bab4/64 scope link 
       valid_lft forever preferred_lft forever
[root@ubuntu2204:~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.2   0.0.0.0         UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
复制代码

 

2.10 图文并茂解释TCP/IP 3次握手4次断开

**(1)三次握手**

 初始状态:client端是CLOSE状态,server端是LISTEN状态。

第一次握手:client向server发送一个SYN报文,并指明client的初始化序列号ISN(seq=x),发送完成后client处于SYN_SENT状态;
第二次握手:server收到client发送的SYN报文后,会以自己的SYN报文作为应答,并且指定自己的初始化序列号ISN(seq=y),同时把client的ISN+1作为自己的确认号(ack=x+1)
第三次握手:client收到server发送的SYN报文后,会给server再发送一个ACK报文,同时把server的ISN+1作为自己的确认号(ack=y+1),由于是第二个报文,所以在初始序列号上+1(seq=x+1)

**(2)四次挥手**

 初始状态:client和server端均是ESTABLISH状态。

第一次挥手:client向server发送一个FIN报文,报文中指定一个序列号(seq=u),发送完成后client处于FIN-WAIT1状态。
第二次挥手:server收到FIN报文后,会发送ACK响应报文,同时把client的seq+1作为自己的确认号(ack=u+1);
第三次挥手:server发送ACK确认报文后,进入CLOSE-WAIT状态,做好释放server到client的连接准备,此时server同意端口连接,发送一个FIN报文,并指定一个序列号(seq=w),确认号位(ack=u+1);
第四次挥手:client收到FIN报文后,发送一个ACK报文作为应答,并把server的seq+1作为子机的确认号(ack=w+1),此时客户端处于TIME-WAIT状态,经过计时器设置的2MSL后,客户端进入CLOSE状态。

 

2.11 网卡绑定bond0的实现过程

复制代码
#centos7.9 bond0配置
[root@centos7 network-scripts]#cat ifcfg-bond0
NAME=bond0
TYPE=bond
DEVICE=bond0
BOOTPROTO=none
IPADDR=192.168.100.11
PREFIX=24
BONDING_OPTS="mode=1 miimon=100 fail_over_mac=1"
GATEWAY=192.168.100.2
DNS1=114.114.114.114
DNS2=8.8.8.8

[root@centos7 network-scripts]#cat ifcfg-eth0
NAME=eth0
DEVICE=eth0
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
ONBOOT=yes

[root@centos7 network-scripts]#cat ifcfg-eth1
NAME=eth1
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
ONBOOT=yes

#查看ip
[root@centos7 network-scripts]#ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.100.11  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::250:56ff:fe2a:72b1  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:2a:72:b1  txqueuelen 1000  (Ethernet)
        RX packets 27  bytes 2700 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2902 (2.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:50:56:2a:72:b1  txqueuelen 1000  (Ethernet)
        RX packets 1440  bytes 176445 (172.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 964  bytes 202807 (198.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:50:56:2a:72:b1  txqueuelen 1000  (Ethernet)
        RX packets 100  bytes 17706 (17.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 261  bytes 42096 (41.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 68  bytes 5636 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5636 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#查看bond0工作状态
[root@centos7 ~]#cat /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup) (fail_over_mac active)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:3b:83:99
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:2a:72:b1
Slave queue ID: 0

#模拟eth0网卡故障(vmware软件上断开网卡连接)
[root@centos7 ~]#cat /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup) (fail_over_mac active)
Primary Slave: None
Currently Active Slave: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 1
Permanent HW addr: 00:50:56:3b:83:99
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:2a:72:b1
Slave queue ID: 0
复制代码

 

2.12 实现免密登陆脚本, expect登陆远程主机,将生成的密钥写入到目标主机, expect测试远程登陆

复制代码
#/bin/bash
# -------------------------------------------
# Filename:          expect_sshlogin.sh
# Version:           1.0
# Date:              2022/12/2
# Author:            zcloud
# Email:             zcloud@mgedu.com
# Website:           zcloud.maedu.com
# Description:       ssh nopass login
# Copyright:         2022 zcloud
# License:           GPL
# --------------------------------------------

menu_select (){
    while true;do
        select MENU in 安装mysql 安装apache 免密登录主机 脚本退出;do
        case $REPLY in
        1)
            install_mysql
            break
            ;; 
        2)
            install_apache
            break
            ;;
        3)
            keyless_login
            break
            ;;
        4)  
            echo exit_script
            exit
            ;;
        esac
        done
    done
}

keyless_config (){
    declare -a iparray
 
    if [[ ! -f ~/.ssh/id_rsa ]];then
        ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/null
    else
       echo "id_rsa file exist, no need to create."
    fi
    
    read -p "免密登录初始化配置,请输入需要免密登录的主机IP,以空格间隔: " iplist
    iparray=($iplist) 
    for ipaddr in ${iparray[@]};do
        expect <<EOF
        set timeout 20
        spawn ssh-copy-id $ipaddr
        expect {
                "yes/no" { send "yes\n";exp_continue }
                "password:" { send "123456\n" }
        } 
        expect eof
EOF
    done 
    echo "所有主机免密登录初始化配置完成!"
}

install_mysql (){
    echo "install mysql finish!"
    exit 0
}

install_apache (){
    echo "install apache finish!"
    exit 0
}

keyless_login () {
    read -p "请输入需要免密登录的主机IP:" ipaddr 
    ssh $ipaddr
}

main () {
    keyless_config
    menu_select
}

main
复制代码
复制代码
#脚本运行验证
[root@centos7 shell]#sh expect_sshlogin.sh 
id_rsa file exist, no need to create.
免密登录初始化配置,请输入需要免密登录的主机IP,以空格间隔: 192.168.100.12 192.168.100.204
spawn ssh-copy-id 192.168.100.12
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.12's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.12'"
and check to make sure that only the key(s) you wanted were added.

spawn ssh-copy-id 192.168.100.204
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.204's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.204'"
and check to make sure that only the key(s) you wanted were added.

所有主机免密登录配置完成!
1) 安装mysql
2) 安装apache
3) 免密登录主机
4) 脚本退出
#? 3
请输入需要免密登录的主机IP:192.168.100.12
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-53-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Dec  3 02:58:23 AM UTC 2022

  System load:  0.10791015625     Processes:             231
  Usage of /:   7.6% of 96.88GB   Users logged in:       1
  Memory usage: 10%               IPv4 address for eth0: 192.168.100.12
  Swap usage:   0%                IPv4 address for eth0: 192.168.100.13

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

76 updates can be applied immediately.
12 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable


Last login: Sat Dec  3 02:57:12 2022 from 192.168.100.1
[root@ubuntu2204:~]#exit
logout
Connection to 192.168.100.12 closed.
1) 安装mysql
2) 安装apache
3) 免密登录主机
4) 脚本退出
#? 3
请输入需要免密登录的主机IP:192.168.100.204
Last login: Sun Dec  4 10:57:11 2022 from 192.168.100.1
[root@centos7 ~]# exit
logout
Connection to 192.168.100.204 closed.
1) 安装mysql
2) 安装apache
3) 免密登录主机
4) 脚本退出
#? 4
exit_script
[root@centos7 shell]#
复制代码

 

posted @   zcloudsre  阅读(116)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示