1. 背景
在服务器运维中,经常需要挂载或者扩展存储空间。
lvm(logical volume manager)是linux下强大的磁盘管理工具,支持动态调整分区大小、快照、raid等功能。
本文将详细介绍如何在centos linux 7.6上添加一块新的500gb硬盘,并进行lvm配置为以后扩容做准备。
2. 准备工作
- 操作系统:centos linux 7.6
- 新硬盘:500gb
- 分区表类型:gpt(支持大于2tb的磁盘,并具有更好的兼容性)
- 目标:将新硬盘初始化为lvm,并挂载到
/data目录
3. 操作过程
3.1. 确认硬盘
首先,用lsblk命令检查系统是否识别了新磁盘:
[hljkjc@hljkjc ~]$ lsblk name maj:min rm size ro type mountpoint vda 253:0 0 200g 0 disk ├─vda1 253:1 0 4g 0 part [swap] └─vda2 253:2 0 196g 0 part / vdb 253:16 0 500g 0 disk
切换到root账号操作,或者用fdisk -l命令查看:
[hljkjc@hljkjc ~]$ fdisk -l disk /dev/vda: 214.7 gb, 214748364800 bytes, 419430400 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: 0x000bcc64 device boot start end blocks id system /dev/vda1 2048 8390655 4194304 82 linux swap / solaris /dev/vda2 * 8390656 419430366 205519855+ 83 linux disk /dev/vdb: 536.9 gb, 536870912000 bytes, 1048576000 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
可以看到系统新添加的物理硬盘为/dev/vdb,容量为536.9gib。
3.2. 创建新分区
使用 fdisk 或 parted 创建 lvm 分区(以 fdisk 为例):
fdisk /dev/vdb
在交互界面中按顺序输入:
- 1)
n→ 新建分区; - 2)
p→ 创建主分区; - 3)
1→ 分区号(默认1); - 4) 起始扇区(直接回车,默认);
- 5)结束扇区(直接回车,使用全部空间);
- 6)
t→ 修改分区类型 ; - 7)
8e→ 设置为 lvm 类型(linux lvm); - 8)
w→ 保存并退出
[root@hljkjc hljkjc]# fdisk /dev/vdb
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 0xa7b107d2.
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): 1
first sector (2048-1048575999, default 2048):
using default value 2048
last sector, +sectors or +size{k,m,g} (2048-1048575999, default 1048575999):
using default value 1048575999
partition 1 of type linux and of size 500 gib is set
command (m for help): t
selected partition 1
hex code (type l to list all codes): l
0 empty 24 nec dos 81 minix / old lin bf solaris
1 fat12 27 hidden ntfs win 82 linux swap / so c1 drdos/sec (fat-
2 xenix root 39 plan 9 83 linux c4 drdos/sec (fat-
3 xenix usr 3c partitionmagic 84 os/2 hidden c: c6 drdos/sec (fat-
4 fat16 <32m 40 venix 80286 85 linux extended c7 syrinx
5 extended 41 ppc prep boot 86 ntfs volume set da non-fs data
6 fat16 42 sfs 87 ntfs volume set db cp/m / ctos / .
7 hpfs/ntfs/exfat 4d qnx4.x 88 linux plaintext de dell utility
8 aix 4e qnx4.x 2nd part 8e linux lvm df bootit
9 aix bootable 4f qnx4.x 3rd part 93 amoeba e1 dos access
a os/2 boot manag 50 ontrack dm 94 amoeba bbt e3 dos r/o
b w95 fat32 51 ontrack dm6 aux 9f bsd/os e4 speedstor
c w95 fat32 (lba) 52 cp/m a0 ibm thinkpad hi eb beos fs
e w95 fat16 (lba) 53 ontrack dm6 aux a5 freebsd ee gpt
f w95 ext'd (lba) 54 ontrackdm6 a6 openbsd ef efi (fat-12/16/
10 opus 55 ez-drive a7 nextstep f0 linux/pa-risc b
11 hidden fat12 56 golden bow a8 darwin ufs f1 speedstor
12 compaq diagnost 5c priam edisk a9 netbsd f4 speedstor
14 hidden fat16 <3 61 speedstor ab darwin boot f2 dos secondary
16 hidden fat16 63 gnu hurd or sys af hfs / hfs+ fb vmware vmfs
17 hidden hpfs/ntf 64 novell netware b7 bsdi fs fc vmware vmkcore
18 ast smartsleep 65 novell netware b8 bsdi swap fd linux raid auto
1b hidden w95 fat3 70 disksecure mult bb boot wizard hid fe lanstep
1c hidden w95 fat3 75 pc/ix be solaris boot ff bbt
1e hidden w95 fat1 80 old minix
hex code (type l to list all codes): 8e
changed type of partition 'linux' to 'linux lvm'
command (m for help): w
the partition table has been altered!
calling ioctl() to re-read partition table.
syncing disks.
[root@hljkjc hljkjc]#
完成验证区
[root@hljkjc hljkjc]# fdisk -l /dev/vdb disk /dev/vdb: 536.9 gb, 536870912000 bytes, 1048576000 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: 0xa7b107d2 device boot start end blocks id system /dev/vdb1 2048 1048575999 524286976 8e linux lvm
3.3. 创建物理卷(pv)
将 /dev/vdb 初始化为lvm物理卷:
[root@hljkjc hljkjc]#pvcreate /dev/vdb1 bash: pvcreate: command not found # 安装lvm2 [root@hljkjc hljkjc]#yum install lvm2 # 安装成功后重新执行 `pvcreate /dev/vdb1` [root@hljkjc hljkjc]# pvcreate /dev/vdb1 physical volume "/dev/vdb1" successfully created. [root@hljkjc hljkjc]#
查看pv:
[root@hljkjc hljkjc]# pvcreate /dev/vdb1 physical volume "/dev/vdb1" successfully created. [root@hljkjc hljkjc]# pvdisplay "/dev/vdb1" is a new physical volume of "<500.00 gib" --- new physical volume --- pv name /dev/vdb1 vg name pv size <500.00 gib allocatable no pe size 0 total pe 0 free pe 0 allocated pe 0 pv uuid 9y6dum-vefu-qjqw-clfa-cpg1-xm9t-xqzhqc
3.4. 创建卷组(vg)
将物理卷加入卷组,卷组名为 data_vg:
[root@hljkjc hljkjc]# vgcreate data_vg /dev/vdb1 volume group "data_vg" successfully created [root@hljkjc hljkjc]#
查看vg:
[root@hljkjc hljkjc]# vgdisplay --- volume group --- vg name data_vg system id format lvm2 metadata areas 1 metadata sequence no 1 vg access read/write vg status resizable max lv 0 cur lv 0 open lv 0 max pv 0 cur pv 1 act pv 1 vg size <500.00 gib pe size 4.00 mib total pe 127999 alloc pe / size 0 / 0 free pe / size 127999 / <500.00 gib vg uuid gmqole-g48x-dv0k-ffrs-picr-n3jr-urfbel [root@hljkjc hljkjc]#
3.5. 创建逻辑卷(lv)
在卷组中创建逻辑卷,这里命名为 data_lv,大小 100%vg:
[root@hljkjc hljkjc]# lvcreate -l 100%free -n data_lv data_vg logical volume "data_lv" created. [root@hljkjc hljkjc]#
查看lv:
[root@hljkjc hljkjc]# lvdisplay --- logical volume --- lv path /dev/data_vg/data_lv lv name data_lv vg name data_vg lv uuid de7jjl-uof3-lfsc-xs6f-aoj9-wy3d-s446k1 lv write access read/write lv creation host, time hljkjc, 2025-09-12 14:29:03 +0800 lv status available # open 0 lv size <500.00 gib current le 127999 segments 1 allocation inherit read ahead sectors auto - currently set to 8192 block device 252:0 [root@hljkjc hljkjc]#
3.6. 格式化并挂载
3.6.1. 格式化
使用 ext4 或 xfs 格式化lv,这里采用 xfs 进行格式化操作:
[root@hljkjc hljkjc]# mkfs.xfs /dev/data_vg/data_lv
meta-data=/dev/data_vg/data_lv isize=512 agcount=4, agsize=32767744 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=131070976, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=63999, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@hljkjc hljkjc]#
3.6.2. 创建挂载点并挂载
[root@hljkjc hljkjc]# mkdir /data [root@hljkjc hljkjc]# mount /dev/data_vg/data_lv /data [root@hljkjc hljkjc]# df -th filesystem type size used avail use% mounted on devtmpfs devtmpfs 16g 0 16g 0% /dev tmpfs tmpfs 16g 0 16g 0% /dev/shm tmpfs tmpfs 16g 8.8m 16g 1% /run tmpfs tmpfs 16g 0 16g 0% /sys/fs/cgroup /dev/vda2 ext4 193g 3.0g 182g 2% / tmpfs tmpfs 3.2g 0 3.2g 0% /run/user/0 tmpfs tmpfs 3.2g 0 3.2g 0% /run/user/1000 /dev/mapper/data_vg-data_lv xfs 500g 33m 500g 1% /data [root@hljkjc hljkjc]#

3.6.3. 开机自动挂载
备份/etc/fstab文件
cp /etc/fstab{,.bak}
编辑 /etc/fstab:
[root@hljkjc hljkjc]# echo "/dev/data_vg/data_lv /data xfs defaults 0 0" >> /etc/fstab [root@hljkjc hljkjc]#
重启服务器后验证:
[root@hljkjc hljkjc]# mount -a [root@hljkjc hljkjc]# df -h filesystem size used avail use% mounted on devtmpfs 16g 0 16g 0% /dev tmpfs 16g 0 16g 0% /dev/shm tmpfs 16g 8.7m 16g 1% /run tmpfs 16g 0 16g 0% /sys/fs/cgroup /dev/vda2 193g 3.0g 182g 2% / /dev/mapper/data_vg-data_lv 500g 33m 500g 1% /data tmpfs 3.2g 0 3.2g 0% /run/user/1000 [root@hljkjc hljkjc]# df -h /data filesystem size used avail use% mounted on /dev/mapper/data_vg-data_lv 500g 33m 500g 1% /data [root@hljkjc hljkjc]#
关键命令总结
| 名称 | 命令示例 |
|---|---|
| 确认磁盘 | lsblk或 fdisk -l |
| 创建分区 | fdisk /dev/sda→ 设置类型 8e |
| 创建物理卷 | pvcreate /dev/vdb1 |
| 创建卷组 | vgcreate data_vg /dev/vdb1 |
| 创建逻辑卷 | lvcreate -l 100%free -n data_lv data_vg |
| 格式化 | mkfs.xfs /dev/data_vg/data_lv |
| 挂载 | mount /dev/data_vg/data_lv /data |
| 开机挂载 | echo “…” >> /etc/fstab |
注意事项
1)如果磁盘已存在数据,操作前请备份。
2)如需调整分区大小(如分配部分空间),可在 fdisk 中指定结束扇区。
3)lvm 允许动态扩展,未来可通过 lvextend 和 resize2fs 调整容量。
完成上述步骤后,500gb 磁盘将作为 lvm 逻辑卷挂载到 /data 并可供使用。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论