码迷,mamicode.com
首页 > 其他好文 > 详细

LVM管理器

时间:2018-06-10 11:52:47      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:linux   lvm   跨主机迁移主机   

LVM管理器

 

1LVM简介

       LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一种逻辑层,来提高磁盘分区管理的灵活性。

       LVM工作原理:是通过将底层的物理硬盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的磁盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的磁盘管理操作。比如说增加一个物理硬盘,这个时候上层的服务是感觉不到的,因为呈现给上层服务的是以逻辑卷的方式。

       LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。

2LVM基本术语

       前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的存储卷,在存储卷上建立文件系统。首先我们讨论以下几个LVM术语:

       物理存储介质(PhysicalStorageMedia指系统的物理存储设备:磁盘,如:/dev/hda/dev/sda等,是存储系统最底层的存储单元

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

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

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

       物理块(Physical ExtentPE每一个物理卷PV被划分为称为PE的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷由大小等同的基本单元PE组成。

       逻辑块(Logical ExtentLE逻辑卷LV也被划分为可被寻址的基本单位,称为LE。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。

       简单来说:

       PV:物理磁盘的分区

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

       LV:从VG中划分的逻辑分区

如下图示PVVGLV三者关系:

技术分享图片

3LVM创建和管理

       LVMLogical Volume ManagerVersion2

?   dm: device mapper:将一个或多个底层块设备组织成一个逻辑设备的模块

?   设备名/dev/dm-#

?   软链接:

              /dev/mapper/VG_NAME-LV_NAME

                            /dev/mapper/vol0-root

              /dev/VG_NAME/LV_NAME

                            /dev/vol0/root

3.1LVM更改文件系统的容量

       LVM可以弹性的更改LVM的容量

       通过交换PE进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量。

技术分享图片

3.2pv管理工具

       1)显示pv信息

              pvs简要pv信息显示

                     pvdisplay

       2)创建pv

              pvcreate /dev/Device

例子:

       使用磁盘分区创建物理卷(pv

[root@centos6 ~]#fdisk /dev/sdb -l             #查看磁盘分区ID类型

 

Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x4074ebc8

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         132     1060258+  8e  Linux LVM

/dev/sdb2             133         394     2104515   8e  Linux LVM  

#8e为磁盘分区类型为LVM管理器

       创建pv

[root@centos6 ~]#pvcreate /dev/sdb{1,2}               #创建pv

  Physical volume "/dev/sdb1" successfully created

  Physical volume "/dev/sdb2" successfully created

提示:物理卷创建成功

[root@centos6 ~]#pvs             #查看pv

  PV         VG   Fmt  Attr PSize PFree

  /dev/sdb1       lvm2 ---- 1.01g 1.01g

  /dev/sdb2       lvm2 ---- 2.01g 2.01g

[root@centos6 ~]#pvdisplay         #查看pv卷详细信息

  "/dev/sdb1" is a new physical volume of "1.01 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdb1            #设备名

  VG Name                                          #VG名(新创建pv未加入到VG组显示空名VG组)

  PV Size               1.01 GiB               #PV容量

  Allocatable           NO

  PE Size               0                         #PE大小(需指定PE大小)

  Total PE              0                             #PE数量

  Free PE               0                                   #PE使用空间

  Allocated PE          0

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  "/dev/sdb2" is a new physical volume of "2.01 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdb2

  VG Name              

  PV Size               2.01 GiB

  Allocatable           NO

  PE Size               0  

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

3.3vg管理工具

?1)显示卷组

              vgs

              vgdisplay

?2)创建卷组

              vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]

?3)管理卷组

              vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]      #扩展卷组

              vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]      #移除卷组

?4)删除卷组

              先做pvmove,再做vgremove

例子:

[root@centos6 ~]#vgcreate -s 16M vg0 /dev/sdb{1,2}         #创建vg卷组,-s 指定PE大小

  Volume group "vg0" successfully created             #提示创建卷组成功

[root@centos6 ~]#pvs             #

  PV         VG   Fmt  Attr PSize PFree

  /dev/sdb1  vg0  lvm2 a--u 1.00g 1.00g

  /dev/sdb2  vg0  lvm2 a--u 2.00g 2.00g

[root@centos6 ~]#pvdisplay         #查看pv详细信息

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0            #将pv加入vg组

  PV Size               1.01 GiB / not usable 11.41 MiB     #PV大小(其中not usable表示不够PE分配,PE是以16M来分配PV大小)

  Allocatable           yes

  PE Size               16.00 MiB      #PE大小(手动指定)

  Total PE              64             #PE数量(PE数量*PE大小得出设备大小)

  Free PE               64             #可用PE数量

  Allocated PE          0              #PE未被分配给逻辑卷

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               vg0

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              128

  Free PE               128

  Allocated PE          0

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

[root@centos6 ~]#vgs             #查看卷组

  VG   #PV #LV #SN Attr   VSize VFree

  vg0    2   0   0 wz--n- 3.00g 3.00g     #vg大小3G

[root@centos6 ~]#vgdisplay         #查看卷组详细信息

  --- Volume group ---

  VG Name               vg0

  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               3.00 GiB       #VG大小

  PE Size               16.00 MiB      #PE大小

  Total PE              192            #PE数量

  Alloc PE / Size       0 / 0  

  Free  PE / Size       192 / 3.00 GiB

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

[root@centos6 ~]#blkid

/dev/sda1: UUID="fcf2e544-0db0-4f27-9bdd-380bddaaee4d" TYPE="ext4"

/dev/sda2: UUID="d7c928d9-40b6-420e-99de-74877dbdccf5" TYPE="ext4"

/dev/sda3: UUID="d9193730-2fc6-44d0-93cb-a8b7a120a5bf" TYPE="ext4"

/dev/sda5: UUID="858ff62c-536c-4e5a-b7aa-bc949fcf20d2" TYPE="swap"

