CentOS 6中创建及管理LVM逻辑卷

LVM简介:

LVM是逻辑管理卷的简写,其可以将多块物理磁盘的分区组成一个大的卷组(可以把他想象成是一个大的资源池),再在卷组上划分出逻辑卷并在其上建立文件系统最终供用户使用。

LVM的好处是可以动态的增加逻辑卷的大小,这样就不用面临随着业务发展,原本的分区空间不足的尴尬啦!

下面是LVM的结构图,最下面是物理磁盘分区,其上是物理卷(一个物理磁盘分区对应一个物理卷),再往上是我称之为“资源池”的卷组,这个“资源池”的容量是由组成其的物理卷的总容量决定的,而最上面则是可以被当成“普通分区”来使用的逻辑卷,逻辑卷可以划分很多个(默认一个卷组可以划分256个逻辑卷),只要其加起来的容量不超过“资源池”的容量就好!

创建LVM:

一、安装LVM软件包

通常情况下,CentOS中都有自带LVM软件包,但为了以防万一,我还是把安装命令贴出来吧~

[root@localhost ~]# yum install -y lvm

二、创建物理磁盘的分区

记得要先添加一块空磁盘呐,本实验中我添加了一块8GB大小的SCSI磁盘。

查看新添加的磁盘。

[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.2G 0 rom /media/cdrom
sda 8:0 0 8G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 7.5G 0 part
 ├─VolGroup-lv_root (dm-0) 253:0 0 6.7G 0 lvm /
 └─VolGroup-lv_swap (dm-1) 253:1 0 816M 0 lvm [SWAP]
sdb 8:16 0 8G 0 disk

如果新加的磁盘不能被系统立即识别请参考此篇文章。

https://www.ibadboy.net/archives/1895.html

使用fdisk工具对新加磁盘进行分区。

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x028d3c84.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
     switch off the mode (command 'c') and change display units to
     sectors (command 'u').
Command (m for help): n          //新建磁盘分区
Command action
   e   extended
   p   primary partition (1-4)
p            //新建为主分区
Partition number (1-4): 1             //设置分区编号
First cylinder (1-1044, default 1):   s           //设置起始扇区
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044):522           //设置结束扇区,这里我结束扇区设置为总扇区数的一半,相当于把分区容量设置为总容量的一半
Command (m for help): n          //新建第二块磁盘分区
Command action
   e   extended
   p   primary partition (1-4)
p           //新建为主分区
Partition number (1-4): 2            //设置分区编号
First cylinder (523-1044, default 523):          //设置起始扇区
Using default value 523
Last cylinder, +cylinders or +size{K,M,G} (523-1044, default 1044):             //设置结束扇区
Using default value 1044
Command (m for help): t             //更改分区的标识
Partition number (1-4): 1           //选择编号为1的分区进行操作
Hex code (type L to list codes): 8e //将分区标识更改为“Linux LVM”,其对应的代码是8e,这里可以输入list来查看所有标识对应的代码
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t             //更改分区的标识
Partition number (1-4): 2           //选择编号为2的分区进行操作
Hex code (type L to list codes): 8e //将分区标识更改为“Linux LVM”
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): p             //查看刚刚新建的分区
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x028d3c84
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         522     4192933+  8e  Linux LVM
/dev/sdb2             523        1044     4192965   8e  Linux LVM
Command (m for help): w             //保存并退出fdisk工具
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

三、创建物理卷

使用新创建的/dev/sdb1和/dev/sdb2这两个分区分别组建物理卷。

[root@localhost ~]# pvcreate /dev/sdb[12]
  Physical volume "/dev/sdb1" successfully created          //提示说物理卷创建成功
  Physical volume "/dev/sdb2" successfully created

查看物理卷。

