文件和系统管理


1.用fdisk创建分区:

fdisk命令后跟一块硬盘路径

fdisk /dev/sda

 按m显示帮助:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

由帮助可知n是创建分区:

先创建一个主分区

以下步骤为交互式

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): 1
First sector (2048-41943039, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +1G

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

p是显示创建的分区:

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot Start     End Sectors Size Id Type
/dev/sda1        2048 2099199 2097152   1G 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): e
Partition number (2-4, default 2): 2
First sector (2099200-41943039, default 2099200): 
Last sector, +sectors or +size{K,M,G,T,P} (2099200-41943039, default 41943039): +10G

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

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended

创建一个逻辑分区(只有创建扩展后才有):

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 (2101248-23070719, default 2101248): 
Last sector, +sectors or +size{K,M,G,T,P} (2101248-23070719, default 23070719): +1G       

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

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended
/dev/sda5       2101248  4198399  2097152   1G 83 Linux

只要数字是1到4之间就是主分区或者扩展分区,5以后就是逻辑分区。

但是现在分了吗,实际上并没有真的分,我们查看a硬盘的分区表:

[root@centos8 ~ 817]#hexdump -C -n 512 /dev/sda -v
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000170  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000180  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
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 00 00  |................|
00000200

所以刚才的行为还仍在规划中,所以这些操作你都可以撤销,在fdisk中有两个选项可以退出:

w:存盘退出
q:不存盘退出

那我们就存盘退出一下:

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

这个时候就真的存盘退出了,而且同步磁盘:

[root@centos8 ~ 825]#lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part 
├─sda2        8:2    0    1K  0 part 
└─sda5        8:5    0    1G  0 part 
sdb           8:16   0   10G  0 disk 
sr0          11:0    1 10.1G  0 rom  /run/media/root/CentOS-8-5-2111-x86_64-dvd
nvme0n1     259:0    0  200G  0 disk 
├─nvme0n1p1 259:1    0    1G  0 part /boot
├─nvme0n1p2 259:2    0   50G  0 part /
├─nvme0n1p3 259:3    0   30G  0 part /data
├─nvme0n1p4 259:4    0    1K  0 part 
└─nvme0n1p5 259:5    0    4G  0 part [SWAP]

2.分区分完后你又不想要,当然可以回去删。

删的时候还有一个问题:

就是假如我们现在有两个逻辑分区:

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended
/dev/sda5       2101248  4198399  2097152   1G 83 Linux
/dev/sda6       4200448  6297599  2097152   1G 83 Linux

我们删除第5个

Command (m for help): d
Partition number (1,2,5,6, default 6): 5

Partition 5 has been deleted.

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended
/dev/sda5       4200448  6297599  2097152   1G 83 Linux

我们把5删了以后,6就变成5了。我们由此得出结论:分区设备的名称是不稳定的,既然有可能变化就不要把它写进配置文件里面,避免配置文件失效

当然如果我们现在保存,就会发生分区不连贯

 这样就会很别扭。

假如我现在再继续分的话新的逻辑分区会是7还是6?

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 6
First sector (2101248-23070719, default 2101248): 
Last sector, +sectors or +size{K,M,G,T,P} (2101248-4200447, default 4200447): +3G
Value out of range.

发现报错了,但是我们扩展分区明明还有9个g,理论还是够的,但是为什么会出现超出容量,这是因为,你现在这3个G是从被空出的那一部分开始分的,这一块连续的空间只有1个G,所以才会不够。如果你想分多,就不能用默认起点了,当然我们一般不会出现这种情况。

那我们就还分1g:

Last sector, +sectors or +size{K,M,G,T,P} (2101248-4200447, default 4200447): +1G

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

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended
/dev/sda5       4200448  6297599  2097152   1G 83 Linux
/dev/sda6       2101248  4198399  2097152   1G 83 Linux

Partition table entries are not in disk order

你会发现排在前面的成6后面是5,不伦不类的

 那怎么办?就重新删重新来吧。

