当前位置: > Linux教程 > Linux学习 >

LVM逻辑卷管理技术

时间:2017-03-25 17:28来源:linux.it.net.cn 作者:IT

1、LVM逻辑卷管理技术产生的背景 
企业日益变化的存储需要使得传统的磁盘分区存储显得不够灵活 

2、磁盘分区存储 
clip_image001

对于这样的三个物理分区的话,迟早有一天会被数据填满,因为它是死的,无法进行缩放。 
假设下面表示的是/dev/sda磁盘上的三个分区:

clip_image002

1)、初始化/dev/sda上的这三个物理分区:

clip_image003

命令:# pvcreate /dev/sda1 /dev/sda2 /dev/sda3 或#pvcreate /dev/sda[1-3]

2)、将这三个物理卷PV ,合并成一个卷组(VG Volume Group)

clip_image004

命令:#vgcreate vg0 /dev/sda[1-2]

3)、从卷组VG 0 中创建逻辑卷LV ,逻辑卷名称为date

clip_image005

命令:# lvcreate –n date –L 10M vg0

4)、扩充LV逻辑卷date的大小:

clip_image006

命令:#e2fsadm –L +10M /dev/vg0/date

5)、将另外一个PV逻辑卷/dev/sda3增加到VG中:

clip_image007

命令:#vgextend vg0 /dev/sda3

6)、 从逻辑卷组LV中先移除一部分物理分区:

clip_image008

命令:1、#pvmove /dev/sda1          <--------将sda1中的数据移到其它的物理卷中

        2、#vgreduce vg0 /dev/sda1  <--------将sda1分区从vg0卷组中移出

----------------------------------实际的配置过程----------------------------------

[root@localhost root]# fdisk /dev/sdb <---------在/dev/sdb分区上做这个实验

Command (m for help): n 
Command action 
   e   extended 
   p   primary partition (1-4) 

Partition number (1-4): 1 
First cylinder (1-522, default 1): 
Using default value 1 
Last cylinder or +size or +sizeM or +sizeK (1-522, default 522): 200

Command (m for help): t    <---------转换分区类型 
Selected partition 1 
Hex code (type L to list codes): L <--------列出所有分区的类型代码

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

Command (m for help): n 
Command action 
   e   extended 
   p   primary partition (1-4) 

Partition number (1-4): 2 
First cylinder (201-522, default 201): 
Using default value 201 
Last cylinder or +size or +sizeM or +sizeK (201-522, default 522): 401

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): n 
Command action 
   e   extended 
   p   primary partition (1-4) 

Partition number (1-4): 3 
First cylinder (402-522, default 402): 
Using default value 402 
Last cylinder or +size or +sizeM or +sizeK (402-522, default 522): 
Using default value 522

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

Command (m for help): p   <------打印出分区表信息

Disk /dev/sdb: 4294 MB, 4294967296 bytes 
255 heads, 63 sectors/track, 522 cylinders 
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks       Id   System 
/dev/sdb1             1        200    1606468+  8e   Linux LVM 
/dev/sdb2           201       401    1614532+  8e   Linux LVM 
/dev/sdb3           402       522    971932+   8e   Linux LVM

Command (m for help): w  <--------保存退出~! 
The partition table has been altered!

Calling ioctl() to re-read partition table. 
Syncing disks. 
[root@localhost root]# pvcreate /dev/sdb[1-3]    <---------创建物理卷PV 
pvcreate -- ERROR: "/etc/lvmtab" doesn't exist; please run vgscan <--初次创建,要运行vgscan,生成配置文件/etc/lvmtab

[root@localhost root]# vgscan   <-----运行vgscan命令,生成配置文件/etc/lvmtab . 
vgscan -- reading all physical volumes (this may take a while...) 
vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created  <----提示已经成功创建所需要的两个文件 
vgscan -- WARNING: This program does not do a VGDA backup of your volume group

[root@localhost root]# pvcreate /dev/sdb[1-3]    <------再次创建pv物理分区 
pvcreate -- physical volume "/dev/sdb1" successfully created 
pvcreate -- physical volume "/dev/sdb2" successfully created 
pvcreate -- physical volume "/dev/sdb3" successfully created

[root@localhost root]# vgcreate vg0 /dev/sdb[1-2]    <--------创建vg卷组 
vgcreate -- INFO: using default physical extent size 4 MB 
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte 
vgcreate -- doing automatic backup of volume group "vg0" 
vgcreate -- volume group "vg0" successfully created and activated

[root@localhost root]# vgdisplay vg0   <----------查看VG卷组的信息 
--- Volume group --- 
VG Name               vg0 
VG Access             read/write 
VG Status             available/resizable 
VG #                    0 
MAX LV                256 
Cur LV                  0 
Open LV               0 
MAX LV Size           255.99 GB 
Max PV                256 
Cur PV                2 
Act PV                2 
VG Size               3.06 GB    <---------可用的VG的总大小 
PE Size               4 MB        <---------扩展块的大小为4M 
Total PE              784         <---------总共包含的扩展块数量 
Alloc PE / Size       0 / 0      <----------以PE size的形式来分配大小 
Free  PE / Size       784 / 3.06 GB 
VG UUID               fsfUSc-GH4e-lQGl-EjS6-EMKF-56xa-V6Qpy4

