23.Linux硬盘分区管理
23.Linux硬盘分区管理
文章目录
- 23.Linux硬盘分区管理
- 硬盘为什么分区
- MBR 分区方案
- fdisk 工具
- fdisk命令语法
- 非交互方式管理
- GPT分区方案
- gdisk 工具
- gdisk命令语法
- wipefs 工具
- parted 工具
- 设置单位
- 管理 MBR 磁盘
- 设置磁盘分区管理方案
- 扩展分区
- 删除分区
- 免交互操作
- 管理 GPT 磁盘
- 设置磁盘分区管理方案
- 文件系统持久化挂载
- 环境准备
- 持久化挂载
- 取消持久化挂载
- 使用分区方案创建文件系统流程:
- 反向清理操作:
- 综合案例:文件系统空间不足
- 情况1:大量的大文件占用空间
- 情况2:删除文件后,空间没有释放
- 情况3:大量的小文件占用空间
- 彻底擦除硬盘上数据
硬盘为什么分区
- 将操作系统文件与用户文件分隔开,避免应用数据太多撑满操作系统盘。
- 限制应用或用户的可用空间。
- 如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上其他分区。
- 用于创建交换分区。
- 限制磁盘空间使用,以提高诊断工具和备份镜像的性能。
- 便于定制文件系统,例如有的文件系统存放大量小文件,有的文件系统存放大量大文件。
MBR 分区方案
自 1982年以来,主启动记录(MBR)分区方案指定了在运行 BIOS 固件的系统上如何对磁盘进行分区:

-
该方案支持最多4个主分区。
-
在 Linux系统上,管理员可以使用扩展分区和逻辑分区来创建最多 15个分区。
逻辑分区是可以格式化(format),扩展分区是不可以格式化。
-
MBR 记录用4个字节(1byte=8bit)存储分区的总扇区数,最大能表示2的32次方的扇区个数,按每扇区512字节计算,每个分区最大不能超过 2 TiB。
类比为:笔记本只有4个usb接口,如果不够用,则将第四个usb接口外接扩展坞,由扩展坞提供多个usb接口。
通常,我们将磁盘第一个扇区称为主引导扇区,位于硬盘的柱面0、磁头0、扇区1的位置,这一扇区包含MBR引导代码,承担系统启动职能。它不属于磁盘上任何分区,因而分区空间内的格式化命令不能清除主引导记录的任何信息。
主引导扇区由三个部分组成:

