LVM卷管理

简介:

逻辑卷的结构图


wKiom1cusrHjFfEvAABMXt576-E849.png



物理硬盘:存储文件设备(/dev/sdb

PV(物理卷):硬盘的分区或者卷

VG(卷组):由一个或者多个物理卷组成的LVM卷。

LV(逻辑卷):VG上的分区,可以在LV上创建文件系统。

PE:PE上的最小存储单元,默认为4MB



1.下面我们以实际的创建过程做演示:

创建两个主分区,一个为4G,一个为6G
[root@192 ~]# fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p#指定主分区

Partition number (1-4): 1#指定分区号码
First cylinder (1-7832, default 1): 
Using default value 1#指定分区起始柱面
Last cylinder, +cylinders or +size{K,M,G} (1-7832, default 7832): +4G#指定分区大小





Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (524-7832, default 524): 
Using default value 524
Last cylinder, +cylinders or +size{K,M,G} (524-7832, default 7832): +6G


Command (m for help): p


Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0xb089f8a1


Device Boot      Start         End      Blocks   Id  System#查看已创建的分区

/dev/sdb1               1         523     4200966   83  Linux
/dev/sdb2             524        1307     6297480   83  Linux


Command (m for help): L#查看逻辑卷需指定的设备类型


 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1


Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e#调整逻辑卷的设备类型
Changed system type of partition 1 to 8e (Linux LVM)


Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e#调整逻辑卷的设备类型
Changed system type of partition 2 to 8e (Linux LVM)


Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.


2.查看内核识别的分区

[root@192 ~]# cat /proc/partitions
major minor  #blocks  name


   8        0   20971520 sda
   8        1     512000 sda1
   8        2   15360000 sda2
   8        3    1024000 sda3
   8        4          1 sda4
   8        5    4073472 sda5
   8       16   62914560 sdb
   8       17    4200966 sdb1#刚分的两个区
   8       18    6297480 sdb2#刚分的两个区


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

  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created


4.kpartx -af /dev/sdb


5.[root@192 ~]# pvs#显示创建的pv

  PV               VG   Fmt  Attr PSize PFree
  /dev/mapper/sdb1      lvm2 ---  4.01g 4.01g
  /dev/mapper/sdb2      lvm2 ---  6.01g 6.01g


6.[root@192 ~]# pvdisplay#查看逻辑卷详细信息

  "/dev/mapper/sdb1" is a new physical volume of "4.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/mapper/sdb1
  VG Name               
  PV Size               4.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4d2BDI-gJzg-1ZKa-0zcV-7eSg-Bytv-D3LT7y
   
  "/dev/mapper/sdb2" is a new physical volume of "6.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/mapper/sdb2
  VG Name               
  PV Size               6.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               oDcOG6-I7sd-IVHk-gNqi-8CdF-QurW-SPd8x1


7.vgcreate myvg /dev/sdb{1,2}#创建vg并把组名命名为myvg 

Volume group "myvg" successfully created


8.[root@192 ~]# vgs#查看已创建的卷组

  VG   #PV #LV #SN Attr   VSize  VFree 
  myvg   2   0   0 wz--n- 10.01g 10.01g


9.[root@192 ~]# fdisk /dev/sdc#新加一块磁盘sdc

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p  
Partition number (1-4): 1
First cylinder (1-1305, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 
Using default value 1305


Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)


Command (m for help): p


Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x66ea7a6a


   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1305    10482381   8e  Linux LVM


Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.


10.[root@192 ~]# cat /proc/partitions#查看内核是否识别

major minor  #blocks  name


   8        0   20971520 sda
   8        1     512000 sda1
   8        2   15360000 sda2
   8        3    1024000 sda3
   8        4          1 sda4
   8        5    4073472 sda5
   8       16   62914560 sdb
   8       17    4200966 sdb1
   8       18    6297480 sdb2
   8       19    4200997 sdb3
   8       32   10485760 sdc
   8       33   10482381 sdc1


11.[root@192 ~]# pvcreate /dev/sdc1#先创建物理卷pv

  Physical volume "/dev/sdc1" successfully created

[root@192 ~]# pvs#查看已有的pv

  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1  myvg lvm2 a--  4.00g 4.00g
  /dev/sdb2  myvg lvm2 a--  6.00g 6.00g
 /dev/sdc1  myvg lvm2 a--  9.99g 9.99g
[root@192 ~]# pvdisplay#查看已有的pv的详细信息

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               myvg
  PV Size               4.01 GiB / not usable 6.51 MiB
  Allocatable           yes 
  PE Size               8.00 MiB#逻辑卷最基本的块
  Total PE              512
  Free PE               512
  Allocated PE          0
  PV UUID               4d2BDI-gJzg-1ZKa-0zcV-7eSg-Bytv-D3LT7y
   
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               myvg
  PV Size               6.01 GiB / not usable 5.88 MiB
  Allocatable           yes 
  PE Size               8.00 MiB
  Total PE              768
  Free PE               768
  Allocated PE          0
  PV UUID               oDcOG6-I7sd-IVHk-gNqi-8CdF-QurW-SPd8x1
   
  --- Physical volume ---#下面显示的是新加的pv

  PV Name               /dev/sdc1
  VG Name               myvg
  PV Size               10.00 GiB / not usable 4.70 MiB
  Allocatable           yes 
  PE Size               8.00 MiB
  Total PE              1279
  Free PE               1279
  Allocated PE          0
  PV UUID               6iS5Sn-lvUu-F3pC-3WGZ-ao6l-kWuy-Tfwt0X


12.[root@192 ~]# vgextend myvg /dev/sdc1#把新加的pv(/dev/sdc1)加入卷组myvg

  Volume group "myvg" successfully extended
[root@192 ~]# vgs#查看vg信息

  VG   #PV #LV #SN Attr   VSize  VFree 
  myvg   3   0   0 wz--n- 19.99g 19.99g
[root@192 ~]# vgdisplay#查看vg详细信息

  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  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               19.99 GiB
  PE Size               8.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2559 / 19.99 GiB
  VG UUID               3cZU0o-LILk-BdGf-e0gm-JjDE-TI33-1VWFlh


13.[root@192 ~]# lvcreate -L 3G -n lv1 myvg#从卷组myvg中创建一个逻辑卷命名为lv1,大小为3G  Logical volume "lv1" created
[root@192 ~]# lvs#查看已创建的逻辑卷

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  myvg -wi-a----- 3.00g                                                    
[root@192 ~]# lvdisplay#查看已创建的逻辑卷的详细信息

  --- Logical volume ---
  LV Path                /dev/myvg/lv1
  LV Name                lv1
  VG Name                myvg
  LV UUID                hEC4C3-twue-FQ7K-7y1t-oeG2-N0xx-2eFXLS
  LV Write Access        read/write
  LV Creation host, time 192.168.10.123, 2016-05-06 14:30:35 +0800
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             384
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


14.[root@192 ~]# mke2fs -t ext4 -b 2048 /dev/myvg/lv1#对新创建的逻辑卷lv1进行格式化,把该逻辑卷格式化为ext4的类型,指定块大小为2048

mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=2048 (log=1)
分块大小=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
196608 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=538968064
96 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104


正在写入inode表: 完成                            
Creating journal (32768 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.


15.[root@192 /]# mount /dev/myvg/lv1 /u01#挂载逻辑卷/dev/myvg/lv1 到 /u01下面

[root@192 /]# df -h#查看已挂载的分区,看到逻辑卷已挂载到/u01下面

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              15G  4.0G  9.7G  29% /
tmpfs                 499M   72K  499M   1% /dev/shm
/dev/sda1             477M   28M  424M   7% /boot
/dev/sda5             3.8G  7.8M  3.6G   1% /opt
/dev/mapper/myvg-lv1  2.9G   11M  2.8G   1% /u01


16.假如逻辑卷空间不够用,那我们需要扩展逻辑卷

先查看卷组的剩余可用空间

[root@192 /]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree 
  myvg   3   1   0 wz--n- 19.99g 14.99g


17.[root@192 /]# lvextend -L 5G /dev/myvg/lv1 #直接把逻辑卷扩展到5G

  Size of logical volume myvg/lv1 changed from 3.00 GiB (384 extents) to 5.00 GiB (640 extents).
  Logical volume lv1 successfully resized


18.[root@192 /]# lvs#查看扩展后的逻辑卷的大小

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  myvg -wi-ao---- 5.00g


19.[root@192 /]# df -h#查看分区信息,发现我们只扩展了逻辑卷的物理边界,逻辑卷的逻辑边界并没有动,还是为原来创建的3G

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              15G  4.0G  9.7G  29% /
tmpfs                 499M   72K  499M   1% /dev/shm
/dev/sda1             477M   28M  424M   7% /boot
/dev/sda5             3.8G  7.8M  3.6G   1% /opt
/dev/mapper/myvg-lv1  2.9G   11M  2.8G   1% /u01


20.[root@192 /]# resize2fs /dev/myvg/lv1#使用resize2fs扩充逻辑卷的逻辑边界,使用在线扩充resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/lv1 is mounted on /u01; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/myvg/lv1 to 2621440 (2k) blocks.
The filesystem on /dev/myvg/lv1 is now 2621440 blocks long.


21.[root@192 /]# df -h#再查看分区信息,可以看到逻辑卷/dev/myvg/lv1已扩充到5G

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              15G  4.0G  9.7G  29% /
tmpfs                 499M   72K  499M   1% /dev/shm
/dev/sda1             477M   28M  424M   7% /boot
/dev/sda5             3.8G  7.8M  3.6G   1% /opt
/dev/mapper/myvg-lv1  4.9G   11M  4.6G   1% /u01



逻辑卷扩展步骤:


1.先确定扩展的大小:并确保所属的卷组有足够的剩余空间
2.扩展物理边界:
    Lvextend -L [+]Size /path/to/lv_device
3.扩展逻辑边界:
    resize2fs /path/to/lv_device


缩减逻辑卷步骤:


1.卸载卷,并执行强制检测
e2fsck   -f  /path/to/lv_device
2.缩减逻辑边界:
resize2fs /path/to/lv_device SIZE
3.缩减物理边界:
lvreduce -L [-]Size /path/to/lv_device



LVM一些常用的命令

物理卷管理命令:pv
     pvcreate,pvremove,pvscan,pvs,pvdisplay,pvmove
卷组管理命令:vg
     vgcreate,vgs,vgdisplay,vgextend,vgremove,vgrename
     vgcreate
            -s: PE大小,默认4M

逻辑卷的管理命令:lv
     lvcreate,lvdisplay,lvs,lvextend,lvreduce,lvremove

      lvcreate
            - L : 空间大小
            - n :  lv 名称


本文转自服务器运维博客51CTO博客,原文链接http://blog.51cto.com/shamereedwine/1771163如需转载请自行联系原作者

neijiade10000
相关文章
|
Linux 开发工具
LVM管理
简单实践
218 0
LVM管理
|
Oracle 关系型数据库 开发工具