Command (m for help): d
Partition number (1,2,5,6, default 6): 6

Partition 6 has been deleted.

Command (m for help): d
Partition number (1,2,5, default 5): 5

Partition 5 has been deleted.

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended

我们现在这么分是随意的,因为上边没数据,但是一旦有了数据,你这么删数据就丢了。

重新分一下存盘退出:

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 (2101248-23070719, default 2101248): 
Last sector, +sectors or +size{K,M,G,T,P} (2101248-23070719, default 23070719): +1G

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

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 6
First sector (4200448-23070719, default 4200448):    
Last sector, +sectors or +size{K,M,G,T,P} (4200448-23070719, default 23070719): +2G

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

Command (m for help): p
Disk /dev/sda: 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: 0x1b83cd29

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1          2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 23070719 20971520  10G  5 Extended
/dev/sda5       2101248  4198399  2097152   1G 83 Linux
/dev/sda6       4200448  8394751  4194304   2G 83 Linux

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

我们用lsblk看一下:

[root@centos8 ~ 828]#lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part 
├─sda2        8:2    0    1K  0 part 
├─sda5        8:5    0    1G  0 part 
└─sda6        8:6    0    2G  0 part 
sdb           8:16   0   10G  0 disk 
sr0          11:0    1 10.1G  0 rom  /run/media/root/CentOS-8-5-2111-x86_64-dvd
nvme0n1     259:0    0  200G  0 disk 
├─nvme0n1p1 259:1    0    1G  0 part /boot
├─nvme0n1p2 259:2    0   50G  0 part /
├─nvme0n1p3 259:3    0   30G  0 part /data
├─nvme0n1p4 259:4    0    1K  0 part 
└─nvme0n1p5 259:5    0    4G  0 part [SWAP]

3.至于gdisk和fdisk用法基本一样:

[root@centos8 ~ 830]#gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.
Command (? for help):

用?查帮助:

Command (? for help): ?
b       back up GPT data to a file
c       change a partition's name
d       delete a partition
i       show detailed information on a partition
l       list known partition types
n       add a new partition
o       create a new empty GUID partition table (GPT)
p       print the partition table
q       quit without saving changes
r       recovery and transformation options (experts only)
s       sort partitions
t       change a partition's type code
v       verify disk
w       write table to disk and exit
x       extra functionality (experts only)
?       print this menu

p选项也是查看分区表:

Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): B15AB69F-EA66-42AD-8999-61EEF33F6679
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 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

n选项也是创建,不过要比fdisk范围大多了,最大可以分128个:

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: +1G
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/sdb: 20971520 sectors, 10.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): B15AB69F-EA66-42AD-8999-61EEF33F6679
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 18874301 sectors (9.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

用w存盘退出:

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/sdb.
The operation has completed successfull

就这么完了。

4.对于旧硬盘创键一个新分区可能会出现内存不同步的情况,我们就用partprobe这个命令

partprobe

当然这个命令在centOS6不好使。

在centOS6上同步是比较麻烦的,还得分是增加还是删

同步增加:

#partx -a /dev/sda

同步删除:

#partx -d --nr 6-7 /dev/sda

5.1)文件系统是我们第二步要做的事,我们现在已经把硬盘分好区,就相当于把房子拆成一个一个房间,但是还没装修那,也不能在毛坯房住。事实上,毛坯房也不是不能住。事实上,我们建这个文件系统问题并不是绝对的,你当然也可以直接分完区把数据放在分区里,不用文件系统,理论上是可以的。没有文件系统就会带来一个缺点就是没有文件,文件系统的作用就是:提供文件管理功能,没有文件系统就没有文件,也就是说你放到分区上的数据就不是文件形式了,那就是0101形式了。那换句话说,你把数据放在磁盘上,就跟我们 hexdump -C -n 512 /dev/sdb -v 这个命令一个道理,看到的就是0101.我们那以后你要访问这个分区上的数据,所显示的就是0101。我们通常更习惯用文件方式访问更方便,所以你这个必须要创建文件系统了。文件系统就是把我们磁盘上的空间以文件的形式进行组织,把数据以一个一个的文件帮我们提供起来,所以文件系统一般是有必要的。