/dev/sdb1: UUID="EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k" TYPE="LVM2_member"       #卷组成员

/dev/sdb2: UUID="pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4" TYPE="LVM2_member" #卷组成员

3.4lv管理工具

?1显示逻辑卷

              lvs

              Lvdisplay

?2创建逻辑卷

              lvcreate -L #[mMgGtT] -n NAME VolumeGroup

              lvcreate -l 60%VG -n mylv testvg

              lvcreate -l 100%FREE -n yourlv testvg

       选项:

              -L VG大小分配            例子:-L +5G

              -l  PE大小分配            例子:-l +100

              -l 100%FREE100%FREEvg剩余空间创建逻辑卷

              +5G表示在原来空间增加5G空间

                     例:原1G空间增加5G空间,则空间为6G空间

              5G表示在原有空间增加到5G空间,并不是在原有空间加5G空间。

                     例:原1G空间增加到5G,则空间为5G空间

?3删除逻辑卷

              lvremove /dev/VG_NAME/LV_NAME

?4重设文件系统大小

              fsadm [options] resize device [new_size[BKMGTEP]]

              resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]

例子:1、创建逻辑卷

[root@centos6 ~]#lvcreate -n lv0 -l 100 vg0            #创建逻辑卷

  Logical volume "lv0" created.

 

[root@centos6 ~]#lvs                     #查看逻辑卷

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv0  vg0  -wi-a----- 1.56g       #lv0大小1.56G

[root@centos6 ~]#lvdisplay         #查看逻辑卷详细信息

  --- Logical volume ---

  LV Path                /dev/vg0/lv0     #lv路径不是真正的,而是软链接名真正是dm-0

  LV Name                lv0

  VG Name                vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              available

  # open                 0

  LV Size                1.56 GiB

  Current LE             100

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:0

[root@centos6 ~]#ll /dev/vg0/lv0       #查看lv路径

lrwxrwxrwx. 1 root root 7 Jun  8 19:04 /dev/vg0/lv0 -> ../dm-0

       创建另一个逻辑卷,先查看卷组空间情况

[root@centos6 ~]#vgs     #查看卷组剩余空间

  VG   #PV #LV #SN Attr   VSize VFree

  vg0    2   1   0 wz--n- 3.00g 1.44g     #VFree表示剩余空间大小

[root@centos6 ~]#vgdisplay      #查看vg详细信息

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  2

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               0

  Max PV                0

  Cur PV                2

  Act PV                2

  VG Size               3.00 GiB

  PE Size               16.00 MiB

  Total PE              192

  Alloc PE / Size       100 / 1.56 GiB

  Free  PE / Size       92 / 1.44 GiB         #PE剩余大小,92个PE,容量为1.44G

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

[root@centos6 ~]#lvcreate -n lv1 -l 100%FREE vg0             #创建逻辑卷,将vg剩余空间创建逻辑卷

  Logical volume "lv1" created.

[root@centos6 ~]#lvs

 

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv0  vg0  -wi-a----- 1.56g                                                   

  lv1  vg0  -wi-a----- 1.44g                                                   

[root@centos6 ~]#vgdisplay

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  3

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               0

  Max PV                0

  Cur PV                2

  Act PV                2

  VG Size               3.00 GiB

  PE Size               16.00 MiB

  Total PE              192

  Alloc PE / Size       192 / 3.00 GiB

  Free  PE / Size       0 / 0          #PE空间为0,表示不能再创建逻辑卷

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

[root@centos6 ~]#ll /dev/mapper/vg0-lv1         #查看lv1真正名

lrwxrwxrwx. 1 root root 7 Jun  8 19:18 /dev/mapper/vg0-lv1 -> ../dm-1

              2对新创建逻辑卷创建文件系统

[root@centos6 ~]#blkid /dev/vg0/lv0         #查看逻辑卷是否有文件系统

[root@centos6 ~]#mkfs.ext4 /dev/vg0/lv0         #逻辑卷增加文件系统

[root@centos6 ~]#mkfs.ext4 /dev/vg0/lv1         #逻辑卷增加文件系统

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

94272 inodes, 376832 blocks

18841 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=385875968

12 block groups

32768 blocks per group, 32768 fragments per group

7856 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912

 

Writing inode tables: done                           

Creating journal (8192 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 28 mounts or

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

[root@centos6 ~]#blkid          #查看逻辑卷文件系统

/dev/sda1: UUID="fcf2e544-0db0-4f27-9bdd-380bddaaee4d" TYPE="ext4"

/dev/sda2: UUID="d7c928d9-40b6-420e-99de-74877dbdccf5" TYPE="ext4"

/dev/sda3: UUID="d9193730-2fc6-44d0-93cb-a8b7a120a5bf" TYPE="ext4"

/dev/sda5: UUID="858ff62c-536c-4e5a-b7aa-bc949fcf20d2" TYPE="swap"

/dev/sdb1: UUID="EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k" TYPE="LVM2_member"

/dev/sdb2: UUID="pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4" TYPE="LVM2_member"

/dev/mapper/vg0-lv0: UUID="f5874770-f912-49b0-abd5-0d7f08e727b1" TYPE="ext4"

/dev/mapper/vg0-lv1: UUID="ae47578c-1ad1-48f4-aff5-b277ed1b62ba" TYPE="ext4"

#新增逻辑卷成员添加文件系统

              3增加开机启动自动挂载逻辑卷

[root@centos6 ~]#vim /etc/fstab          #增加开机自动挂载逻辑卷

       。。。。

UUID=f5874770-f912-49b0-abd5-0d7f08e727b1 /mnt/lv0 ext4 defaults 0 0           #增加逻辑卷挂载信息

提示:此例只挂载一个逻辑卷,另外一个可以自行尝试挂载。

[root@centos6 ~]#mkdir /mnt/lv0               #创建挂载点

[root@centos6 ~]#mount -a                  #自动挂载

[root@centos6 ~]#df -h           #查看挂载情况

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  1.6G  2.4M  1.5G   1% /mnt/lv0

4、扩展和缩减逻辑卷

?1)扩展逻辑卷:

              lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME

              resize2fs /dev/VG_NAME/LV_NAME

              lvresize -r -l  +100%FREE  /dev/ VG_NAME/LV_NAME

                     -r    扩展同时同步文件系统

