Linux-分区管理-fdisk、gdisk、mkfs、mount、umount、fstab、xfs文件系统修复

1、硬盘分区-fdisk

1.1、命令介绍

fdisk 仅支持分配小于2TB的磁盘。
  查看当前设备fdisk -l
  对设备进行分区fdisk /dev/sdb

分区命令
  m : 显示帮助
  n : 创建新分区
  d : 删除分区
  p : 查看分区
  w : 保存分区
  q : 退出

1.2、示例1-给20G硬盘分2个分区

1.2.1、当前硬盘情况

]# fdisk -l | grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors

1.2.2、分两个分区

~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0xd6b64e34.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p # 第一主分区
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 20971519 # 分10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): n  
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p # 创建第二分区
Partition number (2-4, default 2): 
First sector (20971520-41943039, default 20971520): 
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-41943039, default 41943039):  # 剩下全部划分到第二个分区
Using default value 41943039
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): p # 打印当前分区情况
Disk /dev/sdb: 21.5 GB, 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 label type: dos
Disk identifier: 0xd6b64e34

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
/dev/sdb2        20971520    41943039    10485760   83  Linux

Command (m for help): w # 保存分区表

2、硬盘分区-gdisk

2.1、命令介绍

# 安装工具包
yum install gdisk -y

gdisk支持分配大于2TB的磁盘。
查看当前设备 gdisk [-1] device。
对设备进行分区 gdisk /dev/sdb。

分区命令
  ? : 显示帮助
  n : 创建新分区
  p : 打印分区
  w : 保存分区
  q : 退出

2.2、示例1-给20G硬盘分2个分区

2.2.1、当前硬盘情况

~]# gdisk -l /dev/sdb  | grep sdb
Disk /dev/sdb: 41943040 sectors, 20.0 GiB

2.2.2、分两个分区

~]# gdisk /dev/sdb
Command (? for help): n # 创建第1分区
Partition number (1-128, default 1): 
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G # 第一个分区分为10G
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): n # 创建第2分区
Partition number (2-128, default 2): 
First sector (34-41943006, default = 20973568) or {+-}size{KMGTP}: 
Last sector (20973568-41943006, default = 41943006) or {+-}size{KMGTP}:  # 最低
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: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 703C018B-A48B-42F9-93E7-5FE11908195A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20973567   10.0 GiB    8300  Linux filesystem
   2        20973568        41943006   10.0 GiB    8300  Linux filesystem
Command (? for help): w #写入分区表

3、格式化硬盘-mkfs

3.1、命令介绍

mkfs

作用:格式化硬盘:
-b :设定数据区块占用空间大小,目前支持1024、20484096 bytes每个块;
-t :用来指定什么类型的文件系统,可以是ext4、xfs ;
-N : 设定inode数量,防止Inode数量不够导致磁盘不足;

3.2、格式化整个硬盘

mkfs -t ext4 /dev/sdb

3.3、格式化指定分区

mkfs -t xfs /dev/sdb1

4、临时挂载硬盘-mount

4.1、命令介绍

临时挂载,通过mount进行挂载,但重启将会失效。我们称为临时生效。
-t :指定文件系统挂载分区;
-a :检查并且挂载/etc/fstab 配置文件中未挂载的设备;
-o :指定挂载参数,ro、rw ;

4.2、临时挂载

4.2.1、临时挂载硬盘-可读写

~]# mkdir /mnt/db1
~]# mount -t xfs /dev/sdb1 /mnt/db1
~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
...
/dev/sdb1                 10G   33M   10G   1% /mnt/db1

4.2.2、临时挂载硬盘-只读

~]# mount -t xfs -o ro /dev/sdb2 /mnt/db2

# 无权限写入
~]# touch /mnt/db2/file touch: cannot touch ‘/mnt/db2/file’: Read-only file system

5、临时卸载硬盘-umount

5.1、命令介绍

如果不想使用可以使用umount [device|directory]进行临时卸载。
-1 : 强制卸载;

5.2、卸载硬盘

