磁盘存储和文件系统
前言:Linux中使用一块空硬盘三步:
1.设备分区(fdisk、gdisk)
2.创建文件系统(mkfs)
3.挂载新的文件系统(mount)
1.查看磁盘具体信息
#CentOS6可以显示磁盘的CHS的具体信息(cylinder、head、sector/track)
[root@centos6 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000898ed
Device Boot Start End Blocks Id System
/dev/sda1 * 1 262 2097152 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 262 523 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 523 574 409600 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 574 26109 205110272 5 Extended
/dev/sda5 574 26109 205109248 83 Linux
[root@centos6 ~]#
#CentOS7、8已经看不见了
[root@CentOS7 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a9850
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 216008703 2097152 82 Linux swap / Solaris
/dev/sda4 216008704 419430399 101710848 5 Extended
/dev/sda5 216010752 320868351 52428800 83 Linux
[root@CentOS7 ~]#
[root@centos8 ~]#fdisk -l
Disk /dev/sdc: 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: gpt
Disk identifier: AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
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: 0x64f77bbe
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 209717247 209715200 100G 83 Linux
/dev/sda2 209717248 314574847 104857600 50G 83 Linux
/dev/sda3 314574848 318769151 4194304 2G 82 Linux swap / Solaris
/dev/sda4 318769152 419430399 100661248 48G 5 Extended
/dev/sda5 * 318773248 320870399 2097152 1G 83 Linux
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
#通过加入具体的参数可以看到CHS的信息
[root@CentOS7 ~]# fdisk -u=cylinder -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a9850
Device Boot Start End Blocks Id System
/dev/sda1 * 1 131 1048576 83 Linux
/dev/sda2 131 13185 104857600 83 Linux
/dev/sda3 13185 13446 2097152 82 Linux swap / Solaris
/dev/sda4 13446 26109 101710848 5 Extended
/dev/sda5 13447 19974 52428800 83 Linux
[root@centos8 ~]#fdisk -u=cylinder -l
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Geometry: 255 heads, 63 sectors/track, 1305 cylinders
Units: cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Geometry: 255 heads, 2 sectors/track, 26108 cylinders
Units: cylinders of 510 * 512 = 261120 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x64f77bbe
Device Boot Start End Cylinders Size Id Type
/dev/sda1 5 411211 411207 100G 83 Linux
/dev/sda2 411211 616814 205604 50G 83 Linux
/dev/sda3 616814 625038 8225 2G 82 Linux swap / Solaris
/dev/sda4 625038 822413 197375 48G 5 Extended
/dev/sda5 * 625046 629158 4113 1G 83 Linux
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Geometry: 255 heads, 63 sectors/track, 2610 cylinders
Units: cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xda250a9c
#识别SSD和机械硬盘类型 1表示机械,0表示SSD
[root@CentOS8 ~]# lsblk -d -o name,rota
NAME ROTA
sda 1
sr0 1
sr1 1
nvme0n1 0
[root@CentOS8 ~]
2.查看分区表信息
MBR和GPT分区的区别:
- MBR最多只能管理4个主分区或3个主分区和1个扩展分区(N个逻辑分区),而GPT最多可以管理128个分区;
- MBR仅支持32位的逻辑块地址(LBA),而GPT支持64位的LBA;
- GPT支持磁盘自我修复和备份功能,可以防止分区表损坏;
- MBR无法管理超过2.2TB的硬盘,而GPT没有这个限制。
#查看MBR的相关信息,前512字节,其中最后标志位55 aa是结束位,前面的64字节是跟分区表相关的
[root@centos8 ~]#hexdump -C -n 512 /dev/sda
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u|
00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 01 8b |.........|...t..|
00000040 4c 02 cd 13 ea 00 7c 00 00 eb fe 00 00 00 00 00 |L.....|.........|
00000050 00 00 00 00 00 00 00 00 00 00 00 80 01 00 00 00 |................|
00000060 00 00 00 00 ff fa 90 90 f6 c2 80 74 05 f6 c2 70 |...........t...p|
00000070 74 02 b2 80 ea 79 7c 00 00 31 c0 8e d8 8e d0 bc |t....y|..1......|
00000080 00 20 fb a0 64 7c 3c ff 74 02 88 c2 52 be 05 7c |. ..d|<.t...R..||
00000090 b4 41 bb aa 55 cd 13 5a 52 72 3d 81 fb 55 aa 75 |.A..U..ZRr=..U.u|
000000a0 37 83 e1 01 74 32 31 c0 89 44 04 40 88 44 ff 89 |7...t21..D.@.D..|
000000b0 44 02 c7 04 10 00 66 8b 1e 5c 7c 66 89 5c 08 66 |D.....f..\|f.\.f|
000000c0 8b 1e 60 7c 66 89 5c 0c c7 44 06 00 70 b4 42 cd |..`|f.\..D..p.B.|
000000d0 13 72 05 bb 00 70 eb 76 b4 08 cd 13 73 0d 5a 84 |.r...p.v....s.Z.|
000000e0 d2 0f 83 de 00 be 85 7d e9 82 00 66 0f b6 c6 88 |.......}...f....|
000000f0 64 ff 40 66 89 44 04 0f b6 d1 c1 e2 02 88 e8 88 |d.@f.D..........|
00000100 f4 40 89 44 08 0f b6 c2 c0 e8 02 66 89 04 66 a1 |.@.D.......f..f.|
00000110 60 7c 66 09 c0 75 4e 66 a1 5c 7c 66 31 d2 66 f7 |`|f..uNf.\|f1.f.|
00000120 34 88 d1 31 d2 66 f7 74 04 3b 44 08 7d 37 fe c1 |4..1.f.t.;D.}7..|
00000130 88 c5 30 c0 c1 e8 02 08 c1 88 d0 5a 88 c6 bb 00 |..0........Z....|
00000140 70 8e c3 31 db b8 01 02 cd 13 72 1e 8c c3 60 1e |p..1......r...`.|
00000150 b9 00 01 8e db 31 f6 bf 00 80 8e c6 fc f3 a5 1f |.....1..........|
00000160 61 ff 26 5a 7c be 80 7d eb 03 be 8f 7d e8 34 00 |a.&Z|..}....}.4.|
00000170 be 94 7d e8 2e 00 cd 18 eb fe 47 52 55 42 20 00 |..}.......GRUB .|
00000180 47 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 |Geom.Hard Disk.R|
00000190 65 61 64 00 20 45 72 72 6f 72 0d 0a 00 bb 01 00 |ead. Error......|
000001a0 b4 0e cd 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 |.....<.u........|
000001b0 00 00 00 00 00 00 00 00 be 7b f7 64 00 00 00 04 |.........{.d....| #00 44就是分区表的开头
000001c0 01 04 83 fe c2 ff 00 08 00 00 00 00 80 0c 00 fe |................| # 83 下面这四个表示的类型
000001d0 c2 ff 83 fe c2 ff 00 08 80 0c 00 00 40 06 00 fe |............@...| # 83
000001e0 c2 ff 82 fe c2 ff 00 08 c0 12 00 00 40 00 00 fe |............@...| # 82
000001f0 c2 ff 05 fe c2 ff 00 08 00 13 00 f8 ff 05 55 aa |..............U.| # 05
00000200 #55 aa 是结束位标志
[root@centos8 ~]#
#fdisk -l这个命令获取的相关信息就是通过分区表获取到的然后整合好的
[root@centos8 ~]#fdisk -l
Disk /dev/sdc: 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: gpt
Disk identifier: AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
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: 0x64f77bbe
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 209717247 209715200 100G 83 Linux # Id这一列就是分区表上面的类型 83、83、82、5、83
/dev/sda2 209717248 314574847 104857600 50G 83 Linux
/dev/sda3 314574848 318769151 4194304 2G 82 Linux swap / Solaris
/dev/sda4 318769152 419430399 100661248 48G 5 Extended
/dev/sda5 * 318773248 320870399 2097152 1G 83 Linux
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
[root@centos8 ~]#
2.1备份MBR中的分区表
由于MBR分区表损坏的情况下,那么分区就会搞不清,自然而然数据也就无法访问了。所以下面如何备份MBR中的64byte的分区表,MBR前512字节是描述信息,减去后面的64字节分区表,和最后两字节的结束标志就是446字节
#备份MBR中的分区表
#这里用到"dd"工具,通常用于复制文件、将数据从一个设备复制到另一个设备 、备份和还原磁盘映像等操作。详细用法参考"Linux命令大全详解"
[root@centos8 ~]#dd if=/dev/sda of=/data/mbr bs=1 count=64 skip=446
64+0 records in
64+0 records out
64 bytes copied, 0.000172599 s, 371 kB/s
[root@centos8 ~]#ll /data/mbr
-rw-r--r-- 1 root root 64 Mar 3 22:17 /data/mbr
[root@centos8 ~]#
#查看详细信息
[root@centos8 ~]#hexdump -C /data/mbr
00000000 00 04 01 04 83 fe c2 ff 00 08 00 00 00 00 80 0c |................|
00000010 00 fe c2 ff 83 fe c2 ff 00 08 80 0c 00 00 40 06 |..............@.|
00000020 00 fe c2 ff 82 fe c2 ff 00 08 c0 12 00 00 40 00 |..............@.|
00000030 00 fe c2 ff 05 fe c2 ff 00 08 00 13 00 f8 ff 05 |................|
00000040
[root@centos8 ~]#
#如果是备份的话,最好是异地备份这样才有效
[root@centos8 ~]#scp /data/mbr 10.0.0.102:
2.2 破坏分区表
#下面模拟破坏分区表以后,使用备份的分区表进行恢复
#思路:将字符设备/dev/zero写入到分区表当中
[root@centos8 ~]#dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
64+0 records in
64+0 records out
64 bytes copied, 0.00017075 s, 375 kB/s
[root@centos8 ~]#
#查看分区表是否被破坏
[root@centos8 ~]#hexdump -C /dev/sda -n 512 -v
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u|
00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 01 8b |.........|...t..|
00000040 4c 02 cd 13 ea 00 7c 00 00 eb fe 00 00 00 00 00 |L.....|.........|
00000050 00 00 00 00 00 00 00 00 00 00 00 80 01 00 00 00 |................|
00000060 00 00 00 00 ff fa 90 90 f6 c2 80 74 05 f6 c2 70 |...........t...p|
00000070 74 02 b2 80 ea 79 7c 00 00 31 c0 8e d8 8e d0 bc |t....y|..1......|
00000080 00 20 fb a0 64 7c 3c ff 74 02 88 c2 52 be 05 7c |. ..d|<.t...R..||
00000090 b4 41 bb aa 55 cd 13 5a 52 72 3d 81 fb 55 aa 75 |.A..U..ZRr=..U.u|
000000a0 37 83 e1 01 74 32 31 c0 89 44 04 40 88 44 ff 89 |7...t21..D.@.D..|
000000b0 44 02 c7 04 10 00 66 8b 1e 5c 7c 66 89 5c 08 66 |D.....f..\|f.\.f|
000000c0 8b 1e 60 7c 66 89 5c 0c c7 44 06 00 70 b4 42 cd |..`|f.\..D..p.B.|
000000d0 13 72 05 bb 00 70 eb 76 b4 08 cd 13 73 0d 5a 84 |.r...p.v....s.Z.|
000000e0 d2 0f 83 de 00 be 85 7d e9 82 00 66 0f b6 c6 88 |.......}...f....|
000000f0 64 ff 40 66 89 44 04 0f b6 d1 c1 e2 02 88 e8 88 |d.@f.D..........|
00000100 f4 40 89 44 08 0f b6 c2 c0 e8 02 66 89 04 66 a1 |.@.D.......f..f.|
00000110 60 7c 66 09 c0 75 4e 66 a1 5c 7c 66 31 d2 66 f7 |`|f..uNf.\|f1.f.|
00000120 34 88 d1 31 d2 66 f7 74 04 3b 44 08 7d 37 fe c1 |4..1.f.t.;D.}7..|
00000130 88 c5 30 c0 c1 e8 02 08 c1 88 d0 5a 88 c6 bb 00 |..0........Z....|
00000140 70 8e c3 31 db b8 01 02 cd 13 72 1e 8c c3 60 1e |p..1......r...`.|
00000150 b9 00 01 8e db 31 f6 bf 00 80 8e c6 fc f3 a5 1f |.....1..........|
00000160 61 ff 26 5a 7c be 80 7d eb 03 be 8f 7d e8 34 00 |a.&Z|..}....}.4.|
00000170 be 94 7d e8 2e 00 cd 18 eb fe 47 52 55 42 20 00 |..}.......GRUB .|
00000180 47 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 |Geom.Hard Disk.R|
00000190 65 61 64 00 20 45 72 72 6f 72 0d 0a 00 bb 01 00 |ead. Error......|
000001a0 b4 0e cd 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 |.....<.u........|
000001b0 00 00 00 00 00 00 00 00 be 7b f7 64 00 00 00 00 |.........{.d....|
000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200
[root@centos8 ~]#
#使用fdisk查看已经看不到分区情况
[root@centos8 ~]#fdisk -l
Disk /dev/sdc: 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: gpt
Disk identifier: AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
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: 0x64f77bbe
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
[root@centos8 ~]#
2.3 恢复分区表
#1.使用救援模式:按下Ctrl+Alt+Insert,0.5秒内只按一下ESc进入到选择光驱(CD-ROM Drive)界面,
#2.然后进去救援模式(Troubleshooting),故障排除
#3.选择"Rescure a CentOS Linux system",将备份到其他机器上的分区表通过scp拷贝回来,但是救援模式是没有网络的
#4.提示:Please make a selection from then above:1
#5.Please press ENTER get a shell:按下回车
sh-4.4#ip a
可以看到,ens160这个网卡并没有ip

#给当前网卡ens160添加IP地址
sh-4.4#ip a a 10.0.0.100/24 dev ens160
sh-4.4#ip a #再次查看发现已经有ip了,10.0.0.100
#ping一下MBR分区表远程备份的机器是否通达
sh-4.4#ping 10.0.0.102
sh-4.4#scp 10.0.0.102:/root/mbr .
#查看分区表
sh-4.4#hexdump -C -n 512 /dev/sda -v
#使用dd命令进行恢复
sh-4.4#dd if=mbr of=/dev/sda bs=1 count=64 seek=446
#再次使用hexdump查看恢复成功

重启即可!!!Ctrl+Alt+Insert
3.MBR分区
3.1划分主分区
#查看当前的分区
[root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 50G 0 part /data
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part /boot
sdb 8:16 0 20G 0 disk
sdc 8:32 0 10G 0 disk
sr0 11:0 1 9.5G 0 rom
sr1 11:1 1 10.1G 0 rom
[root@centos8 ~]#
#下面对sdb这块硬盘进行分区
#创建MBR分区,必须现有主分区或者扩展分区,有了扩展分区才能创建逻辑分区
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.
Command (m for help): n #输入n创建新的分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p #输入p,创建主分区
Partition number (1-4, default 1): 1 #1-4是主分区或者扩展分区,5以后才是逻辑分区
First sector (2048-41943039, default 2048): #回车,默认从起始位置开始划分扇区
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G #划分多少扇区,这里我们直接给容量,至于扇区的数量让他自己计算
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): p #输入p查看当前的分区
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
3.2划分扩展分区
#上面已经划分了一个主分区,下面划分扩展分区
Command (m for help): n #继续划分
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e #选择扩展分区
Partition number (2-4, default 2): #默认回车就是编号2扩展分区
First sector (4196352-41943039, default 4196352): #默认回车从之前划分主分区1结束的位置开始划分
Last sector, +sectors or +size{K,M,G,T,P} (4196352-41943039, default 41943039): +10G #10G的空间,自动计算扇区数量
Created a new partition 2 of type 'Extended' and of size 10 GiB.
Command (m for help): p #查看当前的分区
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 25167871 20971520 10G 5 Extended
Command (m for help):
3.3划分逻辑分区
#扩展分区不能直接使用,必须划分逻辑分区才可以使用
Command (m for help): n #继续划分逻辑分区
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5) #可以看到这里的选项已经变成了逻辑分区
Select (default p): l #选择逻辑分区
Adding logical partition 5 #逻辑分区的编号是系统自动选择,认为无法干涉
First sector (4198400-25167871, default 4198400): #选择逻辑分区默认开始的位置
Last sector, +sectors or +size{K,M,G,T,P} (4198400-25167871, default 25167871): +2G #划分容量为2G,自动计算扇区
Created a new partition 5 of type 'Linux' and of size 2 GiB.
Command (m for help): p #查看当前分区
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 25167871 20971520 10G 5 Extended
/dev/sdb5 4198400 8392703 4194304 2G 83 Linux
Command (m for help):w #w是存盘退出,系统才会对磁盘划分分区,如果是quit代表不存盘退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos8 ~]#
#如果想要删除里面的分区,使用d命令。假如里面现在有sdb5、sdb6两个分区,如果将sdb5分区删掉以后,再次查看里面的sdb5还在,这是因为sdb6这块硬盘自动改名成为了sdb5,这就代表Linux里面分区的名字是不稳定的。
#查看是否分区
[root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 50G 0 part /data
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part /boot
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 10G 0 disk
sr0 11:0 1 9.5G 0 rom
sr1 11:1 1 10.1G 0 rom
[root@centos8 ~]#
4.GPT分区
#使用GPT进行分区
[root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 50G 0 part /data
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part /boot
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 10G 0 disk
sr0 11:0 1 9.5G 0 rom
sr1 11:1 1 10.1G 0 rom
[root@centos8 ~]#
#现在对sdc这块磁盘使用GPT进行分区
[root@centos8 ~]#gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n #gdisk和fdisk的命令一样,使用n进行划分分区
Partition number (1-128, default 1): #选择分区的编号,gpt有128个分区,默认是1
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: #默认划分的位置
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: +3G #空间3G,计算扇区数量
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): #默认类型
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sdc: 20971520 sectors, 10.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 14679997 sectors (7.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 6293503 3.0 GiB 8300 Linux filesystem
Command (? for help):w #保存退出
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y #确定
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@centos8 ~]#
#通过fdisk -l查看
[root@centos8 ~]#fdisk -l
Disk /dev/sdc: 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: gpt #GPT分区
Disk identifier: AEA0EA65-4F91-4841-9BC3-E9530AED5DD0
Device Start End Sectors Size Type
/dev/sdc1 2048 6293503 6291456 3G Linux filesystem
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 #dos代表MBR分区
Disk identifier: 0x64f77bbe
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 209717247 209715200 100G 83 Linux
/dev/sda2 209717248 314574847 104857600 50G 83 Linux
/dev/sda3 314574848 318769151 4194304 2G 82 Linux swap / Solaris
/dev/sda4 318769152 419430399 100661248 48G 5 Extended
/dev/sda5 * 318773248 320870399 2097152 1G 83 Linux
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xda250a9c
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 25167871 20971520 10G 5 Extended
/dev/sdb5 4198400 8392703 4194304 2G 83 Linux
[root@centos8 ~]#
5.分区没生效
我们上面划分的是一块新的硬盘,如果是对已经使用的硬盘进行划分分区的话,很可能划分以后,看不到分区,这是因为划分以后分区表必须加载到内存当中系统才会识别到,否则看不到。
#划分sda
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 250G 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
sdb 8:16 0 20G 0 disk
sdc 8:32 0 10G 0 disk
sr0 11:0 1 4.4G 0 rom
[root@centos7 ~]#
[root@centos7 ~]#fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (316674048-419430399,默认为 316674048):
将使用默认值 316674048
Last 扇区, +扇区 or +size{K,M,G} (316674048-419430399,默认为 419430399):+1G
分区 5 已设置为 Linux 类型,大小设为 1 GiB
命令(输入 m 获取帮助):p #可以看到已经划分了sda5这个分区
磁盘 /dev/sda:268.4 GB, 268435456000 字节,524288000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000bfad6
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 316671999 52428800 83 Linux
/dev/sda4 316672000 419430399 51379200 5 Extended
/dev/sda5 316674048 318771199 1048576 83 Linux
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. #可以看到这里已经告警了
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@centos7 ~]#
#查看是否有sda5这个分区
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 250G 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
sdb 8:16 0 20G 0 disk
sdc 8:32 0 10G 0 disk
sr0 11:0 1 4.4G 0 rom
[root@centos7 ~]#
#虽然看不到sda5这个分区,但其实已经划分成功了
root@centos7 ~]#fdisk /dev/sda -l
磁盘 /dev/sda:268.4 GB, 268435456000 字节,524288000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000bfad6
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 316671999 52428800 83 Linux
/dev/sda4 316672000 419430399 51379200 5 Extended
/dev/sda5 316674048 318771199 1048576 83 Linux
[root@centos7 ~]#
#原因就是没有加载到内存中,centos7有这个问题,centos8会自动更新
[root@centos7 ~]#partprobe #加载到内存中
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 250G 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 1G 0 part
sdb 8:16 0 20G 0 disk
sdc 8:32 0 10G 0 disk
sr0 11:0 1 4.4G 0 rom
[root@centos7 ~]#
#已经生效了
#Centos6同步比较特殊
#删除分区同步:partx -a --nr 6 /dev/sda #6是分区的编号或者6-7
#增加分区同步:partx -a /dev/sda
6.文件系统
#查看分区的文件系统
[root@centos8 ~]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 /
├─sda2 xfs 933c390a-5904-4b01-aa6a-447c771b7f08 /data
├─sda3 swap 98f53a44-1fb7-4139-85c6-a3c2d1633cd7 [SWAP]
├─sda4
├─sda5 ext4 8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot
└─sda6
sdb
├─sdb1
├─sdb2
└─sdb5
sdc
└─sdc1
sr0 iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00
sr1 iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00
[root@centos8 ~]#
#查看有哪些文件系统
[root@centos8 ~]#ls /lib/modules/`uname -r`/kernel/fs
binfmt_misc.ko.xz cachefiles ceph cifs cramfs dlm ext4 fat fscache fuse gfs2 isofs jbd2 lockd mbcache.ko.xz nfs nfs_common nfsd nls overlayfs pstore squashfs udf xfs
[root@centos8 ~]#
[root@centos8 ~]#mkfs.ext4 /dev/sdb1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: 2d508d02-801c-411d-bbcb-471d18142dd3
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 ~]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 /
├─sda2 xfs 933c390a-5904-4b01-aa6a-447c771b7f08 /data
├─sda3 swap 98f53a44-1fb7-4139-85c6-a3c2d1633cd7 [SWAP]
├─sda4
├─sda5 ext4 8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot
└─sda6
sdb
├─sdb1 ext4 2d508d02-801c-411d-bbcb-471d18142dd3
├─sdb2
└─sdb5
sdc
└─sdc1
sr0 iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00
sr1 iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00
[root@centos8 ~]#
#查看uuid
[root@centos8 ~]#blkid
/dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="c124261d-b1fd-41bf-94c5-0c6d1b87504a"
/dev/sda1: UUID="ad19dd45-b7ee-485c-87e6-3c9bb8ec3996" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="64f77bbe-01"
/dev/sda2: UUID="933c390a-5904-4b01-aa6a-447c771b7f08" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="64f77bbe-02"
/dev/sda3: UUID="98f53a44-1fb7-4139-85c6-a3c2d1633cd7" TYPE="swap" PARTUUID="64f77bbe-03"
/dev/sda5: UUID="8fbb4a9c-1506-4c4a-889d-b78163091bb0" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="64f77bbe-05"
/dev/sda6: PARTUUID="64f77bbe-06"
/dev/sdb1: UUID="2d508d02-801c-411d-bbcb-471d18142dd3" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="da250a9c-01"
/dev/sdb5: PARTUUID="da250a9c-05"
/dev/sr0: BLOCK_SIZE="2048" UUID="2020-11-02-15-15-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTUUID="6b8b4567" PTTYPE="dos"
/dev/sr1: BLOCK_SIZE="2048" UUID="2021-11-13-01-04-26-00" LABEL="CentOS-8-5-2111-x86_64-dvd" TYPE="iso9660" PTUUID="5fb10f71" PTTYPE="dos"
[root@centos8 ~]#
#继续分区,这次使用xfs
[root@centos8 ~]#mkfs.xfs /dev/sdb2
mkfs.xfs: /dev/sdb2 appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos8 ~]#
#报错的原因是因为/dev/sdb2是一个扩展分区,无法直接使用,只能对逻辑分区进行划分文件系统
[root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 50G 0 part /data
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 1G 0 part /boot
└─sda6 8:6 0 1M 0 part
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 1K 0 part #大小只有1k,是一个逻辑分区
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 3G 0 part
sr0 11:0 1 9.5G 0 rom
sr1 11:1 1 10.1G 0 rom
[root@centos8 ~]#
[root@centos8 ~]#mkfs.xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos8 ~]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 /
├─sda2 xfs 933c390a-5904-4b01-aa6a-447c771b7f08 /data
├─sda3 swap 98f53a44-1fb7-4139-85c6-a3c2d1633cd7 [SWAP]
├─sda4
├─sda5 ext4 8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot
└─sda6
sdb
├─sdb1 ext4 2d508d02-801c-411d-bbcb-471d18142dd3
├─sdb2
└─sdb5 xfs ae3ec919-7b0d-49d9-958e-6994c6c2d3ca
sdc
└─sdc1
sr0 iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00
sr1 iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00
[root@centos8 ~]#
#下面命令查看ext2、ext3、ext4文件系统的超级块的信息,描述当前分区有哪些块组,以及当前文件系统的元数据、特性。
root@centos8 ~]#dumpe2fs /dev/sdb1
dumpe2fs 1.45.6 (20-Mar-2020)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: 2d508d02-801c-411d-bbcb-471d18142dd3
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: 131072
Block count: 524288
Reserved block count: 26214
Free blocks: 498132
Free inodes: 131061
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: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Sun Mar 5 13:17:59 2023
Last mount time: n/a
Last write time: Sun Mar 5 13:17:59 2023
Mount count: 0
Maximum mount count: -1
Last checked: Sun Mar 5 13:17:59 2023
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: 68d605e3-35cc-4cf7-b4d2-8d01320ce2fc
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0xb431b3b9
Journal features: (none)
Journal size: 64M
Journal length: 16384
Journal sequence: 0x00000001
Journal start: 0
Group 0: (Blocks 0-32767) csum 0x19ce
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-256
Block bitmap at 257 (+257), csum 0xa34592c3
Inode bitmap at 273 (+273), csum 0x4a068dfb
Inode table at 289-800 (+289)
24281 free blocks, 8181 free inodes, 2 directories, 8181 unused inodes
Free blocks: 8487-32767
Free inodes: 12-8192
Group 1: (Blocks 32768-65535) csum 0x05af [INODE_UNINIT, BLOCK_UNINIT]
Backup superblock at 32768, Group descriptors at 32769-32769
Reserved GDT blocks at 32770-33024
Block bitmap at 258 (bg #0 + 258), csum 0x00000000
Inode bitmap at 274 (bg #0 + 274), csum 0x00000000
Inode table at 801-1312 (bg #0 + 801)
32511 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 33025-65535
Free inodes: 8193-16384
Group 2: (Blocks 65536-98303) csum 0xfcb6 [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 259 (bg #0 + 259), csum 0x00000000
Inode bitmap at 275 (bg #0 + 275), csum 0x00000000
Inode table at 1313-1824 (bg #0 + 1313)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 65536-98303
Free inodes: 16385-24576
Group 3: (Blocks 98304-131071) csum 0x626c [INODE_UNINIT, BLOCK_UNINIT]
Backup superblock at 98304, Group descriptors at 98305-98305
Reserved GDT blocks at 98306-98560
Block bitmap at 260 (bg #0 + 260), csum 0x00000000
Inode bitmap at 276 (bg #0 + 276), csum 0x00000000
Inode table at 1825-2336 (bg #0 + 1825)
32511 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 98561-131071
Free inodes: 24577-32768
Group 4: (Blocks 131072-163839) csum 0x235f [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 261 (bg #0 + 261), csum 0x00000000
Inode bitmap at 277 (bg #0 + 277), csum 0x00000000
Inode table at 2337-2848 (bg #0 + 2337)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 131072-163839
Free inodes: 32769-40960
Group 5: (Blocks 163840-196607) csum 0x90e1 [INODE_UNINIT, BLOCK_UNINIT]
Backup superblock at 163840, Group descriptors at 163841-163841
Reserved GDT blocks at 163842-164096
Block bitmap at 262 (bg #0 + 262), csum 0x00000000
Inode bitmap at 278 (bg #0 + 278), csum 0x00000000
Inode table at 2849-3360 (bg #0 + 2849)
32511 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 164097-196607
Free inodes: 40961-49152
Group 6: (Blocks 196608-229375) csum 0x69f8 [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 263 (bg #0 + 263), csum 0x00000000
Inode bitmap at 279 (bg #0 + 279), csum 0x00000000
Inode table at 3361-3872 (bg #0 + 3361)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 196608-229375
Free inodes: 49153-57344
Group 7: (Blocks 229376-262143) csum 0xadea [INODE_UNINIT, BLOCK_UNINIT]
Backup superblock at 229376, Group descriptors at 229377-229377
Reserved GDT blocks at 229378-229632
Block bitmap at 264 (bg #0 + 264), csum 0x00000000
Inode bitmap at 280 (bg #0 + 280), csum 0x00000000
Inode table at 3873-4384 (bg #0 + 3873)
32511 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 229633-262143
Free inodes: 57345-65536
Group 8: (Blocks 262144-294911) csum 0x5289 [INODE_UNINIT]
Block bitmap at 265 (bg #0 + 265), csum 0xe0342d43
Inode bitmap at 281 (bg #0 + 281), csum 0x00000000
Inode table at 4385-4896 (bg #0 + 4385)
16384 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 278528-294911
Free inodes: 65537-73728
Group 9: (Blocks 294912-327679) csum 0x59c2 [INODE_UNINIT, BLOCK_UNINIT]
Backup superblock at 294912, Group descriptors at 294913-294913
Reserved GDT blocks at 294914-295168
Block bitmap at 266 (bg #0 + 266), csum 0x00000000
Inode bitmap at 282 (bg #0 + 282), csum 0x00000000
Inode table at 4897-5408 (bg #0 + 4897)
32511 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 295169-327679
Free inodes: 73729-81920
Group 10: (Blocks 327680-360447) csum 0xa0db [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 267 (bg #0 + 267), csum 0x00000000
Inode bitmap at 283 (bg #0 + 283), csum 0x00000000
Inode table at 5409-5920 (bg #0 + 5409)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 327680-360447
Free inodes: 81921-90112
Group 11: (Blocks 360448-393215) csum 0x0526 [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 268 (bg #0 + 268), csum 0x00000000
Inode bitmap at 284 (bg #0 + 284), csum 0x00000000
Inode table at 5921-6432 (bg #0 + 5921)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 360448-393215
Free inodes: 90113-98304
Group 12: (Blocks 393216-425983) csum 0x7f32 [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 269 (bg #0 + 269), csum 0x00000000
Inode bitmap at 285 (bg #0 + 285), csum 0x00000000
Inode table at 6433-6944 (bg #0 + 6433)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 393216-425983
Free inodes: 98305-106496
Group 13: (Blocks 425984-458751) csum 0xf7ab [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 270 (bg #0 + 270), csum 0x00000000
Inode bitmap at 286 (bg #0 + 286), csum 0x00000000
Inode table at 6945-7456 (bg #0 + 6945)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 425984-458751
Free inodes: 106497-114688
Group 14: (Blocks 458752-491519) csum 0x3595 [INODE_UNINIT, BLOCK_UNINIT]
Block bitmap at 271 (bg #0 + 271), csum 0x00000000
Inode bitmap at 287 (bg #0 + 287), csum 0x00000000
Inode table at 7457-7968 (bg #0 + 7457)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 458752-491519
Free inodes: 114689-122880
Group 15: (Blocks 491520-524287) csum 0x86c1 [INODE_UNINIT]
Block bitmap at 272 (bg #0 + 272), csum 0x0fc34a5a
Inode bitmap at 288 (bg #0 + 288), csum 0x00000000
Inode table at 7969-8480 (bg #0 + 7969)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 491520-524287
Free inodes: 122881-131072
[root@centos8 ~]#
#查看xfs文件系统的信息
[root@centos8 ~]#xfs_info /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos8 ~]#
6.1修复文件系统
#修复但不一定还原数据,下面就一sdb1为例
[root@centos8 ~]#mount /dev/sdb1 /mnt/
[root@centos8 ~]#df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
devtmpfs devtmpfs 980332 0 980332 0% /dev
tmpfs tmpfs 998404 0 998404 0% /dev/shm
tmpfs tmpfs 998404 8936 989468 1% /run
tmpfs tmpfs 998404 0 998404 0% /sys/fs/cgroup
/dev/sda1 xfs 104806400 3847904 100958496 4% /
/dev/sda2 xfs 52403200 2035480 50367720 4% /data
/dev/sda5 ext4 999320 144364 786144 16% /boot
tmpfs tmpfs 199680 0 199680 0% /run/user/0
/dev/sdb1 ext4 1998672 6144 1871288 1% /mnt
[root@centos8 ~]#
[root@centos8 ~]#cp /etc/fstab /mnt/f1
[root@centos8 ~]#ls /mnt/
f1 lost+found
[root@centos8 ~]#
#使用tune2fs这个命令和dump2fs一样都是查看ext系列的信息,但是前者只查看超级块信息,不查看分组,这两个命令都不能查看xfs系列
[root@centos8 ~]#tune2fs /dev/sdb1 -l
tune2fs 1.45.6 (20-Mar-2020)
Filesystem volume name: <none>
Last mounted on: /mnt
Filesystem UUID: 2d508d02-801c-411d-bbcb-471d18142dd3
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
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 131072
Block count: 524288
Reserved block count: 26214
Free blocks: 498132
Free inodes: 131061
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: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Sun Mar 5 13:17:59 2023
Last mount time: Sun Mar 5 14:20:42 2023
Last write time: Sun Mar 5 14:20:42 2023
Mount count: 1
Maximum mount count: -1
Last checked: Sun Mar 5 13:17:59 2023
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: 68d605e3-35cc-4cf7-b4d2-8d01320ce2fc
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x8fae1806
[root@centos8 ~]#
#将0写入到文件中,就相当于破坏了文件
[root@centos8 ~]#dd if=/dev/zero of=/dev/sdb1 bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.000684753 s, 1.5 GB/s
[root@centos8 ~]#
#无法查看了
[root@centos8 ~]#tune2fs /dev/sdb1 -l
tune2fs 1.45.6 (20-Mar-2020)
tune2fs: Bad magic number in super-block while trying to open /dev/sdb1
[root@centos8 ~]#
#修复之前一定要先取消挂载
[root@centos8 ~]#umount /mnt
[root@centos8 ~]#fsck /dev/sdb1 -y
fsck from util-linux 2.32.1
e2fsck 1.45.6 (20-Mar-2020)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks...
/dev/sdb1 was not cleanly unmounted, check forced.
Resize inode not valid. Recreate? yes
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
Block bitmap differences: +(98304--98560) +(163840--164096) +(229376--229632) +(294912--295168)
Fix? yes
Free blocks count wrong for group #0 (24280, counted=24281).
Fix? yes
Free blocks count wrong for group #1 (32511, counted=32510).
Fix? yes
Free inodes count wrong for group #0 (8181, counted=8180).
Fix? yes
Free inodes count wrong (131061, counted=131060).
Fix? yes
Padding at end of inode bitmap is not set. Fix? yes
/dev/sdb1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdb1: 12/131072 files (0.0% non-contiguous), 26157/524288 blocks
[root@centos8 ~]#
#再次查看修复成功,发现文件也还在
[root@centos8 ~]#mount /dev/sdb1 /mnt/
[root@centos8 ~]#ls /mnt/
f1 lost+found
[root@centos8 ~]#
7.挂载
- 将文件系统挂载到指定的挂载点才可以使用。使用mount
- 一个挂载点同一时间只能挂载一个设备
- 一个挂载点同一时间挂载了多个设备,只能看到最后一个设备的数据,其他设备的数据将被隐藏
- 一个设备可以同时挂载到多个挂载点
- 通常挂载点一般是已存在空的目录
[root@centos8 ~]#mount /dev/sdb1 /mnt/
#/dev/sdb1是分区的名称
#/mnt/ 是关联的文件夹,是一个存在的并且是一个空的目录,如果不空的话,那么/mnt下数据会被隐藏
#取消挂载
[root@centos8 ~]#umount /mnt
#挂载的时候设置只读,因为默认是rw权限
[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=980332k,nr_inodes=245083,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/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
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/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda1 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=21285)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda2 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda5 on /boot type ext4 (rw,relatime)
/etc/auto.misc on /misc type autofs (rw,relatime,fd=5,pgrp=897,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=31314)
-hosts on /net type autofs (rw,relatime,fd=11,pgrp=897,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=31361)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=199680k,mode=700)
/dev/sdb1 on /mnt type ext4 (rw,relatime) #默认是读写权限
[root@centos8 ~]#
#因为/dev/sdb1已经挂载到了/mnt/下,所以想要重现挂载更改挂载选项的话,那么就一定要先取消之前挂载,再执行mount
[root@centos8 ~]#mount -o ro /dev/sdb1 /mnt/
mount: /mnt: /dev/sdb1 already mounted on /mnt.
[root@centos8 ~]
[root@centos8 ~]#mount /dev/sdb1 /mnt/
#/dev/sdb1是分区的名称
#/mnt/ 是关联的文件夹,是一个存在的并且是一个空的目录,如果不空的话,那么/mnt下数据会被隐藏
#取消挂载
[root@centos8 ~]#umount /mnt
#挂载的时候设置只读,因为默认是rw权限
[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=980332k,nr_inodes=245083,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/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
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/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda1 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=21285)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda2 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda5 on /boot type ext4 (rw,relatime)
/etc/auto.misc on /misc type autofs (rw,relatime,fd=5,pgrp=897,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=31314)
-hosts on /net type autofs (rw,relatime,fd=11,pgrp=897,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=31361)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=199680k,mode=700)
/dev/sdb1 on /mnt type ext4 (rw,relatime) #默认是读写权限
[root@centos8 ~]#
#因为/dev/sdb1已经挂载到了/mnt/下,所以想要重现挂载更改挂载选项的话,那么就一定要先取消之前挂载,再执行mount
[root@centos8 ~]#mount -o ro /dev/sdb1 /mnt/
mount: /mnt: /dev/sdb1 already mounted on /mnt.
[root@centos8 ~]
[root@centos8 ~]#umount /mnt
[root@centos8 ~]#mount -o ro /dev/sdb1 /mnt #这里的ro是:read-only
[root@centos8 ~]#touch /mnt/a.txt
touch: cannot touch '/mnt/a.txt': Read-only file system #可以看到已经不能创建文件了,可以使用mount查看是否变成了ro
[root@centos8 ~]#
#需要注意的是,umount的时候如果有人在访问文件夹是无法取消挂载的
root@centos8 ~]#cd /mnt/
[root@centos8 mnt]#umount /mnt
umount: /mnt: target is busy.
[root@centos8 mnt]#
#如果我们想看到谁在使用,使用fuser
[root@centos8 mnt]#fuser -v /mnt/
USER PID ACCESS COMMAND
/mnt: root kernel mount /mnt
root 2032 ..c.. bash #可以看到是root这个用户访问
[root@centos8 mnt]#
#强制退出使用用户,直接踢出终端了
[root@centos8 mnt]#fuser -km /mnt
/mnt: 2032c
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(10.0.0.100-CentOS8.5) at 19:11:34.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
#为了避免这种情况我们使用remount,相当于先umount,然后再mount,重新挂载一个已经挂载的文件系统,可以在保持其挂载点和参数不变的同时修改文件系统的某些挂载选项
[root@centos8 mnt]#mount -o remount,rw /mnt
#查看某个文件夹是否是真正的挂载点
[root@centos8 ~]#findmnt /data
TARGET SOURCE FSTYPE OPTIONS
/data /dev/sda2 xfs rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
[root@centos8 ~]#findmnt /mnt
TARGET SOURCE FSTYPE OPTIONS
/mnt /dev/sdb1 ext4 rw,relatime
[root@centos8 ~]#
#查看进程
[root@centos8 ~]#cd /mnt/
[root@centos8 mnt]#lsof /mnt/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 3009 root cwd DIR 8,17 4096 2 /mnt
lsof 3049 root cwd DIR 8,17 4096 2 /mnt
lsof 3050 root cwd DIR 8,17 4096 2 /mnt
[root@centos8 mnt]#
[root@centos8 ~]#umount /mnt
[root@centos8 ~]#mount -o ro /dev/sdb1 /mnt #这里的ro是:read-only
[root@centos8 ~]#touch /mnt/a.txt
touch: cannot touch '/mnt/a.txt': Read-only file system #可以看到已经不能创建文件了,可以使用mount查看是否变成了ro
[root@centos8 ~]#
#需要注意的是,umount的时候如果有人在访问文件夹是无法取消挂载的
root@centos8 ~]#cd /mnt/
[root@centos8 mnt]#umount /mnt
umount: /mnt: target is busy.
[root@centos8 mnt]#
#如果我们想看到谁在使用,使用fuser
[root@centos8 mnt]#fuser -v /mnt/
USER PID ACCESS COMMAND
/mnt: root kernel mount /mnt
root 2032 ..c.. bash #可以看到是root这个用户访问
[root@centos8 mnt]#
#强制退出使用用户,直接踢出终端了
[root@centos8 mnt]#fuser -km /mnt
/mnt: 2032c
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(10.0.0.100-CentOS8.5) at 19:11:34.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
#为了避免这种情况我们使用remount,相当于先umount,然后再mount,重新挂载一个已经挂载的文件系统,可以在保持其挂载点和参数不变的同时修改文件系统的某些挂载选项
[root@centos8 mnt]#mount -o remount,rw /mnt
#查看某个文件夹是否是真正的挂载点
[root@centos8 ~]#findmnt /data
TARGET SOURCE FSTYPE OPTIONS
/data /dev/sda2 xfs rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
[root@centos8 ~]#findmnt /mnt
TARGET SOURCE FSTYPE OPTIONS
/mnt /dev/sdb1 ext4 rw,relatime
[root@centos8 ~]#
#查看进程
[root@centos8 ~]#cd /mnt/
[root@centos8 mnt]#lsof /mnt/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 3009 root cwd DIR 8,17 4096 2 /mnt
lsof 3049 root cwd DIR 8,17 4096 2 /mnt
lsof 3050 root cwd DIR 8,17 4096 2 /mnt
[root@centos8 mnt]#
7.1永久挂载
在Linux中,我们可以使用mount命令将一个文件系统挂载到指定的挂载点。但是,该挂载点是临时的,一旦系统重启后,挂载就会消失。如果希望文件系统在系统重启后仍然挂载,就需要将其添加到/etc/fstab文件中。这个文件是Linux系统中用于存储文件系统挂载信息的配置文件。在下面的内容中,我们将详细讲解如何使用/etc/fstab文件来永久挂载文件系统。
[root@centos8 mnt]#vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Apr 20 06:18:15 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
首先,让我们来看一下/etc/fstab文件的格式。该文件中的每一行描述了一个文件系统的挂载信息,其格式如下:
<file system> <mount point> <type> <options> <dump> <pass>
各个字段的含义如下:
<file system>
:待挂载的文件系统的设备名或UUID,设备名不稳定建议使用uuid(UUID使用blkid命令查看)。<mount point>
:文件系统挂载的目标路径。<type>
:文件系统的类型,如ext4、nfs等。<options>
:文件系统挂载的选项,如rw(读写)、ro(只读)等。<dump>
:备份工具dump使用的标志位,一般设置为0。<pass>
:文件系统检查顺序,一般设置为0。
现在,我们来看一个实际的例子。假设我们要将/dev/sdb1设备上的ext4文件系统永久挂载到/mnt/mydisk目录下。那么,我们可以在/etc/fstab文件的末尾添加如下一行:
/dev/sdb1 /mnt/mydisk ext4 defaults 0 2
这行配置的含义是:将/dev/sdb1设备上的ext4文件系统以默认的选项挂载到/mnt/mydisk目录下,并设置dump标志位为0,检查顺序为2。这样,当系统重启后,/dev/sdb1设备上的ext4文件系统就会自动挂载到/mnt/mydisk目录下。
除了上述常见的配置选项外,还有一些其他的选项可以在/etc/fstab文件中使用。这些选项可以通过输入man fstab
命令来查看。需要注意的是,在编辑/etc/fstab文件之前,最好备份一下该文件,以免出现错误导致系统无法启动。
7.2将设备挂载到文件夹中
#查看uuid
[root@centos8 mnt]#blkid
/dev/sda1: UUID="ad19dd45-b7ee-485c-87e6-3c9bb8ec3996" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="64f77bbe-01"
/dev/sda2: UUID="933c390a-5904-4b01-aa6a-447c771b7f08" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="64f77bbe-02"
/dev/sda3: UUID="98f53a44-1fb7-4139-85c6-a3c2d1633cd7" TYPE="swap" PARTUUID="64f77bbe-03"
/dev/sda5: UUID="8fbb4a9c-1506-4c4a-889d-b78163091bb0" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="64f77bbe-05"
/dev/sr0: BLOCK_SIZE="2048" UUID="2020-11-02-15-15-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTUUID="6b8b4567" PTTYPE="dos"
/dev/sr1: BLOCK_SIZE="2048" UUID="2021-11-13-01-04-26-00" LABEL="CentOS-8-5-2111-x86_64-dvd" TYPE="iso9660" PTUUID="5fb10f71" PTTYPE="dos"
/dev/sdb5: UUID="ae3ec919-7b0d-49d9-958e-6994c6c2d3ca" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="da250a9c-05"
/dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="c124261d-b1fd-41bf-94c5-0c6d1b87504a"
/dev/sda6: PARTUUID="64f77bbe-06"
/dev/sdb1: UUID="2d508d02-801c-411d-bbcb-471d18142dd3" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="da250a9c-01"
[root@centos8 mnt]#
[root@centos8 mnt]#mkdir /data/mysql
#将/dev/sdb1挂载到/data/mysql中
[root@centos8 mnt]#vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Apr 20 06:18:15 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
#保存退出
#使用配置文件生效
[root@centos8 mnt]#mount -a
[root@centos8 mnt]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 50G 0 part /data
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 1G 0 part /boot
└─sda6 8:6 0 1M 0 part
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part /data/mysql
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 3G 0 part
sr0 11:0 1 9.5G 0 rom
sr1 11:1 1 10.1G 0 rom
[root@centos8 mnt]#
8.swap分区
在Linux操作系统中,swap(交换分区)是一种特殊的分区,用于将内存中暂时不使用的数据转移到磁盘上,以释放内存空间,从而避免系统内存不足的情况。当系统内存紧张时,操作系统会将一部分内存中的数据写入swap分区,以释放空间供其他应用程序使用。当应用程序再次需要访问该数据时,操作系统将从swap分区中读取数据并重新加载到内存中。
Linux系统中,我们可以使用以下命令查看系统中swap的使用情况:
$ swapon -s
[root@centos8 mnt]#free -h
total used free shared buff/cache available
Mem: 1.9Gi 219Mi 1.4Gi 8.0Mi 254Mi 1.5Gi
Swap: 2.0Gi 0B 2.0Gi
[root@centos8 mnt]#
该命令会显示当前系统中所有已经启用的swap分区及其使用情况。
可以使用以下命令创建一个swap分区:
$ sudo mkswap /dev/sdb1
该命令将/dev/sdb1设备格式化为swap分区,并将其标记为可用的swap分区。需要注意的是,在创建swap分区之前,必须确保该分区没有被挂载或正在使用。
为了启用新创建的swap分区,我们需要将其添加到/etc/fstab文件中。具体来说,我们需要在/etc/fstab文件中添加如下一行:
/dev/sdb1 swap swap defaults 0 0
#最好写成uuid
其中,第一个参数是swap分区的设备名,第二个参数是分区的类型(应该设置为"swap"),第三个参数也是"swap",第四个参数是挂载选项,第五个参数是dump标志位(通常设置为0),第六个参数是fsck标志位(通常设置为0)。
如果我们需要暂时禁用swap分区,可以使用以下命令:
$ sudo swapoff /dev/sdb1
这将导致系统停止使用/dev/sdb1分区作为swap分区。如果我们需要永久禁用该分区作为swap分区,可以编辑/etc/fstab文件并将该行注释掉或删除。
8.1划分swap分区
[root@centos8 mnt]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 /
├─sda2 xfs 933c390a-5904-4b01-aa6a-447c771b7f08 /data
├─sda3 swap 98f53a44-1fb7-4139-85c6-a3c2d1633cd7 [SWAP]
├─sda4
├─sda5 ext4 8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot
└─sda6
sdb
├─sdb1 ext4 2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql
├─sdb2
└─sdb5 xfs ae3ec919-7b0d-49d9-958e-6994c6c2d3ca
sdc
└─sdc1
sr0 iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00
sr1 iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00
#注意在fdisk分区的时候,要将swap分区的id修改成为82
#fdisk -l或者在fdisk /dev/sdb分区的时候输入p,可以看到id是多少
[root@centos8 mnt]#fdisk /dev/sdc
Command (m for help): t #输入t进行更改类型
Selected partition 1
Hex code (type L to list all codes): L #输入L查看有哪些
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Hex code (type L to list all codes):82 #输入82,代表是Linux swap
然后w保存退出
[root@centos8 mnt]#mkswap /dev/sdc1
Setting up swapspace version 1, size = 3 GiB (3221221376 bytes)
no label, UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9
[root@centos8 mnt]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 /
├─sda2 xfs 933c390a-5904-4b01-aa6a-447c771b7f08 /data
├─sda3 swap 98f53a44-1fb7-4139-85c6-a3c2d1633cd7 [SWAP]
├─sda4
├─sda5 ext4 8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot
└─sda6
sdb
├─sdb1 ext4 2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql
├─sdb2
└─sdb5 xfs ae3ec919-7b0d-49d9-958e-6994c6c2d3ca
sdc
└─sdc1 swap 515fb6d1-e7bd-4021-9863-5ca3d037e4d9
sr0 iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00
sr1 iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00
[root@centos8 mnt]#
#写到/etc/fstab文件中
[root@centos8 mnt]#vim /etc/fstab
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9 none swap defaults 0 0
#生效
[root@centos8 mnt]#swapon -a
[root@centos8 mnt]#free -h
total used free shared buff/cache available
Mem: 1.9Gi 221Mi 1.4Gi 8.0Mi 255Mi 1.5Gi
Swap: 5.0Gi 0B 5.0Gi
[root@centos8 mnt]#swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -2
/dev/sdc1 partition 3145724 0 -3
[root@centos8 mnt]#
8.2更改swap优先级
[root@centos8 mnt]#swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -2
/dev/sdc1 partition 3145724 0 -3
[root@centos8 mnt]#
#如果有多个swap分区的话,我们知道sdc是最外面的硬盘,所以我们优先使用sdc,其次在使用sdb
#写到/etc/fstab文件中
[root@centos8 mnt]#vim /etc/fstab
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9 none swap defaults,pri=55 0 0 #或者直接去掉defaults也行,直接写pri=55
#先停止swap,再重新启动
[root@centos8 ~]#swapoff -a
[root@centos8 ~]#swapon -a
[root@centos8 ~]#swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -2
/dev/sdc1 partition 4194300 0 55 #优先级已经更改了
[root@centos8 ~]#
#在生产中我们一般使用一个swap即可
8.3测试交换分区
执行
dd if=/dev/zero of=/dev/null bs=8G count=1
命令时,它会使用dd
命令将/dev/zero中的数据输出到/dev/null中,并且一次性地读取8GB的数据。但是,由于这些数据立即被丢弃,因为它直接将数据从/dev/zero
(即一个无限连续的字节零流)复制到/dev/null
(即一个黑洞设备,对它的写入操作不会有任何效果)因此它不会对系统的交换分区产生影响,也不会使swap的used值发生变化。
[root@centos8 ~]#dd if=/dev/zero of=/dev/null bs=9G count=1
dd: memory exhausted by input buffer of size 9663676416 bytes (9.0 GiB)
[root@centos8 ~]#free -h
total used free shared buff/cache available
Mem: 2.9Gi 259Mi 1.5Gi 8.0Mi 1.1Gi 2.4Gi
Swap: 6.0Gi 0B 6.0Gi
[root@centos8 ~]#dd if=/dev/zero of=/dev/null bs=8G count=1
^[[A0+1 records in
0+1 records out
2147479552 bytes (2.1 GB, 2.0 GiB) copied, 22.1131 s, 97.1 MB/s
[root@centos8 ~]#
#当我们使用free -h查看内存的变化swap并没有任何变化
在CentOS 8中,可以使用如下命令来安装 stress
工具:
[root@centos8 ~]#dnf install stress
#然后,你可以使用如下命令来让系统生成一些占用内存的进程:
sudo stress --vm 1 --vm-bytes 8G --timeout 30s
#其中,--vm 参数表示要生成占用内存的进程,--vm-bytes 参数表示每个进程所占用的内存大小,--timeout 参数表示测试的时长。这个命令会在30秒内让系统生成一个占用2GB内存的进程,观察期间内存和交换分区的使用情况即可。
#使用free -h发现swap交换分区在变化
[root@centos8 ~]#free -h
total used free shared buff/cache available
Mem: 2.9Gi 2.8Gi 82Mi 0.0Ki 50Mi 24Mi
Swap: 6.0Gi 4.5Gi 1.5Gi
[root@centos8 ~]#
8.4禁用swap
在后面使用kubernetes的时候,必须要禁用
swap
才可以使用,下面学习如何禁用swap。
#将/etc/fstab里面有关swap交换分区的两行注释掉
[root@centos8 mnt]#vim /etc/fstab
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
#UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
#UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9 none swap defaults,pri=55 0 0
#禁用swap分区
[root@centos8 ~]#swapoff -a
[root@centos8 ~]#swapon -s
[root@centos8 ~]#free -h
total used free shared buff/cache available
Mem: 2.9Gi 218Mi 2.6Gi 8.0Mi 93Mi 2.5Gi
Swap: 0B 0B 0B
[root@centos8 ~]#
#如果机器很多我们使用脚本
[root@centos8 ~]#sed -i.bak '/swap/d' /etc/fstab
8.5使用文件充当swap
#创建一个文件,文件大小需要根据系统需要来确定,一般建议设置为内存大小的两倍。
[root@centos8 ~]#fallocate -l 6G /swapfile
[root@centos8 ~]#ll -h /swapfile
-rw-r--r-- 1 root root 6.0G Mar 6 13:53 /swapfile
#这里面充当swap,里面以后是放内存有关的数据,所以需要修改权限
[root@centos8 ~]#chmod 0 /swapfile
[root@centos8 ~]#ll /swapfile
---------- 1 root root 6442450944 Mar 6 13:53 /swapfile
#创建交换分区
[root@centos8 ~]#mkswap /swapfile
Setting up swapspace version 1, size = 6 GiB (6442446848 bytes)
no label, UUID=2d1c8d11-548f-44e0-8a49-9f1f1d8c6039
[root@centos8 ~]#
[root@centos8 ~]#blkid /swapfile
/swapfile: UUID="2d1c8d11-548f-44e0-8a49-9f1f1d8c6039" TYPE="swap"
[root@centos8 ~]#
#写入到文件当中永久挂载
#UUID是针对设备来讲的,文件不能写uuid,只能用文件名/swapfile
[root@centos8 mnt]#vim /etc/fstab
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
#UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
#UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9 none swap defaults,pri=55 0 0
/swapfile none swap defaults 0 0
#交换分区生效
[root@centos8 ~]#swapon -a
[root@centos8 ~]#free -h
total used free shared buff/cache available
Mem: 2.9Gi 226Mi 2.5Gi 8.0Mi 159Mi 2.5Gi
Swap: 6.0Gi 0B 6.0Gi
[root@centos8 ~]#
[root@centos8 ~]#swapon -s
Filename Type Size Used Priority
/swapfile file 6291452 0 -2
[root@centos8 ~]#
#如果当前分区满了,可以将文件充当的swap移动到其他分区
#禁用
[root@centos8 ~]#swapoff -a
[root@centos8 ~]#mv /swapfile /data
#重新配置文件
[root@centos8 mnt]#vim /etc/fstab
UUID=ad19dd45-b7ee-485c-87e6-3c9bb8ec3996 / xfs defaults 0 0
UUID=8fbb4a9c-1506-4c4a-889d-b78163091bb0 /boot ext4 defaults 1 2
UUID=933c390a-5904-4b01-aa6a-447c771b7f08 /data xfs defaults 0 0
#UUID=98f53a44-1fb7-4139-85c6-a3c2d1633cd7 none swap defaults 0 0
UUID=2d508d02-801c-411d-bbcb-471d18142dd3 /data/mysql ext4 defaults 0 0
#UUID=515fb6d1-e7bd-4021-9863-5ca3d037e4d9 none swap defaults,pri=55 0 0
/data/swapfile none swap defaults 0 0
#再次生效
[root@centos8 ~]#swapon -a
0 Comments