LVM

LVM

一:建立PV

# pvcreate /dev/sdb{6,7,8,9}

# pvscan 查看建立的PV

# pvdisplay 查看詳細信息

 

二:在pv基礎上建立VG

(1) 把/dev/sdb{6,7,8}建立一個VG,名爲testvg

# vgcreate -s 16M testvg /dev/sdb{6,7,8}

# vgscan

 

2) 查看建立的vg

[root@centos ~]# vgdisplay

  --- Volume group ---

  VG Name               testvg

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

  PE Size               16.00 MiB

  Total PE              192

  Alloc PE / Size       0 / 0   

  Free  PE / Size       192 / 3.00 GiB

  VG UUID               9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D

 

(3)把一個/sdb9增加到testvg

# vgextend testvg /dev/sdb9

 

(4)再次查看vg,PE256

[root@centos ~]# vgdisplay

  --- Volume group ---

  VG Name               testvg

  System ID             

  Format                lvm2

  Metadata Areas        4

  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                4

  Act PV                4

  VG Size               4.00 GiB

  PE Size               16.00 MiB

 Total PE              256

  Alloc PE / Size       0 / 0   

 Free  PE / Size       256 / 4.00 GiB  PE塊和容量都增加了

  VG UUID               9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D

 

VG的基礎上建立LV

1)建立testlvPE256個。

# lvcreate -l 256 -n testlv testvg

(2)格式化和掛載

# mke2fs -t ext4 /dev/testvg/testlv 

(3)建立掛載點,並掛載

# mkdir /mnt/lvm

# mount /dev/testvg/testlv /mnt/lvm

 

4)查看掛在後的lv

[root@centos ~]# df -h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/testvg-testlv

                             3.9G  8.0M  3.7G   1% /mnt/lvm

 

LV 擴容

 (1)以sdb10 作爲擴容盤,新建立一個PV

# pvcreate /dev/sdb10

 

 2查看建立的pv

  [root@centos ~]# pvs

  PV         VG        Fmt  Attr PSize  PFree

  /dev/sda2  vg_centos lvm2 a--  19.51g    0 

  /dev/sdb10           lvm2 ---   3.93g 3.93g

  /dev/sdb6  testvg    lvm2 a--   1.00g    0 

  /dev/sdb7  testvg    lvm2 a--   1.00g    0 

  /dev/sdb8  testvg    lvm2 a--   1.00g    0 

/dev/sdb9  testvg    lvm2 a--   1.00g    0  

 

  (2) 利用vgextend /sdb10 增加到testvg

# vgextend testvg /dev/sdb10 

 (3)再次查看

 [root@centos ~]# pvs

  PV         VG        Fmt  Attr PSize  PFree

  /dev/sda2  vg_centos lvm2 a--  19.51g    0 

  /dev/sdb10 testvg    lvm2 a--   3.92g 3.92g   已經添加到卷組testvg

  /dev/sdb6  testvg    lvm2 a--   1.00g    0 

  /dev/sdb7  testvg    lvm2 a--   1.00g    0 

  /dev/sdb8  testvg    lvm2 a--   1.00g    0 

  /dev/sdb9  testvg    lvm2 a--   1.00g    0

 

 (4) 利用vgdisplay查看增加的塊

   # vgdisplay

  --- Volume group ---

  VG Name               testvg

  System ID             

  Format                lvm2

  Metadata Areas        5

  Metadata Sequence No  4

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               1

  Max PV                0

  Cur PV                5

  Act PV                5

  VG Size               7.92 GiB

  PE Size               16.00 MiB

  Total PE              507

  Alloc PE / Size       256 / 4.41 GiB

Free  PE / Size       251 / 3.92 GiB     增加的251PE 

  VG UUID               d6YPIx-vfID-TL5H-Fmcy-nZVi-G7uq-LTVgQ0

 

5)利用lvresize來增加PE塊到LV

 # lvresize -l +251 /dev/testvg/testlv 

  Size of logical volume testvg/testlv changed from 4.00 GiB (256 extents) to 7.92 GiB (507 extents).

  

6)利用 lvdisplay 查看增加後的容量。

 

 

7)用resize2fs 講文件系統裏容量確實增加

 # resize2fs /dev/testvg/testlv 

