linux硬盘分区管理
文章目录
- linux硬盘分区管理
- 硬盘为什么要分区?
- MBR 分区方案
- fsisk 工具
- fdisk命令语法
- 查看分区
- 创建分区
- 删除分区
- 非交互方式管理
- GPT 分区方案
- gdisk 工具
- gdisk命令语法
- 转换分区表方案
- 创建分区
- 更改分区名称
- 查看分区详细信息
- 删除分区
- wipefs 工具
- parted 工具
- 查看分区表
- 设置单位
- 管理 MBR 磁盘
- 设置磁盘分区管理方案
- 免交互操作
- 管理 GPT 磁盘
- 设置磁盘分区管理方案
- 创建分区
- 扩展分区
- 删除分区
- 免交互操作
- 文件系统持久化挂载
- 环境准备
- 持久化挂载
- 取消持久化挂载
- 综合案例:文件系统空间不足
- 情况1:大量的大文件占用空间
- 情况2:删除文件后,空间没有释放
- 情况3:大量的小文件占用空间
- 思考:如何彻底擦除硬盘上数据
linux硬盘分区管理
硬盘为什么要分区?
- 将操作系统文件与用户文件分隔开,避免应用数据太多撑满操作系统盘。
- 限制应用或用户的可用空间。
- 如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上其他分区。
- 用于创建交换分区。
- 限制磁盘空间使用,以提高诊断工具和备份镜像的性能。
- 便于定制文件系统,例如有的文件系统存放大量小文件,有的文件系统存放大量大文件。
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(十六进制)。
fsisk 工具
fdisk 工具可用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况进行划分分区。
fdisk命令语法
[root@server ~ 11:44:11]# fdisk -h
用法:fdisk [选项] <磁盘> 更改分区表fdisk [选项] -l <磁盘> 列出分区表fdisk -s <分区> 给出分区大小(块数)选项:-b <大小> 扇区大小(512、1024、2048或4096)-c[=<模式>] 兼容模式:“dos”或“nondos”(默认)-h 打印此帮助文本-u[=<单位>] 显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)-v 打印程序版本-C <数字> 指定柱面数-H <数字> 指定磁头数-S <数字> 指定每个磁道的扇区数
除了使用-l选项查看分区表,其他选项暂时都不用。
查看分区
fdisk工具大部分操作通过交互式完成,出了查看分区表。
DOS disklabel 指的硬盘管理方式是MBR。
我们使用上一章准备的一块硬盘/dev/sdb。
# 方法1:
[root@server ~ 11:45:59]# fdisk -l /dev/sdb磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0008dc87设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux# 方法2:
[root@server ~ 11:47:41]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。命令(输入 m 获取帮助):m
命令操作a toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)命令(输入 m 获取帮助):p 磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0008dc87设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux命令(输入 m 获取帮助):q
#p是打印分区表
#q是退出管理
创建分区
#查看是否有分区
[root@server ~ 09:47:29]# fdisk /dev/sdb -l磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节您在 /var/spool/mail/root 中有新邮件#创建第一个分区
[root@server ~ 10:10:56]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。Device does not contain a recognized partition table
使用磁盘标识符 0x22fed9e1 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):m
命令操作a toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)命令(输入 m 获取帮助):p磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x22fed9e1设备 Boot Start End Blocks Id System#n是选择分区类型,回车选择默认分区类型:primary
命令(输入 m 获取帮助):n
Partition type:p primary (0 primary, 0 extended, 4 free)e extended
Select (default p): `回车`
Using default response p
# 直接回车,分区号,使用默认值1
分区号 (1-4,默认 1):`回车`
# 直接回车,设置分区起始位置为:默认值2048扇区,也就是1M位置。
起始 扇区 (2048-41943039,默认为 2048):`回车`
将使用默认值 2048# 设置分区结束位置,输入+2G,也就是起始位置之后2
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+5G
分区 1 已设置为 Linux 类型,大小设为 5 GiB命令(输入 m 获取帮助):p磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x22fed9e1设备 Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux# 输入w,保存更改并退出
# 输入q,不保存更改并退出
命令(输入 m 获取帮助):w
The partition table has been altered!Calling ioctl() to re-read partition table.
正在同步磁盘。#格式化
[root@server ~ 10:16:01]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
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@server ~ 10:21:18]# mount /dev/sdb1 /data1#复制一个文件
[root@server ~ 10:23:14]# cp /etc/host* /data1
[root@server ~ 10:23:19]# ls /data1
host.conf hostname hosts hosts.allow hosts.deny[root@server ~ 10:21:34]# df -h /data1
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 5.0G 33M 5.0G 1% /data1
注意:如果此时分区表未生成,执行以下命令,通知kernel重新生成分区表。有时候重启系统才会生成最新分区表。
partprobe
删除分区
#创建第二个分区,并删除
[root@server ~ 10:15:38]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。命令(输入 m 获取帮助):n
Partition type:p primary (1 primary, 0 extended, 3 free)e extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (10487808-41943039,默认为 10487808):
将使用默认值 10487808
Last 扇区, +扇区 or +size{K,M,G} (10487808-41943039,默认为 41943039):
将使用默认值 41943039
分区 2 已设置为 Linux 类型,大小设为 15 GiB命令(输入 m 获取帮助):
# 输入p,打印分区表
命令(输入 m 获取帮助):p磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x22fed9e1设备 Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 41943039 15727616 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@server ~ 10:25:57]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@server ~ 10:26:10]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part /data1
└─sdb2 8:18 0 15G 0 part
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html/dvd#删除
[root@server ~ 10:26:17]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。命令(输入 m 获取帮助):d
分区号 (1,2,默认 2):2
分区 2 已删除命令(输入 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@server ~ 10:26:50]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part /data1
└─sdb2 8:18 0 15G 0 part
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html/dvd#更新
[root@server ~ 10:26:53]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@server ~ 10:27:03]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 5G 0 part /data1
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html/dvd
如果硬盘空间比较大,需要的文件系统数量超过4个,那么就需要借助扩展分区创建逻辑分区了。
非交互方式管理
创建
[root@server ~ 16:17:32]# vim fdisk-create.txt
[root@server ~ 16:37:21]# cat fdisk-create.txt
n
p
1
2048
+2G
p
w[root@server ~ 16:37:27]# fdisk /dev/sdb <fdisk-create.txt
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。Device does not contain a recognized partition table
使用磁盘标识符 0x5b7f79dc 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):Partition type:p primary (0 primary, 0 extended, 4 free)e extended
Select (default p): 分区号 (1-4,默认 1):起始 扇区 (2048-41943039,默认为 2048):Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):分区 1 已设置为 Linux 类型,大小设为 2 GiB命令(输入 m 获取帮助):
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x5b7f79dc设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux命令(输入 m 获取帮助):The partition table has been altered!Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@server ~ 16:37:50]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
└─md1 9:1 0 20G 0 raid1
[root@server ~ 16:38:11]#
删除
[root@server ~ 16:39:10]# vim fdisk-delete.txt
[root@server ~ 16:39:32]# cat fdisk-delete.txt
d
p
w[root@server ~ 16:39:35]# fdisk /dev/sdb <fdisk-delete.txt
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。命令(输入 m 获取帮助):已选择分区 1
分区 1 已删除命令(输入 m 获取帮助):
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x5b7f79dc设备 Boot Start End Blocks Id System命令(输入 m 获取帮助):The partition table has been altered!Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@server ~ 16:39:45]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─md1 9:1 0 20G 0 raid1
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 ~ 10:31:12]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 5G 0 part /data1
sdc 8:32 0 200G 0 disk
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/html/dvd
#先安装
[root@server ~ 16:41:03]# yum install -y gdisk
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 gdisk.x86_64.0.0.8.10-3.el7 将被 安装
--> 解决依赖关系完成依赖关系解决============================================================Package 架构 版本 源 大小
============================================================
正在安装:gdisk x86_64 0.8.10-3.el7 base 190 k事务概要
============================================================
安装 1 软件包总下载量:190 k
安装大小:660 k
Downloading packages:
gdisk-0.8.10-3.el7.x86_64.rpm | 190 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction正在安装 : gdisk-0.8.10-3.el7.x86_64 1/1 验证中 : gdisk-0.8.10-3.el7.x86_64 1/1 已安装:gdisk.x86_64 0:0.8.10-3.el7 完毕![root@server ~ 16:41:44]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: MBR onlyBSD: not presentAPM: not presentGPT: not present***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory.
***************************************************************Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 3C57D7F2-E85E-45E4-8DF7-9A6DEBD8240D
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 41942973 sectors (20.0 GiB)Number Start (sector) End (sector) Size Code Name
[root@server ~ 16:41:49]#
转换分区表方案
gdisk工具用于管理gpt分区,所以我们需要将磁盘的分区管理方案由MBR转换成GPT。
[root@server ~ 16:41:49]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: MBR onlyBSD: not presentAPM: not presentGPT: not present***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************# 输入?,查看帮助信息。gdisk管理命令跟fdisk很相似。
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# 输入o,将磁盘的分区管理方案由MBR转换成GPT
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FF25CFA0-378F-4B77-AA2E-482703AF0A92
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 41942973 sectors (20.0 GiB)Number Start (sector) End (sector) Size Code Name# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (? for help): wFinal 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@server ~ 16:43:22]#
创建分区
[root@server ~ 16:43:22]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10# 这里识别到了GPT分区方案
Partition table scan:MBR: protectiveBSD: not presentAPM: not presentGPT: presentFound valid GPT with protective MBR; using GPT.# 输入n,创建新分区
Command (? for help): n# 设置分区ID
Partition number (1-128, default 1): `回车`# 设置分区起始位置
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: `回车`# 设置分区结束位置
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'# 设置分区类型,输入L查看所有分区类型
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'# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FF25CFA0-378F-4B77-AA2E-482703AF0A92
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 37748669 sectors (18.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 4196351 2.0 GiB 8300 Linux filesystem
更改分区名称
Command (? for help): c
Using 1
Enter name: data01Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FF25CFA0-378F-4B77-AA2E-482703AF0A92
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 37748669 sectors (18.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 4196351 2.0 GiB 8300 data01
查看分区详细信息
Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 564B9BF1-296D-4A62-943F-718003FB6866
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'
删除分区
# 输入d,删除分区ID为1的分区
Command (? for help): d
Using 1[root@server ~ 16:50:41]# wipefs -a /dev/sdbCommand (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FF25CFA0-378F-4B77-AA2E-482703AF0A92
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 41942973 sectors (20.0 GiB)Number Start (sector) End (sector) Size Code Name
总体
[root@server ~ 10:32:10]# gdisk -l /dev/sdc
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: not presentBSD: not presentAPM: not presentGPT: not presentCreating new GPT entries.
Disk /dev/sdc: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C05C088F-A1A2-48AD-8716-CDCA
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419
Partitions will be aligned on 2048-sector boundaries
Total free space is 419430333 sectors (200.0 GiB)Number Start (sector) End (sector) Size C
[root@server ~ 10:40:08]# 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: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 4F5DE214-0B47-477D-8ED5-C28E
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419
Partitions will be aligned on 2048-sector boundaries
Total free space is 419430333 sectors (200.0 GiB)Number Start (sector) End (sector) Size CCommand (? for help): n
Partition number (1-128, default 1):
First sector (34-419430366, default = 2048) or {+-}s
Last sector (2048-419430366, default = 419430366) or: +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): p
Disk /dev/sdc: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 4F5DE214-0B47-477D-8ED5-C28EB4AD28CA
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 398458813 sectors (190.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 20973567 10.0 GiB 8300 Linux filesystemCommand (? for help): n
Partition number (2-128, default 2):
First sector (34-419430366, default = 20973568) or {+-}size{KMGTP}:
Last sector (20973568-419430366, default = 419430366) or {+-}size{KMGTP}: +100G
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: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 4F5DE214-0B47-477D-8ED5-C28EB4AD28CA
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 188743613 sectors (90.0 GiB)Number Start (sector) End (sector) Size Code Name1 2048 20973567 10.0 GiB 8300 Linux filesystem2 20973568 230688767 100.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
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.[root@server ~ 10:42:49]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root253:0 0 50G 0 lvm /├─centos-swap253:1 0 2G 0 lvm [SWAP]└─centos-home253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 5G 0 part /data1
sdc 8:32 0 200G 0 disk
├─sdc1 8:33 0 10G 0 part
└─sdc2 8:34 0 100G 0 part
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/ht
[root@server ~ 10:43:10]# mkfs.ext4 /dev/sdc1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
80 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, 1605632Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成[root@server ~ 10:43:33]# mkdir /webapp{1,2}
[root@server ~ 10:44:16]# mount /dev/sdc1 /webapp1[root@server ~ 10:45:06]# mkfs.xfs /dev/sdc2
meta-data=/dev/sdc2 isize=512 agcount=4, agsize=6553600 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=26214400, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=12800, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server ~ 10:46:31]# blkid /dev/sdc2
/dev/sdc2: UUID="c9c4dcab-c982-4cb5-9423-02ccaea9c393" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="14492e56-50e2-4a1c-ad09-48a3fcca0db5"
[root@server ~ 10:46:53]# mount /dev/sdc2 /webapp2
[root@server ~ 10:46:59]# df
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 1001840 0 1001840 0% /dev
tmpfs 1013928 0 1013928 0% /dev/shm
tmpfs 1013928 9832 1004096 1% /run
tmpfs 1013928 0 1013928 0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 2800128 49603072 6% /
/dev/sda1 1038336 142304 896032 14% /boot
/dev/mapper/centos-home 49250820 33068 49217752 1% /home
tmpfs 202788 0 202788 0% /run/user/0
/dev/sr0 4635056 4635056 0 100% /usr/share/nginx/html/dvd
/dev/sdb1 5232640 33012 5199628 1% /data1
/dev/sdc1 10190100 36888 9612540 1% /webapp1
/dev/sdc2 104806400 32992 104773408 1% /webapp2
[root@server ~ 10:47:02]##删除
[root@server ~ 10:30:55]# umount /webapp2
[root@server ~ 10:49:57]# df /webapp2
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/centos-root 52403200 2800152 49603048 6% /
[root@server ~ 10:50:03]# 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): d
Partition number (1-2): 2Command (? for help): wFinal 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.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
[root@server ~ 10:51:41]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@server ~ 10:51:49]# df -h /dev/sdc
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 979M 0 979M 0% /dev
[root@server ~ 10:52:02]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root253:0 0 50G 0 lvm /├─centos-swap253:1 0 2G 0 lvm [SWAP]└─centos-home253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 5G 0 part /data1
sdc 8:32 0 200G 0 disk
└─sdc1 8:33 0 10G 0 part /webapp1
sr0 11:0 1 4.4G 0 rom /usr/share/nginx/ht
[root@server ~ 10:52:09]#
wipefs 工具
作用:清除磁盘分区表信息。
注意:数据无价,操作需谨慎,最好提前备份。
[root@server ~ 16:50:41]# wipefs -a /dev/sdb
# 清除未挂载磁盘的分区表[root@server ~ 16:51:15]# wipefs -af /dev/sdb
# 禁止使用 -f 强制清除分区表
parted 工具
parted 工具既可以管理采用MBR分区方案的磁盘,又可以管理采用GPT分区方案的磁盘。
parted 命令同时支持交互式操作和非交互式操作(编写脚本)。
我们先来看看交互式操作。
操作流程:
- 查看分区表。如果是未初始化硬盘,创建分区。
- 设置单位(MiB)
- 创建分区
- 调整分区大小
- 调整分区类型
- 删除分区
查看分区表
[root@server ~ 11:19:58]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb# 查看帮助,输入help
Welcome to GNU Parted! Type 'help' to view a list
of commands.(parted) help align-check TYPE N checkpartition N for TYPE(min|opt) alignmenthelp [COMMAND] printgeneral help, or help on COMMANDmklabel,mktable LABEL-TYPE create anew disklabel (partition table)mkpart PART-TYPE [FS-TYPE] START END make apartitionname NUMBER NAME namepartition NUMBER as NAMEprint [devices|free|list,all|NUMBER] displaythe partition table, available devices,free space, all found partitions, or aparticular partitionquit exitprogramrescue START END rescue alost partition near START and ENDresizepart NUMBER END resizepartition NUMBERrm NUMBER deletepartition NUMBERselect DEVICE choosethe device to editdisk_set FLAG STATE changethe FLAG on selected devicedisk_toggle [FLAG] togglethe state of FLAG on selected deviceset NUMBER FLAG STATE changethe FLAG on partition NUMBERtoggle [NUMBER [FLAG]] togglethe state of FLAG on partition NUMBERunit UNIT set thedefault unit to UNITversion displaythe version number and copyrightinformation of GNU Parted(parted) (parted) mklabel msdos# 查看分区表,输入 print(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system 标志(parted) mkpart分区类型? primary/主分区/extended/扩展分区? primary文件系统类型? [ext2]? ext4起始点? 1Mib 结束点? 10241Mib(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system 标志1 1049kB 10.7GB 10.7GB primary xfs(parted) unit MiB(parted) print
Model: ATA 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 标志1 1.00MiB 10241MiB 10240MiB primary xfs
设置单位
parted工具默认单位是MB(103K),设置为MiB(210KiB)
(parted) unit GiB(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system 标志1 0.00GiB 10.0GiB 10.0GiB primary xfs
管理 MBR 磁盘
设置磁盘分区管理方案
# 输入mklabel或mktable设置磁盘分区管理方案
# 设置分区方案为msdos,也就是MBR,输入mklabel msdos
[root@server ~ 16:51:15]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.(parted) mklabel msdos
警告: 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(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system 标志
**注意:**parted命令所做更改立刻生效。
创建分区
# 创建分区,输入 mkpart
(parted) mkpart分区名称? []? webapp1# 设置分区文件系统类型,输入 xfs,实际不生效,格式化文件系统仍需手动操作文件系统类型? [ext2]? ext4起始点? 1MiB结束点? 20481MiB (parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1049kB 21.5GB 21.5GB ext4 webapp1#方法二 (parted) mkpart webapp2 ext4 20481 40961
警告: You requested a partition from 20.5GB to 41.0GB
(sectors 40001953..80001953).
The closest location we can manage is 21.5GB to 41.0GB
(sectors 41945088..80001953).
Is this still acceptable to you?是/Yes/否/No? no (parted) mkpart webapp2 ext4 20482 40962
警告: You requested a partition from 20.5GB to 41.0GB
(sectors 40003906..80003906).
The closest location we can manage is 21.5GB to 41.0GB
(sectors 41945088..80003906).
Is this still acceptable to you?是/Yes/否/No? yes (parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1049kB 21.5GB 21.5GB ext4 webapp12 21.5GB 41.0GB 19.5GB webapp2#删除
(parted) rm 2 (parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1049kB 21.5GB 21.5GB ext4 webapp1[root@server ~ 11:30:42]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 10G 0 part /data1
sdc 8:32 0 200G 0 disk
└─sdc1 8:33 0 20G 0 part
sr0 11:0 1 4.4G 0 rom
免交互操作
# 设置磁盘分区管理方案
[root@server ~ 17:05:06]# parted /dev/sdb mklabel msdos
警告: 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
信息: You may need to update /etc/fstab.# 查看分区
[root@server ~ 17:05:17]# parted /dev/sdb unit MiB print
Model: ATA 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 标志# 创建分区
[root@server ~ 17:05:24]# parted /dev/sdb unit MiB mkpart primary 1 2049
信息: You may need to update /etc/fstab.# 扩展分区
[root@server ~ 17:05:33]# parted /dev/sdb unit MiB resizepart 1 5121
信息: You may need to update /etc/fstab.[root@server ~ 17:05:40]# parted /dev/sdb unit MiB print
Model: ATA 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 标志1 1.00MiB 5121MiB 5120MiB primary# 删除分区
[root@server ~ 17:05:50]# parted /dev/sdb rm 1
信息: You may need to update /etc/fstab.[root@server ~ 17:05:57]# parted /dev/sdb unit MiB print
Model: ATA 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 标志[root@server ~ 17:06:03]#
[root@server ~ 11:39:23]# parted /dev/sdc
GNU Parted 3.1
使用 /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of
commands.(parted) mklabel gpt(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志(parted) mkpart primary 10242 20470(parted) print
Model: ATA 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 标志1 1.00MiB 10241MiB 10240MiB primary xfs2 10242MiB 20470MiB 10228MiB primary(parted) rm 2 [root@server ~ 11:27:21]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 10G 0 part
[root@server ~ 11:27:30]# mount /dev/sdb1 /data1
[root@server ~ 11:30:34]# df -h /data1
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 5.0G 33M 5.0G 1% /data1
管理 GPT 磁盘
管理GPT磁盘基本与管理MBR磁盘一致,除了创建分区。
设置磁盘分区管理方案
[root@server ~ 17:06:03]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.(parted) mklabel gpt
警告: 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(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志
创建分区
(parted) unit MiB (parted) mkpart分区名称? []? data01文件系统类型? [ext2]? xfs起始点? 1结束点? 2049(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1.00MiB 2049MiB 2048MiB data01
扩展分区
(parted) resizepart 1 5121(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1.00MiB 5121MiB 5120MiB data01
删除分区
(parted) rm 1(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志
免交互操作
# 设置磁盘分区管理方案
[root@server ~ 17:14:37]# parted /dev/sdb mklabel gpt
警告: 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
信息: You may need to update /etc/fstab.# 查看分区
[root@server ~ 17:14:47]# parted /dev/sdb print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志# 创建分区
[root@server ~ 17:14:52]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.[root@server ~ 17:14:57]# parted /dev/sdb unit MiB print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1.00MiB 2049MiB 2048MiB data01# 扩展分区
[root@server ~ 17:15:03]# parted /dev/sdb unit MiB resizepart 1 5121
信息: You may need to update /etc/fstab.[root@server ~ 17:15:09]# parted /dev/sdb unit MiB print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志1 1.00MiB 5121MiB 5120MiB data01# 删除分区
[root@server ~ 17:15:14]# parted /dev/sdb rm 1
信息: You may need to update /etc/fstab.[root@server ~ 17:15:19]# parted /dev/sdb unit MiB print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name 标志
文件系统持久化挂载
环境准备
利用 parted 创建一个分区,并格式化为xfs文件系统。
[root@server ~ 17:19:11]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.[root@server ~ 17:19:57]# mkfs.xfs /dev/sdb1
mkfs.xfs: cannot open /dev/sdb1: 设备或资源忙
[root@server ~ 17:20:04]# mkfs.xfs -f /dev/sdb1
持久化挂载
当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /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 ~ 13:51:37]# blkid /dev/sdb1
/dev/sdc1: UUID="a25b98fe-8209-42b4-864a-16f54b1f4d0d" TYPE="ext4" PARTLABEL="webapp1" PARTUUID="cd8deb62-2a61-4030-92ce-d07e0b5d8d54" [root@server ~ 13:52:47]# mount UUID="a25b98fe-8209-42b4-864a-16f54b1f4d0d" /webapp1
[root@server ~ 13:53:32]# ls /webapp1
lost+found
[root@server ~ 13:53:41]# cp /etc/host* /webapp1
[root@server ~ 13:54:03]# df -h /webapp1
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdc1 9.8G 37M 9.2G 1% /webapp1
[root@server ~ 13:54:18]# vim /etc/fstab
#只读
[root@server ~ 14:00:37]# grep webapp /etc/fstab
UUID="a25b98fe-8209-42b4-864a-16f54b1f4d0 /webapp ext4 ro 0 0
[root@server ~ 13:57:20]# touch /webapp/f1
touch: 无法创建"/webapp/f1": 没有那个文件或目录
#修改进行可读可写
[root@server ~ 13:57:29]# mount -o rw,remount /webapp1
[root@server ~ 13:57:50]# cp /etc/fstab /webapp1
[root@server ~ 13:58:05]# ls /webapp1
fstab host.conf hostname hosts hosts.allow hosts.deny lost+found
[root@server ~ 13:58:10]#
取消持久化挂载
删除/etc/fstab中对应条目即可。
综合案例:文件系统空间不足
情况1:大量的大文件占用空间
清理
[root@server ~ 14:14:49]# umount -a
umount: /:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup/systemd:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /run:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /dev:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
[root@server ~ 14:15:07]# df
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 1001840 0 1001840 0% /dev
tmpfs 1013928 9836 1004092 1% /run
tmpfs 1013928 0 1013928 0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 2801696 49601504 6% /
[root@server ~ 14:15:09]# wipefs -a /dev/sdb
/dev/sdb:2 个字节已擦除,位置偏移为 0x000001fe (dos):55 aa
/dev/sdb: calling ioclt to re-read partition table: 成功
准备环境
[root@server ~ 14:15:27]# parted /dev/sdb mklabel gpt
信息: You may need to update /etc/fstab.[root@server ~ 14:36:53]# parted /dev/sdb unit MiB mkpart data01 xfs起始点? 1结束点? 2049
信息: You may need to update /etc/fstab.[root@server ~ 14:37:48]# 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:38:05]# 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:38:17]# mkdir /myapp-1
[root@server ~ 14:38:27]# mount /dev/sdb1 /myapp-1
[root@server ~ 14:38:41]# cp -r /etc /myapp-1
[root@server ~ 14:38:52]# ls /myapp-1
etc
[root@server ~ 14:39:00]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: 写入"/myapp-1/etc/bigfile" 出错: 设备上没有空间
记录了1968+0 的读入
记录了1967+0 的写出
2063007744字节(2.1 GB)已复制,7.92726 秒,260 MB/秒
[root@server ~ 14:40:02]#
原因:大文件占用大量空间。
解决方法:找到文件后删除。
[root@server ~ 14:40:02]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 2.0G 432K 100% /myapp-1
[root@server ~ 14:40:31]# find /myapp-1/ -size +100M
/myapp-1/etc/bigfile
[root@server ~ 14:40:51]# du -s /myapp-1/*|sort -n |tail -2
2052112 /myapp-1/etc
[root@server ~ 14:41:15]# du -s /myapp-1/etc/*| sort -n |tail -2
23520 /myapp-1/etc/selinux
2014656 /myapp-1/etc/bigfile
[root@server ~ 14:41:43]# du -s /myapp-1/etc/bigfile/*| sort -n |tail -2
du: 无法访问"/myapp-1/etc/bigfile/*": 不是目录
[root@server ~ 14:42:02]# rm -f /myapp-1/etc/bigfile
[root@server ~ 14:42:16]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 71M 2.0G 4% /myapp-1
[root@server ~ 14:42:33]#
找到文件后,删除即可。
情况2:删除文件后,空间没有释放
准备环境
[root@server ~ 14:55:26]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: 写入"/myapp-1/etc/bigfile" 出错: 设备上没有空间
记录了1968+0 的读入
记录了1967+0 的写出
2062548992字节(2.1 GB)已复制,1.40337 秒,1.5 GB/秒
[root@server ~ 14:56:09]# tail -f /myapp-1/etc/bigfile &
[1] 2961
您在 /var/spool/mail/root 中有新邮件
[root@server ~ 15:07:46]# rm -f /myapp-1/etc/bigfile
[root@server ~ 15:08:12]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 2.0G 880K 100% /myapp-1
原因:被删除的文件,仍然有程序在使用。
解决方法:找到像一个的程序并,终止程序。
[root@server ~ 15:08:58]# lsof | grep delete |grep /myapp-1[root@server ~ 15:09:42]# kill 2961[root@server ~ 15:10:05]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 71M 2.0G 4% /myapp-1#方法二
[root@server ~ 17:24:12]# > /myapp-1/etc/bigfile
建议:清理大文件,先使用重定向清空文件内容,再删除文件。
情况3:大量的小文件占用空间
准备环境
[root@server ~ 20:35:59]# parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073[root@server ~ 20:41:04]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 32768, 98304, 163840, 229376Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@server ~ 20:45:50]# mount /dev/sdb1 /myapp-2[root@server ~ 20:47:34]# df -i /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb1 65536 12 65524 1% /myapp-2[root@server ~ 20:48:13]# touch /myapp-2/file-{00001..65530}
touch: 无法创建"/myapp-2/file-65525": 设备上没有空间
touch: 无法创建"/myapp-2/file-65526": 设备上没有空间
touch: 无法创建"/myapp-2/file-65527": 设备上没有空间
touch: 无法创建"/myapp-2/file-65528": 设备上没有空间
touch: 无法创建"/myapp-2/file-65529": 设备上没有空间
touch: 无法创建"/myapp-2/file-65530": 设备上没有空间
[root@server ~ 20:48:31]# df -h /myapp-2
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 976M 4.4M 905M 1% /myapp-2
[root@server ~ 20:48:43]# df -i /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb1 65536 65536 0 100% /myapp-2
原因:文件系统中inode使用完了。
解决方法:删除大量的小文件或者将这些小文件备份到其他地方。
[root@server ~ 20:48:49]# df -ih /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb1 64K 64K 0 100% /myapp-2# 思路1:直接将这些小文件移走或删除
[root@server ~ 20:51:22]# rm -f /myapp-2/file-0*
... ...
[root@server ~ 20:51:45]# df -ih /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb1 64K 55K 9.8K 85% /myapp-2# 思路2:合并大量小文件为单个文件
[root@server ~ 20:53:14]# cat /myapp-2/file-{00001..10000} > file-00001
思考:如何彻底擦除硬盘上数据
解答:
- 逻辑破坏:用0填充所有数据,例如
dd if=/dev/zero of=/dev/sdb
。 - 物理破坏,例如消磁。