LVM(Logical Volume Manager)

April 16, 2017

什麼是 LVM

LVM 可以整合多個實體 partition 在一起, 讓這些 partitions 看起來就像是一個磁碟一樣。還可以在未來新增或移除其他的實體 partition 到這個 LVM 管理的磁碟當中。 by 鳥歌

Physical Volume, PV

利用 fdisk 將劃分的硬碟調整 system ID8e

Volume Group, VG

VG 就是 LVM 組合起來的大磁碟,多個 PV 組合而成。

Logical Volume, LV

最後用來格式化,建立分割區,最後裝置名稱為 /dev/vg_NAME/lv_name

PV、VG、LV 整理

“鳥歌”

實作

/dev/sdb 新增一顆 1GB 硬碟,切分三個 LVM 磁區分別為 100 MB。

以下是變換預設磁區為 LVM

Command (m for help): t
Partition number (1-3, default 3): 1
Partition type (type L to list all types): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): t
Partition number (1-3, default 3): 2
Partition type (type L to list all types): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): t
Partition number (1-3, default 3): 3
Partition type (type L to list all types): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x49871a65

Device     Boot  Start    End Sectors  Size Id Type
/dev/sdb1         2048 206847  204800  100M 8e Linux LVM
/dev/sdb2       206848 411647  204800  100M 8e Linux LVM
/dev/sdb3       411648 616447  204800  100M 8e Linux LVM

建立三組 PV

$ for i in $(seq 1 3) ; do sudo pvcreate /dev/sdb$i; done
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created

掃瞄檢查 PV 狀態

$ sudo pvscan
  PV /dev/sda5   VG ubuntu-vg       lvm2 [19.52 GiB / 916.00 MiB free]
  PV /dev/sdb3                      lvm2 [100.00 MiB]
  PV /dev/sdb1                      lvm2 [100.00 MiB]
  PV /dev/sdb2                      lvm2 [100.00 MiB]
  Total: 4 [19.81 GiB] / in use: 1 [19.52 GiB] / in no VG: 3 [300.00 MiB]
$ sudo pvdisplay /dev/sdb1
  "/dev/sdb1" is a new physical volume of "100.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               100.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               gzH4Bq-9csW-FSRP-jSvy-pOvf-0D0S-W8mVpx

建立 VG

建立一組 VG,名稱為 bossvg 並將 /dev/sdb1/dev/sdb2 加入

$ sudo vgcreate bossvg /dev/sdb1
  Volume group "bossvg" successfully created
$ sudo vgextend bossvg /dev/sdb2
  Volume group "bossvg" successfully extended
$ sudo vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "ubuntu-vg" using metadata type lvm2
  Found volume group "bossvg" using metadata type lvm2
cch@ubuntu:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.52 GiB
  PE Size               4.00 MiB
  Total PE              4997
  Alloc PE / Size       4768 / 18.62 GiB
  Free  PE / Size       229 / 916.00 MiB
  VG UUID               Nc3WGO-0HbI-rht1-YtjR-78be-deZp-Ezc2dM

  --- Volume group ---
  VG Name               bossvg
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               192.00 MiB
  PE Size               4.00 MiB
  Total PE              48
  Alloc PE / Size       0 / 0
  Free  PE / Size       48 / 192.00 MiB
  VG UUID               fPJc75-tztw-Qszi-n9t9-5mfO-Vehn-F2WO9u
$ sudo pvscan
  PV /dev/sda5   VG ubuntu-vg       lvm2 [19.52 GiB / 916.00 MiB free]
  PV /dev/sdb1   VG bossvg          lvm2 [96.00 MiB / 96.00 MiB free]
  PV /dev/sdb2   VG bossvg          lvm2 [96.00 MiB / 96.00 MiB free]
  PV /dev/sdb3                      lvm2 [100.00 MiB]
  Total: 4 [19.80 GiB] / in use: 3 [19.71 GiB] / in no VG: 1 [100.00 MiB]

