Skip to content

Instantly share code, notes, and snippets.

@xxrjun
Last active November 14, 2023 15:21
Show Gist options
  • Save xxrjun/863b9a9733b20da1905d8223b38770cd to your computer and use it in GitHub Desktop.
Save xxrjun/863b9a9733b20da1905d8223b38770cd to your computer and use it in GitHub Desktop.
了解LVM

LVM (Logical Volume Manager)

紀錄自己學習LVM

LVM 的優點

  • 方便調整 Logical Volume 的大小
  • 可以安裝任何 Filesystem 在 Logical Volume 上
  • 易餘替代故障的 Disk

LVM 的架構

LVM-Diagram LVM 架構圖片來源: Arch Linux Tutorial – Configure LVM on Arch Linux 2017

LVM(Logical Volume Manager) 架構下,物理硬碟空間(Physical Drives)會被劃分成一個或多個分割區(Partitions)Volume Group(VG) 是一種儲存池(Storage Pool),會包含一個或多個 Physical Volumes,其大小也會隨著所有包含的 Physical Volumes 之大小而有所不同。

輸入 lsblk 指令可以列出系統可用的 Block Device。以下範例中 sda 是硬碟(對應到 Physical Drives),sda1 - sda3 是分割區(Partitions)。

$ lsblk
NAME                 MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                    8:0    0    80G  0 disk
├─sda1                 8:1    0     1M  0 part
├─sda2                 8:2    0     2G  0 part /boot
└─sda3                 8:3    0    78G  0 part
  └─ubuntu--vg-ubuntu--lv

輸入 pvdisplay 可以查看系統中的 Physical Volume。其中

  • PV Name 為 Physical Volume 的名稱(在這同分割區名稱)
  • PV Size 為 Physical Volume 的大小(在這同分割區大小)。
$ pvdisplay
--- Physical volume ---
PV Name               /dev/sda3
VG Name               ubuntu-vg
PV Size               <78.00 GiB / not usable 16.50 KiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              19967
Free PE               12800
Allocated PE          7167
PV UUID               g6WsDR-nJP3-YYYv-72b7-wRQB-51bs-G2Kj4A

實際上一個 Physical Volume 可以包含一至多個 Physical Partition。圖片來源: IBM

partition and physical volume

使用 vgdisplay 可以查看系統中的 Volume Group。其中

  • VG Name 為 Volume Group 的名稱
  • VG Size 為 Volume Group 的大小
  • Free PE 為 Volume Group 中可用的 Physical Extent 數量。

以下範例中因為此 Volume Group 只有一個 Physical Volume /dev/sda3 ,所以 VG SizePV Size 相同。

$ vgdisplay
--- Volume group ---
VG Name               ubuntu-vg
System ID
Format                lvm2
Metadata Areas        1
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                1
Act PV                1
VG Size               <78.00 GiB
PE Size               4.00 MiB
Total PE              19967
Alloc PE / Size       19967 / <78.00 GiB
Free  PE / Size       0 / 0
VG UUID               xVsU8T-uO85-oDID-5809-pWVu-bGfH-2snBHs

Logical Volume(LV) 是在 Volume Group 上創建出的邏輯分割區。使用 lvdisplay 可以查看系統中的 Logical Volume。其中

  • LV Name 為 Logical Volume 的名稱
  • VG Name 為 Logical Volume 所屬的 Volume Group
  • LV Size 為 Logical Volume 的大小
$ lvdisplay
--- Logical volume ---
LV Path                /dev/ubuntu-vg/ubuntu-lv
LV Name                ubuntu-lv
VG Name                ubuntu-vg
LV UUID                GaPUCO-zGvo-Zbvs-ep37-zKtP-5xH3-Xq61R2
LV Write Access        read/write
LV Creation host, time ubuntu-server, 2023-03-08 14:59:10 +0000
LV Status              available
# open                 1
LV Size                <78.00 GiB
Current LE             19967
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

為了讓使用者能夠更方便地進行資料管理與使用,可以將每個 Logical Volume 都視為一個獨立的硬碟,並將 檔案系統(Filesystem) 安裝在 Logical Volume 上。使用 df -h 可以查看檔案系統的使用情況,如以下範例

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              795M  1.1M  794M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G   13G   61G  18% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  130M  1.7G   8% /boot
tmpfs                              795M  4.0K  795M   1% /run/user/1000

參考資料(References)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment