LVM逻辑卷操作实录

之前讲了LVM基础详细说明,现在记录下LVM逻辑卷的操作实录。

一、准备物理分区(Physical Partions)

首先,我们需要选择用于 LVM 的物理存储器。这些通常是标准分区,但也可以是已创建的 Linux Software RAID 卷。这里我利用fdisk命令,将sdb、sdc两块磁盘分了两个区sdb1、sdc1, 通过fdisk的t指令指定分区为8e类型(Linux LVM) ,如下:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x00099e91

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   104857599    52427776   83  Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x8c6de33b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM

Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0xe60d5065

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    62914559    31456256   8e  Linux LVM

二、创建物理卷PV(Physical Volumes)

[root@localhost ~]# pvcreate /dev/sdb1    # 如果没有pvcreate\命令,可以使用yum install -y lvm2进行安装
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdc1    #可以使用pcvreate /dev/{sdb1,sdc1}
  Physical volume "/dev/sdc1" successfully created.

三、创建卷组VG(Volume Groups),命名为vg_test1

[root@localhost ~]# vgcreate vg_test1 /dev/sdb1 
  Volume group "vg_test1" successfully created
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_test1
  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               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               o8efai-0E75-XBW3-Oaxn-pIf3-BiLS-SpAAD3

四、创建逻辑卷LV(Logical Volumes) ,命名为lv_test1

创建逻辑卷的命令为lvcreate,-l 参数为定PE数来设定逻辑分区大小,也可以使用-L参数直接指定逻辑分区大小,-n参数指定逻辑分区名称。

[root@localhost ~]# lvcreate -L 10G -n lv_test1 vg_test1
  Logical volume "lv_test1" created.
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_test1/lv_test1
  LV Name                lv_test1
  VG Name                vg_test1
  LV UUID                zcIjip-NOAH-zNyI-lPrB-1f44-wHoi-cgtTSz
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-07-05 15:22:45 +0800
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

五、创建文件系统并挂载

在逻辑卷上创建ext4文件系统:

[root@localhost ~]# mkfs -t ext4 /dev/vg_test1/lv_test1    # 可以直接使用mkfs.ext4进行格式化
[root@localhost ~]# mount /dev/vg_test1/lv_test1 /data/
[root@localhost ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/sda1                       50G  1.5G   49G   3% /
devtmpfs                       903M     0  903M   0% /dev
tmpfs                          912M     0  912M   0% /dev/shm
tmpfs                          912M  8.6M  904M   1% /run
tmpfs                          912M     0  912M   0% /sys/fs/cgroup
tmpfs                          183M     0  183M   0% /run/user/0
/dev/mapper/vg_test1-lv_test1  9.8G   37M  9.2G   1% /data

六、动态调整LVM分区大小

LVM的最大好处就是可以动态地调整分区大小,而无须重新启动,下面让我们来体验一下吧!
继续上面的实例,现假设逻辑卷 /dev/vg_test1/lv_test1空间不足,需要增加其大小,我们分两种情况讨论:

1、当卷组中有剩余的空间

通过vgdisplay命令可以检查当前卷组空间使用情况:

[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_test1
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       2559 / <10.00 GiB
  VG UUID               o8efai-0E75-XBW3-Oaxn-pIf3-BiLS-SpAAD3

确定当前卷组剩余空间10GB,剩余PE数量为2559个。在这里将所有的剩余空间全部增加给逻辑卷 /dev/vg_test1/lv_test1

[root@localhost ~]# lvextend -l +2559 /dev/vg_test1/lv_test1 
  Size of logical volume vg_test1/lv_test1 changed from 10.00 GiB (2560 extents) to <20.00 GiB (5119 extents).
  Logical volume vg_test1/lv_test1 successfully resized.

修改逻辑卷大小后,通过resize2fs来修改文件系统的大小:

[root@localhost ~]# resize2fs /dev/vg_test1/lv_test1 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg_test1/lv_test1 is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vg_test1/lv_test1 is now 5241856 blocks long.

再次查看文件系统的大小:

[root@localhost ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/sda1                       50G  1.5G   49G   3% /
devtmpfs                       903M     0  903M   0% /dev
tmpfs                          912M     0  912M   0% /dev/shm
tmpfs                          912M  8.6M  904M   1% /run
tmpfs                          912M     0  912M   0% /sys/fs/cgroup
tmpfs                          183M     0  183M   0% /run/user/0
/dev/mapper/vg_test1-lv_test1   20G   44M   19G   1% /data

2、当卷组中空间不足时

当卷组中没有足够的空间用于扩展逻辑卷的大小时,就需要增加卷组的容量,而增加卷组容量的唯一办法就是向卷组中添加新的物理卷。
首先需要对新增加的磁盘进行分区、创建物理卷等工作,接下来是利用vgextend命令将新的物理卷加入到卷组中, 这里使用/dev/sdc1。

[root@localhost ~]# vgextend vg_test1 /dev/sdc1 
  Volume group "vg_test1" successfully extended
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_test1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               49.99 GiB
  PE Size               4.00 MiB
  Total PE              12798
  Alloc PE / Size       5119 / <20.00 GiB
  Free  PE / Size       7679 / <30.00 GiB
  VG UUID               o8efai-0E75-XBW3-Oaxn-pIf3-BiLS-SpAAD3

对逻辑卷进行扩容

[root@localhost ~]# lvextend -L +20G /dev/vg_test1/lv_test1  
  Size of logical volume vg_test1/lv_test1 changed from <20.00 GiB (5119 extents) to <40.00 GiB (10239 extents).
  Logical volume vg_test1/lv_test1 successfully resized.

修改逻辑卷大小后,通过resize2fs来修改文件系统的大小:

[root@localhost ~]# resize2fs /dev/vg_test1/lv_test1 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg_test1/lv_test1 is mounted on /data; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/vg_test1/lv_test1 is now 10484736 blocks long.

再次查看文件系统的大小:

[root@localhost ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/sda1                       50G  1.5G   49G   3% /
devtmpfs                       903M     0  903M   0% /dev
tmpfs                          912M     0  912M   0% /dev/shm
tmpfs                          912M  8.6M  904M   1% /run
tmpfs                          912M     0  912M   0% /sys/fs/cgroup
tmpfs                          183M     0  183M   0% /run/user/0
/dev/mapper/vg_test1-lv_test1   40G   48M   38G   1% /data

参考文章:https://www.cnblogs.com/kevingrace/p/5845634.html

你可能感兴趣的:(LVM)