?2)缩减逻辑卷:

              umount /dev/VG_NAME/LV_NAME

              e2fsck -f /dev/VG_NAME/LV_NAME

              resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]

              lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME

              mount

1、例子:扩展空间案例

       模拟逻辑卷空间不够用,需要扩展,但是卷组也没有空间,需要增加物理磁盘扩展卷组给逻辑卷

       1)模拟逻辑卷空间不足

[root@centos6 ~]#dd if=/dev/zero of=/mnt/lv0/f1 bs=1M count=500        #创建500文件

500+0 records in

500+0 records out

524288000 bytes (524 MB) copied, 10.5683 s, 49.6 MB/s

[root@centos6 ~]#dd if=/dev/zero of=/mnt/lv0/f2 bs=1M count=500 conv=fdatasync

500+0 records in

500+0 records out

524288000 bytes (524 MB) copied, 0.736254 s, 712 MB/s

[root@centos6 ~]#df -h

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  1.6G 1003M  461M  69% /mnt/lv0                     #逻辑卷空间不足

       2VM虚拟机增加物理磁盘

       3)在vmware虚拟机,新加硬盘不用重启的生效方法

[root@centos6 ~]#echo '- - -' >/sys/class/scsi_host/host0/scan          #扫描添加磁盘(仅限vm使用)

[root@centos6 ~]#lsblk

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0  100G  0 disk

├─sdb1               8:17   0    1G  0 part

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

└─sdb2               8:18   0    2G  0 part

  ├─vg0-lv0 (dm-0) 253:0    0  1.6G  0 lvm  /mnt/lv0

  └─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

sda                  8:0    0  120G  0 disk

├─sda1               8:1    0  300M  0 part /boot

├─sda2               8:2    0   20G  0 part /

├─sda3               8:3    0   10G  0 part /app

├─sda4               8:4    0    1K  0 part

└─sda5               8:5    0    4G  0 part [SWAP]

sr0                 11:0    1  3.7G  0 rom 

sdc                  8:32   0   20G  0 disk

       4)将新增物理磁盘修改成物理卷

[root@centos6 ~]#pvcreate /dev/sdc                  #创建物理卷

  Physical volume "/dev/sdc" successfully created

       5)扩展卷组(将新增加物理卷扩展到卷组)

[root@centos6 ~]#pvs      #扩展卷组前查看物理卷情况                          

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1  vg0  lvm2 a--u  1.00g     0

  /dev/sdb2  vg0  lvm2 a--u  2.00g     0

  /dev/sdc        lvm2 ---- 20.00g 20.00g            #新增物理卷后面无VG名,表示没有扩展到vg卷组

[root@centos6 ~]#vgextend vg0 /dev/sdc          #将新增加物理卷扩展到卷组(不能创建卷组)

  Volume group "vg0" successfully extended

[root@centos6 ~]#pvs             #查看物理卷

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1  vg0  lvm2 a--u  1.00g     0

  /dev/sdb2  vg0  lvm2 a--u  2.00g     0

  /dev/sdc   vg0  lvm2 a--u 19.98g 19.98g     #扩展卷组成功

[root@centos6 ~]#vgdisplay         #查看卷组是否扩展成功

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  4

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               1

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               22.98 GiB

  PE Size               16.00 MiB

  Total PE              1471

  Alloc PE / Size       192 / 3.00 GiB

  Free  PE / Size       1279 / 19.98 GiB                   #Free PE空间扩展成功

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

       6)扩展逻辑卷空间

[root@centos6 ~]#lvdisplay

  --- Logical volume ---

  LV Path                /dev/vg0/lv0

  LV Name                lv0

  VG Name                vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              available

  # open                 1

  LV Size                1.56 GiB

  Current LE             100                #逻辑卷的扩展盘区,表示100LE大小

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:0

  

  --- Logical volume ---

  LV Path                /dev/vg0/lv1

  LV Name                lv1

  VG Name                vg0

  LV UUID                RM0Uss-17Di-yZnP-Rra6-pUvw-l3lT-EiHW5I

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:18:19 +0800

  LV Status              available

  # open                 0

  LV Size                1.44 GiB

  Current LE             92

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:1

[root@centos6 ~]#lvextend -L +5G /dev/vg0/lv0                   #逻辑卷增加5G大小

  Size of logical volume vg0/lv0 changed from 1.56 GiB (100 extents) to 6.56 GiB (420 extents).

  Logical volume lv0 successfully resized.

[root@centos6 ~]#lvs                     #查看逻辑卷是否增加5G空间

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv0  vg0  -wi-ao---- 6.56g       #lv0增加成功                                                 

  lv1  vg0  -wi-a----- 1.44g                                                    

[root@centos6 ~]#df -h           #查看逻辑卷的挂载点是否增加5G空间

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  1.6G 1003M  461M  69% /mnt/lv0         #逻辑卷挂载的目录没有增加5G

       7)同步扩展空间的文件系统(不同步逻辑卷挂载点不能生效扩展的空间)

[root@centos6 ~]#resize2fs /dev/vg0/lv0          #同步逻辑卷扩展空间到挂载点的文件系统上

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 1

Performing an on-line resize of /dev/vg0/lv0 to 1720320 (4k) blocks.

The filesystem on /dev/vg0/lv0 is now 1720320 blocks long.

 

