歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux如何創建使用邏輯卷管理LVM

Linux如何創建使用邏輯卷管理LVM

日期:2017/3/1 17:50:43   编辑:Linux技術

  LVM是邏輯卷管理,是Linux下對磁盤分區管理的一種機制,相信不少人想要知道Linux如何創建LVM和LVM的使用方法,因此小編將針對LVM的創建和使用做個詳細介紹,一起來學習下吧。

  LVM講解:

  圖像大綱:

 Linux如何創建使用LVM

  1)LVM是什麼,作用:

  LVM稱為邏輯卷管理,作用是提高磁盤分區的管理和使用靈活性。

  通過邏輯卷LVM可以有效的管理和分配磁盤空間,可以把分散的小分區組合成一個大的卷組,從卷組中分出需要的空間做出邏輯卷,這樣使得磁盤擴容和縮減方便易用。還有著備份的功能,指的就是lvm快照。

  2)創建實例:

  磁盤-》分區-》ID修改8e-》保存。

  我用來一個磁盤通過分區來做的。還可以每個磁盤分一個區來做。

  Device Boot Start End Blocks Id System

  /dev/sdb1 1 1306 10490413+ 8e Linux LVM

  /dev/sdb2 1307 2612 10490445 8e Linux LVM

  /dev/sdb3 2613 3918 10490445 8e Linux LVM

  3)創建物理卷:通過pvcreate命令。

  pvcreate /dev/sdb1

  pvcreate /dev/sdb2

  pvcreate /dev/sdb3

  [root@station106 ~]# pvs(查看創建的物理卷)。

  PV VG Fmt Attr PSize PFree

  /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g

  /dev/sdb1 lvm2 a-- 10.00g 10.00g

  /dev/sdb2 lvm2 a-- 10.00g 10.00g

  /dev/sdb3 lvm2 a-- 10.00g 10.00g

  pvdisplay 查看更加詳細的內容。

  [root@station106 ~]# pvdisplay

  --- Physical volume ---

  PV Name /dev/sdb1

  VG Name myvg

  PV Size 10.00 GiB / not usable 4.54 MiB

  Allocatable yes

  PE Size 4.00 MiB

  Total PE 2560

  Free PE 2560

  Allocated PE 0

  PV UUID XP8QfA-WQDd-1v9p-QKxk-AztZ-Nr9e-QbN6Gu

  --- Physical volume ---

  PV Name /dev/sdb2

  VG Name myvg

  PV Size 10.00 GiB / not usable 4.58 MiB

  Allocatable yes

  PE Size 4.00 MiB

  Total PE 2560

  Free PE 2560

  Allocated PE 0

  PV UUID f8Dtdd-fV9e-1yB7-1B8P-21mU-CU2f-fQDAxe

  --- Physical volume ---

  PV Name /dev/sdb3

  VG Name myvg

  PV Size 10.00 GiB / not usable 4.58 MiB

  Allocatable yes

  PE Size 4.00 MiB

  Total PE 2560

  Free PE 2560

  Allocated PE 0

  PV UUID rKNL0x-WXq2-gHYD-T27r-Gf05-a4y3-f4zqzt

  4)創建邏輯卷:通過vgcreate。

  [root@station106 ~]# vgcreate myvg /dev/sdb{1,2,3}

  Volume group “myvg” successfully created

 Linux如何創建使用LVM

  [root@station106 ~]# vgs

  VG #PV #LV #SN Attr VSize VFree

  myvg 3 0 0 wz--n- 30.00g 30.00g

  5)創建邏輯卷,lvcreate。

  [root@station106 ~]# lvcreate -L 10G -n test myvg

  Logical volume “test” created

  -L:空間大小。

  -n:邏輯卷名稱。

  [root@station106 ~]# lvdisplay /dev/myvg/test

  --- Logical volume ---

  LV Path /dev/myvg/test

  LV Name test

  VG Name myvg

  LV UUID wY1YUF-f0o6-jzu1-mtTL-64p0-qvfC-3QfF8y

  LV Write Access read/write

  LV Creation host, time station106.magelinux.com, 2014-02-10 11:18:43 +0800

  LV Status available

  # open 0

  LV Size 10.00 GiB

  Current LE 2560

  Segments 1

  Allocation inherit

  Read ahead sectors auto

  - currently set to 256

  Block device 253:4

  6)掛載使用創建好的邏輯卷。

  mke2fs -t ext4 /dev/myvg/test 格式化。

  mount /dev/myvg/test /mnt/掛載使用。

  7)假如有一天邏輯卷不夠用了。怎麼從卷組擴展呢?如下分析。

  邏輯卷擴展的大致步驟:

  1,先確定擴展的大小,並確保所屬的卷組有足夠的剩余空間。

  我現在要擴展5G,本身是10G的大小。命令如下:

  [root@station106 ~]# lvextend -L +5G /dev/myvg/test

  Extending logical volume test to 15.00 GiB

  Logical volume test successfully resized

  2,擴展物理邊界。物理邊界指的是本身的物理邊緣大小。

  [root@station106 ~]# lvdisplay /dev/myvg/test

  --- Logical volume ---

  LV Path /dev/myvg/test

  LV Name test

  VG Name myvg

  LV UUID wY1YUF-f0o6-jzu1-mtTL-64p0-qvfC-3QfF8y

  LV Write Access read/write

  LV Creation host, time station106.magelinux.com, 2014-02-10 11:18:43 +0800

  LV Status available

  # open 1

  LV Size 15.00 GiB

  Current LE 3840

  Segments 2

  Allocation inherit

  Read ahead sectors auto

  - currently set to 256

  Block device 253:4

  當擴展完物理邊界後你會發現,掛載中的邏輯卷還是本身的大小。

  [root@station106 ~]# df -hT

  Filesystem Type Size Used Avail Use% Mounted on

  /dev/mapper/vg0-root

  ext4 20G 387M 19G 3% /

  tmpfs tmpfs 242M 0 242M 0% /dev/shm

  /dev/sda1 ext4 194M 29M 156M 16% /boot

  /dev/mapper/vg0-usr

  ext4 9.9G 1.9G 7.6G 20% /usr

  /dev/mapper/vg0-var

  ext4 20G 288M 19G 2% /var

  /dev/mapper/myvg-test

  ext4 9.9G 151M 9.2G 2% /mnt

  擴展邏輯邊界,邏輯邊界指的是掛載後,在使用中的邏輯卷也直接識別了。

  [root@station106 ~]# resize2fs /dev/myvg/test

  resize2fs 1.41.12 (17-May-2010)

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

  old desc_blocks = 1, new_desc_blocks = 1

  Performing an on-line resize of /dev/myvg/test to 3932160 (4k) blocks.

  The filesystem on /dev/myvg/test is now 3932160 blocks long.

  [root@station106 ~]# df -hT

  Filesystem Type Size Used Avail Use% Mounted on

  /dev/mapper/vg0-root

  ext4 20G 387M 19G 3% /

  tmpfs tmpfs 242M 0 242M 0% /dev/shm

  /dev/sda1 ext4 194M 29M 156M 16% /boot

  /dev/mapper/vg0-usr

  ext4 9.9G 1.9G 7.6G 20% /usr

  /dev/mapper/vg0-var

  ext4 20G 288M 19G 2% /var

  /dev/mapper/myvg-test

  ext4 15G 153M 14G 2% /mnt

  上述就是擴展邏輯卷部分。

  實例演示:縮小邏輯卷。首先為了測試我去原先掛載的錄下創建一些文件,測試一下縮小後會不會導致文件消失。

  縮小邏輯卷的步驟如下:

  1)先卸載之前的掛載,並執行強制檢測。

  [root@station106 ~]# umount /mnt/

  [root@station106 ~]# e2fsck -f /dev/myvg/test

  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/myvg/test: 13/983040 files (0.0% non-contiguous), 100864/3932160 blocks

  2)縮減邏輯邊界。

  [root@station106 ~]# resize2fs /dev/myvg/test 10G

  resize2fs 1.41.12 (17-May-2010)

  Resizing the filesystem on /dev/myvg/test to 2621440 (4k) blocks.

  The filesystem on /dev/myvg/test is now 2621440 blocks long.

  3)縮減物理邊界。

  [root@station106 ~]# lvreduce -L 10G /dev/myvg/test

  WARNING: Reducing active logical volume to 10.00 GiB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

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

  Reducing logical volume test to 10.00 GiB

  Logical volume test successfully resized

  縮減後查看:

  [root@station106 ~]# lvdisplay /dev/myvg/test

  --- Logical volume ---

  LV Path /dev/myvg/test

  LV Name test

  VG Name myvg

  LV UUID wY1YUF-f0o6-jzu1-mtTL-64p0-qvfC-3QfF8y

  LV Write Access read/write

  LV Creation host, time station106.magelinux.com, 2014-02-10 11:18:43 +0800

  LV Status available

  # open 0

  LV Size 10.00 GiB-》這裡又變成了10G。

  Current LE 2560

  Segments 1

  Allocation inherit

  Read ahead sectors auto

  - currently set to 256

  Block device 253:4

  掛載繼續使用並且查看文件是否存在。

  [root@station106 ~]# mount /dev/myvg/test /mnt/

  [root@station106 ~]# cd /mnt/

  [root@station106 mnt]# ls

  accp benet lost+found--》查看還是存在。未丟失。

  [root@station106 mnt]# df -hT

  Filesystem Type Size Used Avail Use% Mounted on

  /dev/mapper/vg0-root

  ext4 20G 387M 19G 3% /

  tmpfs tmpfs 242M 0 242M 0% /dev/shm

  /dev/sda1 ext4 194M 29M 156M 16% /boot

  /dev/mapper/vg0-usr

  ext4 9.9G 1.9G 7.6G 20% /usr

  /dev/mapper/vg0-var

  ext4 20G 288M 19G 2% /var

  /dev/mapper/myvg-test

  ext4 9.9G 151M 9.2G 2% /mnt

  假設VG卷組有一天,被邏輯卷用完了,怎麼繼續擴展卷組?

  1)創建一個物理卷。

  [root@station106 mnt]# pvcreate /dev/sdb5

  Physical volume “/dev/sdb5” successfully created

  2)把新的物理機加入到卷組中。

  [root@station106 mnt]# vgextend myvg /dev/sdb5

  Volume group “myvg” successfully extended

  查看增加到的卷組。

  [root@station106 mnt]# vgextend myvg /dev/sdb5

  Volume group “myvg” successfully extended

  [root@station106 mnt]# vgdisplay myvg

  --- Volume group ---

  VG Name myvg

  System ID

  Format lvm2

  Metadata Areas 4

  Metadata Sequence No 5

  VG Access read/write

  VG Status resizable

  MAX LV 0

  Cur LV 1

  Open LV 1

  Max PV 0

  Cur PV 4

  Act PV 4

  VG Size 40.00 GiB--》從30G到40G了。

  PE Size 4.00 MiB

  Total PE 10240

  Alloc PE / Size 2560 / 10.00 GiB

  Free PE / Size 7680 / 30.00 GiB

  VG UUID KyFWJg-BU15-KrOA-uef4-Cckf-eoTz-oM38nu

  有擴展卷組,就必須有縮減。縮減過程如下。

  [root@station106 mnt]# vgreduce myvg /dev/sdb5

  Removed “/dev/sdb5” from volume group “myvg”

  [root@station106 mnt]# vgdisplay myvg

  --- Volume group ---

  VG Name myvg

  System ID

  Format lvm2

  Metadata Areas 3

  Metadata Sequence No 6

  VG Access read/write

  VG Status resizable

  MAX LV 0

  Cur LV 1

  Open LV 1

  Max PV 0

  Cur PV 3

  Act PV 3

  VG Size 30.00 GiB

  PE Size 4.00 MiB

  Total PE 7680

  Alloc PE / Size 2560 / 10.00 GiB

  Free PE / Size 5120 / 20.00 GiB

  VG UUID KyFWJg-BU15-KrOA-uef4

  -Cckf-eoTz-oM38nu

  LVM做快照。

  快照是一種邏輯卷,通常為只讀。通俗化說:“快照大小決定於備份的大小”

  作用,能保持快照之前的操作,如後續操作有了影響,可以恢復之前的一些操作,也相當於快照之前做了備份,創建快照後,後續的操作隊快照之前的無任何影響。

  [root@station106 mnt]# lvcreate -s -L 50M -n snap -p r /dev/myvg/test

  Rounding up size to full physical extent 52.00 Mi

  創建的快照名稱snap。

  -L 設置大小。

  -p:permission,設置生成快照的讀寫權限,默認為RW;r為只讀。

  -s:指定lvcreate生成的是一個快照。

  -n:指定快照名稱

  掛載快照:下面是備份的內容。

  mount /dev/myvg/snap /me

  [root@station106 me]# ls

  accp benet lost+found

  然後備份處快照中文件即可,備份後及時刪除快照。

  [root@station106 ~]# lvremove /dev/myvg/snap

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

  Logical volume “snap” successfully removed

  上面就是LVM的創建和使用的介紹了,本文通過幾個實例詳細介紹了LVM的創建,包括邏輯卷的創建、掛載和擴展,希望對你有所幫助。

Copyright © Linux教程網 All Rights Reserved