1. 程式人生 > 實用技巧 >11.第九章 磁碟儲存和檔案系統管理

11.第九章 磁碟儲存和檔案系統管理

第一部分

1、建立一個2G的檔案系統,塊大小為2048byte,預留1%可用空間,檔案系統ext4,卷標為TEST,要求此分割槽開機後自動掛載至/test目錄,且預設有acl掛載選項

#新新增一塊10G磁碟
[root@centos8 ~]# scandisk
[root@centos8 ~]# alias scandisk
alias scandisk='echo '\''- - -'\'' > /sys/class/scsi_host/host0/scan;echo '\''- - -'\'' > /sys/class/scsi_host/host1/scan;echo '\''- - -'\'' > /sys/class/scsi_host/host2/scan'

[root@centos8 ~]# 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    4G  0 part 
sdb       8:16   0   20G  0 disk 
├─sdb1    8:17   0    1G  0 part /mnt/sdb1
├─sdb2    8:18   0    2G  0 part 
├─sdb3    8:19   0    1K  0 part 
└─sdb5    8:21   0    3G  0 part 
sdc       8:32   0   10G  0 disk 
├─sdc1    8:33   0    1G  0 part 
└─sdc2    8:34   0    2G  0 part 
sdd       8:48   0   10G  0 disk 	#這個是新新增的磁碟
sr0      11:0    1  7.7G  0 rom  
nvme0n1 259:0    0    5G  0 disk 

[root@centos8 ~]# fdisk /dev/sdd

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x0712eaa0.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0x0712eaa0

Device     Boot Start     End Sectors Size Id Type
/dev/sdd1        2048 4196351 4194304   2G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@centos8 ~]# 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    4G  0 part 
sdb       8:16   0   20G  0 disk 
├─sdb1    8:17   0    1G  0 part /mnt/sdb1
├─sdb2    8:18   0    2G  0 part 
├─sdb3    8:19   0    1K  0 part 
└─sdb5    8:21   0    3G  0 part 
sdc       8:32   0   10G  0 disk 
├─sdc1    8:33   0    1G  0 part 
└─sdc2    8:34   0    2G  0 part 
sdd       8:48   0   10G  0 disk 
└─sdd1    8:49   0    2G  0 part 	#新建的分割槽
sr0      11:0    1  7.7G  0 rom  
nvme0n1 259:0    0    5G  0 disk 

[root@centos8 ~]# mkfs.ext4 -b 2048 -m 1 -L TEST /dev/sdd1	#-b 2048 塊大小為2048byte   -m 1  預留1%  -L TEST  卷標名為TEST
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 1048576 2k blocks and 131072 inodes
Filesystem UUID: 0f8a2804-a927-4cde-9d3c-8066d7148386
Superblock backups stored on blocks: 
	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816

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

[root@centos8 ~]# tune2fs -l /dev/sdd1
tune2fs 1.45.4 (23-Sep-2019)
Filesystem volume name:   TEST	#卷標名為TEST。
Last mounted on:          <not available>
Filesystem UUID:          0f8a2804-a927-4cde-9d3c-8066d7148386
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl	#centos8 ext4檔案系統,預設就有acl功能,不需要單獨設定
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              131072
Block count:              1048576
Reserved block count:     10485		#預留塊大小
Free blocks:              1011035
Free inodes:              131061
First block:              0
Block size:               2048	#塊大小為2048。
Fragment size:            2048
Group descriptor size:    64
Reserved GDT blocks:      512
Blocks per group:         16384
Fragments per group:      16384
Inodes per group:         2048
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Thu Dec  3 22:58:44 2020
Last mount time:          n/a
Last write time:          Thu Dec  3 22:58:44 2020
Mount count:              0
Maximum mount count:      -1
Last checked:             Thu Dec  3 22:58:44 2020
Check interval:           0 (<none>)
Lifetime writes:          1058 kB
Reserved blocks uid:      0 (user root)	#預留給誰使用,root。
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:	          256
Required extra isize:     32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      c99589a0-f6eb-4dd6-a178-b4113c273353
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0xc13d6cca

[root@centos8 ~]# mkdir /test	#建立掛載目錄
[root@centos8 ~]# blkid /dev/sdd1	#檢視UUID和檔案系統型別
/dev/sdd1: LABEL="TEST" UUID="0f8a2804-a927-4cde-9d3c-8066d7148386" TYPE="ext4" PARTUUID="0712eaa0-01"
[root@centos8 ~]# vim /etc/fstab
#新增下面行
UUID=0f8a2804-a927-4cde-9d3c-8066d7148386 /test                   ext4   defaults         0  0 
:wq

[root@centos8 ~]# mount -a	#自動掛載
[root@centos8 ~]# mount 
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=905164k,nr_inodes=226291,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=34,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=23440)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sda1 on /boot type ext4 (rw,relatime)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=184384k,mode=700)
/dev/sdb1 on /mnt/sdb1 type ext4 (rw,noatime,nodiratime)
/dev/sdd1 on /test type ext4 (rw,relatime)	#已經掛載