[root@centos6 ~]#df -h           #查看逻辑卷挂载点扩展空间是否生效

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  6.5G 1004M  5.2G  17% /mnt/lv0         #扩展空间同步生效

[root@centos6 ~]#lvextend -r -l +100%FREE /dev/vg0/lv0         #扩展同时顺便同步文件系统

  Size of logical volume vg0/lv0 changed from 6.56 GiB (420 extents) to 21.55 GiB (1379 extents).

  Logical volume lv0 successfully resized.

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/mapper/vg0-lv0 is mounted on /mnt/lv0; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 2

Performing an on-line resize of /dev/mapper/vg0-lv0 to 5648384 (4k) blocks.

The filesystem on /dev/mapper/vg0-lv0 is now 5648384 blocks long.

 

[root@centos6 ~]#df -h

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0   22G 1005M   20G   5% /mnt/lv0

[root@centos6 ~]#vgdisplay

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  6

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               1

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               22.98 GiB

  PE Size               16.00 MiB

  Total PE              1471

  Alloc PE / Size       1471 / 22.98 GiB

  Free  PE / Size       0 / 0   #空间为0

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

2、例子:缩减逻辑卷空间

       1)先备份

       2)取消挂载

[root@centos6 ~]#umount /mnt/lv0/

       3)执行缩减文件系统

[root@centos6 ~]#resize2fs /dev/vg0/lv0 10G         #直接执行缩减文件系统提示先执行检查

resize2fs 1.41.12 (17-May-2010)

Please run 'e2fsck -f /dev/vg0/lv0' first.                    #提示先检查逻辑卷

[root@centos6 ~]#fsck -f /dev/vg0/lv0             #检查逻辑卷

fsck from util-linux-ng 2.17.2

e2fsck 1.41.12 (17-May-2010)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/mapper/vg0-lv0: 13/1364624 files (7.7% non-contiguous), 350946/5648384 blocks

[root@centos6 ~]#resize2fs /dev/vg0/lv0 10G         #再执行缩减文件系统

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/vg0/lv0 to 2621440 (4k) blocks.

The filesystem on /dev/vg0/lv0 is now 2621440 blocks long.

       4)缩减逻辑卷空间

[root@centos6 ~]#lvreduce -L 10G /dev/vg0/lv0                   #缩减逻辑卷空间

  WARNING: Reducing active logical volume to 10.00 GiB.

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce vg0/lv0? [y/n]:       #你真想缩减逻辑卷?输入y表示同意

  Size of logical volume vg0/lv0 changed from 21.55 GiB (1379 extents) to 10.00 GiB (640 extents).

  Logical volume lv0 successfully resized.

       5)重新挂载逻辑卷

[root@centos6 ~]#mount -a           #重新挂载逻辑卷

[root@centos6 ~]#df -h

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  9.9G 1004M  8.4G  11% /mnt/lv0   #逻辑卷空间和文件系统大小缩减10G

5、删除VGPV

       例子:假设某块硬盘亮黄灯,快无法使用了,需要将此硬盘中的数据迁移到其他硬盘中

[root@centos6 ~]#pvs

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1  vg0  lvm2 a--u  1.00g     0

  /dev/sdb2  vg0  lvm2 a--u  2.00g     0

  /dev/sdc   vg0  lvm2 a--u 19.98g 11.55g     #/dev/sdc硬盘假设将要无法使用的硬盘

       1)先检查坏硬盘中使用多少PE,再将这些PE迁移到同一个卷组中的其他成员中(物理卷),其他成员是否有足够空间来存放坏硬盘中的PE空间

[root@centos6 ~]#pvdisplay         #查看物理卷可用空间

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0

  PV Size               1.01 GiB / not usable 11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0       #sdb1物理卷没有可用空间

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               vg0

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0       #sdb2没有可用空间

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  

  --- Physical volume ---

  PV Name               /dev/sdc          #将要损坏的硬盘

  VG Name               vg0

  PV Size               20.00 GiB / not usable 16.00 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1279

  Free PE               739

  Allocated PE          540     #已使用540PE空间

  PV UUID               kxDllV-U4cE-zicK-GSx0-ELGm-0hLL-gV77ve

提示:显然/dev/sdb设备分区无法存放/dev/sdc中的PE空间,需要增加硬盘或/dev/sdb还可能再划分分区也可以。

[root@centos6 ~]#lsblk                 #查看sdb磁盘空间

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0  100G  0 disk      #sdb总大小100G,已用3G空间

├─sdb1               8:17   0    1G  0 part

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

└─sdb2               8:18   0    2G  0 part

  ├─vg0-lv0 (dm-0) 253:0    0   10G  0 lvm  /mnt/lv0

  └─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

sda                  8:0    0  120G  0 disk

├─sda1               8:1    0  300M  0 part /boot

├─sda2               8:2    0   20G  0 part /

├─sda3               8:3    0   10G  0 part /app

├─sda4               8:4    0    1K  0 part

└─sda5               8:5    0    4G  0 part [SWAP]

sr0                 11:0    1  3.7G  0 rom 

sdc                  8:32   0   20G  0 disk

└─vg0-lv0 (dm-0)   253:0    0   10G  0 lvm  /mnt/lv0

       增加磁盘分区来存放坏硬盘占用PE空间

[root@centos6 ~]#fdisk /dev/sdb         #划分分区

 

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): 3        #主分区号

First cylinder (395-13054, default 395):    #保持默认

Using default value 395

Last cylinder, +cylinders or +size{K,M,G} (395-13054, default 13054): +20G                     #分配20G大小

 

Command (m for help): p        #查看磁盘分区

 

Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x4074ebc8

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         132     1060258+  8e  Linux LVM

/dev/sdb2             133         394     2104515   8e  Linux LVM