2)当然文件系统会额外的增加一些负担,比方肯定有文件系统的元数据,之前说的文件操作的时候的节点表,节点表里它把硬盘里的分区简单地分成两块,一个是元数据,还一个是存放数据的位置。那个元数据包括文件的节点的节点编号,文件的大小属性权限。那些都是要占空间的,元数据也要占空间。所以说也是有代价的。所以在有些特定的情况下,我们可能真的不需要文件系统,比方说,有些追求高性能,直接以0101访问,这种叫RAW文件系统,说的就是没有文件系统,你只能以二进制的方式来管理上面的空间。

6.文件系统概念

文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构;即在存储设备上组织文件的 方法。操作系统中负责管理和存储文件信息的软件结构称为文件管理系统,简称文件系统.

从系统角度来看,文件系统是对文件存储设备的空间进行组织和分配,负责文件存储并对存入的文件进 行保护和检索的系统。具体地说,它负责为用户建立文件,存入、读出、修改、转储文件,控制文件的 存取,安全控制,日志,压缩,加密等

7.查看文件系统

[root@centos8 ~ 835]#lsblk -f
NAME        FSTYPE  LABEL                      UUID                                 MOUNTPOINT
sda                                                                                 
├─sda1                                                                              
├─sda2                                                                              
├─sda5                                                                              
└─sda6                                                                              
sdb                                                                                 
└─sdb1                                                                              
sr0         iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00               /run/media/root/Cent
nvme0n1                                                                             
├─nvme0n1p1 xfs                                b3631a44-f636-461a-a651-05ad78ab6f7e /boot
├─nvme0n1p2 xfs                                95c58cc8-c171-4df2-9e23-3b825af5d2d9 /
├─nvme0n1p3 xfs                                a52f0b13-1f54-47d3-9660-b1dcafbfc0fb /data
├─nvme0n1p4                                                                         
└─nvme0n1p5 swap                               f2672235-5b02-449e-81c7-a7675a04d9af [SWAP]

8.查看当前内核支持的文件系统:

ls /lib/modules/`uname -r`/kernel/fs

centOS8:

binfmt_misc.ko.xz  cifs    ext4     fuse   jbd2           nfs         nls        squashfs
cachefiles         cramfs  fat      gfs2   lockd          nfs_common  overlayfs  udf
ceph               dlm     fscache  isofs  mbcache.ko.xz  nfsd        pstore     xfs

ubuntu:

9p       befs            cifs    f2fs      hfs      lockd       nilfs2    overlayfs  romfs
adfs     bfs             coda    fat       hfsplus  minix       nls       pstore     sysv
affs     binfmt_misc.ko  cramfs  freevxfs  hpfs     ncpfs       ntfs      qnx4       ubifs
afs      btrfs           dlm     fscache   isofs    nfs         ocfs2     qnx6       udf
aufs     cachefiles      efs     fuse      jffs2    nfs_common  omfs      quota      ufs
autofs4  ceph            exofs   gfs2      jfs      nfsd        orangefs  reiserfs   xfs

9.创建文件系统

创建一个ext4的文件系统

[root@centos8 ~ 843]#mkfs.ext4 /dev/sda1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: ed639085-e5fd-4ed2-b572-81f1860541eb
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

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

创建完以后,确认一下:

[root@centos8 ~ 845]#lsblk -f
NAME        FSTYPE  LABEL                      UUID                                 MOUNTPOINT
sda                                                                                 
├─sda1      ext4                               ed639085-e5fd-4ed2-b572-81f1860541eb 
├─sda2                                                                              
├─sda5                                                                              
└─sda6                                                                              
sdb                                                                                 
└─sdb1                                                                              
sr0         iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00               /run/media/root/Cent
nvme0n1                                                                             
├─nvme0n1p1 xfs                                b3631a44-f636-461a-a651-05ad78ab6f7e /boot
├─nvme0n1p2 xfs                                95c58cc8-c171-4df2-9e23-3b825af5d2d9 /
├─nvme0n1p3 xfs                                a52f0b13-1f54-47d3-9660-b1dcafbfc0fb /data
├─nvme0n1p4                                                                         
└─nvme0n1p5 swap                               f2672235-5b02-449e-81c7-a7675a04d9af [SWAP]