[root@centos8 ~]# 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    4G  0 part 
sdb       8:16   0   20G  0 disk 
├─sdb1    8:17   0    1G  0 part /mnt/sdb1
├─sdb2    8:18   0    2G  0 part 
├─sdb3    8:19   0    1K  0 part 
└─sdb5    8:21   0    3G  0 part 
sdc       8:32   0   10G  0 disk 
├─sdc1    8:33   0    1G  0 part 
└─sdc2    8:34   0    2G  0 part 
sdd       8:48   0   10G  0 disk 
└─sdd1    8:49   0    2G  0 part /test	#已經掛載
sr0      11:0    1  7.7G  0 rom  
nvme0n1 259:0    0    5G  0 disk 
[root@centos8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          905164       0    905164   0% /dev
tmpfs             921932       0    921932   0% /dev/shm
tmpfs             921932    8940    912992   1% /run
tmpfs             921932       0    921932   0% /sys/fs/cgroup
/dev/sda2      104806400 2152664 102653736   3% /
/dev/sda3       52403200  398448  52004752   1% /data
/dev/sda1         999320  137644    792864  15% /boot
tmpfs             184384       0    184384   0% /run/user/0
/dev/sdb1        1031836    2821    972491   1% /mnt/sdb1
/dev/sdd1        2031306    9236   1992908   1% /test
#已經掛載

2、寫一個指令碼,完成如下功能:

​ (1)列出當前系統識別到的所有磁碟裝置

​ (2)如磁碟數量為1,則顯示其空間使用資訊

​ 否則,則顯示最後一個磁碟上的空間使用資訊

[root@centos8 ~]# vim disk_used.sh 
#!/bin/bash
#
#*************************************************************
#Author:          zhanghui
#QQ:              19661891
#Date:            2020-12-04
#FileName:        disk_used.sh
#URL:             www.neteagles.cn
#Description:     The test script
#Copyright (C):   2020 All rights reserved
#***********************************************************
disk=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p'`
disk_num=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p'| wc -l`
disk_end=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p' | sed -n '$p'`
if [ $disk_num -eq 1 ];then
	df | grep "/dev/$disk"
else
	df | grep "/dev/$disk_end"
fi
:wq

[root@centos8 ~]# bash disk_used.sh 
/dev/sda2      104806400 4868684  99937716   5% /
/dev/sda3       52403200  398400  52004800   1% /data
/dev/sda1         999320  192580    737928  21% /boot

#新增兩塊硬碟
[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
sdc      8:32   0    5G  0 disk 
sr0     11:0    1  7.7G  0 rom  
[root@centos8 ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x2e83806e.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0x2e83806e

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 4196351 4194304   2G 83 Linux

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 
First sector (4196352-10485759, default 4196352): 
Last sector, +sectors or +size{K,M,G,T,P} (4196352-10485759, default 10485759): 

Created a new partition 2 of type 'Linux' and of size 3 GiB.

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0x2e83806e

Device     Boot   Start      End Sectors Size Id Type
/dev/sdb1          2048  4196351 4194304   2G 83 Linux
/dev/sdb2       4196352 10485759 6289408   3G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
├─sdb1   8:17   0    2G  0 part 
└─sdb2   8:18   0    3G  0 part 
sdc      8:32   0    5G  0 disk 
sr0     11:0    1  7.7G  0 rom  
[root@centos8 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: c520fbef-2726-4ab2-b54f-b654bd6c6a1f
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@centos8 ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 786176 4k blocks and 196608 inodes
Filesystem UUID: b5efe116-aec1-41e7-80a2-3791b3ee5669
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@centos8 ~]# mkdir /mnt/sdb{1,2}
[root@centos8 ~]# mount /dev/sdb1 /mnt/sdb1
[root@centos8 ~]# mount /dev/sdb2 /mnt/sdb2
[root@centos8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          893740       0    893740   0% /dev
tmpfs             921916       0    921916   0% /dev/shm
tmpfs             921916    9528    912388   2% /run
tmpfs             921916       0    921916   0% /sys/fs/cgroup
/dev/sda2      104806400 4868932  99937468   5% /
/dev/sda3       52403200  398400  52004800   1% /data
/dev/sda1         999320  192580    737928  21% /boot
tmpfs             184380    1168    183212   1% /run/user/42
tmpfs             184380       4    184376   1% /run/user/0
/dev/sdb1        1998672    6144   1871288   1% /mnt/sdb1
/dev/sdb2        3029776    9216   2846944   1% /mnt/sdb2



[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
├─sdb1   8:17   0    2G  0 part /mnt/sdb1
└─sdb2   8:18   0    3G  0 part /mnt/sdb2
sdc      8:32   0    5G  0 disk 
sr0     11:0    1  7.7G  0 rom  
[root@centos8 ~]# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x2885d32c.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +3G

Created a new partition 1 of type 'Linux' and of size 3 GiB.

Command (m for help): p
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0x2885d32c

Device     Boot Start     End Sectors Size Id Type
/dev/sdc1        2048 6293503 6291456   3G 83 Linux

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 
First sector (6293504-10485759, default 6293504): 
Last sector, +sectors or +size{K,M,G,T,P} (6293504-10485759, default 10485759): 

Created a new partition 2 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0x2885d32c

Device     Boot   Start      End Sectors Size Id Type
/dev/sdc1          2048  6293503 6291456   3G 83 Linux
/dev/sdc2       6293504 10485759 4192256   2G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
├─sdb1   8:17   0    2G  0 part /mnt/sdb1
└─sdb2   8:18   0    3G  0 part /mnt/sdb2
sdc      8:32   0    5G  0 disk 
├─sdc1   8:33   0    3G  0 part 
└─sdc2   8:34   0    2G  0 part 
sr0     11:0    1  7.7G  0 rom  
[root@centos8 ~]# mkfs.ext4 /dev/sdc1
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: e4eac13f-f7aa-4cb1-9c72-d492f16231b9
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@centos8 ~]# mkfs.ext4 /dev/sdc2
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 524032 4k blocks and 131072 inodes
Filesystem UUID: 91996752-51a6-42c3-9398-fdc9c58aa39f
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@centos8 ~]# mkdir /mnt/sdc{1,2}
[root@centos8 ~]# mount /dev/sdc1 /mnt/sdc1
[root@centos8 ~]# mount /dev/sdc2 /mnt/sdc2
[root@centos8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          893740       0    893740   0% /dev
tmpfs             921916       0    921916   0% /dev/shm
tmpfs             921916    9536    912380   2% /run
tmpfs             921916       0    921916   0% /sys/fs/cgroup
/dev/sda2      104806400 4868932  99937468   5% /
/dev/sda3       52403200  398400  52004800   1% /data
/dev/sda1         999320  192580    737928  21% /boot
tmpfs             184380    1168    183212   1% /run/user/42
tmpfs             184380       4    184376   1% /run/user/0
/dev/sdb1        1998672    6144   1871288   1% /mnt/sdb1
/dev/sdb2        3029776    9216   2846944   1% /mnt/sdb2
/dev/sdc1        3030800    9216   2847916   1% /mnt/sdc1
/dev/sdc2        2030416    6144   1903084   1% /mnt/sdc2

[root@centos8 ~]# bash disk_used.sh 
/dev/sdc1        3030800    9216   2847916   1% /mnt/sdc1
/dev/sdc2        2030416    6144   1903084   1% /mnt/sdc2

3、將CentOS6的CentOS-6.10-x86_64-bin-DVD1.iso和CentOS-6.10-x86_64-bin-DVD2.iso兩個檔案,合併成一個CentOS-6.10-x86_64-Everything.iso檔案,並將其配置為yum源

方法1:

先新增一個光碟機,把CentOS-6.10-x86_64-bin-DVD1.iso載入到/dev/sr0和把CentOS-6.10-x86_64-bin-DVD2.iso載入到/dev/sr1

[root@centos6 ~]# mkdir /mnt/iso	#建立複製DVD1和DVD2的目錄
[root@centos6 ~]# mkdir /mnt/dvd1	#建立掛載DVD1的目錄
[root@centos6 ~]# mkdir /mnt/dvd2	#建立掛載DVD2的目錄
[root@centos6 ~]# mount /dev/sr0 /mnt/dvd1	#掛載DVD1
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@centos6 ~]# mount /dev/sr1 /mnt/dvd2	#掛載DVD2
mount: block device /dev/sr1 is write-protected, mounting read-only
[root@centos6 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      100902304  853756  94916260   1% /
tmpfs             501508       0    501508   0% /dev/shm
/dev/sda1         999320   30712    916180   4% /boot
/dev/sda3       51475068   53064  48800564   1% /data
/dev/sr0         3897932 3897932         0 100% /mnt/dvd1
/dev/sr1         2136278 2136278         0 100% /mnt/dvd2
[root@centos6 ~]# ls /mnt/dvd1
CentOS_BuildTag  images                    repodata                       RPM-GPG-KEY-CentOS-Testing-6
EFI              isolinux                  RPM-GPG-KEY-CentOS-6           TRANS.TBL
EULA             Packages                  RPM-GPG-KEY-CentOS-Debug-6
GPL              RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Security-6
[root@centos6 ~]# ls /mnt/dvd2
CentOS_BuildTag  Packages              RPM-GPG-KEY-CentOS-Debug-6     RPM-GPG-KEY-CentOS-Testing-6
EULA             RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-Security-6  TRANS.TBL

[root@centos6 ~]# cp  -av  /mnt/dvd1/*  /mnt/iso	#把DVD1複製到/mnt/iso目錄
[root@centos6 ~]# cp  -v  /mnt/dvd2/Packages/*.rpm  /mnt/iso/Packages/	#複製DVD2到/mnt/iso

#合併TRANS.TBL
#將DVD2中TRANS.TBL的資訊追加到DVD1中TRANS.TBL後面, 並排序儲存 
[root@centos6 ~]# cat  /mnt/dvd2/Packages/TRANS.TBL  >>  /mnt/iso/Packages/TRANS.TBL 
[root@centos6 ~]# mv  /mnt/iso/Packages/{TRANS.TBL,TRANS.TBL.BAK} 
[root@centos6 ~]# sort  /mnt/iso/Packages/TRANS.TBL.BAK  >  /mnt/iso/Packages/TRANS.TBL
[root@centos6 ~]# rm  -rf  /mnt/iso/Packages/TRANS.TBL.BAK
#/mnt/iso已經是合併後的檔案了

[root@centos6 ~]# du -sh /mnt/iso
5.9G	/mnt/iso

[root@centos6 ~]# mkdir /data/iso	#建立iso檔案目錄
[root@centos6 ~]# yum -y install mkisofs	#安裝建立ISO檔案工具
[root@centos6 ~]# mkisofs  -l  -J  -L  -r  -V  "CentOS-6.10-x86_64-Everything"  -o  /data/iso/CentOS-6.10-x86_64-Everything.iso  /mnt/iso	#建立ISO檔案
[root@centos6 ~]# ll -h /data/iso
total 5.8G
-rw-r--r-- 1 root root 5.8G Dec  4 01:38 CentOS-6.10-x86_64-Everything.iso

sz 不支援超過4G的檔案傳輸

用Xftp 把檔案傳到windows裡

#關機移除新新增的光碟機,把/dev/sr0掛載成CentOS-6.10-x86_64-Everything.iso

[root@centos6 ~]# yum -y install autofs	#安裝自動掛載光碟工具
[root@centos6 ~]# service autofs start;chkconfig autofs on	#啟動autofs服務,並開機啟動
Loading autofs4:                                           [  OK  ]
Starting automount:                                        [  OK  ]

[root@centos6 ~]# ls /misc
[root@centos6 ~]# ls /misc/cd
CentOS_BuildTag  images      RELEASE-NOTES-en-US.html    RPM-GPG-KEY-CentOS-Security-6
EFI              isolinux    repodata                    RPM-GPG-KEY-CentOS-Testing-6
EULA             lost+found  RPM-GPG-KEY-CentOS-6        TRANS.TBL
GPL              Packages    RPM-GPG-KEY-CentOS-Debug-6

[root@centos6 ~]# vim /etc/yum.repos.d/test.repo 	#設定光碟映象本地源
[base]
name=base
baseurl=file:///misc/cd
checkgpg=0    
:wq
[root@centos6 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                | 4.0 kB     00:00 ... 
repo id                                             repo name                                        status
base                                                base                                             6,713
repolist: 6,713

方法二:

#先設定光碟機載入CentOS-6.10-x86_64-bin-DVD1.iso
[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  3.7G  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0 48.8G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]

[root@centos6 ~]# dd if=/dev/sr0 bs=1M of=CentOS-6.10-x86_64-Everything.iso
3807+0 records in
3807+0 records out
3991928832 bytes (4.0 GB) copied, 271.833 s, 14.7 MB/s
[root@centos6 ~]# ll CentOS-6.10-x86_64-Everything.iso -h
-rw-r--r-- 1 root root 3.8G Dec  4 18:50 CentOS-6.10-x86_64-Everything.iso

#把光碟機斷開連線,設定光碟機載入CentOS-6.10-x86_64-bin-DVD2.iso,再連線光碟機
[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1    2G  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0 48.8G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]

[root@centos6 ~]# dd if=/dev/sr0 ibs=1k skip=32 of=CentOS-6.10-x86_64-Everything.iso obs=1M seek=3788
#skip=32參考的ISO9660檔案頭,seek=3788,跳過上個映象的大小。
2136246+0 records in
2086+1 records out
2187515904 bytes (2.2 GB) copied, 154.047 s, 14.2 MB/s
[root@centos6 ~]# ll -h CentOS-6.10-x86_64-Everything.iso 
-rw-r--r-- 1 root root 5.8G Dec  4 18:56 CentOS-6.10-x86_64-Everything.iso

sz 不支援超過4G的檔案傳輸
用Xftp 把檔案傳到windows裡

#把光碟機斷開連線,設定CentOS-6.10-x86_64-Everything.iso,再連線光碟機
[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  5.8G  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0 48.8G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]


[root@centos6 ~]# yum -y install autofs	#安裝自動掛載光碟工具
[root@centos6 ~]# service autofs start;chkconfig autofs on	#啟動autofs服務,並開機啟動
Loading autofs4:                                           [  OK  ]
Starting automount:                                        [  OK  ]

[root@centos6 ~]# ls /misc
[root@centos6 ~]# ls /misc/cd
CentOS_BuildTag  images      RELEASE-NOTES-en-US.html    RPM-GPG-KEY-CentOS-Security-6
EFI              isolinux    repodata                    RPM-GPG-KEY-CentOS-Testing-6
EULA             lost+found  RPM-GPG-KEY-CentOS-6        TRANS.TBL
GPL              Packages    RPM-GPG-KEY-CentOS-Debug-6

[root@centos6 ~]# vim /etc/yum.repos.d/test.repo 	#設定光碟映象本地源
[base]
name=base
baseurl=file:///misc/cd
checkgpg=0    
:wq
[root@centos6 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                | 4.0 kB     00:00 ... 
repo id                                             repo name                                        status
base                                                base                                             6,713
repolist: 6,713

第二部分 RAID

1、建立一個可用空間為1G的RAID裝置,檔案系統為ext4,有一個空閒盤,開機可自動掛載至/backup目錄

[root@centos8 ~]# scandisk
[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    1G  0 disk 
sdc      8:32   0    1G  0 disk 
sdd      8:48   0    1G  0 disk 
sr0     11:0    1  7.7G  0 rom 
#新增三塊新硬碟/dev/sdb、/dev/sdc和/dev/sdd

[root@centos8 ~]# dnf -y install mdadm

[root@centos8 ~]#  mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/sd{b,c,d}
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
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

[root@centos8 ~]# 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    4G  0 part  [SWAP]
sdb      8:16   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 
sdc      8:32   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 
sdd      8:48   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 
sr0     11:0    1  7.7G  0 rom  

[root@centos8 ~]# mkfs.ext4 /dev/md0
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 261632 4k blocks and 65408 inodes
Filesystem UUID: 5540936f-bfe4-46ff-8fbb-88e071f5499a
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

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


[root@centos8 ~]# tune2fs -l /dev/md0
tune2fs 1.45.4 (23-Sep-2019)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          5540936f-bfe4-46ff-8fbb-88e071f5499a
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              65408
Block count:              261632
Reserved block count:     13081
Free blocks:              252781
Free inodes:              65397
First block:              0
Block size:               4096
Fragment size:            4096
Group descriptor size:    64
Reserved GDT blocks:      127
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8176
Inode blocks per group:   511
Flex block group size:    16
Filesystem created:       Fri Dec  4 16:56:44 2020
Last mount time:          n/a
Last write time:          Fri Dec  4 16:56:44 2020
Mount count:              0
Maximum mount count:      -1
Last checked:             Fri Dec  4 16:56:44 2020
Check interval:           0 (<none>)
Lifetime writes:          533 kB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:	          256
Required extra isize:     32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      d8599d77-afd5-4e2f-80c6-f206e7dc2bab
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x066b1a44

[root@centos8 ~]# mkdir /backup

[root@centos8 ~]# blkid /dev/md0
/dev/md0: UUID="5540936f-bfe4-46ff-8fbb-88e071f5499a" TYPE="ext4"

[root@centos8 ~]# vim /etc/fstab
#新增下面內容
UUID=5540936f-bfe4-46ff-8fbb-88e071f5499a /backup                 ext4    defaults        0 0  
:wq

[root@centos8 ~]# mount -a
[root@centos8 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        884M     0  884M   0% /dev
tmpfs           901M     0  901M   0% /dev/shm
tmpfs           901M  8.7M  892M   1% /run
tmpfs           901M     0  901M   0% /sys/fs/cgroup
/dev/sda2       100G  2.1G   98G   3% /
/dev/sda3        50G  390M   50G   1% /data
/dev/sda1       976M  135M  775M  15% /boot
tmpfs           181M     0  181M   0% /run/user/0
/dev/md0        990M  2.6M  921M   1% /backup
[root@centos8 ~]# reboot
[root@centos8 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        884M     0  884M   0% /dev
tmpfs           901M     0  901M   0% /dev/shm
tmpfs           901M  8.7M  892M   1% /run
tmpfs           901M     0  901M   0% /sys/fs/cgroup
/dev/sda2       100G  2.1G   98G   3% /
/dev/sda3        50G  390M   50G   1% /data
/dev/sda1       976M  135M  775M  15% /boot
/dev/md0        990M  2.6M  921M   1% /backup
tmpfs           181M     0  181M   0% /run/user/0

[root@centos8 ~]# 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    4G  0 part  [SWAP]
sdb      8:16   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 /backup
sdc      8:32   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 /backup
sdd      8:48   0    1G  0 disk  
└─md0    9:0    0 1022M  0 raid1 /backup
sr0     11:0    1  7.7G  0 rom   

2、建立由三塊硬碟組成的可用空間為2G的RAID5裝置,要求其chunk大小為256k,檔案系統為ext4,開機可自動掛載至/mydata目錄

[root@centos8 ~]# scandisk
[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0    1G  0 disk 
sdc      8:32   0    1G  0 disk 
sdd      8:48   0    1G  0 disk 
sr0     11:0    1  7.7G  0 rom 
#新增三塊新硬碟/dev/sdb、/dev/sdc和/dev/sdd

[root@centos8 ~]# dnf -y install mdadm
[root@centos8 ~]# mdadm -C /dev/md1 -a yes -l 5 -n 3 -c 256 /dev/sd{b,c,d}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

[root@centos8 ~]# 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    4G  0 part  [SWAP]
sdb      8:16   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 
sdc      8:32   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 
sdd      8:48   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 
sr0     11:0    1  7.7G  0 rom  
[root@centos8 ~]# mkfs.ext4 /dev/md1
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 523264 4k blocks and 130816 inodes
Filesystem UUID: d4447cca-1a84-44c3-b3f6-9a7956091233
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 
[root@centos8 ~]# tune2fs -l /dev/md1
tune2fs 1.45.4 (23-Sep-2019)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          d4447cca-1a84-44c3-b3f6-9a7956091233
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              130816
Block count:              523264
Reserved block count:     26163
Free blocks:              505316
Free inodes:              130805
First block:              0
Block size:               4096
Fragment size:            4096
Group descriptor size:    64
Reserved GDT blocks:      255
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8176
Inode blocks per group:   511
RAID stride:              64
RAID stripe width:        128
Flex block group size:    16
Filesystem created:       Fri Dec  4 17:24:39 2020
Last mount time:          n/a
Last write time:          Fri Dec  4 17:24:40 2020
Mount count:              0
Maximum mount count:      -1
Last checked:             Fri Dec  4 17:24:39 2020
Check interval:           0 (<none>)
Lifetime writes:          1045 kB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:	          256
Required extra isize:     32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      f959b864-fcb7-4eb0-8088-f04be25671fd
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x7e59f1b1


[root@centos8 ~]# mkdir /mydata
[root@centos8 ~]# blkid /dev/md1
/dev/md1: UUID="d4447cca-1a84-44c3-b3f6-9a7956091233" TYPE="ext4"

[root@centos8 ~]# vim /etc/fstab
#新增下面內容
UUID=d4447cca-1a84-44c3-b3f6-9a7956091233 /mydata                 ext4    defaults        0 0 
:wq
[root@centos8 ~]# mount -a
[root@centos8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          905164       0    905164   0% /dev
tmpfs             921932       0    921932   0% /dev/shm
tmpfs             921932    8900    913032   1% /run
tmpfs             921932       0    921932   0% /sys/fs/cgroup
/dev/sda2      104806400 2149500 102656900   3% /
/dev/sda3       52403200  398400  52004800   1% /data
/dev/sda1         999320  137584    792924  15% /boot
tmpfs             184384       0    184384   0% /run/user/0
/dev/md1         2027408    6144   1900228   1% /mydata

[root@centos8 ~]# reboot
[root@centos8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          905164       0    905164   0% /dev
tmpfs             921932       0    921932   0% /dev/shm
tmpfs             921932    8908    913024   1% /run
tmpfs             921932       0    921932   0% /sys/fs/cgroup
/dev/sda2      104806400 2148524 102657876   3% /
/dev/sda3       52403200  398400  52004800   1% /data
/dev/sda1         999320  137580    792928  15% /boot
/dev/md1         2027408    6144   1900228   1% /mydata
tmpfs             184384       0    184384   0% /run/user/0

[root@centos8 ~]# 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    4G  0 part  [SWAP]
sdb      8:16   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 /mydata
sdc      8:32   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 /mydata
sdd      8:48   0    1G  0 disk  
└─md1    9:1    0    2G  0 raid5 /mydata
sr0     11:0    1  7.7G  0 rom  

第三部分 LVM

1、建立一個至少有兩個PV組成的大小為20G的名為testvg的VG;需求PE大小為16MB,而後在卷組中的建立大小為5G的邏輯卷testlv;掛載至/users目錄

[root@centos8 ~]# scandisk
[root@centos8 ~]# 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    4G  0 part [SWAP]
sdb      8:16   0   15G  0 disk 
sdc      8:32   0    5G  0 disk 
sr0     11:0    1  7.7G  0 rom 
#新增兩塊硬碟sdb、sdc

[root@centos8 ~]# pvs /dev/sd{b,c}
-bash: pvs: command not found
[root@centos8 ~]# dnf -y install lvm2

[root@centos8 ~]# pvs
[root@centos8 ~]# vgs
[root@centos8 ~]# lvs
[root@centos8 ~]# pvcreate /dev/sd{b,c}		#把兩塊硬碟加到物理卷
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
[root@centos8 ~]# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb      lvm2 ---  15.00g 15.00g
  /dev/sdc      lvm2 ---   5.00g  5.00g
  
[root@centos8 ~]# vgcreate -s 16M testvg /dev/sd{b,c}	#把 /dev/sdb和/dev/sdc加入到卷組,塊大小為16M
  Volume group "testvg" successfully created
[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   0   0 wz--n- <19.97g <19.97g
[root@centos8 ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sdb   testvg lvm2 a--  14.98g 14.98g
  /dev/sdc   testvg lvm2 a--   4.98g  4.98g
[root@centos8 ~]# 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	#卷組大小20G
  PE Size               16.00 MiB	#塊大小為16M
  Total PE              1278
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1278 / <19.97 GiB
  VG UUID               ICjCEw-pbWT-0jUt-f4hV-lDOJ-YSSq-o3tq5U

  
[root@centos8 ~]# lvcreate  -L 5G -n testlv testvg	#建立邏輯卷testlv,大小5G
  Logical volume "testlv" created.
[root@centos8 ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 5.00g  

[root@centos8 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                aPZyRk-7n9N-P7ol-ZwJv-2LHI-aOm2-8tED5j
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 19:54:00 +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

[root@centos8 ~]# mkfs.ext4 /dev/testvg/testlv		#建立邏輯卷檔案系統
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: 81a7eb1b-72ae-4d5c-9d0f-44a3daa14610
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

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

[root@centos8 ~]# mkdir /users
[root@centos8 ~]# mount /dev/testvg/testlv /users/	#掛載邏輯卷
[root@centos8 ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  884M     0  884M   0% /dev
tmpfs                     tmpfs     901M     0  901M   0% /dev/shm
tmpfs                     tmpfs     901M  8.7M  892M   1% /run
tmpfs                     tmpfs     901M     0  901M   0% /sys/fs/cgroup
/dev/sda2                 xfs       100G  2.1G   98G   3% /
/dev/sda3                 xfs        50G  390M   50G   1% /data
/dev/sda1                 ext4      976M  135M  775M  15% /boot
tmpfs                     tmpfs     181M     0  181M   0% /run/user/0
/dev/mapper/testvg-testlv ext4      4.9G   20M  4.6G   1% /users	#已被掛載

2、新建使用者archlinux,需求其家目錄為/users/archlinux,而後s

u切換至archlinux使用者,複製/etc/pam.d目錄至自己的家目錄

[root@centos8 ~]# useradd -d /users/archlinux archlinux
[root@centos8 ~]# getent passwd archlinux
archlinux:x:1001:1001::/users/archlinux:/bin/bash
[root@centos8 ~]# su archlinux
[archlinux@centos8 root]$ cp -r /etc/pam.d ~
[archlinux@centos8 root]$ ll -a /users/archlinux/
total 28
drwx------ 3 archlinux archlinux 4096 Dec  6 19:57 .
drwxr-xr-x 4 root      root      4096 Dec  6 19:56 ..
-rw------- 1 archlinux archlinux   93 Dec  6 19:57 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Nov  9  2019 .bash_logout
-rw-r--r-- 1 archlinux archlinux  141 Nov  9  2019 .bash_profile
-rw-r--r-- 1 archlinux archlinux  312 Nov  9  2019 .bashrc
drwxr-xr-x 2 archlinux archlinux 4096 Dec  6 19:56 pam.d

3、擴充套件testlv至7G,需求archlinux使用者的檔案不能丟失

[archlinux@centos8 root]$ exit
exit

[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   1   0 wz--n- <19.97g <14.97g	#卷組可用空間為15G
[root@centos8 ~]#  lvextend -r -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.
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/mapper/testvg-testlv is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/testvg-testlv is now 1835008 (4k) blocks long.

[root@centos8 ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-ao---- 7.00g                                                    
[root@centos8 ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  884M     0  884M   0% /dev
tmpfs                     tmpfs     901M     0  901M   0% /dev/shm
tmpfs                     tmpfs     901M  8.7M  892M   1% /run
tmpfs                     tmpfs     901M     0  901M   0% /sys/fs/cgroup
/dev/sda2                 xfs       100G  2.1G   98G   3% /
/dev/sda3                 xfs        50G  390M   50G   1% /data
/dev/sda1                 ext4      976M  135M  775M  15% /boot
tmpfs                     tmpfs     181M     0  181M   0% /run/user/0
/dev/mapper/testvg-testlv ext4      6.9G   23M  6.5G   1% /users

[root@centos8 ~]# ll -a /users/archlinux/
total 28
drwx------ 3 archlinux archlinux 4096 Dec  6 19:57 .
drwxr-xr-x 4 root      root      4096 Dec  6 19:56 ..
-rw------- 1 archlinux archlinux   93 Dec  6 19:57 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Nov  9  2019 .bash_logout
-rw-r--r-- 1 archlinux archlinux  141 Nov  9  2019 .bash_profile
-rw-r--r-- 1 archlinux archlinux  312 Nov  9  2019 .bashrc
drwxr-xr-x 2 archlinux archlinux 4096 Dec  6 19:56 pam.d	#資料還在

4、收縮testlv至3G,要求archlinux使用者的檔案不能丟失

[root@centos8 ~]# umount /dev/testvg/testlv 	#取消掛載
[root@centos8 ~]# fsck -f /dev/testvg/testlv 	#檢查檔案系統
fsck from util-linux 2.32.1
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/testvg-testlv: 40/458752 files (0.0% non-contiguous), 50972/1835008 blocks
[root@centos8 ~]# resize2fs /dev/testvg/testlv 3G	#縮減檔案系統
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 (4k) blocks long.
 
 [root@centos8 ~]# lvreduce -L 3G /dev/testvg/testlv 	#縮減邏輯卷
  WARNING: Reducing active logical volume to 3.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
  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@centos8 ~]# mount /dev/testvg/testlv /users/	#掛載邏輯卷
[root@centos8 ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  884M     0  884M   0% /dev
tmpfs                     tmpfs     901M     0  901M   0% /dev/shm
tmpfs                     tmpfs     901M  8.7M  892M   1% /run
tmpfs                     tmpfs     901M     0  901M   0% /sys/fs/cgroup
/dev/sda2                 xfs       100G  2.1G   98G   3% /
/dev/sda3                 xfs        50G  390M   50G   1% /data
/dev/sda1                 ext4      976M  135M  775M  15% /boot
tmpfs                     tmpfs     181M     0  181M   0% /run/user/0
/dev/mapper/testvg-testlv ext4      2.9G   16M  2.8G   1% /users	#現在已經縮減至3G

[root@centos8 ~]# ll -a /users/archlinux/
total 28
drwx------ 3 archlinux archlinux 4096 Dec  6 19:57 .
drwxr-xr-x 4 root      root      4096 Dec  6 19:56 ..
-rw------- 1 archlinux archlinux   93 Dec  6 19:57 .bash_history
-rw-r--r-- 1 archlinux archlinux   18 Nov  9  2019 .bash_logout
-rw-r--r-- 1 archlinux archlinux  141 Nov  9  2019 .bash_profile
-rw-r--r-- 1 archlinux archlinux  312 Nov  9  2019 .bashrc
drwxr-xr-x 2 archlinux archlinux 4096 Dec  6 19:56 pam.d
#資料還在

5、對testlv建立快照,並嘗試基於快照備份資料,驗證快照的功能

[root@centos8 ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-ao---- 3.00g 	#邏輯卷還有未使用空間
[root@centos8 ~]# lvcreate -s -L 1G -n testlv-snapshot -p r /dev/testvg/testlv 
  Logical volume "testlv-snapshot" created.
[root@centos8 ~]# lvs
  LV              VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv          testvg owi-aos--- 3.00g                                                    
  testlv-snapshot testvg sri-a-s--- 1.00g      testlv 0.01  

[root@centos8 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                aPZyRk-7n9N-P7ol-ZwJv-2LHI-aOm2-8tED5j
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 19:54:00 +0800
  LV snapshot status     source of
                         testlv-snapshot [active]
  LV Status              available
  # open                 1
  LV Size                3.00 GiB
  Current LE             192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/testvg/testlv-snapshot
  LV Name                testlv-snapshot
  VG Name                testvg
  LV UUID                PvdE7T-kJCO-ogxX-bjZ3-jRbT-SwVn-z9KqJ9
  LV Write Access        read only
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 20:12:33 +0800
  LV snapshot status     active destination for testlv
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             192
  COW-table size         1.00 GiB
  COW-table LE           64
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

[root@centos8 ~]# mkdir /mnt/testlv-snapshot
[root@centos8 ~]# mount /dev/testvg/testlv-snapshot /mnt/testlv-snapshot/
mount: /mnt/testlv-snapshot: WARNING: device write-protected, mounted read-only.

[root@centos8 ~]# ls /mnt/testlv-snapshot/
archlinux  lost+found
[root@centos8 ~]# ls /users/
archlinux  lost+found
[root@centos8 ~]# ls /users/archlinux/
pam.d
[root@centos8 ~]# rm -rf /users/archlinux/pam.d/	#刪除邏輯卷中的牡蠣
[root@centos8 ~]# ls /users/archlinux/	#邏輯卷中已經沒有了
[root@centos8 ~]# ls /mnt/testlv-snapshot/archlinux/
pam.d
#快照中還在

[root@centos8 ~]# rm -rf /mnt/testlv-snapshot/archlinux/pam.d/
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/runuser': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/polkit-1': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/systemd-user': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/passwd': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/password-auth': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/vmtoolsd': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/system-auth': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/vlock': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/runuser-l': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/sssd-shadowutils': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/su': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/postlogin': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/remote': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/sudo-i': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/fingerprint-auth': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/sshd': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/config-util': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/su-l': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/smartcard-auth': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/sudo': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/other': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/login': Read-only file system
rm: cannot remove '/mnt/testlv-snapshot/archlinux/pam.d/crond': Read-only file system
#快照中的檔案不能刪除


[root@centos8 ~]# umount /dev/testvg/testlv	#取消掛載邏輯卷
[root@centos8 ~]# umount /mnt/testlv-snapshot/	#取消掛載快照
[root@centos8 ~]# lvs
  LV              VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv          testvg owi-a-s--- 3.00g                                                    
  testlv-snapshot testvg sri-a-s--- 1.00g      testlv 0.01                                   
[root@centos8 ~]# lvconvert --merge /dev/testvg/testlv-snapshot 	#合併快照
  Merging of volume testvg/testlv-snapshot started.
  testvg/testlv: Merged: 100.00%

[root@centos8 ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 3.00g  #快照只是一次性的,還原過後就沒有了
  
[root@centos8 ~]# mount /dev/testvg/testlv /users/
	#掛載邏輯卷

[root@centos8 ~]# ll /users/
total 20
drwx------ 3 archlinux archlinux  4096 Dec  6 19:57 archlinux
drwx------ 2 root      root      16384 Dec  6 19:54 lost+found
[root@centos8 ~]# ll /users/archlinux/
total 4
drwxr-xr-x 2 archlinux archlinux 4096 Dec  6 19:56 pam.d
#還原成做快照時的檔案了