8# df -h /mnt/lvm/  增量成功。

  Filesystem            Size  Used Avail Use% Mounted on

  /dev/mapper/testvg-testlv

                                                      7.7G  9.0M  7.3G   1% /mnt/lvm

 

五:縮小LV

(1)假如我們要把/dev/sdb6 抽離出來,先找出容量和PE塊數

[root@centos ~]# pvdisplay

  --- Physical volume ---

  PV Name               /dev/sdb6

  VG Name               testvg

  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               B3LTgT-zLCO-hseh-EdlT-jm3T-Z57d-WqesPv

 

(2)卸載lvm

[root@centos ~]# umount /mnt/lvm/

 

(3)強制檢查LV

[root@centos ~]# e2fsck -f /dev/testvg/testlv 

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/testvg/testlv: 11/524288 files (0.0% non-contiguous), 67983/2076672 blocks

[root@centos ~]# resize2fs /dev/testvg/testlv 6G

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/testvg/testlv to 1572864 (4k) blocks.

The filesystem on /dev/testvg/testlv is now 1572864 blocks long.

 

(4)縮小分區到6G

[root@centos ~]# resize2fs /dev/testvg/testlv 6G       縮小到6G

 

   這個最好總的PV減去/dev/sdb6後的大小,縮減太多容易浪費。比哪縮減到6900M,我這時縮減多了

 

5)縮小LV邏輯邊界到6G

[root@centos ~]# lvreduce -L 6G /dev/testvg/testlv  LV縮小到6G

  WARNING: Reducing active logical volume to 6.00 GiB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

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

  Size of logical volume testvg/testlv changed from 7.92 GiB (507 extents) to 6.00 GiB (384 extents).

  Logical volume testlv successfully resized

 

6)再次掛載LV

[root@centos ~]# mount /dev/testvg/testlv /mnt/lvm

[root@centos ~]# df -h /mnt/lvm

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/testvg-testlv

                      5.8G  8.0M  5.5G   1% /mnt/lvm

 

7)查看PV上的空閒塊

[root@centos lvm]# pvdisplay

--- Physical volume ---

  PV Name               /dev/sdb10

  VG Name               testvg

  PV Size               3.93 GiB / not usable 8.06 MiB

  Allocatable           yes 

  PE Size               16.00 MiB

  Total PE              251

  Free PE               123   移除的空閒塊在/dev/sdb10上,這個自己注意觀察

  Allocated PE          128

  PV UUID               GXWe7H-uNPt-mERp-gKk6-kz70-yUtj-XLXp0y

(7)sdb6上佔用塊移動到空閒塊/sdb10

[root@centos lvm]# pvmove /dev/sdb6 /dev/sdb10

 

  /dev/sdb6: Moved: 0.0%

  /dev/sdb6: Moved: 73.4%

  /dev/sdb6: Moved: 100.0%

 

8)把/dev/sdb6vg中移出去

[root@centos lvm]# vgreduce testvg    /dev/sdb6 從testvg中移出去

  Removed "/dev/sdb6" from volume group "testvg"

 

[root@centos lvm]# pvscan

  PV /dev/sdb7    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb8    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb9    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb10   VG testvg      lvm2 [3.92 GiB / 944.00 MiB free]

  PV /dev/sda2    VG vg_centos   lvm2 [19.51 GiB / 0    free]

  PV /dev/sdb6                   lvm2 [1.01 GiB]

 

9)把/sdb6/PV中移出

[root@centos lvm]# pvremove /dev/sdb6  從PV中移除去

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

 

10)在pv中已經沒有sdb6