建立一組 LV

從 bossvg 分出 130 MB 給 bosslv,並格式化,將他 mount/mnt/templv

$ sudo lvcreate --size 130MB -n bosslv bossvg
  Rounding up size to full physical extent 132.00 MiB
  Logical volume "bosslv" created.
$ sudo lvscan
  ACTIVE            '/dev/ubuntu-vg/root' [17.67 GiB] inherit
  ACTIVE            '/dev/ubuntu-vg/swap_1' [976.00 MiB] inherit
  ACTIVE            '/dev/bossvg/bosslv' [132.00 MiB] inherit
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                do4I3J-zub8-7rGz-h0vn-LENG-TlsX-agNYfm
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2018-11-27 21:21:14 +0800
  LV Status              available
  # open                 1
  LV Size                17.67 GiB
  Current LE             4524
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/swap_1
  LV Name                swap_1
  VG Name                ubuntu-vg
  LV UUID                DtptD7-aHhN-oqr1-fhRa-ZkSy-39HN-O13099
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2018-11-27 21:21:15 +0800
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

  --- Logical volume ---
  LV Path                /dev/bossvg/bosslv
  LV Name                bosslv
  VG Name                bossvg
  LV UUID                fb7AXW-sj3u-Ut2r-SGJf-03Kl-dyFB-lUjzkI
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2019-09-01 00:57:10 +0800
  LV Status              available
  # open                 0
  LV Size                132.00 MiB
  Current LE             33
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2

格式化

$ sudo mkfs.ext4 /dev/bossvg/bosslv
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 135168 1k blocks and 33864 inodes
Filesystem UUID: b652f4b2-5903-4c17-a6e1-86d71b41bcfe
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

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

掛載

$ sudo mkdir /mnt/templv
cch@ubuntu:~$ sudo mount /dev/bossvg/bosslv /mnt/templv/
cch@ubuntu:~$ ls /mnt/templv/
lost+found
$ df -Th /mnt/templv/
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/bossvg-bosslv ext4  124M  1.6M  114M   2% /mnt/templv

VG 放大

再新增 30MB 空間

$ sudo lvresize --size +30MB /dev/bossvg/bosslv
  Rounding size to boundary between physical extents: 32.00 MiB
  Size of logical volume bossvg/bosslv changed from 132.00 MiB (33 extents) to 164.00 MiB (41 extents).
  Logical volume bosslv successfully resized.
$ sudo vgdisplay; sudo lvscan
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.52 GiB
  PE Size               4.00 MiB
  Total PE              4997
  Alloc PE / Size       4768 / 18.62 GiB
  Free  PE / Size       229 / 916.00 MiB
  VG UUID               Nc3WGO-0HbI-rht1-YtjR-78be-deZp-Ezc2dM

  --- Volume group ---
  VG Name               bossvg
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               192.00 MiB
  PE Size               4.00 MiB
  Total PE              48
  Alloc PE / Size       41 / 164.00 MiB
  Free  PE / Size       7 / 28.00 MiB
  VG UUID               fPJc75-tztw-Qszi-n9t9-5mfO-Vehn-F2WO9u

  ACTIVE            '/dev/ubuntu-vg/root' [17.67 GiB] inherit
  ACTIVE            '/dev/ubuntu-vg/swap_1' [976.00 MiB] inherit
  ACTIVE            '/dev/bossvg/bosslv' [164.00 MiB] inherit

移除 LVM 流程

  1. 先卸載系統上面的 LVM 檔案系統 (包括快照與所有 LV);
  2. 使用 lvremove 移除 LV ;
  3. 使用 vgchange -a n VGname 讓 VGname 這個 VG 不具有 Active 的標誌;
  4. 使用 vgremove 移除 VG:
  5. 使用 pvremove 移除 PV;
  6. 最後,使用 fdisk 修改 ID 回來啊!

By 鳥歌

參考

鳥歌 LVM