/dev/sdb3             395        3006    20980890   83  Linux              #需要修改分区ID,默认ID83

 

Command (m for help): t         #修改分区ID

Partition number (1-4): 3        #修改分区号

Hex code (type L to list codes): 8e              #修改成8eID

Changed system type of partition 3 to 8e (Linux LVM)         #将分区3系统类型修改成8eLinux LVM

 

Command (m for help): p

 

Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x4074ebc8

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         132     1060258+  8e  Linux LVM

/dev/sdb2             133         394     2104515   8e  Linux LVM

/dev/sdb3             395        3006    20980890   8e  Linux LVM           #修改完成

 

Command (m for help): w                     #最后保存

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

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)            #提示内核同步磁盘分区表失败,您可以重启或使用partprobe来同步磁盘分区表

Syncing disks.

[root@centos6 ~]#lsblk                 #查看sdb3分区是否同步

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0  100G  0 disk

├─sdb1               8:17   0    1G  0 part

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

└─sdb2               8:18   0    2G  0 part

  ├─vg0-lv0 (dm-0) 253:0    0   10G  0 lvm  /mnt/lv0

  └─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

sda                  8:0    0  120G  0 disk

├─sda1               8:1    0  300M  0 part /boot

├─sda2               8:2    0   20G  0 part /

├─sda3               8:3    0   10G  0 part /app

├─sda4               8:4    0    1K  0 part

└─sda5               8:5    0    4G  0 part [SWAP]

sr0                 11:0    1  3.7G  0 rom 

sdc                  8:32   0   20G  0 disk

└─vg0-lv0 (dm-0)   253:0    0   10G  0 lvm  /mnt/lv0

[root@centos6 ~]#partx -a /dev/sdb            #同步磁盘分区表到内核中

BLKPG: Device or resource busy

error adding partition 1

BLKPG: Device or resource busy

error adding partition 2

BLKPG: Device or resource busy

error adding partition 3

[root@centos6 ~]#lsblk

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0  100G  0 disk

├─sdb1               8:17   0    1G  0 part

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

├─sdb2               8:18   0    2G  0 part

├─vg0-lv0 (dm-0) 253:0    0   10G  0 lvm  /mnt/lv0

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

└─sdb3               8:19   0   20G  0 part          #同步分区表成功

sda                  8:0    0  120G  0 disk

├─sda1               8:1    0  300M  0 part /boot

├─sda2               8:2    0   20G  0 part /

├─sda3               8:3    0   10G  0 part /app

├─sda4               8:4    0    1K  0 part

└─sda5               8:5    0    4G  0 part [SWAP]

sr0                 11:0    1  3.7G  0 rom 

sdc                  8:32   0   20G  0 disk

└─vg0-lv0 (dm-0)   253:0    0   10G  0 lvm  /mnt/lv0

       2)将新增分区加入物理卷中

[root@centos6 ~]#pvcreate /dev/sdb3                #/dev/sdb3加入物理卷

  Physical volume "/dev/sdb3" successfully created

[root@centos6 ~]#pvs             #查看是否加入物理卷

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1  vg0  lvm2 a--u  1.00g     0

  /dev/sdb2  vg0  lvm2 a--u  2.00g     0

  /dev/sdb3       lvm2 ---- 20.01g 20.01g           #加入物理卷

  /dev/sdc   vg0  lvm2 a--u 19.98g 11.55g

       3)将新增物理卷加入卷组中

[root@centos6 ~]#vgextend vg0 /dev/sdb3        #/dev/sdb3加入vg卷组中

  Volume group "vg0" successfully extended

[root@centos6 ~]#vgdisplay         #查看vg卷组空间大小

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        4

  Metadata Sequence No  8

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               1

  Max PV                0

  Cur PV                4

  Act PV                4

  VG Size               42.98 GiB

  PE Size               16.00 MiB

  Total PE              2751

  Alloc PE / Size       732 / 11.44 GiB

  Free  PE / Size       2019 / 31.55 GiB            #vgPE可用空间大小

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

[root@centos6 ~]#pvdisplay         #查看物理卷

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0

  PV Size               1.01 GiB / not usable 11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               vg0

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  

  --- Physical volume ---

  PV Name               /dev/sdc              #此时sdc硬盘PE空间可以迁移到同一卷组其他成员中

  VG Name               vg0

  PV Size               20.00 GiB / not usable 16.00 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1279

  Free PE               739

  Allocated PE          540

  PV UUID               kxDllV-U4cE-zicK-GSx0-ELGm-0hLL-gV77ve

  

  --- Physical volume ---

  PV Name               /dev/sdb3

  VG Name               vg0

  PV Size               20.01 GiB / not usable 9.15 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1280

  Free PE               1280

  Allocated PE          0

  PV UUID               CqnVOx-CZFO-Hf6w-VyVh-KZmZ-buFr-KSYzj2

       4)迁移坏硬盘已使用PE空间到同一卷组其他成员中

[root@centos6 ~]#pvmove /dev/sdc                   #迁移/dev/sdc已使用PE空间到同一卷组其他成员中

  /dev/sdc: Moved: 0.2%

  /dev/sdc: Moved: 100.0%

提示:pvmove自动检测坏硬盘已用PE空间迁移到哪个物理卷中,注意pvmove迁移的是PE空间不是数据,显然把PE空间移过去等于把数据迁移过去。

[root@centos6 ~]#pvdisplay         #迁移后查看/dev/sdcPE使用空间

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0

  PV Size               1.01 GiB / not usable 11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               vg0

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  

  --- Physical volume ---

  PV Name               /dev/sdc

  VG Name               vg0

  PV Size               20.00 GiB / not usable 16.00 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1279

  Free PE               1279

  Allocated PE          0                #PE使用空间为0,表示没有被逻辑卷占用

  PV UUID               kxDllV-U4cE-zicK-GSx0-ELGm-0hLL-gV77ve

  

  --- Physical volume ---

  PV Name               /dev/sdb3

  VG Name               vg0

  PV Size               20.01 GiB / not usable 9.15 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1280

  Free PE               740

  Allocated PE          540

  PV UUID               CqnVOx-CZFO-Hf6w-VyVh-KZmZ-buFr-KSYzj2

       5)删除VG中的/dev/sdc物理卷