[root@centos lvm]# pvscan

  PV /dev/sdb7    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb8    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb9    VG testvg      lvm2 [1.00 GiB / 0    free]

  PV /dev/sdb10   VG testvg      lvm2 [3.92 GiB / 944.00 MiB free]

  PV /dev/sda2    VG vg_centos   lvm2 [19.51 GiB / 0    free]

  Total: 5 [26.43 GiB] / in use: 5 [26.43 GiB] / in no VG: 0 [0   

 

已經沒有/dev/sdb6

(11)pv裏剩下的塊在加回到LV裏。避免浪費。

[root@centos ~]# lvresize -l +59 /dev/testvg/testlv 

  Size of logical volume testvg/testlv changed from 6.00 GiB (384 extents) to 6.92 GiB (443 extents).

  Logical volume testlv successfully resized

 

 

快照的建立:

1:查看VG上已經沒有空閒PE塊了

2:把 /dev/sdb6添加到VG

[root@centos ~]# pvcreate /dev/sdb6

[root@centos ~]# vgextend testvg /dev/sdb6

  Volume group "testvg" successfully extended

 

3:查看VG中空閒PE

[root@centos ~]# vgdisplay 

  --- Volume group ---

  VG Name               testvg

  System ID             

  Format                lvm2

  Metadata Areas        5

  Metadata Sequence No  20

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               1

  Max PV                0

  Cur PV                5

  Act PV                5

  VG Size               7.92 GiB

  PE Size               16.00 MiB

  Total PE              507

  Alloc PE / Size       443 / 6.92 GiB

  Free  PE / Size       64 / 1.00 GiB

  VG UUID               9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D

 

3:建立快照區:

[root@centos ~]# lvcreate -l 64 -s -n testss /dev/testvg/testlv 

  Logical volume "testss" created

  註解:-l :用來指定用多少PE

        -s :用來說明使用快照功能

        -n :用來指定快照區的名稱,名稱後接的LV完整路徑

 

4:查看建立的快照區

[root@centos ~]# lvdisplay /dev/testvg/testss 

  --- Logical volume ---

  LV Path                /dev/testvg/testss

  LV Name                testss

  VG Name                testvg

  LV UUID                UI89Oh-fqtD-vNtD-LQ6v-ikGH-Vt6x-0BJK5g

  LV Write Access        read/write

  LV Creation host, time centos, 2016-03-24 16:46:16 +0800

  LV snapshot status     active destination for testlv

  LV Status              available

  # open                 0

  LV Size                6.92 GiB   被快照的LV磁盤容量

  Current LE             443

  COW-table size         1.00 GiB   快照區的大小

  COW-table LE           64      快照區的PE數量

  Allocated to snapshot  0.00%

  Snapshot chunk size    4.00 KiB

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:2

 

5:建立掛載快照掛載點,並掛載

[root@centos lvm]# mkdir /mnt/snapshot

[root@centos lvm]# mount /dev/testvg/testss /mnt/snapshot/

 

6:查看掛載同步

[root@centos lvm]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/testvg-testlv

                      6.7G   45M  6.3G   1% /mnt/lvm

/dev/mapper/testvg-testss

                      6.7G   45M  6.3G   1% /mnt/snapshot

7:卸載掛載的快照

[root@centos lvm]# umount /mnt/snapshot/

 

8:測試,我添加和刪除一些LVM中的文件,再次掛載上快照,會發現不一樣了

 

[root@centos lvm]# mount /dev/testvg/testss /mnt/snapshot/

[root@centos lvm]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/testvg-testlv

                      6.7G  628M  5.8G  10% /mnt/lvm

/dev/mapper/testvg-testss

                      6.7G   45M  6.3G   1% /mnt/snapshot

 

9:我們把快照裏的文件備份打包到/backups

[root@centos ]# mkdir -p /backups/

[root@centos ]# cd /mnt/snapshot/

[root@centos snapshot]# tar -jcv -f /backups/lvm.tar.bz2 *

 

10:還原時卸載掉快照,並刪除快照

[root@centos ~]# umount /mnt/snapshot/

[root@centos ~]# lvremove /dev/testvg/testss 

Do you really want to remove active logical volume testss? [y/n]: y

  Logical volume "testss" successfully removed

 

11:卸載LVM,並格式化掛載LVM。最後解壓數據

[root@centos ~]# umount /mnt/lvm/

[root@centos ~]# mke2fs -t ext4 /dev/testvg/testlv 

[root@centos backups]# cd /backups/

[root@centos backups]# tar -jxf lvm.tar.bz2 -C /mnt/lvm/

 

12:最後重新掛載LVM和快照,數據就會同步了。

wKioL1cHvmrip_LaAAB4Lx5r-FY627.png 


 

快照損壞的再次同步LVM就行了

# umount /mnt/snapshot

# lvremove /dev/testvg/testss/

查看VG中的空閒PE

# vgdisplay  

# lvcreate -s -l 64 -n testss /dev/testvg/testlv 

# mount /dev/testvg/testss /mnt/snapshot/

# ll /mnt/snapshot/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章