说明已经已经成功了,同时它会把这个文件系统分配一些标识,比方说它的uuid,这个uuid我们可以用这个命令可以查看:

[root@centos8 ~ 846]#blkid
/dev/nvme0n1: PTUUID="9d86d599" PTTYPE="dos"
/dev/nvme0n1p1: UUID="b3631a44-f636-461a-a651-05ad78ab6f7e" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-01"
/dev/nvme0n1p2: UUID="95c58cc8-c171-4df2-9e23-3b825af5d2d9" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-02"
/dev/nvme0n1p3: UUID="a52f0b13-1f54-47d3-9660-b1dcafbfc0fb" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-03"
/dev/nvme0n1p5: UUID="f2672235-5b02-449e-81c7-a7675a04d9af" TYPE="swap" PARTUUID="9d86d599-05"
/dev/sr0: 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/sdb1: PARTLABEL="Linux filesystem" PARTUUID="0e1ffa20-0037-4550-bff2-10d65e09ac2a"
/dev/sda1: UUID="ed639085-e5fd-4ed2-b572-81f1860541eb" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="1b83cd29-01"
/dev/sda5: PARTUUID="1b83cd29-05"
/dev/sda6: PARTUUID="1b83cd29-06"

这个命令可以看到我们新分的分区的编号比如"ed639085-e5fd-4ed2-b572-81f1860541eb",上面就有uuid。只有有文件系统才有。

再创建一个xfs的文件系统:

[root@centos8 ~ 847]#mkfs.xfs /dev/sda2
mkfs.xfs: /dev/sda2 appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.

发现创建不了,因为sda2是一个扩展分区,扩展分区不能直接用,他必须在里面分小逻辑分区,小逻辑分区才能创建文件系统。

所以我们把它选成5或者6:

[root@centos8 ~ 848]#mkfs.xfs /dev/sda5
meta-data=/dev/sda5              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, 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

我们再来lsblk -f看一下

[root@centos8 ~ 849]#lsblk -f
NAME        FSTYPE  LABEL                      UUID                                 MOUNTPOINT
sda                                                                                 
├─sda1      ext4                               ed639085-e5fd-4ed2-b572-81f1860541eb 
├─sda2                                                                              
├─sda5      xfs                                a06e3506-1efc-4e9d-8846-a3629c863c59 
└─sda6                                                                              
sdb                                                                                 
└─sdb1                                                                              
sr0         iso9660 CentOS-8-5-2111-x86_64-dvd 2021-11-13-01-04-26-00               /run/media/root/Cent
nvme0n1                                                                             
├─nvme0n1p1 xfs                                b3631a44-f636-461a-a651-05ad78ab6f7e /boot
├─nvme0n1p2 xfs                                95c58cc8-c171-4df2-9e23-3b825af5d2d9 /
├─nvme0n1p3 xfs                                a52f0b13-1f54-47d3-9660-b1dcafbfc0fb /data
├─nvme0n1p4                                                                         
└─nvme0n1p5 swap                               f2672235-5b02-449e-81c7-a7675a04d9af [SWAP]

已经有了,也可以用blkid看一下:

[root@centos8 ~ 859]#blkid | grep '\/dev\/sda5'
/dev/sda5: UUID="a06e3506-1efc-4e9d-8846-a3629c863c59" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="1b83cd29-05"

[root@centos8 ~ 865]#blkid | sed -n '/\/dev\/sda5/p'
/dev/sda5: UUID="a06e3506-1efc-4e9d-8846-a3629c863c59" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="1b83cd29-05"