[root@centos6 ~]#vgreduce vg0 /dev/sdc                 #删除vg0中的/dev/sdc物理卷

  Removed "/dev/sdc" from volume group "vg0"

[root@centos6 ~]#pvdisplay         #查看/dev/sdc是否被从vg0卷组中删除

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0

  PV Size               1.01 GiB / not usable 11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               vg0

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  

  --- Physical volume ---

  PV Name               /dev/sdb3

  VG Name               vg0

  PV Size               20.01 GiB / not usable 9.15 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1280

  Free PE               740

  Allocated PE          540

  PV UUID               CqnVOx-CZFO-Hf6w-VyVh-KZmZ-buFr-KSYzj2

  

  "/dev/sdc" is a new physical volume of "20.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdc

  VG Name                                   #已被vg0卷组删除,此时变成独立物理卷

  PV Size               20.00 GiB

  Allocatable           NO

  PE Size               0  

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               kxDllV-U4cE-zicK-GSx0-ELGm-0hLL-gV77ve

       6)从物理卷中删除/dev/sdc磁盘

[root@centos6 ~]#pvremove /dev/sdc         #删除物理卷中的/dev/sdc物理卷

  Labels on physical volume "/dev/sdc" successfully wiped

[root@centos6 ~]#pvs             #dev/sdc物理卷被删除,变成普通磁盘

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1  vg0  lvm2 a--u  1.00g     0

  /dev/sdb2  vg0  lvm2 a--u  2.00g     0

  /dev/sdb3  vg0  lvm2 a--u 20.00g 11.56g

[root@centos6 ~]#lsblk

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0  100G  0 disk

├─sdb1               8:17   0    1G  0 part

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

├─sdb2               8:18   0    2G  0 part

├─vg0-lv0 (dm-0) 253:0    0   10G  0 lvm  /mnt/lv0

└─vg0-lv1 (dm-1) 253:1    0  1.4G  0 lvm 

└─sdb3               8:19   0   20G  0 part

  └─vg0-lv0 (dm-0) 253:0    0   10G  0 lvm  /mnt/lv0

sda                  8:0    0  120G  0 disk

├─sda1               8:1    0  300M  0 part /boot

├─sda2               8:2    0   20G  0 part /

├─sda3               8:3    0   10G  0 part /app

├─sda4               8:4    0    1K  0 part

└─sda5               8:5    0    4G  0 part [SWAP]

sr0                 11:0    1  3.7G  0 rom 

sdc                  8:32   0   20G  0 disk        #sdc变成普通磁盘

       7)现在可以将此硬盘从服务器中拔出。

 

6、跨主机迁移卷组

迁移准备前操作:

       1)查看目标主机与源主机卷组及逻辑卷名称是否有重名(模拟此案例有重名)

[root@centos7 ~]#pvcreate /dev/sdb1         #目标主机创建物理卷

[root@centos7 ~]#vgcreate vg0 /dev/sdb1         #目标主机创建卷组

[root@centos7 ~]#lvcreate -n lv0 -l 100%FREE vg0             #目标主机创建逻辑卷

[root@centos7 ~]#mkfs.xfs /dev/vg0/lv0           #逻辑卷创建文件系统

[root@centos7 ~]#mkdir /mnt/lv0        #新建挂载点

[root@centos7 ~]#mount /dev/vg0/lv0 /mnt/lv0                     #挂载逻辑卷

[root@centos7 ~]#lsblk          #查看逻辑卷挂载情况

NAME                                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                                       8:0    0  120G  0 disk

├─sda1                                    8:1    0  300M  0 part /boot

├─sda2                                    8:2    0   20G  0 part /

├─sda3                                    8:3    0    4G  0 part [SWAP]

├─sda4                                    8:4    0    1K  0 part

└─sda5                                    8:5    0   10G  0 part /app

sdb                                       8:16   0  108G  0 disk

└─sdb1                                    8:17   0    2G  0 part

  └─vg0-lv0                             253:0    0    2G  0 lvm  /mnt/lv0

sr0                                      11:0    1  8.8G  0 rom

1、源主机上操作

       1)将centos6主机卷组迁移到centos7主机上,但是两个主机卷组及逻辑卷同名,需要先修改centos6主机卷组及逻辑卷名称

[root@centos6 ~]#vgrename vg0 centos6vg0                  #修改Centos6主机上卷组名称

  Volume group "vg0" successfully renamed to "centos6vg0"

[root@centos6 ~]#vgdisplay         #查看修改后卷组名

  --- Volume group ---

  VG Name               centos6vg0        #修改后卷组名

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  13

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               1

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               23.00 GiB

  PE Size               16.00 MiB

  Total PE              1472

  Alloc PE / Size       732 / 11.44 GiB

  Free  PE / Size       740 / 11.56 GiB

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

   修改逻辑卷名称

[root@centos6 ~]#lvrename /dev/centos6vg0/lv0 /dev/centos6vg0/centos6lv0              #修改逻辑卷名称

  Renamed "lv0" to "centos6lv0" in volume group "centos6vg0"

[root@centos6 ~]#lvrename /dev/centos6vg0/lv1 /dev/centos6vg0/centos6lv1              #修改逻辑卷名称

  Renamed "lv1" to "centos6lv1" in volume group "centos6vg0"

