有同仁看了上文《Linux添加硬盘扩充已有分区存储空间方式》一文后,提出疑问,现在很多云服务器本来没有逻辑卷,添加数据盘后需要自行添加,如何处理?

此文将以某云服务器为例,详细进行解说。

总体步骤

  • 磁盘初始化分区

  • 创建物理卷

  • 创建卷组

  • 创建逻辑卷

  • 通知文件系统生效

磁盘初始化分区

 [root@VM_15_115_centos ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/vda1 50G .6G 45G % /
devtmpfs .8G .8G % /dev
tmpfs .8G 24K .8G % /dev/shm
tmpfs .8G 464K .8G % /run
tmpfs .8G .8G % /sys/fs/cgroup
tmpfs .6G .6G % /run/user/
[root@VM_15_115_centos ~]# fdisk -l 磁盘 /dev/vda:53.7 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x0009ac89 设备 Boot Start End Blocks Id System
/dev/vda1 * Linux 磁盘 /dev/vdb:536.9 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节 [root@VM_15_115_centos ~]# fdisk /dev/vdb
欢迎使用 fdisk (util-linux 2.23.)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 Device does not contain a recognized partition table
使用磁盘标识符 0xae10029f 创建新的 DOS 磁盘标签。 命令(输入 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) 命令(输入 m 获取帮助):g
Building a new GPT disklabel (GUID: AB878B71--455A-9C62-3EBA92ECA888) 命令(输入 m 获取帮助):p 磁盘 /dev/vdb:536.9 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:gpt
Disk identifier: AB878B71--455A-9C62-3EBA92ECA888 # Start End Size Type Name 命令(输入 m 获取帮助):m
命令操作
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
v verify the partition table
w write table to disk and exit
x extra functionality (experts only) 命令(输入 m 获取帮助):n
分区号 (-,默认 ):
第一个扇区 (-,默认 ):
Last sector, +sectors or +size{K,M,G,T,P} (-,默认 ):
已创建分区 命令(输入 m 获取帮助):p 磁盘 /dev/vdb:536.9 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:gpt
Disk identifier: AB878B71--455A-9C62-3EBA92ECA888 # Start End Size Type Name
500G Linux filesyste 命令(输入 m 获取帮助):w
The partition table has been altered! Calling ioctl() to re-read partition table.
正在同步磁盘。

执行结果

 [root@VM_15_115_centos ~]# fdisk -l

 磁盘 /dev/vda:53.7 GB,  字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x0009ac89 设备 Boot Start End Blocks Id System
/dev/vda1 * Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. 磁盘 /dev/vdb:536.9 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:gpt
Disk identifier: AB878B71--455A-9C62-3EBA92ECA888 # Start End Size Type Name
500G Linux filesyste

第二块磁盘已经初始化完毕,空间500G

创建物理卷

 [root@VM_15_115_centos ~]# pvdisplay
[root@VM_15_115_centos ~]# pvcreate /dev/sdb1
Device /dev/sdb1 not found.
[root@VM_15_115_centos ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
[root@VM_15_115_centos ~]# 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
Total PE
Free PE
Allocated PE
PV UUID gV2Rlx-sFHV-9nJT-biZn-iXYf-rl1H-442MZF

注意:设备路径,有些设备是 /dev/sdb 有些是 /dev/vdb ,需要具体根据 fdisk -l 的结果确定。

创建卷组

 [root@VM_15_115_centos ~]# vgdisplay
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]# vgcreate centos /dev/vdb1
Volume group "centos" successfully created

创建结果

 [root@VM_15_115_centos ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size <500.00 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / <500.00 GiB
VG UUID ndS3xn-MIrv-cn2Y-TTxb-pWYi-EBhB-f9wCp2

创建逻辑卷

 [root@VM_15_115_centos ~]# lvdisplay
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]# lvcreate -l +%FREE centos
Logical volume "lvol0" created.

注意:centos为上文创建的卷组的名字

此处将该卷组全部剩余空间划入本逻辑卷

创建结果:

 [root@VM_15_115_centos ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/lvol0
LV Name lvol0
VG Name centos
LV UUID ulWla4-C4SA-WE2e-HnjW-kHbB-TVSp-hstVYQ
LV Write Access read/write
LV Creation host, time VM_15_115_centos, -- :: +
LV Status available
# open
LV Size <500.00 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : [root@VM_15_115_centos ~]#

通知文件系统生效

新卷则建议采用 xfs 类型,当然,采用 ext4 亦可。

但需要注意的是,xfs文件系统使用 xfs_growfs,ext文件系统使用 resize2fs;xfs文件系统只支持增大不支持缩小。

先格式化逻辑卷磁盘分区,卷的名字从 lvdisplay 中查找

 [root@VM_15_115_centos ~]# mkfs.xfs /dev/centos/lvol0
meta-data=/dev/centos/lvol0 isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt=, sparse=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal log bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
[root@VM_15_115_centos ~]#

然后修改文件系统挂载表文件/etc/fstab

 [root@VM_15_115_centos ~]# cat /etc/fstab
UUID=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 noatime,acl,user_xattr
proc /proc proc defaults
sysfs /sys sysfs noauto
debugfs /sys/kernel/debug debugfs noauto
devpts /dev/pts devpts mode=,gid=
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]# vi /etc/fstab
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]# cat /etc/fstab
UUID=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 noatime,acl,user_xattr
proc /proc proc defaults
sysfs /sys sysfs noauto
debugfs /sys/kernel/debug debugfs noauto
devpts /dev/pts devpts mode=,gid=
/dev/centos/lvol0 /data xfs defaults
[root@VM_15_115_centos ~]#

再挂载,然后查看挂载结果

 [root@VM_15_115_centos ~]# mount -a
[root@VM_15_115_centos ~]#
[root@VM_15_115_centos ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/vda1 50G .6G 45G % /
devtmpfs .8G .8G % /dev
tmpfs .8G 24K .8G % /dev/shm
tmpfs .8G 496K .8G % /run
tmpfs .8G .8G % /sys/fs/cgroup
tmpfs .6G .6G % /run/user/
/dev/mapper/centos-lvol0 500G 33M 500G % /data
[root@VM_15_115_centos ~]#

至此,操作成功。

05-07 15:49
查看更多