Linux系统的磁盘管理

Posted jks212454

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统的磁盘管理相关的知识,希望对你有一定的参考价值。

一、环境介绍

1.Linux系统版本

[root@node1 ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.0 (Ootpa)
[root@node1 ~]# 

2.系统磁盘情况

[root@node1 ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
sdb           8:16   0   20G  0 disk 
sdc           8:32   0   20G  0 disk 
sdd           8:48   0   20G  0 disk 
sr0          11:0    1 1024M  0 rom  
nvme0n1     259:0    0   20G  0 disk 
├─nvme0n1p1 259:1    0  500M  0 part /boot
├─nvme0n1p2 259:2    0    2G  0 part [SWAP]
└─nvme0n1p3 259:3    0 17.5G  0 part /
[root@node1 ~]# 

二、磁盘介绍

1.磁盘分区理解

将磁盘划分为逻辑存储单元,这些单元成为分区,对不同的分区执行不同的功能。

2.MBR分区方案

分区类型:主分区、扩展分区和逻辑分区
限制:最多有4个主分区,磁盘最大为2TB
MBR:主引导记录(MBR),也被称为主引导扇区,是计算机开机以后访问硬盘时所必须要读取的第一个扇区。

MBR(主引导扇区)+主分区1+主分区2+主分区3+扩展分区4(包括逻辑分区5+逻辑分区6+未使用空间)

2.GPT分区分区方案

GPT:GPT使用全局唯一表示符GUID来识别磁盘和分区
GPT提供分区表备份功能,主GPT位于磁盘头部,备份的GPT位于磁盘尾部。
限制:最多有128个分区,磁盘最大为8ZiB
主GPT+sda1+sda2+sda3+…+备GPT

三、分区工具fidsk使用

1.查看磁盘和分区信息

fdisk工具分区为MBR分区

[root@node1 ~]# fdisk -l
Disk /dev/nvme0n1: 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: 0x9bbd6bb3

Device         Boot   Start      End  Sectors  Size Id Type
/dev/nvme0n1p1 *       2048  1026047  1024000  500M 83 Linux
/dev/nvme0n1p2      1026048  5220351  4194304    2G 82 Linux swap / Solaris
/dev/nvme0n1p3      5220352 41940991 36720640 17.5G 83 Linux




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


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


Disk /dev/sdc: 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


Disk /dev/sdd: 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

2.查看指定设备信息

[root@node1 ~]# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 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: 0x9bbd6bb3

Device         Boot   Start      End  Sectors  Size Id Type
/dev/nvme0n1p1 *       2048  1026047  1024000  500M 83 Linux
/dev/nvme0n1p2      1026048  5220351  4194304    2G 82 Linux swap / Solaris
/dev/nvme0n1p3      5220352 41940991 36720640 17.5G 83 Linux
[root@node1 ~]# fdisk -l /dev/sda
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
[root@node1 ~]# 

3.对新磁盘进行分区

[root@node1 ~]# 
[root@node1 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x962ab0e1.

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

Created a new partition 1 of type 'Linux' and of size 5 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: 0x962ab0e1

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1        2048 10487807 10485760   5G 83 Linux

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

[root@node1 ~]# 

4.格式化文件系统

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

四、gdisk工具使用

1.gdisk进行分区

gdisk工具主要为gpt分区

[root@node1 ~]# 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): n
Partition number (1-128, default 1): 
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +6G
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): 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 successfully.
[root@node1 ~]# lsblk

2.查看sdb磁盘下分区情况

[root@node1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
└─sda1        8:1    0    5G  0 part 
sdb           8:16   0   20G  0 disk 
└─sdb1        8:17   0    6G  0 part 
sdc           8:32   0   20G  0 disk 
sdd           8:48   0   20G  0 disk 
sr0          11:0    1 1024M  0 rom  
nvme0n1     259:0    0   20G  0 disk 
├─nvme0n1p1 259:1    0  500M  0 part /boot
├─nvme0n1p2 259:2    0    2G  0 part [SWAP]
└─nvme0n1p3 259:3    0 17.5G  0 part /
[root@node1 ~]# 

五、挂载文件系统

[root@node1 ~]# 
[root@node1 ~]# mkdir /mnt/test
[root@node1 ~]# mount /dev/sda1 /mnt/test/
[root@node1 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        970M     0  970M   0% /dev
tmpfs           984M     0  984M   0% /dev/shm
tmpfs           984M  9.3M  974M   1% /run
tmpfs           984M     0  984M   0% /sys/fs/cgroup
/dev/nvme0n1p3   18G   11G  7.3G  59% /
/dev/nvme0n1p1  495M  140M  356M  29% /boot
overlay          18G   11G  7.3G  59% /var/lib/docker/overlay2/3851b60316c4c9b3d888c4e6133589bee2882b3e231cf2c4d9ff42eca7a4a390/merged
overlay          18G   11G  7.3G  59% /var/lib/docker/overlay2/8c7e59c24a0b2648c82f41eeddba522e58e06c6809ff702f641f6377b60e8d1f/merged
tmpfs           197M  4.0K  197M   1% /run/user/0
/dev/sda1       5.0G   68M  5.0G   2% /mnt/test
[root@node1 ~]# 

六、查看文件系统信息

[root@node1 ~]# lsblk -fs
NAME      FSTYPE LABEL UUID                                 MOUNTPOINT
sda1      xfs          7060a61e-a422-4d5e-b37d-f6998463dddd /mnt/test
└─sda                                                       
sdb1                                                        
└─sdb                                                       
sdc                                                         
sdd                                                         
sr0                                                         
nvme0n1p1 xfs          525a30a7-d484-4ed5-9f38-f827f54e29ff /boot
└─nvme0n1                                                   
nvme0n1p2 swap         e6cf8733-5eec-4942-9429-c3e9087b6ff0 [SWAP]
└─nvme0n1                                                   
nvme0n1p3 xfs          b7190d80-906f-4b9d-9ab4-5a503ecaea2c /
└─nvme0n1                                                   
[root@node1 ~]# 

七、fsck文件系统修复工具

1.fsck使用语法

语法
fsck [-sACVRP] [-t fstype] [--] [fsck-options] filesys [...]
参数 :

filesys : device 名称(eg./dev/sda1),mount 点 (eg. / 或 /usr)
-t : 给定档案系统的型式,若在 /etc/fstab 中已有定义或 kernel 本身已支援的则不需加上此参数
-s : 依序一个一个地执行 fsck 的指令来检查
-A : 对/etc/fstab 中所有列出来的 partition 做检查
-C : 显示完整的检查进度
-d : 列印 e2fsck 的 debug 结果
-p : 同时有 -A 条件时,同时有多个 fsck 的检查一起执行
-R : 同时有 -A 条件时,省略 / 不检查
-V : 详细显示模式
-a : 如果检查有错则自动修复
-r : 如果检查有错则由使用者回答是否修复

2.修复ext4格式文件系统

[root@node1 ~]# fsck -t ext4 /dev/sda2
fsck from util-linux 2.32.1
e2fsck 1.44.3 (10-July-2018)
/dev/sda2: clean, 11/327680 files, 42078/1310720 blocks
[root@node1 ~]# 

3.fsck自动修复

[root@node1 ~]# fsck -a /dev/sda2
fsck from util-linux 2.32.1
/dev/sda2: clean, 11/327680 files, 42078/1310720 blocks
[root@node1 ~]# 

4.e2fsck自动修复

[root@node1 ~]# e2fsck -y /dev/sda2
e2fsck 1.44.3 (10-July-2018)
/dev/sda2: clean, 11/327680 files, 42078/1310720 blocks
[root@node1 ~]# 

八、xfs_repair文件系统修复工具

[root@node1 ~]# xfs_repair /dev/sda1
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done
[root@node1 ~]# 

以上是关于Linux系统的磁盘管理的主要内容,如果未能解决你的问题,请参考以下文章

Linux磁盘系统——管理磁盘的命令

Linux磁盘管理系列 — 磁盘配额管理

Linux系统的磁盘管理

Linux系统管理之磁盘管理与文件系统

Linux磁盘及文件系统管理1

linux的磁盘管理与文件系统!