Linux LVM的创建及扩展

LVM Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,使用lvm用户可以在无需停机的情况下方便地调整各个分区大小。

LVM使用分层结构,如下图所示

Linux LVM的创建及扩展

基本术语

物理卷(Physical VolumePV

指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

*卷组(Volume GroupVG

类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。

*逻辑卷(Logical VolumeLV

类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。

                               (注释:基本术语此段摘抄于百度百科)

简单来说就是:

PV:是物理的磁盘分区

VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。

LV:也就是从VG中划分的逻辑分区

如下图所示PVVGLV三者关系

Linux LVM的创建及扩展


本文将介绍怎么在linux中创建和管理LVM

[[email protected] ~]#fdisk -l

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x000eeb02

 

  Device Boot      Start         End      Blocks  Id  System

/dev/sda1  *           1          64      512000  83  Linux

Partition 1 does not end on cylinderboundary.

/dev/sda2              64        1339   10240000   83  Linux

/dev/sda3            1339        1594    2048000   82  Linux swap / Solaris

 

首先添三块硬盘,每块大小为2G

添加完成后,我们来查看一下:

[[email protected] ~]#fdisk -l

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x000eeb02

 

  Device Boot      Start         End      Blocks  Id  System

/dev/sda1  *           1          64      512000  83  Linux

Partition 1 does not end on cylinderboundary.

/dev/sda2              64        1339   10240000   83  Linux

/dev/sda3            1339        1594    2048000   82  Linux swap / Solaris

 

Disk /dev/sdd: 2147MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

 

 

Disk /dev/sdc: 2147MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

 

 

Disk /dev/sdb: 2147MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

从上边的显示结果可以看出,我们已经添加成功了。

 

创建物理卷PV

 

[[email protected]~]# pvcreate /dev/sdb

  Physical volume "/dev/sdb"successfully created

[[email protected]~]# pvcreate /dev/sdc

  Physical volume "/dev/sdc"successfully created

[[email protected]~]# pvcreate /dev/sdd

  Physical volume "/dev/sdd"successfully created

 

使用pvdisplay命令检查物理卷的创建情况

[[email protected]~]# pvdisplay

  "/dev/sdb" is a new physical volumeof "2.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdb

  VG Name              

  PV Size               2.00 GiB

  Allocatable           NO

  PE Size               0  

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID              MAvlCx-FFkq-KnGw-THgz-YZ27-wB31-LD2frW

  

  "/dev/sdc" is a new physical volumeof "2.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdc

  VG Name               

  PV Size               2.00 GiB

  Allocatable           NO

  PE Size               0  

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID              MX1ioY-HSYM-LAmC-fZea-Zccc-qEXG-nkQMUf

  

  "/dev/sdd" is a new physical volumeof "2.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdd

  VG Name              

  PV Size               2.00 GiB

  Allocatable           NO

  PE Size               0  

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID              TQ7aAp-WwIz-sD51-3UoY-lkqM-8v1L-nWYScF

 

创建卷组VG

 

接下来我们开始创建卷组VG名为'volume-group1',使用/dev/sdb, /dev/sdc /dev/sdd3创建。

  

[[email protected]~]# vgcreate Volume-group1 /dev/sdb /dev/sdc /dev/sdd

  Volume group "Volume-group1"successfully created

 

使用命令vgdisplay验证卷组

 

[[email protected]~]# vgdisplay

  --- Volume group ---

  VGName               Volume-group1

  System ID            

  Format               lvm2

  Metadata Areas        3

  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                3

  Act PV                3

  VG Size               5.99 GiB

  PE Size               4.00 MiB

  Total PE              1533

  Alloc PE / Size       0 / 0  

  Free PE / Size       1533 / 5.99 GiB

  VG UUID               9sCwZQ-uFpc-EcSA-c1Jw-BZ75-RRtg-EWaDam

 

从输出中,我们可以看见卷组的使用量/总量。物理卷给卷组提供空间。只要在这个卷组中还有可用空间,我们就可以随意创建逻辑卷。

 

创建逻辑卷LV

创建一个名为' lv1'、大小为100MB的逻辑卷

 

[[email protected] ~]# lvcreate -L 500M -n lv1 Volume-group1

  Logicalvolume "lv1" created

[[email protected] ~]# lvdisplay

  ---Logical volume ---

  LVPath                /dev/Volume-group1/lv1

  LVName                lv1

  VGName                Volume-group1

  LVUUID               2VZ8nz-oSkQ-dBYS-6Okj-OxsR-ctZo-6Yvr8n

  LV WriteAccess        read/write

  LVCreation host, time localhost.localdomain, 2016-01-21 21:59:10 +0800

  LV Status              available

  #open                 0

  LVSize                500.00 MiB

  CurrentLE             125

 Segments               1

 Allocation             inherit

  Readahead sectors     auto

  -currently set to     256

  Blockdevice           253:0

现在逻辑卷已经准备好了,我们可以格式化和挂载逻辑卷,就像其它ext2/3/4分区一样.

[[email protected] ~]# mkfs.ext4 /dev/Volume-group1/lv1

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=1024 (log=0)

分块大小=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

128016 inodes, 512000 blocks

25600 blocks (5.00%) reserved for the super user

第一个数据块=1

Maximum filesystem blocks=67633152

63 block groups

8192 blocks per group, 8192 fragments per group

2032 inodes per group

Superblock backups stored on blocks:

        8193,24577, 40961, 57345, 73729, 204801, 221185, 401409

 

正在写入inode: 完成                           

Creating journal (8192 blocks): 完成

Writing superblocks and filesystem accountinginformation: 完成

 

This filesystem will be automatically checkedevery 29 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[[email protected] ~]# mkdir /lvm-mount

[[email protected] ~]# mount /dev/Volume-group1/lv1 /lvm-mount/

一旦逻辑卷挂载,我们就可以到挂载点 /lvm-mount/ 上读写了

[[email protected] ~]# cd /lvm-mount/

[[email protected] lvm-mount]# ll

总用量 12

drwx------. 2 root root 12288 1  21 22:05lost+found

[[email protected] lvm-mount]# touch cheshi.txt

[[email protected] lvm-mount]# mkdir ceshi

[[email protected] lvm-mount]# ll

总用量 15

drwxr-xr-x. 2 root root  1024 1  21 22:08 ceshi

-rw-r--r--. 1 root root     0 1  21 22:08 cheshi.txt

drwx------. 2 root root 12288 1  21 22:05lost+found

 

扩展一个lvm

 

调整逻辑卷大小的功能是LVM最有用的功能。这个部分会讨论我们怎么样扩展一个存在的逻辑卷。下面,我们将会扩展先前创建的逻辑卷‘lv1’扩大到800MB

[[email protected] ~]# lvextend -L +300M /dev/Volume-group1/lv1

  Extending logical volume lv1 to 800.00 MiB

  Logical volume lv1 successfully resized

[[email protected] ~]# lvdisplay

  --- Logical volume ---

  LV Path                /dev/Volume-group1/lv1

  LV Name                lv1

  VG Name                Volume-group1

  LV UUID               2VZ8nz-oSkQ-dBYS-6Okj-OxsR-ctZo-6Yvr8n

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain,2016-01-21 21:59:10 +0800

  LV Status              available

  # open                 1

  LV Size                800.00 MiB

  Current LE             200

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:0