10.查看ext系统的文件系统信息

1)

[root@centos8 ~ 871]#dumpe2fs /dev/sda1
dumpe2fs 1.45.6 (20-Mar-2020)
Filesystem volume name:   
Last mounted on:          
Filesystem UUID:          ed639085-e5fd-4ed2-b572-81f1860541eb
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:              65536
Block count:              262144
Reserved block count:     13107
Free blocks:              249189
Free inodes:              65525
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:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Sat Feb 12 22:35:38 2022
Last mount time:          n/a
Last write time:          Sat Feb 12 22:35:38 2022
Mount count:              0
Maximum mount count:      -1
Last checked:             Sat Feb 12 22:35:38 2022
Check interval:           0 ()
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:      e23c0e4a-c67a-442c-9056-831c1a4f43de
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x924dc666
Journal features:         (none)
Journal size:             32M
Journal length:           8192
Journal sequence:         0x00000001
Journal start:            0


Group 0: (Blocks 0-32767) csum 0x891b
  Primary superblock at 0, Group descriptors at 1-1
  Reserved GDT blocks at 2-128
  Block bitmap at 129 (+129), csum 0xbfc99b54
  Inode bitmap at 137 (+137), csum 0xee86f92e
  Inode table at 145-656 (+145)
  28521 free blocks, 8181 free inodes, 2 directories, 8181 unused inodes
  Free blocks: 4247-32767
  Free inodes: 12-8192