umount /mnt/db1
umount -l /mnt/db2

6、永久挂载硬盘-fstab

6.1、fstab作用

如果需要实现永久挂载,则需要将挂载相关信息写入/etc/fstab配置文件中实现。

6.2、永久挂载流程

配置文件规范︰设备名称|挂载的入口目录|文件系统类型|挂载参数|是否备份|是否检查
1、获取设备名称,或者获取设备UUID
2、手动临时挂载测试;
3、写入/etc/fstab配置文件;
4、使用mount -a检查是否存在错误;

6.3、/etc/fstab配置解析

/etc/fstab配置文件格式
第一列:指定需要挂载的设备
       设备名称:/dev/sdb1
       设备ID:UUID
第二列:挂载的入口目录。
第三列:文件系统类型
       xfs类型
       ext4类型
第四列:挂载参数
       async/sync   # 使用同步或异步方式存储数据;默认async
       user/nouser  # 是否允许普通用户使用mount命令挂载。默认nouser
       exec/noexe   # 是否允许可执行文件执行。默认exec
       suid/nosuid  # 是否允许存在suid 属性的文件。默认suid
       auto/noauto  # 执行mount -a命令时,此文件系统是否被主动挂载。默认 auto
       rw/ro        # 是否以只读或者读写模式进行挂载。默认rw
       default      # 具有rw,suid,dev,exec,auto, nouser,async等参数;
第五列:是否要备份磁盘
       0 # 不做备份
       1 # 每天进行备份操作
       2 # 不定日期的进行备份操作

第六列:开机是否检验扇区
       0 # 不要检验磁盘是否有坏道.
       1 # 检验
       2 # 校验(当1级别检验完成之后进行2级别检验)

6.4、给sdb1配置永久挂载

6.4.1、查询设备UUID

]# blkid | grep sdb1
/dev/sdb1: UUID="184ea481-1a66-495c-8a63-2cf853608a92" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="0a2e0b71-052f-49bb-869d-2f1fa590bc3a" 

6.4.2、测试挂载

mount UUID="184ea481-1a66-495c-8a63-2cf853608a92" /mnt/db1

# 测试完成,就卸载掉

6.4.3、方法1:手动配置-写入/etc/fstab配置

]# tail -1 /etc/fstab 
UUID=184ea481-1a66-495c-8a63-2cf853608a92 /mnt/db1 xfs defaults 0 0

]# mount -a

]# df -h | grep db1 /dev/sdb1 10G 33M 10G 1% /mnt/db1

6.4.4、方法2:自动配置-写入etc/fstab配置

blkid | grep /dev/sdb1  | awk -F '[: ]+' '{print $2}' | sed -r 's#(.*)#\1 /mnt/db1 xfs defaults 0 0 #g'>>/etc/fstab 

]# tail -1 /etc/fstab 
UUID="184ea481-1a66-495c-8a63-2cf853608a92" /mnt/db1 xfs defaults 0 0

7、xfs文件系统修复

7.1、模拟故障

7.1.1、分一个区并且格式化

# 这里自己分区,不再重复记录

# 格式化硬盘
mkfs.xfs /dev/sdb1 

 

7.1.2、挂载硬盘并且写入数据

mount /dev/sdb1 /mnt/
echo "Hello" >/mnt/new.txt

 

7.1.3、往硬盘填充数据

dd if=/dev/zero of=/dev/sdb bs=300M count=1

 

7.1.4、马上卸载硬盘

umount /mnt/

 

7.1.5、再挂载看看报错

]# mount /dev/sdb1  /mnt/
mount: mount /dev/sdb1 on /mnt failed: Structure needs cleaning

 

7.2、挂载报错:Structure needs cleaning,修复文件系统

7.2.1、尝试修复

xfs_repair /dev/sdb1
mount /dev/sdb1  /mnt/
# 修复好,就可以挂载了

 

7.2.2、强制修复

xfs_repair -L /dev/sdb1

 

posted @ 2023-04-21 17:01  小粉优化大师  阅读(521)  评论(0编辑  收藏  举报