Linux环境下硬盘LVM机制扩容

Linux环境下硬盘LVM机制扩容


一、专业术语的概念

  • 分区:
    • 主分区:也称引导分区,至少1个,最多4个;
    • 扩展分区:分出主分区后剩余部分可以分成扩展分区,最多1个,也可以没有;
    • 逻辑分区:扩展分区不能直接使用,需分成若干逻辑分区才行,因此扩展分区包含逻辑分区;
    • 硬盘容量=主分区容量+扩展分区容量;
    • 扩展分区容量=各个逻辑分区的容量之和;
  • 卷:
    • 物理卷(PV):对应的是硬件存储设备,是磁盘或磁盘分区;
    • 逻辑卷(LV)由卷组中的物理卷动态分配而来;
    • 卷组(VG):由一个或多个物理卷组成的逻辑组,其最小存储单元为物理区域(PE);

二、在已有虚拟硬盘上扩容(以VMware为例)

LVM机制下存储结构图

1.调整硬盘大小 ,将20G变为40G

在这里插入图片描述 通过lsblk可以看到sda这块硬盘已变为40G,但sda1,sda2两个主分区共占用20G(注意centos-root和centos-swap是主分区中挂载的两个逻辑卷并不是逻辑分区,可通过parted /dev/sda print查看分区类型),新增的20G待分配。

[root@host2 /]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1  973M  0 rom  

[root@host2 /]# parted /dev/sda print
Number  Start   End     Size    Type     File system  标志
 1      1049kB  1075MB  1074MB  primary  xfs          启动
 2      1075MB  21.5GB  20.4GB  primary               lvm

2.加分区:

  • 检查分区表类型。分区表分为mbr和gpt两种类型,先通过parted -l查看硬盘用的是什么类型的分区表,Partition Table字段表示分区表类型,msdos为mbr类型,这是用fdisk命令去操作分区,如果是gpt类型,则用gdisk命令。
[root@host2 /]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
  • 增加新分区。通过fdisk将之前新增的20G分配为编号3的主分区。
[root@host2 /]# fdisk /dev/sda
欢迎使用 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)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (41943040-83886079,默认为 41943040):
将使用默认值 41943040
Last 扇区, +扇区 or +size{K,M,G} (41943040-83886079,默认为 83886079):
将使用默认值 83886079
分区 3 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 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@host2 /]# partprobe /dev/sda
  • 格式化文件类型。新分区一般需要格式化文件类型后才能使用,这里我们使用的LVM进行硬盘管理,则只需格式化最后新建的逻辑卷,可通过lsblk -f查看文件系统类型。
 //查看分区的文件类型,LVM2_member表示采用LVM进行管理
 [root@host2 /]# lsblk -f
NAME            FSTYPE      LABEL           UUID                                   MOUNTPOINT
sda                                                                                
├─sda1          xfs                         e4bfe65d-98d2-4911-9f41-68aeb7984622   /boot
└─sda2          LVM2_member                 13aAGy-cZqs-V1Vr-K7mi-sxNs-bVRN-zfB2k0 
  ├─centos-root xfs                         429d8c44-9eab-4814-878c-60ad2ca393e3   /
  └─centos-swap swap                        2658efdf-2332-4c16-9ee6-09096028fe2d   [SWAP]
sr0             iso9660     CentOS 7 x86_64 2020-11-03-14-55-29-00  
  • 如果没有用LVM管理,则格式化分区后再挂载到文件系统就可以实现扩容了。

3.卷扩容:

  • 新分区绑定物理卷(注:物理卷、卷组、逻辑卷属于lvm范畴,所以需要通过lvm命令进入到lvm管理界面中进行操作)。
lvm> pvcreate /dev/sda3
 Physical volume "/dev/sda3" successfully created.
  • 将物理卷加入到卷组中。
lvm> vgextend centos /dev/sda3
Volume group "centos" successfully extended
//可通过vgdisplay命令查看卷组free PE /Site数值变化
//加入之前
lvm> vgdisplay
--- Volume group ---
VG Name               centos
Free  PE / Size       0 / 0   
//加入之后
lvm> vgdisplay 
--- Volume group ---
VG Name               centos
Free  PE / Size       5119 / <20.00 GiB
  • 逻辑卷这里扩容有两种方式(现在有20G容量,10G对已有逻辑卷扩容,10G建个新逻辑卷):

第一种:对已有逻辑卷扩容(这里不用再去手动格式化卷)

//对centos卷组下的root逻辑卷扩容10G
lvm> lvextend -L +10G /dev/centos/root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <27.00 GiB (6911 extents).
  Logical volume centos/root successfully resized.
//扩容前后LV Size对比
lvm> lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                d2FFMX-D0aW-0z1b-SU1k-jTM3-wEBc-3k44T9
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-07-19 19:28:21 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
 - currently set to     8192
  Block device           253:0
  
lvm> lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                d2FFMX-D0aW-0z1b-SU1k-jTM3-wEBc-3k44T9
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-07-19 19:28:21 +0800
  LV Status              available
  # open                 1
  LV Size                <27.00 GiB
  Current LE             6911
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
 - currently set to     8192
  Block device           253:0
//

第二种:新建逻辑卷,格式化然后进行挂载

//创建test逻辑卷
lvm> lvcreate -l 2559 -n test centos
  Logical volume "test" created.
//格式化
[root@host2 /]# mkfs.xfs /dev/centos/test
//创建挂载点挂载
[root@host2 /]# mkdir /testfile
[root@host2 /]# mount /dev/centos/test /testfile
//成功
[root@host2 /]# df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/centos-test   10G   33M   10G    1% /testfile

4.文件系统扩容:

//此时可看到显示的卷已经有27G了,但对应/dev/mapper/centos-root文件系统还是17G
[root@host2 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0   19G  0 part 
│ ├─centos-root 253:0    0   27G  0 lvm  /
│ └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
└─sda3            8:3    0   20G  0 part 
  └─centos-root 253:0    0   27G  0 lvm  /

[root@host2 /]# df -h
文件系统                   容量  已用   可用   已用% 挂载点
/dev/mapper/centos-root   17G  2.9G   15G   17% /

所以需要通过resize2fs或xfs_growfs命令同步调整文件系统的大小

[root@host2 /]# xfs_growfs /dev/mapper/centos-root
[root@host2 /]# df -h
文件系统                   容量  已用   可用    已用% 挂载点
/dev/mapper/centos-root   27G  2.9G   25G   11% /

总结

额外加硬盘扩容跟上述虚拟硬盘本身扩容操作差不多,值得注意的是硬盘有没有采用LVM去进行管理,有的话需要去操作卷,没有则直接加分区挂载即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值