[root@centos6 ~]#lvdisplay         #查看修改后的逻辑卷名称

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv0

  LV Name                centos6lv0        #修改后逻辑卷名称

  VG Name                centos6vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              available

  # open                 1

  LV Size                10.00 GiB

  Current LE             640

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:0

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv1

  LV Name                centos6lv1        #修改后的逻辑卷名称

  VG Name                centos6vg0

  LV UUID                RM0Uss-17Di-yZnP-Rra6-pUvw-l3lT-EiHW5I

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:18:19 +0800

  LV Status              available

  # open                 0

  LV Size                1.44 GiB

  Current LE             92

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:1

       2)取消逻辑卷挂载

[root@centos6 ~]#df -h                  #查看逻辑卷挂载情况

Filesystem           Size  Used Avail Use% Mounted on

/dev/sda2             20G  4.4G   15G  24% /

tmpfs                491M   72K  491M   1% /dev/shm

/dev/sda3            9.8G   23M  9.2G   1% /app

/dev/sda1            283M   35M  233M  14% /boot

/dev/mapper/vg0-lv0  9.9G 1004M  8.4G  11% /mnt/lv0

[root@centos6 ~]#umount /mnt/lv0            #取消逻辑卷挂载

       3)停用卷组

[root@centos6 ~]#vgchange -an centos6vg0            #禁用卷组

  0 logical volume(s) in volume group "centos6vg0" now active

[root@centos6 ~]#lvdisplay         #查看逻辑卷状态

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv0

  LV Name                centos6lv0

  VG Name                centos6vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              NOT available            #lv状态无法使用

  LV Size                10.00 GiB

  Current LE             640

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv1

  LV Name                centos6lv1

  VG Name                centos6vg0

  LV UUID                RM0Uss-17Di-yZnP-Rra6-pUvw-l3lT-EiHW5I

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:18:19 +0800

  LV Status              NOT available            #lv状态无法使用

  LV Size                1.44 GiB

  Current LE             92

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

       4)导出卷组

[root@centos6 ~]#vgexport centos6vg0             #导出卷组

  Volume group "centos6vg0" successfully exported

[root@centos6 ~]#vgdisplay         #查看卷组状态

  --- Volume group ---

  VG Name               centos6vg0

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  16

  VG Access             read/write

  VG Status             exported/resizable      #卷组状态为导出状态

  MAX LV                0

  Cur LV                2

  Open LV               0

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               23.00 GiB

  PE Size               16.00 MiB

  Total PE              1472

  Alloc PE / Size       732 / 11.44 GiB

  Free  PE / Size       740 / 11.56 GiB

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

[root@centos6 ~]#pvdisplay                #查看物理卷中卷组状态

  Physical volume "/dev/sdb1" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               centos6vg0 (exported)           #卷组显示导出状态

  PV Size               1.01 GiB / not usable 11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  Physical volume "/dev/sdb2" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdb2

  VG Name               centos6vg0 (exported)           #卷组显示导出状态

  PV Size               2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  Physical volume "/dev/sdb3" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdb3

  VG Name               centos6vg0 (exported)           #卷组显示导出状态

  PV Size               20.01 GiB / not usable 9.15 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1280

  Free PE               740

  Allocated PE          540

  PV UUID               CqnVOx-CZFO-Hf6w-VyVh-KZmZ-buFr-KSYzj2

       5Centos6关机拔出sdb硬盘(该环境在VM上做实验,所以关机拔硬盘;真正服务器支持热挺拔硬盘)

在目标主机上操作

       6)查找出Centos6中卷组所在硬盘,拔出硬盘安装到Centos7主机上


 技术分享图片  技术分享图片

       7)在VM虚拟Centos7上添加Centos6主机上的硬盘(添加过程省略)

技术分享图片

       8)查看Centos7是否添加硬盘成功

[root@centos7 ~]#lsblk          #添加硬盘没有同步

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda           8:0    0  120G  0 disk

├─sda1        8:1    0  300M  0 part /boot

├─sda2        8:2    0   20G  0 part /

├─sda3        8:3    0    4G  0 part [SWAP]

├─sda4        8:4    0    1K  0 part

└─sda5        8:5    0   10G  0 part /app

sdb           8:16   0  108G  0 disk

└─sdb1        8:17   0    2G  0 part

  └─vg0-lv0 253:0    0    2G  0 lvm  /mnt/lv0

sr0          11:0    1  8.8G  0 rom 

[root@centos7 ~]#echo '- - -' >/sys/class/scsi_host/host2/scan           #同步分区表(此命令仅限VM使用)

[root@centos7 ~]#lsblk                 #查看硬盘是否添加成功

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda           8:0    0  120G  0 disk

├─sda1        8:1    0  300M  0 part /boot

├─sda2        8:2    0   20G  0 part /

├─sda3        8:3    0    4G  0 part [SWAP]

├─sda4        8:4    0    1K  0 part

└─sda5        8:5    0   10G  0 part /app

sdb           8:16   0  108G  0 disk

└─sdb1        8:17   0    2G  0 part

  └─vg0-lv0 253:0    0    2G  0 lvm  /mnt/lv0

sdc           8:32   0  100G  0 disk                     #centos6硬盘添加成功

├─sdc1        8:33   0    1G  0 part

├─sdc2        8:34   0    2G  0 part

└─sdc3        8:35   0   20G  0 part

sr0          11:0    1  8.8G  0 rom

       9)还原卷组导入状态

