磁盘是存储里最常见的存储介质,不管是新服务器上线、存储扩容,还是系统迁移,都离不开对磁盘的操作,今天分享一下磁盘存储管理的知识。
MBR分区(Master Boot Record)
- 支持最大2TB的磁盘容量
- 最多支持4个主分区
fdisk命令
基本语法:fdisk [选项] 设备名
常用操作:
# 查看磁盘分区信息
fdisk -l
# 对指定磁盘进行分区操作
fdisk /dev/sdb
常用交互命令:
- p:显示分区表
- n:创建新分区
- d:删除分区
- t:改变分区类型
- w:保存并退出
- q:不保存退出
- m:查看帮助
#fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
GPT分区(GUID Partition Table)
- 支持超大容量磁盘(理论上可达18EB)
- 支持128个主分区(在Linux下)
- 分区表有备份
gdisk命令
基本语法:gdisk [选项] 设备名
常用操作:
# 查看磁盘分区信息,设备不可省略
gdisk -l /dev/sdb
# 对指定磁盘进行分区操作
gdisk /dev/sdb
常用交互命令和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
新磁盘处理流程
1 识别新磁盘
lsblk命令 - 查看块设备信息(常用参数f、a、l,一般不加)
用法:
lsblk [选项] [<设备> ...]
常用选项:
-a, --all 打印所有设备
-f, --fs 输出文件系统信息
-h, --help 使用信息(此信息)
-l, --list 使用列表格式的输出
-p, --paths 打印完整设备路径
例
[07:05:45 root@localhost\ [ ~]#lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.4G 0 rom
vda 252:0 0 10G 0 disk
vda1 252:1 0 1G 0 part /boot
vda2 252:2 0 9G 0 part
centos-root 253:0 0 8G 0 lvm /
centos-swap 253:1 0 1G 0 lvm [SWAP]
vdb 252:16 0 5G 0 disk
#↑发现5G磁盘
2 创建分区
MBR分区方式
[root@localhost ~] fdisk /dev/sdb
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical drives)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P}: +8G
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical drives)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (16779264-20971519, default 16779264):
Last sector, +sectors or +size{K,M,G,T,P}:
Command (m for help): w
GPT分区方式
[root@localhost ~]# gdisk /dev/vdb
GPT fdisk (gdisk) version 0.8.10
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-10485726, default = 10485726) 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/vdb: 10485760 sectors, 5.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): CAD5CEFE-CCD0-4BBC-910A-007CBA9F96D6
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 10485726
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 10485726 5.0 GiB 8300 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/vdb.
The operation has completed successfully.
parted方式创建GPT分区,parted是一种支持MBR和GPT分区表的磁盘分区命令.
[root@localhost ~]#parted /dev/vdb
(parted) mklabel gpt #分区方式,需要MBR格式则这里写mbr
(parted) mkpart vdb1 0% 100%
(parted) quit
parted交互方式
(parted) mkpart vdb1
分区类型? primary/主分区/extended/扩展分区? p
文件系统类型? [ext2]? xfs
起始点? 0
结束点? 100%
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 512B 5369MB 5369MB primary
3 创建逻辑卷(LVM)
LVM(Logical Volume Manager)是Linux下的逻辑卷管理器,它提供了比传统分区更灵活的磁盘管理方式,它可以动态调整容量。
- PV(Physical Volume):物理卷,可以是整个磁盘或分区
- VG(Volume Group):卷组,由一个或多个PV组成的存储池
- LV(Logical Volume):逻辑卷,从VG中分配空间创建的虚拟分区
step1:创建物理卷(PV)
[root@localhost ~]# pvcreate /dev/vdb1 #如果创建多个pv可以继续往后写如/dev/vdb2
Physical volume "/dev/vdb1" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <9.00g 0 #这个是操作系统安装时自动创建的
/dev/vdb1 lvm2 --- <5.00g <5.00g
step1:创建卷组(VG)
[root@localhost ~]# vgcreate vg1 /dev/vdb1 #如果有多个pv可以继续往后写
Volume group "vg1" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <9.00g 0
vg1 1 0 0 wz--n- <5.00g <5.00g
step1:创建逻辑卷(LV)
#-l按比例分配,-L也可按大小分配
[root@localhost ~]# lvcreate -l 100%FREE -n lv1 vg1
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <8.00g
swap centos -wi-ao---- 1.00g
lv1 vg1 -wi-a----- <5.00g
# 创建文件系统
mkfs.xfs /dev/vg1/lv1
# 创建挂载点
mkdir /data
# 挂载
mount /dev/vg1/lv1 /data
# 验证
df -h
#输出类似以下则成功
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 8.0G 1.5G 6.5G 19% /
/dev/vda1 1014M 140M 875M 14% /boot
/dev/mapper/vg1-lv1 5.0G 33M 5.0G 1% /data
配置自动挂载
# 编辑/etc/fstab
vim /etc/fstab
# 添加以下内容
/dev/mapper/vg1-lv1 /data xfs defaults 0 0
# 测试配置,配置正确则没有任何输出
mount -a
4 最佳实践建议
一个完整的文件系统结构如下所示
文件系统(fs)
↑
逻辑卷(Logical Volume, LV)
↑
卷组(Volume Group, VG)
↑
物理卷(Physical Volume, PV)
↑
分区(part)
↑
物理设备(硬盘)
但依据实际情况可有所裁剪
- 虚拟化环境可直接使用硬盘+文件系统组合
- 要求多个分区的推荐使用硬盘+分区+文件系统或硬盘+LVM+文件系统组合
- 物理机环境建议用完整架构,即硬盘+分区+LVM+文件系统