LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。

结构

最下面的一层是3 Physical Volume PV(物理卷) 

上一层 2 Volume Group VG  (卷组)  逻辑存储小匣子:PE:Physical Extend,盘区

最上面一层 1 Logical Volume (逻辑卷)  LV

命令介绍

PV

pvcreate:创建模式

pvs :查看pv信息

pvdisplay:查看pv的详细信息

pvscan :扫描pv信息

pvremove :删除

pvmove :移动  

VG

vgcreate VG_NAME /PATH/TO/PV

     -S #:PE大小,默认为4MB

vgextend 拓展

vgreduce 减少

vgremove 删除

vgdisplay 

vgs

LV

lvcreate 

-n LV_NEME -L #G(大小)

lvremove

lvextend

lvreduce

lvs

lvdisplay

新建7G ,3G,5G逻辑卷到时候实验用   

[root@localhost ~]# fdisk /dev/sda


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): p


Disk /dev/sda: 48.3 GB, 48318382080 bytes

64 heads, 32 sectors/track, 46080 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

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

Disk identifier: 0x0005361c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           2         201      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2             202       10201    10240000   83  Linux

Partition 2 does not end on cylinder boundary.

/dev/sda3           10202       12201     2048000   82  Linux swap / Solaris

Partition 3 does not end on cylinder boundary.

/dev/sda4           12202       46080    34692096    5  Extended

Partition 4 does not end on cylinder boundary.

/dev/sda5           12204       14203     2048000    6  FAT16

/dev/sda6           14204       15228     1049584   fd  Linux raid autodetect

/dev/sda7           15229       16253     1049584   fd  Linux raid autodetect

/dev/sda8           16254       17278     1049584   fd  Linux raid autodetect

/dev/sda9           17279       18303     1049584   fd  Linux raid autodetect

/dev/sda10          18304       19328     1049584   fd  Linux raid autodetect


Command (m for help): n

First cylinder (12202-46080, default 12202): 19329

Last cylinder, +cylinders or +size{K,M,G} (19329-46080, default 46080): +7G


Command (m for help): p


Disk /dev/sda: 48.3 GB, 48318382080 bytes

64 heads, 32 sectors/track, 46080 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

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

Disk identifier: 0x0005361c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           2         201      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2             202       10201    10240000   83  Linux

Partition 2 does not end on cylinder boundary.

/dev/sda3           10202       12201     2048000   82  Linux swap / Solaris

Partition 3 does not end on cylinder boundary.

/dev/sda4           12202       46080    34692096    5  Extended

Partition 4 does not end on cylinder boundary.

/dev/sda5           12204       14203     2048000    6  FAT16

/dev/sda6           14204       15228     1049584   fd  Linux raid autodetect

/dev/sda7           15229       16253     1049584   fd  Linux raid autodetect

/dev/sda8           16254       17278     1049584   fd  Linux raid autodetect

/dev/sda9           17279       18303     1049584   fd  Linux raid autodetect

/dev/sda10          18304       19328     1049584   fd  Linux raid autodetect

/dev/sda11          19329       26497     7341040   83  Linux


Command (m for help): n

First cylinder (12202-46080, default 12202): 26498

Last cylinder, +cylinders or +size{K,M,G} (26498-46080, default 46080): +3G


Command (m for help): p


Disk /dev/sda: 48.3 GB, 48318382080 bytes

64 heads, 32 sectors/track, 46080 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

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

Disk identifier: 0x0005361c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           2         201      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2             202       10201    10240000   83  Linux

Partition 2 does not end on cylinder boundary.

/dev/sda3           10202       12201     2048000   82  Linux swap / Solaris

Partition 3 does not end on cylinder boundary.

/dev/sda4           12202       46080    34692096    5  Extended

Partition 4 does not end on cylinder boundary.

/dev/sda5           12204       14203     2048000    6  FAT16

/dev/sda6           14204       15228     1049584   fd  Linux raid autodetect

/dev/sda7           15229       16253     1049584   fd  Linux raid autodetect

/dev/sda8           16254       17278     1049584   fd  Linux raid autodetect

/dev/sda9           17279       18303     1049584   fd  Linux raid autodetect

/dev/sda10          18304       19328     1049584   fd  Linux raid autodetect

/dev/sda11          19329       26497     7341040   83  Linux

/dev/sda12          26498       29570     3146736   83  Linux


Command (m for help): n

First cylinder (12202-46080, default 12202): 29571

Last cylinder, +cylinders or +size{K,M,G} (29571-46080, default 46080): +5G


Command (m for help): t

Partition number (1-13): 11

Hex code (type L to list codes): 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

Hex code (type L to list codes): 8e

Changed system type of partition 11 to 8e (Linux LVM)


Command (m for help): t