[root@localhost ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "4.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1            //物理卷名称
  VG Name                                    //所属的卷组名称,这里还未加入任何卷组
  PV Size               4.00 GiB             //物理卷大小
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               pTps5V-4vKA-em0G-XR8Y-ltJu-4CeL-KEOMn3
  "/dev/sdb2" is a new physical volume of "4.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2            //物理卷名称
  VG Name                                    //所属的卷组名称
  PV Size               4.00 GiB             //物理卷大小
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               uzW0YS-fl5a-4Qae-6vcG-iXce-a8iB-QYMvf4

四、创建卷组

将刚刚新建的物理卷/dev/sdb1和/dev/sdb2组成一个卷组——vg1

[root@localhost ~]# vgcreate vg1 /dev/sdb[12]
 Volume group "vg1" successfully created          //提示说卷组创建成功卷

查看卷组

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1             //卷组名称
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               7.99 GiB        //卷组容量
  PE Size               4.00 MiB
  Total PE              2046
  Alloc PE / Size       0 / 0
  Free  PE / Size       2046 / 7.99 GiB
  VG UUID               dY8zDo-vD43-DQB8-CpRs-Shla-kWfJ-OuDjhp

五、划分逻辑卷

从总容量为8G(其实不足8G)的卷组vg1中划分四个逻辑卷,分别为lv1、lv2、lv3、lv4,每个大小为2000MB。

[root@localhost ~]# lvcreate -L 2000MB -n lv1 vg1       //-L参数后面接逻辑卷大小,单位可以是KB、MB、GB。
 Logical volume "lv1" created
[root@localhost ~]# lvcreate -L 2000MB -n lv2 vg1
 Logical volume "lv2" created
[root@localhost ~]# lvcreate -L 2000MB -n lv3 vg1
 Logical volume "lv3" created
[root@localhost ~]# lvcreate -L 2000MB -n lv4 vg1
 Logical volume "lv4" created

查看逻辑卷,因为这里的输出信息比较多,我只截取lv1这个逻辑卷的信息做简要说明。

[root@localhost ~]# lvdisplay
 --- Logical volume ---
 LV Path /dev/vg1/lv1          //逻辑卷路径
 LV Name lv1                   //逻辑卷名称
 VG Name vg1                   //逻辑卷所属的卷组的名称
 LV UUID qhypYJ-jPOW-KDDy-NXtO-aWQO-ryHI-Tf04hS
 LV Write Access read/write
 LV Creation host, time localhost.localdomain, 2017-10-11 13:13:54 +0800
 LV Status available
 # open 0
 LV Size 1.95 GiB              //逻辑卷容量
 Current LE 500
 Segments 1
 Allocation inherit
 Read ahead sectors auto
 - currently set to 256
 Block device 253:2

至此,逻辑卷已创建完成,但此时还不能立即使用,还需要在逻辑卷上新建文件系统。

这里为了不让文章显得啰嗦,我只在逻辑卷lv1上创建文件系统。

[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
128000 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=524288000
16 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912
正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

挂载逻辑卷。

[root@localhost ~]# mount /dev/vg1/lv1 /mnt
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  6.7G  816M  5.5G  13% /
tmpfs                         246M     0  246M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      4.2G  4.2G     0 100% /media/cdrom
/dev/mapper/vg1-lv1           2.0G   35M  1.8G   2% /mnt

管理LVM:

卷组的扩容

这里我新增加一块8G的空SCSI磁盘,并在其基础上创建一个8G的物理卷。

将刚刚新建的8G大小的物理卷加入到vg1卷组中,实现对vg1卷组的扩容。

[root@localhost ~]# vgextend vg1 /dev/sdc1
  dev_is_mpath: failed to get device for 8:33
  No physical volume label read from /dev/sdc1
  Physical volume /dev/sdc1 not found
  Physical volume "/dev/sdc1" successfully created
  Volume group "vg1" successfully extended

查看卷组信息,注意看卷组容量。

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  14
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               15.99 GiB          //卷组的总容量已增加
  PE Size               4.00 MiB
  Total PE              4093
  Alloc PE / Size       2000 / 7.81 GiB
  Free  PE / Size       2093 / 8.18 GiB
  VG UUID               dY8zDo-vD43-DQB8-CpRs-Shla-kWfJ-OuDjhp

逻辑卷的扩容

这里对已经挂载在/mnt目录上的lv1逻辑卷进行扩容,将其大小变为10GB。

[root@localhost ~]# lvextend -L 10GB -n /dev/vg1/lv1
 Extending logical volume lv1 to 10.00 GiB
 Logical volume lv1 successfully resized

重设逻辑卷大小,只有正在使用的逻辑卷才需要重设大小。

[root@localhost ~]# resize2fs /dev/vg1/lv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg1/lv1 to 2621440 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 2621440 blocks long.

查看此时lv1的容量,已实现动态扩容。

[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  6.7G  816M  5.5G  13% /
tmpfs                         246M     0  246M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      4.2G  4.2G     0 100% /media/cdrom
/dev/mapper/vg1-lv1           9.9G   37M  9.4G   1% /mnt

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Captcha Code