Group 1: (Blocks 32768-65535) csum 0xec74 [INODE_UNINIT, BLOCK_UNINIT]
  Backup superblock at 32768, Group descriptors at 32769-32769
  Reserved GDT blocks at 32770-32896
  Block bitmap at 130 (bg #0 + 130), csum 0x00000000
  Inode bitmap at 138 (bg #0 + 138), csum 0x00000000
  Inode table at 657-1168 (bg #0 + 657)
  32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  Free blocks: 32897-65535
  Free inodes: 8193-16384
Group 2: (Blocks 65536-98303) csum 0x32a8 [INODE_UNINIT, BLOCK_UNINIT]
  Block bitmap at 131 (bg #0 + 131), csum 0x00000000
  Inode bitmap at 139 (bg #0 + 139), csum 0x00000000
  Inode table at 1169-1680 (bg #0 + 1169)
  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 0x8bb7 [INODE_UNINIT, BLOCK_UNINIT]
  Backup superblock at 98304, Group descriptors at 98305-98305
  Reserved GDT blocks at 98306-98432
  Block bitmap at 132 (bg #0 + 132), csum 0x00000000
  Inode bitmap at 140 (bg #0 + 140), csum 0x00000000
  Inode table at 1681-2192 (bg #0 + 1681)
  32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  Free blocks: 98433-131071
  Free inodes: 24577-32768
Group 4: (Blocks 131072-163839) csum 0xa259 [INODE_UNINIT]
  Block bitmap at 133 (bg #0 + 133), csum 0xdebaf111
  Inode bitmap at 141 (bg #0 + 141), csum 0x00000000
  Inode table at 2193-2704 (bg #0 + 2193)
  24576 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  Free blocks: 139264-163839
  Free inodes: 32769-40960
Group 5: (Blocks 163840-196607) csum 0x793a [INODE_UNINIT, BLOCK_UNINIT]
  Backup superblock at 163840, Group descriptors at 163841-163841
  Reserved GDT blocks at 163842-163968
  Block bitmap at 134 (bg #0 + 134), csum 0x00000000
  Inode bitmap at 142 (bg #0 + 142), csum 0x00000000
  Inode table at 2705-3216 (bg #0 + 2705)
  32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  Free blocks: 163969-196607
  Free inodes: 40961-49152
Group 6: (Blocks 196608-229375) csum 0xa7e6 [INODE_UNINIT, BLOCK_UNINIT]
  Block bitmap at 135 (bg #0 + 135), csum 0x00000000
  Inode bitmap at 143 (bg #0 + 143), csum 0x00000000
  Inode table at 3217-3728 (bg #0 + 3217)
  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 0xc509 [INODE_UNINIT]
  Backup superblock at 229376, Group descriptors at 229377-229377
  Reserved GDT blocks at 229378-229504
  Block bitmap at 136 (bg #0 + 136), csum 0xc5a156a5
  Inode bitmap at 144 (bg #0 + 144), csum 0x00000000
  Inode table at 3729-4240 (bg #0 + 3729)
  32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  Free blocks: 229505-262143
  Free inodes: 57345-65536

扩展:超级块是存元数据的,一般在前几个奇数组会有备份

dumpe2fs这个命令只能看ext系列的文件系统。

2)

[root@centos8 ~ 879]#tune2fs /dev/sda1
tune2fs 1.45.6 (20-Mar-2020)
Usage: tune2fs [-c max_mounts_count] [-e errors_behavior] [-f] [-g group]
        [-i interval[d|m|w]] [-j] [-J journal_options] [-l]
        [-m reserved_blocks_percent] [-o [^]mount_options[,...]]
        [-r reserved_blocks_count] [-u user] [-C mount_count]
        [-L volume_label] [-M last_mounted_dir]
        [-O [^]feature[,...]] [-Q quota_options]
        [-E extended-option[,...]] [-T last_check_time] [-U UUID]
        [-I new_inode_size] [-z undo_file] device
[root@centos8 ~ 880]#tune2fs /dev/sda1 -l
tune2fs 1.45.6 (20-Mar-2020)
Filesystem volume name:   
Last mounted on:          /mnt
Filesystem UUID:          ed639085-e5fd-4ed2-b572-81f1860541eb
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:              65536
Block count:              262144
Reserved block count:     13107
Free blocks:              249189
Free inodes:              65525
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:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Sat Feb 12 22:35:38 2022
Last mount time:          Sat Feb 12 23:35:05 2022
Last write time:          Sat Feb 12 23:35:05 2022
Mount count:              1
Maximum mount count:      -1
Last checked:             Sat Feb 12 22:35:38 2022
Check interval:           0 ()
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:      e23c0e4a-c67a-442c-9056-831c1a4f43de
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x1606e608

11.查看xfs文件系统信息:

xfs_info可以查看:

[root@centos8 ~ 872]#xfs_info /dev/sda5
meta-data=/dev/sda5              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, 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

12.文件系统有可能在使用过程中被损坏,我们就需要修复

1)修复用fsck命令

注意:这个修复不一定可以确保数据不丢失的情况下还原,这个修复顶多让你的文件系统变正常,数据是否恢复就不一定了。

格式:

fsck.FS_TYPE
fsck -t FS_TYPE 

常用选项:

-a 自动修复
-r 交互式修复错误

2)e2fsck:只用于修复ext系列文件系统

-y 自动回答为yes
-f 强制修复
-p 自动进行安全的修复文件系统问题

3)xfs_repair:xfs文件系统专用检测修复工具

-f 修复文件,而设备
-n 只检查
-d 允许修复只读的挂载设备,在单用户下修复 / 时使用,然后立即reboot

注意:修复前一定要取消挂载!!!!

4)范例:修改破坏的ext文件系统

[root@centos8 ~ 873]#mount /dev/sda1 /mnt
[root@centos8 ~ 874]#df -T
Filesystem     Type     1K-blocks     Used Available Use% Mounted on
devtmpfs       devtmpfs    889204        0    889204   0% /dev
tmpfs          tmpfs       916616        0    916616   0% /dev/shm
tmpfs          tmpfs       916616     9700    906916   2% /run
tmpfs          tmpfs       916616        0    916616   0% /sys/fs/cgroup
/dev/nvme0n1p2 xfs       52403200  6402312  46000888  13% /
/dev/nvme0n1p1 xfs        1038336   345564    692772  34% /boot
/dev/nvme0n1p3 xfs       31441920   256588  31185332   1% /data
tmpfs          tmpfs       183320       24    183296   1% /run/user/0
/dev/sr0       iso9660   10540998 10540998         0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd
/dev/sda1      ext4        999320     2564    927944   1% /mnt
[root@centos8 ~ 876]#cp /etc/fstab /mnt/f1
[root@centos8 ~ 877]#cp /etc/fstab /mnt/f2
[root@centos8 ~ 878]#ls /mnt
f1  f2  lost+found
#开始搞破坏
[root@centos8 ~ 881]#dd if=/dev/zero of=/dev/sda1 bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00208132 s, 504 MB/s
[root@centos8 ~ 884]#tune2fs /dev/sda1 -l                      
tune2fs 1.45.6 (20-Mar-2020)
tune2fs: Bad magic number in super-block while trying to open /dev/sda1
[root@centos8 ~ 885]#ls /mnt
[root@centos8 ~ 886]#df
Filesystem                1K-blocks                 Used Available Use% Mounted on
devtmpfs                     889204                    0    889204   0% /dev
tmpfs                        916616                    0    916616   0% /dev/shm
tmpfs                        916616                 9696    906920   2% /run
tmpfs                        916616                    0    916616   0% /sys/fs/cgroup
/dev/nvme0n1p2             52403200              6402312  46000888  13% /
/dev/nvme0n1p1              1038336               345564    692772  34% /boot
/dev/nvme0n1p3             31441920               256588  31185332   1% /data
tmpfs                        183320                   24    183296   1% /run/user/0
/dev/sr0                   10540998             10540998         0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd
/dev/sda1      73786976294838157208 73786976294837160460    980364 100% /mnt
#取消挂载
[root@centos8 ~ 887]#umount /mnt
#开始修复
[root@centos8 ~ 889]#fsck -y /dev/sda1
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...
Superblock has an invalid journal (inode 8).
Clear? yes

*** journal has been deleted ***

Resize inode not valid.  Recreate? yes

Pass 1: Checking inodes, blocks, and sizes
Root inode is not a directory.  Clear? yes

Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Root inode not allocated.  Allocate? yes

/lost+found not found.  Create? yes

Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  +(0--4240) +4243 +(32768--32896) +(98304--98432) +(163840--163968) +(229376--229504)
Fix? yes

Free blocks count wrong for group #0 (28519, counted=28524).
Fix? yes

Free blocks count wrong for group #4 (24576, counted=32768).
Fix? yes

Free blocks count wrong (249187, counted=257384).
Fix? yes

Inode bitmap differences:  +1 +(3--10)
Fix? yes

Free inodes count wrong for group #0 (8180, counted=8181).
Fix? yes

Directories count wrong for group #0 (3, counted=2).
Fix? yes

Free inodes count wrong (65524, counted=65525).
Fix? yes

Padding at end of inode bitmap is not set. Fix? yes

Recreate journal? yes

Creating journal (8192 blocks):  Done.

*** journal has been regenerated ***

/dev/sda1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda1: 11/65536 files (0.0% non-contiguous), 12952/262144 blocks
#确认
[root@centos8 ~ 890]#tune2fs /dev/sda1 -l
tune2fs 1.45.6 (20-Mar-2020)
Filesystem volume name:   
Last mounted on:          
Filesystem UUID:          ed639085-e5fd-4ed2-b572-81f1860541eb
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:              65536
Block count:              262144
Reserved block count:     13107
Free blocks:              249192
Free inodes:              65525
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:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Sat Feb 12 22:35:38 2022
Last mount time:          n/a
Last write time:          Sat Feb 12 23:55:53 2022
Mount count:              0
Maximum mount count:      -1
Last checked:             Sat Feb 12 23:55:53 2022
Check interval:           0 ()
Lifetime writes:          1126 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:      e23c0e4a-c67a-442c-9056-831c1a4f43de
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0xdeb15c16
#再挂回去
[root@centos8 ~ 891]#mount /dev/sda1 /mnt
[root@centos8 ~ 892]#ls /mnt
lost+found
#数据并没有恢复

2022-2-12 23:58