Partition number (1-13): 12

Hex code (type L to list codes): 8e

Changed system type of partition 12 to 8e (Linux LVM)

最后保存

[root@localhost ~]# fdisk -l


Disk /dev/sda: 48.3 GB, 48318382080 bytes

64 heads, 32 sectors/track, 46080 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

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

Disk identifier: 0x0005361c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           2         201      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2             202       10201    10240000   83  Linux

Partition 2 does not end on cylinder boundary.

/dev/sda3           10202       12201     2048000   82  Linux swap / Solaris

Partition 3 does not end on cylinder boundary.

/dev/sda4           12202       46080    34692096    5  Extended

Partition 4 does not end on cylinder boundary.

/dev/sda5           12204       14203     2048000    6  FAT16

/dev/sda6           14204       15228     1049584   fd  Linux raid autodetect

/dev/sda7           15229       16253     1049584   fd  Linux raid autodetect

/dev/sda8           16254       17278     1049584   fd  Linux raid autodetect

/dev/sda9           17279       18303     1049584   fd  Linux raid autodetect

/dev/sda10          18304       19328     1049584   fd  Linux raid autodetect

/dev/sda11          19329       26497     7341040   8e  Linux LVM

/dev/sda12          26498       29570     3146736   8e  Linux LVM

/dev/sda13          29571       34691     5243888   8e  Linux LVM


Disk /dev/md127: 2146 MB, 2146435072 bytes

2 heads, 4 sectors/track, 524032 cylinders

Units = cylinders of 8 * 512 = 4096 bytes

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

I/O size (minimum/optimal): 524288 bytes / 1048576 bytes

Disk identifier: 0x00000000



Disk /dev/md1: 1073 MB, 1073676288 bytes

2 heads, 4 sectors/track, 262128 cylinders

Units = cylinders of 8 * 512 = 4096 bytes

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

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

Disk identifier: 0x00000000

[root@localhost ~]# pvcreate /dev/sda{11,12}

  Physical volume "/dev/sda11" successfully created

  Physical volume "/dev/sda12" successfully created

[root@localhost ~]# pvs

  PV         VG   Fmt  Attr PSize PFree

  /dev/sda11      lvm2 ---  7.00g 7.00g

  /dev/sda12      lvm2 ---  3.00g 3.00g

[root@localhost ~]# pvsdisplay

-bash: pvsdisplay: command not found

[root@localhost ~]# pvdisplay

  "/dev/sda11" is a new physical volume of "7.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sda11

  VG Name               

  PV Size               7.00 GiB

  Allocatable           NO

  PE Size               0   

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               du3bb3-lMzi-QlLP-TnBA-lFT6-KAeq-ILZ2TA

   

  "/dev/sda12" is a new physical volume of "3.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sda12

  VG Name               

  PV Size               3.00 GiB

  Allocatable           NO

  PE Size               0   

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               vAcdQ6-xItu-mGWW-ky4t-TRrF-LXPe-cfGQm2

   

[root@localhost ~]# pvscan

  PV /dev/sda11         lvm2 [7.00 GiB]

  PV /dev/sda12         lvm2 [3.00 GiB]

  Total: 2 [10.00 GiB] / in use: 0 [0   ] / in no VG: 2 [10.00 GiB]

建立卷组

[root@localhost ~]# vgcreate myvg /dev/sda{11,12}

  Volume group "myvg" successfully created

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   2   0   0 wz--n- 9.99g 9.99g

[root@localhost ~]# vgdisplay

  --- Volume group ---

  VG Name               myvg

  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               9.99 GiB

  PE Size               4.00 MiB

  Total PE              2558

  Alloc PE / Size       0 / 0   

  Free  PE / Size       2558 / 9.99 GiB

  VG UUID               WmyW7f-raGk-fOoF-Bmt5-SEeA-yJVz-DNpilW

减少增加卷组

减少

[root@localhost ~]# pvmove /dev/sda12

  No data to move for myvg

[root@localhost ~]# vgreduce myvg /dev/sda12

  Removed "/dev/sda12" from volume group "myvg"

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   1   0   0 wz--n- 7.00g 7.00g

[root@localhost ~]# pvs

  PV         VG   Fmt  Attr PSize PFree

  /dev/sda11 myvg lvm2 a--  7.00g 7.00g

  /dev/sda12      lvm2 ---  3.00g 3.00g

添加

[root@localhost ~]# pvcreate /dev/sda13

  Physical volume "/dev/sda13" successfully created

[root@localhost ~]# vgextend myvg /dev/sda13

  Volume group "myvg" successfully extended