[root@centos7 ~]#pvdisplay         #查看新添加硬盘中物理卷状态

  Physical volume "/dev/sdc1" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdc1

  VG Name               centos6vg0 (exported)           #VG状态还是导出状态

  PV Size               1.01 GiB / not usable <11.41 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              64

  Free PE               0

  Allocated PE          64

  PV UUID               EPcGil-E7Ng-08fP-FS1u-tzqR-MMtc-1kxX0k

  

  Physical volume "/dev/sdc2" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdc2

  VG Name               centos6vg0 (exported)           #VG状态还是导出状态

  PV Size               <2.01 GiB / not usable 7.19 MiB

  Allocatable           yes (but full)

  PE Size               16.00 MiB

  Total PE              128

  Free PE               0

  Allocated PE          128

  PV UUID               pqURc6-F7d9-nKmp-3KGi-rzdE-S9yD-Azm6s4

  

  Physical volume "/dev/sdc3" of volume group "centos6vg0" is exported

  --- Physical volume ---

  PV Name               /dev/sdc3

  VG Name               centos6vg0 (exported)           #VG状态还是导出状态

  PV Size               <20.01 GiB / not usable 9.15 MiB

  Allocatable           yes

  PE Size               16.00 MiB

  Total PE              1280

  Free PE               740

  Allocated PE          540

  PV UUID               CqnVOx-CZFO-Hf6w-VyVh-KZmZ-buFr-KSYzj2

  

  --- Physical volume ---

  PV Name               /dev/sdb1

  VG Name               vg0

  PV Size               2.00 GiB / not usable 4.00 MiB

  Allocatable           yes (but full)

  PE Size               4.00 MiB

  Total PE              511

  Free PE               0

  Allocated PE          511

  PV UUID               csidmP-Bwzd-1xnY-YO0d-hnCw-TocR-Fu5PLp

[root@centos7 ~]#vgimport centos6vg0                   #还原centos6卷组为导入状态

  Volume group "centos6vg0" successfully imported

[root@centos7 ~]#vgdisplay         #查看新添加硬盘中的卷组状态

  --- Volume group ---

  VG Name               centos6vg0

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  17

  VG Access             read/write

  VG Status             resizable         #VG卷组状态已还原

  MAX LV                0

  Cur LV                2

  Open LV               0

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               23.00 GiB

  PE Size               16.00 MiB

  Total PE              1472

  Alloc PE / Size       732 / <11.44 GiB

  Free  PE / Size       740 / 11.56 GiB

  VG UUID               dpILvC-q23x-WCCH-Z1Or-SEH6-xdwS-Qope7b

  

  --- Volume group ---

  VG Name               vg0

  System ID            

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  2

  VG Access             read/write

  VG Status             resizable         #VG卷组状态已还原

  MAX LV                0

  Cur LV                1

  Open LV               1

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               <2.00 GiB

  PE Size               4.00 MiB

  Total PE              511

  Alloc PE / Size       511 / <2.00 GiB

  Free  PE / Size       0 / 0  

  VG UUID               395Vaf-BYr2-8yoc-UudL-N8Qk-vely-qmH40D

[root@centos7 ~]#lvdisplay         #查看添加硬盘中的逻辑卷状态

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv0

  LV Name                centos6lv0

  VG Name                centos6vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              NOT available            #lv状态为无法使用

  LV Size                10.00 GiB

  Current LE             640

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv1

  LV Name                centos6lv1

  VG Name                centos6vg0

  LV UUID                RM0Uss-17Di-yZnP-Rra6-pUvw-l3lT-EiHW5I

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:18:19 +0800

  LV Status              NOT available            #lv状态为无法使用

  LV Size                <1.44 GiB

  Current LE             92

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  

  --- Logical volume ---

  LV Path                /dev/vg0/lv0

  LV Name                lv0

  VG Name                vg0

  LV UUID                Mdt9CZ-kyd6-vjs5-davl-bbtC-TIiR-uDP6tg

  LV Write Access        read/write

  LV Creation host, time centos7.magedu.com, 2018-06-09 14:43:10 +0800

  LV Status              available

  # open                 1

  LV Size                <2.00 GiB

  Current LE             511

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:0

       10)激活centos6vg0卷组,且逻辑卷也被激活

[root@centos7 ~]#vgchange -ay centos6vg0                   #激活卷组

  2 logical volume(s) in volume group "centos6vg0" now active

[root@centos7 ~]#lvdisplay         #查看逻辑卷状态

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv0

  LV Name                centos6lv0

  VG Name                centos6vg0

  LV UUID                YTBvNy-3sg3-BBao-HOsf-d1uQ-oJVX-h6VKNb

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:04:15 +0800

  LV Status              available                     #状态为可用

  # open                 0

  LV Size                10.00 GiB

  Current LE             640

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:1

  

  --- Logical volume ---

  LV Path                /dev/centos6vg0/centos6lv1

  LV Name                centos6lv1

  VG Name                centos6vg0

  LV UUID                RM0Uss-17Di-yZnP-Rra6-pUvw-l3lT-EiHW5I

  LV Write Access        read/write

  LV Creation host, time centos6.magedu.com, 2018-06-08 19:18:19 +0800

  LV Status              available              #状态为可用

  # open                 0

  LV Size                <1.44 GiB

  Current LE             92

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:2

  

  --- Logical volume ---

  LV Path                /dev/vg0/lv0

  LV Name                lv0

  VG Name                vg0

  LV UUID                Mdt9CZ-kyd6-vjs5-davl-bbtC-TIiR-uDP6tg

  LV Write Access        read/write

  LV Creation host, time centos7.magedu.com, 2018-06-09 14:43:10 +0800

  LV Status              available

  # open                 1

  LV Size                <2.00 GiB

  Current LE             511

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:0      

       11)挂载逻辑卷

[root@centos7 ~]#mkdir /mnt/centos6lv0          #创建挂载点

[root@centos7 ~]#mount /dev/centos6vg0/centos6lv0 /mnt/centos6lv0/          #挂载逻辑卷到挂载点上

[root@centos7 ~]#ls /mnt/centos6lv0/        #查看挂载点内容

f1  f2  lost+found

 

 


LVM管理器

标签:linux   lvm   跨主机迁移主机   

原文地址:http://blog.51cto.com/2067926/2126821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!