[root@localhost root]# lvcreate -n data -L 100M vg0   <--------在vg0卷组中,创建逻辑卷lv : data 
lvcreate -- doing automatic backup of "vg0" 
lvcreate -- logical volume "/dev/vg0/data" successfully created

[root@localhost root]# mkfs.ext3 /dev/vg0/data   <---------格式化逻辑卷lv 
mke2fs 1.32 (09-Nov-2002) 
Filesystem label= 
OS type: Linux 
Block size=1024 (log=0) 
Fragment size=1024 (log=0) 
25688 inodes, 102400 blocks 
5120 blocks (5.00%) reserved for the super user 
First data block=1 
13 block groups 
8192 blocks per group, 8192 fragments per group 
1976 inodes per group 
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done 
Creating journal (4096 blocks): done 
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 
[root@localhost root]# mount /dev/vg0/data /mnt   <----挂载逻辑卷 
[root@localhost root]# cp /etc/services /mnt   <--------测试是否可以使用 
[root@localhost root]# lvdisplay /dev/vg0/data   <-------显示lv的详细信息 
--- Logical volume --- 
LV Name                /dev/vg0/data 
VG Name                vg0 
LV Write Access        read/write 
LV Status              available 
LV #                    1 
# open                 1 
LV Size                100 MB                <---------逻辑卷的空间大小 
Current LE             25 
Allocated LE           25 
Allocation             next free 
Read ahead sectors     1024 
Block device           58:0

[root@localhost root]# vgdisplay vg0  <--------显示卷组vg的详细情况 
--- Volume group --- 
VG Name               vg0 
VG Access             read/write 
VG Status             available/resizable 
VG #                  0 
MAX LV                256 
Cur LV                1 
Open LV               1 
MAX LV Size           255.99 GB 
Max PV                256 
Cur PV                2 
Act PV                2 
VG Size               3.06 GB 
PE Size               4 MB            
Total PE              784 
Alloc PE / Size       25 / 100 MB  <----------以PE size的形式来表示已经分配的空间大小 
Free  PE / Size       759 / 2.96 GB <----------以PE size的形式来表示剩余的空间大小 
VG UUID               fsfUSc-GH4e-lQGl-EjS6-EMKF-56xa-V6Qpy4

[root@localhost root]# lvscan  <-------  查看逻辑卷的信息 
lvscan -- ACTIVE            "/dev/vg0/data" [100 MB] 
lvscan -- 1 logical volumes with 100 MB total in 1 volume group 
lvscan -- 1 active logical volumes

[root@localhost root]# df –lh  <-------查看挂载分区的使用情况 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/sda1             5.0G  1.1G  3.6G  24% / 
none                   78M     0   78M   0% /dev/shm 
/dev/vg0/data          97M  4.1M   88M   5% /mnt 
[root@localhost root]# umount /mnt   <------卸载/dev/vg0/data逻辑卷 
[root@localhost root]# e2fsadm -L +200M /dev/vg0/data  <------为逻辑卷/dev/vg0/data扩容200M 
e2fsck 1.32 (09-Nov-2002) 
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/vg0/data: 12/25688 files (0.0% non-contiguous), 7398/102400 blocks 
lvextend -- extending logical volume "/dev/vg0/data" to 300 MB 
lvextend -- doing automatic backup of volume group "vg0" 
lvextend -- logical volume "/dev/vg0/data" successfully extended

resize2fs 1.32 (09-Nov-2002) 
Begin pass 1 (max = 25) 
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Begin pass 2 (max = 1) 
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Begin pass 3 (max = 13) 
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Begin pass 5 (max = 7) 
Moving inode table            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
The filesystem on /dev/vg0/data is now 307200 blocks long.

e2fsadm -- ext2fs in logical volume /dev/vg0/data successfully extended to 300 MB <------已经成功扩容到300M

[root@localhost root]# mount /dev/vg0/data /mnt  <-------再次挂载逻辑卷data 
[root@localhost root]# df –lh <-----查看挂载分区的使用情况 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/sda1             5.0G  1.1G  3.6G  24% / 
none                   78M     0   78M   0% /dev/shm 
/dev/vg0/data         291M  4.1M  272M   2% /mnt   <----------已经成功扩容,并挂载成功. 
[root@localhost root]# vgdisplay vg0  <--------查看VG卷组的信息 
--- Volume group --- 
VG Name               vg0 
VG Access             read/write 
VG Status             available/resizable 
VG #                  0 
MAX LV                256 
Cur LV                1 
Open LV               1 
MAX LV Size           255.99 GB 
Max PV                256 
Cur PV                2 
Act PV                2 
VG Size               3.06 GB 
PE Size               4 MB 
Total PE              784 
Alloc PE / Size       75 / 300 MB 
Free  PE / Size       709 / 2.77 GB 
VG UUID               fsfUSc-GH4e-lQGl-EjS6-EMKF-56xa-V6Qpy4




(责任编辑:IT)
------分隔线----------------------------