[root@localhost ~]# vgdisplay

  --- Volume group ---

  VG Name               myvg

  System ID             

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  3

  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               11.99 GiB

  PE Size               4.00 MiB

  Total PE              3070

  Alloc PE / Size       0 / 0   

  Free  PE / Size       3070 / 11.99 GiB

  VG UUID               WmyW7f-raGk-fOoF-Bmt5-SEeA-yJVz-DNpilW

   

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize  VFree 

  myvg   2   0   0 wz--n- 11.99g 11.99g

添加逻辑卷

[root@localhost ~]# lvcreate -L 50M -n testlv myvg

  Rounding up size to full physical extent 52.00 MiB

  Logical volume "testlv" created.

[root@localhost ~]# mke2fs -t ext4 /dev/myvg/testlv 

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=1024 (log=0)

分块大小=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

13328 inodes, 53248 blocks

2662 blocks (5.00%) reserved for the super user

第一个数据块=1

Maximum filesystem blocks=54525952

7 block groups

8192 blocks per group, 8192 fragments per group

1904 inodes per group

Superblock backups stored on blocks: 

8193, 24577, 40961


正在写入inode表: 完成                            

Creating journal (4096 blocks): 完成

Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 32 mounts or

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

[root@localhost ~]# mount /dev/myvg/testlv /mnt

[root@localhost ~]# lvs

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

  testlv myvg -wi-ao---- 52.00m   

拓展逻辑卷

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize  VFree 

  myvg   2   1   0 wz--n- 11.99g 11.94g

[root@localhost ~]# lvextend -L 5G /dev/myvg/testlv

  Size of logical volume myvg/testlv changed from 52.00 MiB (13 extents) to 5.00 GiB (1280 extents).

  Logical volume testlv successfully resized

[root@localhost ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             9.5G  3.8G  5.3G  42% /

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

/dev/sda1             190M   40M  140M  23% /boot

/dev/sda5             1.9G  3.0M  1.8G   1% /opt

/dev/mapper/myvg-testlv

                       47M  842K   43M   2% /mnt

[root@localhost ~]# lvs

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

  testlv myvg -wi-ao---- 5.00g  

然后再拓展文件系统

[root@localhost ~]# resize2fs -p /dev/myvg/testlv 

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/myvg/testlv is mounted on /mnt; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 20

Performing an on-line resize of /dev/myvg/testlv to 5242880 (1k) blocks.

The filesystem on /dev/myvg/testlv is now 5242880 blocks long.

[root@localhost ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             9.5G  3.8G  5.3G  42% /

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

/dev/sda1             190M   40M  140M  23% /boot

/dev/sda5             1.9G  3.0M  1.8G   1% /opt

/dev/mapper/myvg-testlv

                      4.9G  2.6M  4.6G   1% /mnt

缩减逻辑卷

注意:1,得先卸载;

   2,确保缩减后的空间大小依然能存储原有的所有数据

   3,在缩减之前应该先强行检查文件,以确保文件系统处于一致性状态

[root@localhost ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             9.5G  3.8G  5.3G  42% /

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

/dev/sda1             190M   40M  140M  23% /boot

/dev/sda5             1.9G  3.0M  1.8G   1% /opt

/dev/mapper/myvg-testlv

                      4.9G  2.6M  4.6G   1% /mnt

[root@localhost ~]# mount /mnt

mount: /dev/mapper/myvg-testlv already mounted or /mnt busy

mount: according to mtab, /dev/mapper/myvg-testlv is already mounted on /mnt

[root@localhost ~]# umount /mnt

[root@localhost ~]# e2fsck -f /dev/myvg/testlv 

e2fsck 1.41.12 (17-May-2010)

第一步: 检查inode,块,和大小

第二步: 检查目录结构

第3步: 检查目录连接性

Pass 4: Checking reference counts

第5步: 检查簇概要信息

/dev/myvg/testlv: 11/1218560 files (9.1% non-contiguous), 160637/5242880 blocks

[root@localhost ~]# resize2fs /dev/myvg/testlv  3G

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/myvg/testlv to 3145728 (1k) blocks.

The filesystem on /dev/myvg/testlv is now 3145728 blocks long.


[root@localhost ~]# lvreduce -L 3G /dev/myvg/testlv 

  WARNING: Reducing active logical volume to 3.00 GiB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce testlv? [y/n]: y

  Size of logical volume myvg/testlv changed from 5.00 GiB (1280 extents) to 3.00 GiB (768 extents).

  Logical volume testlv successfully resized

[root@localhost ~]# mount /dev/myvg/testlv /mnt

[root@localhost ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             9.5G  3.8G  5.3G  42% /

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

/dev/sda1             190M   40M  140M  23% /boot

/dev/sda5             1.9G  3.0M  1.8G   1% /opt

/dev/mapper/myvg-testlv

                      3.0G  2.6M  2.8G   1% /mnt