- 引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。
- 磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。
- 结束标志(占2个字节),其值为AA55(十六进制)。
fdisk 工具
fdisk 工具可用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况进行划分分区。
fdisk命令语法
[root@server ~ 16:54:42]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
`sdb 8:16 0 100G 0 disk /data`
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html#必须卸载才能分区
[root@server ~ 16:56:01]# umount /dev/sdb
[root@server ~ 16:56:17]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。Device does not contain a recognized partition table
使用磁盘标识符 0x8a39b416 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):p磁盘 /dev/sdb:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x8a39b416设备 Boot Start End Blocks Id System命令(输入 m 获取帮助):n
Partition type:p primary (0 primary, 0 extended, 4 free)e extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-209715199,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199):+10G
将使用默认值 209715199
分区 1 已设置为 Linux 类型,大小设为 100 GiB#输入n创建分区
命令(输入 m 获取帮助):n
Partition type:p primary (1 primary, 0 extended, 3 free)e extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (20973568-209715199,默认为 20973568):
将使用默认值 20973568
Last 扇区, +扇区 or +size{K,M,G} (20973568-209715199,默认为 209715199):
将使用默认值 209715199
分区 2 已设置为 Linux 类型,大小设为 90 GiB#输入p,打印分区表
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x8a39b416设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 209715199 94370816 83 Linux# 输入w,保存更改并退出
Command (m for help): w# 输入q,退出管理
Command (m for help): q[root@server ~ 17:03:45]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 10G 0 part
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html#格式化文件
[root@server ~ 17:03:51]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=655360 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, 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@server ~ 17:04:24]# mkdir -p /webapp/webapp01
[root@server ~ 17:05:33]# mount /dev/sdb1 /webapp/webapp01
[root@server ~ 17:06:08]# df /webapp/webapp01
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sdb1 10475520 32992 10442528 1% /webapp/webapp01[root@server ~ 17:06:49]# echo hello world from sdb1 > /webapp/webapp01/index.html#编辑文件
[root@server ~ 17:07:20]# vim /etc/nginx/nginx.conf
.....
server {listen 80;listen [::]:80;server_name _;#注释原本的,添加自己的webapp# root /usr/share/nginx/html;root /webapp/webapp01;
......
[root@server ~ 17:08:48]# systemctl restart nginx.service #客户端测试
[root@client ~ 17:09:10]# curl http://10.1.8.10
hello world from sdb1
注意:如果此时分区表未生成,执行以下命令,通知kernel重新生成分区表。有时候重启系统才会生成最新分区表。
[root@server ~ 17:06:55]# partprobe
非交互方式管理
#通过编辑文件去创建修改等等
#将要执行的fdisk命令行写入到一个文本文件
vim fdisk-create.txt
n
p
1
2048
+2G
p
w
#执行
fdisk /dev/sdb < fdisk-create.txt
GPT分区方案
GPT是运行统一可扩展固件接口(UEFI)固件系统上硬盘分区表的标准。
-
MBR分区方案只能管理最大2TiB分区和磁盘。全局唯一标识分区表(GPT,GUID Partition Table)使用8个字节(1byte=8bit)存储分区的总扇区数,可支持最多8 ZiB(=264*512 Byte),即80亿太字节的分区和磁盘。
-
MBR分区方案支持最多15个分区。GPT分区方案最多可提供128个分区。
-
GPT提供分区表信息的冗余。
GPT分区表结构如下:

-
LBA0(保护性MBR),分为两个部分:
-
第一部分是与MBR中446字节相似的区块,用于存放第一阶段的启动引导程序。
-
第二部分是与MBR分区表记录区对应,该区域存储一个特殊标识符0xEE,用于表示该磁盘为GPT格式。若磁盘管理程序(比较老的软件)无法识别该磁盘,不能修改这个分区信息,进一步保护磁盘。
-
-
LBA1(GPT表头记录),记录了分区表自身的位置和大小,同时也记录了前面提到备份用的GPT分区所在位置(最后34个LBA),还放置了分区表的校验码(CRC32),校验码的作用是让操作系统判断GPT的正确与否,倘若发现错误则可以从备份的GPT中恢复正常运行。
-
LBA2-33,记录分区信息,每个LBA可以提供4组的分区记录,默认情况下可以有4×32=128组分区记录。因为每个LBA都有512字节,所以每组分区记录所占128字节,除去每组记录需要的标识符和相关记录信息外,GPT在每组记录中提供了64位记载分区的扇区总数。
-
虽然GPT最大支持128个分区,但是实际使用过程中分区超过第120个会出现无法格式化使用的情况。
gdisk 工具
gdisk工具用于管理采用GPT分区方案的磁盘分区,主要用于管理磁盘容量超过2T的磁盘。
gdisk命令语法
gdisk [ -l ] device
#查看分区表
[root@server ~ 09:43:16]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: not presentBSD: not presentAPM: not presentGPT: not presentCreating new GPT entries.Command (? for help): p
Disk /dev/sdc: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F702AB66-2B49-45A9-8453-4552F1E07ED2
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 209715133 sectors (100.0 GiB)Number Start (sector) End (sector) Size Code NameCommand (? for help): n
Partition number (1-128, default 1):
First sector (34-209715166, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: +20G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM
a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap
a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID
a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap
a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+
af01 Apple RAID af02 Apple RAID offline af03 Apple label
af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot
bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT
eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit
Press the <Enter> key to see more codes:
ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition
fb00 VMWare VMFS fb01 VMWare reserved fc00 VMWare kcore crash p
fd00 Linux RAID
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'Command (? for help): p
Disk /dev/sdc: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F702AB66-2B49-45A9-8453-4552F1E07ED2
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 167772093 sectors (80.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 41945087 20.0 GiB 8300 Linux filesystemCommand (? for help): wFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!Do you want to proceed? (Y/N): y^HY
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@server ~ 09:46:39]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 10G 0 part
sdc 8:32 0 100G 0 disk
└─sdc1 8:33 0 20G 0 part
sr0 11:0 1 4.4G 0 rom
[root@server ~ 09:46:48]# lsblk /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdc 8:32 0 100G 0 disk
└─sdc1 8:33 0 20G 0 part
[root@server ~ 09:46:59]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: protectiveBSD: not presentAPM: not presentGPT: presentFound valid GPT with protective MBR; using GPT.Command (? for help): n
Partition number (2-128, default 2):
First sector (34-209715166, default = 41945088) or {+-}size{KMGTP}:
Last sector (41945088-209715166, default = 209715166) or {+-}size{KMGTP}: +40G
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: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F702AB66-2B49-45A9-8453-4552F1E07ED2
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 83886013 sectors (40.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 41945087 20.0 GiB 8300 Linux filesystem2 41945088 125831167 40.0 GiB 8300 Linux filesystemCommand (? for help): q
[root@server ~ 09:47:37]# lsblk /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdc 8:32 0 100G 0 disk
└─sdc1 8:33 0 20G 0 part #格式化文件系统
[root@server ~ 09:47:40]# mkfs.ext4 /dev/sdc1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done #挂载
[root@server ~ 09:49:19]# mkdir /webapp/aebapp02
[root@server ~ 09:49:36]# mount /dev/sdc1 /webapp/aebapp02#查看文件系统使用情况
[root@server ~ 09:49:59]# df -h /webapp/aebapp02
Filesystem Size Used Avail Use% Mounted on
/dev/sdc1 20G 45M 19G 1% /webapp/aebapp02
[root@server ~ 09:50:15]# cp /etc/host* /webapp/aebapp02
[root@server ~ 09:50:45]# ls /webapp/aebapp02
host.conf hosts hosts.deny
hostname hosts.allow lost+found
注意
- fdisk 与 gdisk 无论是查看还是创建分区、删除分区、保存以及退出都是
fdisk/gdisk -l filename;n;d;w;q
wipefs 工具
作用:清除磁盘分区表信息。
注意:数据无价,操作需谨慎,最好提前备份。
# 清除未挂载磁盘的分区表
[root@server ~ 13:44:43]# wipefs -a /dev/sdc# 禁止使用 -f 强制清除分区表
wipefs -fa /dev/sdb
补充
/dev/sda1 /
/dev/sda2 /boot
/dev/sda3 /webapp
/dev/sda4 /webapp/webapp01/webapp/webapp01/d1/f1 存在哪个分区?/dev/sda4 /webapp/webapp01/webapp/webapp01/f1 存在哪个分区?
dev/sda4 /webapp/webapp01/webapp/f1 存在哪个分区?dev/sda4 /webapp/webapp01/f1 存在哪个分区?/dev/sda1 /
parted 工具
parted 工具既可以管理采用MBR分区方案的磁盘,又可以管理采用GPT分区方案的磁盘。
parted 命令同时支持交互式操作和非交互式操作(编写脚本)。
我们先来看看交互式操作。
操作流程:
- 查看分区表。如果是未初始化硬盘,创建分区。
- 设置单位(MiB)
- 创建分区
- 调整分区大小
- 调整分区类型
- 删除分区
#查看分区表
[root@server ~ 17:25:47]# parted /dev/sdb#输入help获取帮助信息
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) helpalign-check TYPE N check partition N forTYPE(min|opt) alignmenthelp [COMMAND] print general help, or help onCOMMANDmklabel,mktable LABEL-TYPE create a new disklabel(partition table)mkpart PART-TYPE [FS-TYPE] START END make a partitionname NUMBER NAME name partition NUMBER as NAME#print查看单个磁盘的详细分区表 `-l则是全部`print [devices|free|list,all|NUMBER] display the partition table,available devices, free space, all found partitions, or aparticular partition#退出输入quitquit exit programrescue START END rescue a lost partition nearSTART and ENDresizepart NUMBER END resize partition NUMBERrm NUMBER delete partition NUMBERselect DEVICE choose the device to editdisk_set FLAG STATE change the FLAG on selecteddevicedisk_toggle [FLAG] toggle the state of FLAG onselected deviceset NUMBER FLAG STATE change the FLAG on partitionNUMBERtoggle [NUMBER [FLAG]] toggle the state of FLAG onpartition NUMBERunit UNIT set the default unit to UNITversion display the version number andcopyright information of GNU Parted
(parted)
设置单位
parted工具默认单位是MB(103K),设置为MiB(210KiB)
(parted) unit MiB
管理 MBR 磁盘
设置磁盘分区管理方案
# 输入mklabel或mktable设置磁盘分区管理方案
# 设置分区方案为msdos,也就是MBR,输入mklabel msdos
(parted) `mklabel msdos`
**注意:**parted命令所做更改立刻生效。
# 创建分区,输入 mkpart
(parted) `mkpart`# 设置分区类型,输入 primary
Partition type? primary/extended? `primary` # 设置分区文件系统类型,输入 xfs,实际不生效,格式化文件系统仍需手动操作
File system type? [ext2]? `xfs` # 设置分区起始位置,输入 1
Start? `1` # 设置分区结束位置,输入 2049
End? `2049`
(parted) `print`
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1.00MiB 2049MiB 2048MiB primary xfs
扩展分区
# 1 代表分区号,4097代表分区结束位置
(parted) resizepart 1 4097
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1.00MiB 4097MiB 4096MiB primary xfs
删除分区
# 1 代表分区号
(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags
免交互操作
# 设置磁盘分区管理方案
[root@server ~ 18:19:36]# parted /dev/sdb mklabel msdos# 查看分区
[root@server ~ 18:41:31]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags# 创建分区
[root@server ~ 18:42:00]# parted /dev/sdb unit MiB mkpart primary 1 2049[root@server ~ 18:42:33]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1.00MiB 2049MiB 2048MiB primary# 扩展分区
[root@server ~ 18:42:57]# parted /dev/sdb unit MiB resizepart 1 5121[root@server ~ 18:43:22]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1.00MiB 5121MiB 5120MiB primary# 删除分区
[root@server ~ 18:43:41]# parted /dev/sdb rm 1
管理 GPT 磁盘
管理GPT磁盘基本与管理MBR磁盘一致,除了创建分区。
设置磁盘分区管理方案
# 设置分区方案为gpt,输入mklabel gpt
(parted) `mklabel gpt`
#使用parted非交互模式管理gpt磁盘# 清除磁盘分区表信息
wipefs -a /dev/sdc
parted /dev/sdc print# 创建gpt分区表
parted /dev/sdc mklabel gpt
parted /dev/sdc print# 创建分区1
parted /dev/sdc unit MiB mkpart webapp01 xfs 1 20481
parted /dev/sdc unit MiB print# 创建分区2
parted /dev/sdc unit MiB mkpart webapp02 20482 40962
parted /dev/sdc unit MiB print
lsblk /dev/sdc# 删除分区2
parted /dev/sdc rm 2
lsblk /dev/sdc# 格式化并挂载
mkfs.xfs /dev/sdc1
mkfs.xfs -f /dev/sdc1
mount /dev/sdc1 /webapp/webapp01/# 修改uudi
vim /etc/fstab
df -h /webapp/webapp01/
history
文件系统持久化挂载
环境准备
利用 parted 创建一个分区,并格式化为xfs文件系统。
[root@server ~ 18:43:41]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.[root@server ~ 18:44:22]# parted /dev/sdb mklabel gpt
Warning: The existing disk label on /dev/sdb will be
destroyed and all data on this disk will be lost. Do
you want to continue?Yes/No? Yes
Information: You may need to update /etc/fstab.[root@server ~ 18:48:32]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
Information: You may need to update /etc/fstab.[root@server ~ 18:48:40]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
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# 如果格式化时候,提示存在文件系统,则需要-f选项强制格式化
[root@server ~ 18:49:46]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
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
持久化挂载
当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /etc/fstab文件中添加一个条目。
/etc/fstab 是以空格分隔的文件,每行具有六个字段。
第一个字段指定设备。可以使用UUID或device来指定设备。
第二个字段是目录挂载点。通过它可以访问目录结构中的块设备。挂载点必须存在;如果不存在,请使用mkdir命令进行创建。
**第三个字段包含文件系统类型,如xfs或ext4 **。
第四个字段是挂载选项,以逗号分隔的。 defaults是一组常用选项。详细信息参考mount(8) 。
第五个字段指定dump命令是否备份设备。
第六个字段指定fsck顺序字段,决定了在系统启动吋是否应运行fsck命令,以验证文件系统是否干净。 该字段中的值指示了 fsck的运行顺序。 对于XFS文件系统, 请将该字段设为0 ,因为XFS并不使用fsck来检查自己的文件系统状态。 对于ext4 文件系统,如果是根文件系统, 请将该字段设 为 1 ; 如果是其他ext4 文件系统, 则将该字段设为2。 这样, fsck就会先处理根文件系统,然后同步检查不同磁盘上的文件系统,并按顺序检查同一磁盘上的文件系统。
示例:
[root@server ~ 18:49:53]# blkid /dev/sdb1
/dev/sdb1: UUID="f5c35f10-0274-45af-b044-73694989fe01" TYPE="xfs" PARTLABEL="data01" PARTUUID="432d7d79-1a08-4429-bf38-7ab1444a0ab9"
[root@server ~ 18:53:34]#mkdir /data01[root@server ~ 18:54:13]# vim /etc/fstab
# 最后一行增加一个条目
UUID="f5c35f10-0274-45af-b044-73694989fe01" /data01 xfs defaults 0 0# 使用如下命令立刻挂载
[root@server ~ 18:54:59]# mount /data01
# 或者
[root@server ~ 18:55:11]# mount /dev/sdb1# 验证
[root@server ~ 18:55:26]# df -h /data01
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.0G 33M 2.0G 2% /data01# 重启系统验证
[root@server ~ 18:55:44]# reboot
[root@server ~ 18:56:01]# df -h /data01
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.0G 33M 2.0G 2% /data01
取消持久化挂载
删除/etc/fstab中对应条目即可。
使用分区方案创建文件系统流程:
- 创建分区 fdisk或gdisk
- 格式化文件系统 mkfs.xfs 或者 mkfs.ext4
- 挂载 mount device mountpoint
挂载选项实验:
[root@server ~ 10:42:29]# tail -1 /etc/fstab
UUID="51cb03de-bb3a-4702-acec-d42bd63d5a7b" /webapp/webapp01 xfs ro 0 01. 确保挂载选项是ro
mount -o ro,remount /webapp/webapp01
2. 挂载测试
touch /webapp/webapp01/f1
3. 以读写方式重新挂载,并测试
mount -o rw,remount /webapp/webapp01/
4. 修改挂载选项是rw,并测试
touch /webapp/webapp01/f1
反向清理操作:
- 配置服务,不使用该位置
- 卸载 umount device 或者 umount mountpoint
- 删除 /etc/fstab 条目
- 删除分区
- 必要的时候,wipefs删除分区表方案
[root@server ~ 13:34:20]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 20G 0 part /webapp/aebapp02
sdc 8:32 0 100G 0 disk
└─sdc1 8:33 0 20G 0 part /webapp/webapp01
sr0 11:0 1 4.4G 0 rom
[root@server ~ 13:34:33]# vim /etc/nginx/nginx.conf#修改开机挂载
[root@server ~ 13:39:36]# vim /etc/fstab
[root@server ~ 13:40:42]# systemctl restart nginx.service #卸载
[root@server ~ 13:41:57]# umount /webapp/aebapp02
[root@server ~ 13:42:08]# umount /webapp/webapp01
[root@server ~ 13:44:04]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 20G 0 part
sdc 8:32 0 100G 0 disk
└─sdc1 8:33 0 20G 0 part
sr0 11:0 1 4.4G 0 rom #删除分区
[root@server ~ 13:44:08]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.#查看分区
[root@server ~ 13:44:26]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags#清除磁盘分区表信息
[root@server ~ 13:44:43]# wipefs -a /dev/sdc
/dev/sdc: 2 bytes were erased at offset 0x000001fe (dos): 55 aa
/dev/sdc: calling ioclt to re-read partition table: Success
[root@server ~ 13:45:34]# wipefs -a /dev/sdb
/dev/sdb: 2 bytes were erased at offset 0x000001fe (dos): 55 aa
/dev/sdb: calling ioclt to re-read partition table: Success
[root@server ~ 13:45:37]# parted /dev/sdc print
Error: /dev/sdc: unrecognised disk labelModel: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
综合案例:文件系统空间不足
情况1:大量的大文件占用空间
准备环境
[root@server ~ 14:16:10]# parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.[root@server ~ 14:16:44]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
Information: You may need to update /etc/fstab.[root@server ~ 14:16:46]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@server ~ 14:17:09]# mkdir /myapp-1
[root@server ~ 14:17:24]# mount /dev/sdb1 /myapp-1
mount: /dev/sdb1: can't read superblock
[root@server ~ 14:17:38]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 2G 0 part
sdc 8:32 0 100G 0 disk
sr0 11:0 1 4.4G 0 rom #-f强制格式化,原先已经从存在
[root@server ~ 14:18:59]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
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@server ~ 14:19:14]# mount /dev/sdb1 /myapp-1
[root@server ~ 14:22:07]# ls /myapp-1
[root@server ~ 14:23:09]# cp -r /etc/ /myapp-1/
[root@server ~ 14:23:27]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: error writing ‘/myapp-1/etc/bigfile’: No space left on device
1973+0 records in
1972+0 records out
2067988480 bytes (2.1 GB) copied, 34.7464 s, 59.5 MB/s
原因:大文件占用大量空间。
解决方法:找到文件后删除。
[root@server ~ 14:24:05]# rm -fr /myapp-1/etc/bigfile
You have new mail in /var/spool/mail/root
[root@server ~ 15:05:19]# df -h /myapp-1/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.0G 66M 2.0G 4% /myapp-1
情况2:删除文件后,空间没有释放
准备环境
[root@server ~ 15:05:35]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: error writing ‘/myapp-1/etc/bigfile’: No space left on device
1973+0 records in
1972+0 records out
2067791872 bytes (2.1 GB) copied, 17.0873 s, 121 MB/s
[root@server ~ 15:27:54]# tail -f /myapp-1/etc/bigfile &
[1] 2768
[root@server ~ 15:28:26]# rm -f /myapp-1/etc/bigfile
[root@server ~ 15:29:55]# df -h /myapp-1/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.0G 2.0G 528K 100% /myapp-1
情况3:大量的小文件占用空间
准备环境
[root@server ~ 18:56:01]# parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073
Information: You may need to update /etc/fstab.
[root@server ~ 19:01:02]# mkfs.ext4 /dev/sdb2
[root@server ~ 19:01:07]# mkdir /myapp-2
[root@server ~ 19:01:22]# mount /dev/sdb2 /myapp-2
[root@server ~ 19:01:27]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root253:0 0 50G 0 lvm /├─centos_centos7-swap253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
└─sdb2 8:18 0 1G 0 part /myapp-2[root@server ~ 19:01:31]# touch /myapp-2/file-{00001..70000}
touch: cannot touch ‘/myapp-2/file-65526’: No space left on device
touch: cannot touch ‘/myapp-2/file-65527’: No space left on device
................[root@server ~ 19:02:45]# df -h /myapp-2/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb2 976M 4.4M 905M 1% /myapp-2
原因:文件系统中inode使用完了。
解决方法:删除大量的小文件或者将这些小文件备份到其他地方。
[root@server ~ 19:04:09]# df -hi /myapp-2
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sdb2 64K 64K 0 100% /myapp-2# 思路1:直接将这些小文件移走或删除
[root@server ~ 19:04:50]# rm -f /myapp-2/file-0*# 思路2:合并大量小文件为单个文件
[root@server ~ 19:04:56]# cat /myapp-2/file-{00001..10000} > file-00001
彻底擦除硬盘上数据
- 逻辑破坏:用0填充所有数据,例如
dd if=/dev/zero of=/dev/sdb。 - 物理破坏,例如消磁。
