LVM:Logical Volume Manager

一、创建、删除物理卷

    pvcreate /PATH/TO/PV

    pvremove /PATH/TO/PV

    pvdisplay

    pvs    

[root@oracle ~]# pvcreate /dev/sdb{1,2,3,5}
 Physical volume "/dev/sdb1" successfully created.
 Physical volume "/dev/sdb2" successfully created.
 Physical volume "/dev/sdb3" successfully created.
 Physical volume "/dev/sdb5" successfully created.

二、创建、删除卷组

vgcreate VG_NAME /PATH/TO/PV

vgremove VG_NAME 删除卷组

vgdisplay 显示详细信息

vgs 显示卷组名、物理卷个数、容量大小(信息相较简略)

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

[root@oracle ~]# vgcreate test_vg /dev/sdb{1,2,3}
  Volume group "test_vg" successfully created
[root@oracle ~]# vgdisplay 
  --- Volume group ---
  VG Name               ol
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               99.00 GiB
  PE Size               4.00 MiB
  Total PE              25343
  Alloc PE / Size       25342 / 98.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               0fRWww-EJtp-StyQ-Vt2v-IOvU-9kkT-V0spMP
   
  --- Volume group ---
  VG Name               test_vg
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       0 / 0   
  Free  PE / Size       765 / 2.99 GiB
  VG UUID               57ta1G-U2St-BY8s-XU1T-MZHN-NWjC-EucYKQ

三、创建逻辑卷

[root@oracle ~]# vgcreate test_vg /dev/sdb{1,2,3}
  Volume group "test_vg" successfully created
lvcreate -n LV_NAME -L(逻辑卷总大小) #G VG_NAME | -l (逻辑卷里PE个数)#G VG_NAME
[root@oracle ~]# lvcreate -n test_lv -L 2G test_vg 
  Logical volume "test_lv" created.
[root@oracle ~]# lvs
  LV      VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home    ol      -wi-ao---- 45.12g                                                    
  root    ol      -wi-ao---- 50.00g                                                    
  swap    ol      -wi-ao----  3.88g                                                    
  test_lv test_vg -wi-a-----  1.56g

四、格式化为ext3

[root@oracle ~]# mke2fs -j /dev/test_vg/test_lv 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
102544 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=419430400
13 block groups
32768 blocks per group, 32768 fragments per group
7888 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

五、扩展卷组

[root@oracle ~]# vgextend test_vg /dev/sdb5
  Volume group "test_vg" successfully extended

六、扩展逻辑卷

lvextend -L [+]# /PATH/TO/LV

[root@oracle ~]# lvextend -L +500M /dev/test_vg/test_lv 
  Rounding size to boundary between physical extents: 504.00 MiB.
  Size of logical volume test_vg/test_lv changed from 1.56 GiB (200 extents) to 2.05 GiB (263 extents).
  Logical volume test_vg/test_lv successfully resized.

七、指定文件系统

resize2fs /PATH/TO/PV 3G
[root@oracle ~]# resize2fs -p /dev/test_vg/test_lv 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test_vg/test_lv to 538624 (4k) blocks.
Begin pass 1 (max = 4)
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/test_vg/test_lv is now 538624 blocks long.

八、缩减逻辑卷

注意事项:

    1.不能在线缩减,得先卸载

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

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

[root@oracle ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
devtmpfs                     1.8G     0  1.8G   0% /dev
tmpfs                        1.8G  8.0K  1.8G   1% /dev/shm
tmpfs                        1.8G  9.1M  1.8G   1% /run
tmpfs                        1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/mapper/ol-root           50G  8.7G   42G  18% /
/dev/sda1                   1014M  341M  674M  34% /boot
/dev/mapper/ol-home           46G   37M   46G   1% /home
tmpfs                        368M  8.0K  368M   1% /run/user/0
/dev/sr0                     4.4G  4.4G     0 100% /run/media/root/OL-7.3 Server.x86_64
/dev/mapper/test_vg-test_lv  2.0G  2.4M  1.9G   1% /mnt


[root@oracle ~]# umount /mnt


[root@oracle ~]# e2fsck -f /dev/test_vg/test_lv (强制检查文件系统)
e2fsck 1.42.9 (28-Dec-2013)
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/test_vg/test_lv: 11/134096 files (0.0% non-contiguous), 17228/538624 blocks


[root@oracle ~]# resize2fs /dev/test_vg/test_lv 300M(文件系统先缩小300M)
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test_vg/test_lv to 76800 (4k) blocks.
The filesystem on /dev/test_vg/test_lv is now 76800 blocks long.


[root@oracle ~]# lvreduce -L 300M /dev/test_vg/test_lv (物理空间再缩小300M)
  Rounding size to boundary between physical extents: 304.00 MiB.
  WARNING: Reducing active logical volume to 304.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
  
Do you really want to reduce test_vg/test_lv? [y/n]: y

  Size of logical volume test_vg/test_lv changed from 2.05 GiB (263 extents) to 304.00 MiB (38 extents).
  Logical volume test_vg/test_lv successfully resized.

九、快照卷

知识点:

    1、生命周期为整个数据时长;在这段时长内,数据的增长量不能超出快照卷大小;
    2、快照卷应该是只读的;
    3、跟原卷在同一卷组内;
[root@oracle ~]# lvcreate -n test_plv -L 500M -p r /dev/test_vg
  Rounding up size to full physical extent 504.00 MiB
  WARNING: Logical volume test_vg/test_plv not zeroed.
  Logical volume "test_plv" created.