磁盘存储管理 和文件系统管理 RAID
1、磁盘管理
1.1 磁盘分区
两种分区方式:MBR,GPT
0磁道0扇区: 512bytes
446bytes:boot loader
64bytes:分区表,其中每16bytes标识一个分区
2bytes:55AA
1.2 管理分区
列出块设备
lsblk
创建分区命令
fdisk 管理MBR分区
gdisk 管理GPT分区
parted 高级分区操作,
1.3 parted 命令
parted的操作是实时生效的,小心使用
格式:
parted [option]...[设备 [命令 [参数]...]...]
1.4 分区工具fdisk和gdisk
gdisk [device...] 类fdisk 的GPT分区工具
fdisk -l [-u] [device...] 查看分区
fdisk [device...] 管理MBR分区工具
子命令:
p 分区列表
t 更改分区类型
n 创建新分区
d 删除分区
v 校验分区
u 转换单位
w 保存退出
q 不保存退出
查看内核是否已经识别新的分区
cat /proc/partations
非交互式创建分区
echo -e 'n\np\n\n\n+2G\nw\n' | fdisk /dev/sdc
1.5 文件系统管理
1.5.1 创建文件管理工具
-
mkfs命令:
(1) mkfs.FS_TYPE /dev/DEVICE
ext4
xfs
btfs
vfat
(2) mkfs -t FS_TYPE /dev/DEVICE
-L 'LABEL' 设定卷标
- mke2fs:ext系列文件系统专用管理工具
常用选项:
-t {ext2|ext3|ext4|xfs} 指定文件系统类型
-b {1024|2048|4096} 指定块大小
-L 'LABEL' 设定卷标
-i #为数据空间中每第多少个字节创建一个inode;不小于块大小
-N #指定分区中创建多少个inode
-m #默认5%,为管理人员预留空间占总空间的百分比
-o FEATURE[,...] 启用指定特性
-o ^FEATURE 关闭指定特性
-j 相当于 -t ext3
2、逻辑卷管理器
相关工具来自于lvm2 包
2.1 pv管理工具
#显示pv信息
pvs:简要pv信息显示
pvdisplay
#创建pv
pvcreate /dev/DEVICE
#删除pv
pvremove /dev/DEVICE
2.2 vg管理工具
#显示卷组
vgs
vgdisplay
#创建卷组
vgcreate [-s #[KkmMgGtTpPeE]] volumeGroupName physicalDevicepath [PhysicalDevivePath...]
#管理卷组
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevivePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevivePath...]
#删除卷组
先做pvmove
再做 vgremove
2.3 lv管理工具
#显示逻辑卷
lvs
lvdisplay
#创建逻辑卷
lvcreate -l #[mMgGtT] -n Name VolumeGroup
lvcreate -l 60%VG -n mylv testvg
#删除逻辑卷
lvremove /dev/VG_NAME/LV_NAME
2.4 在线扩展逻辑卷
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# ext格式
resize2fs /dev/VG_NAME/LV_NAME
# xfs格式
xfs_growfs MOUNTPOINT
lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME
3、RAID
3.1 总结RAID各个级别及其组合方式和性能的不同
RAID类型:
RAID 0、RAID 1、RAID 4、RAID 5、RAID 6、RAID 10、RAID 01、RAID 50
RAID常用性能:
RAID 0:需要至少2块硬盘,读写性能提升,可用空间为n块硬盘容量之和,无容错能力。
RAID 1:至少需要2块硬盘,硬盘数为2的倍数,可用空间为n/2容量的空间,读性能提升,写性能降低,有冗余能力。
RAID 5:至少需要3块硬盘,硬盘数为3块以上。可用空间为(n-1),读写性能提升,有容错能力,最多允许坏1块盘。
RAID 6:至少需要4块硬盘,硬盘数为4块以上,可用空间为(n-2),读写性能提升,有容错能力,最多允许坏2块盘。
RAID 10:至少需要4块硬盘,硬盘数为4的偶数,可用空间为n/2,读写性能提升,有容错能力,每组镜像最多只能坏1块盘。先组RAID 1再组 RAID 0。
RAID 01:至少4块盘,硬盘数为4的偶数,可用空间为n/2,读写性能提升,有容错能力。不同镜像不能坏存储相同内容的硬盘,先组RAID 0 再组 RAID 1。
RAID 50:至少需要6块盘,硬盘数为3的倍数,可用空间为(n-3),读写性能提升,有容错能力,每组镜像允许坏1块盘,最多允许坏2块硬盘。
3.2 破坏mbr表并修复
# 备份MBR分区表
[root@CentOS8 ~]#dd if=/dev/sda of=/data/mbr.img bs=1 count=64 skip=446
[root@CentOS8 ~]#scp /data/mbr.img 10.0.0.8:
# 破坏MBR分区表
[root@CentOS8 ~]#dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
#重启无法进入系统
[root@CentOS8 ~]#reboot
#用光盘启动,进入rescue mode,选择1)Continue -->> 回车进入shell
#配置网络
sh-4.4#ip addr add 10.0.0.18/24 dev ens33
sh-4.4#scp 10.0.0.8:/root/mbr.img .
#恢复MBR分区表
sh-4.4#dd if=mbr.img of=/dev/sda bs=1 seek=446
sh-4.4#exit
3.3 磁盘管理实例
3.3.1 创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统 ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项
#创建2G分区
[root@CentOS8 ~]#fdisk -l
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 211814399 209715200 100G 83 Linux
/dev/sda3 211814400 218105855 6291456 3G 82 Linux swap / Solaris
/dev/sda4 218105856 419430399 201324544 96G 5 Extended
/dev/sda5 218107904 322965503 104857600 50G 83 Linux
[root@CentOS8 ~]#fdisk /dev/sda
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.
Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (322967552-419430399, default 322967552):
Last sector, +sectors or +size{K,M,G,T,P} (322967552-419430399, default 419430399): +2G
Created a new partition 6 of type 'Linux' and of size 2 GiB.
Command (m for help): p
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 211814399 209715200 100G 83 Linux
/dev/sda3 211814400 218105855 6291456 3G 82 Linux swap / Solaris
/dev/sda4 218105856 419430399 201324544 96G 5 Extended
/dev/sda5 218107904 322965503 104857600 50G 83 Linux
/dev/sda6 322967552 327161855 4194304 2G 83 Linux
Command (m for help): w
The partition table has been altered.
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 3G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 50G 0 part /data
└─sda6 8:6 0 2G 0 part
sr0 11:0 1 8.6G 0 rom
#创建2G ext4文件系统
[root@CentOS8 ~]#mkfs.ext4 -b 2048 -L 'TEST' -m 1 /dev/sda6
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 1048576 2k blocks and 131072 inodes
Filesystem UUID: 344bd85b-cf28-4ace-aa25-a962f56a554f
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 ~]#blkid | grep /dev/sda6
/dev/sda6: LABEL="TEST" UUID="344bd85b-cf28-4ace-aa25-a962f56a554f" BLOCK_SIZE="2048" TYPE="ext4" PARTUUID="3bce8aa0-06"
#创建text挂载点并添加挂载配置使开机自动挂载
[root@CentOS8 ~]#mkdir /test
[root@CentOS8 /]#vim /etc/fstab
UUID=344bd85b-cf28-4ace-aa25-a962f56a554f /test ext4 defaults 0 0
#设置默认挂载acl选项
[root@CentOS8 ~]#tune2fs -o acl /dev/sda6
[root@CentOS8 /]#reboot
[root@CentOS8 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 2031306 9236 1992908 1% /test
[root@CentOS8 ~]#tune2fs -l /dev/sda6
tune2fs 1.45.6 (20-Mar-2020)
Filesystem volume name: TEST
Last mounted on: <not available>
Filesystem UUID: 344bd85b-cf28-4ace-aa25-a962f56a554f
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery 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 #分区挂载选项支持acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
3.3.2 创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小 为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录
#创建两个分区,/dev/sda7和/dev/sda8 大小均为10G 且分区格式为lvm
[root@CentOS8 ~]#fdisk /dev/sda
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.
Command (m for help): p
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 211814399 209715200 100G 83 Linux
/dev/sda3 211814400 218105855 6291456 3G 82 Linux swap / Solaris
/dev/sda4 218105856 419430399 201324544 96G 5 Extended
/dev/sda5 218107904 322965503 104857600 50G 83 Linux
/dev/sda6 322967552 327161855 4194304 2G 83 Linux
Command (m for help): n
All primary partitions are in use.
Adding logical partition 7
First sector (327163904-419430399, default 327163904):
Last sector, +sectors or +size{K,M,G,T,P} (327163904-419430399, default 419430399): +10G
Created a new partition 7 of type 'Linux' and of size 10 GiB.
Command (m for help): t
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): n
All primary partitions are in use.
Adding logical partition 8
First sector (348137472-419430399, default 348137472):
Last sector, +sectors or +size{K,M,G,T,P} (348137472-419430399, default 419430399): +10G
Created a new partition 8 of type 'Linux' and of size 10 GiB.
Command (m for help): t
Partition number (1-8, default 8): 8
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: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 211814399 209715200 100G 83 Linux
/dev/sda3 211814400 218105855 6291456 3G 82 Linux swap / Solaris
/dev/sda4 218105856 419430399 201324544 96G 5 Extended
/dev/sda5 218107904 322965503 104857600 50G 83 Linux
/dev/sda6 322967552 327161855 4194304 2G 83 Linux
/dev/sda7 327163904 348135423 20971520 10G 8e Linux LVM
/dev/sda8 348137472 369108991 20971520 10G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks.
#安装lvm管理工具,查看工具包安装文件
[root@CentOS8 ~]#yum -y install lvm2
[root@CentOS8 ~]#rpm -ql lvm2
#创建物理卷PV
[root@CentOS8 ~]#pvcreate /dev/sda7
Physical volume "/dev/sda7" successfully created.
[root@CentOS8 ~]#pvcreate /dev/sda8
Physical volume "/dev/sda8" successfully created.
[root@CentOS8 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda7 lvm2 --- 10.00g 10.00g
/dev/sda8 lvm2 --- 10.00g 10.00g
#创建卷组VG,且PE大小为16MB
[root@CentOS8 ~]#vgcreate -s 16M testvg /dev/sda{7,8}
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 ~]#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 3peGQY-6UoH-3haX-rQCG-OeEY-jFcj-wKPFNu
#创建逻辑卷LV,大小为5G的testlv
[root@CentOS8 ~]#lvcreate -n testlv -L 5G testvg
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 c2bGkH-5o3O-pM60-Kvxd-VoxM-bzoz-Nlgr0S
LV Write Access read/write
LV Creation host, time CentOS8.localdomain, 2021-03-26 06:21:59 +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 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
testvg 2 1 0 wz--n- <19.97g <14.97g
#创建ext4文件系统并自动挂载/users目录
[root@CentOS8 ~]#mkfs.ext4 /dev/testvg/testlv
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: 2b540cd3-9f3a-4617-8940-1ae155b51474
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 ~]#blkid
/dev/sda7: UUID="F3GTs3-X70f-lifj-Cjbz-QCp5-tk7F-3Qz3q0" TYPE="LVM2_member" PARTUUID="3bce8aa0-07"
/dev/sda8: UUID="Qk2QT3-fUdm-w4QT-3TDO-BUbM-kZbv-PEPNC2" TYPE="LVM2_member" PARTUUID="3bce8aa0-08"
/dev/sr0: BLOCK_SIZE="2048" UUID="2020-11-18-21-39-52-00" LABEL="CentOS-8-3-2011-x86_64-dvd" TYPE="iso9660" PTUUID="6b8b4567" PTTYPE="dos"
/dev/mapper/testvg-testlv: UUID="2b540cd3-9f3a-4617-8940-1ae155b51474" BLOCK_SIZE="4096" TYPE="ext4"
[root@CentOS8 ~]#mkdir /users
[root@CentOS8 ~]#vim /etc/fstab
#最后一行,添加
UUID=2b540cd3-9f3a-4617-8940-1ae155b51474 /users ext4 defaults 0 0
#手动挂载文件系统
[root@CentOS8 ~]#mount -a
[root@CentOS8 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv 4.9G 20M 4.6G 1% /users