LVM 邏輯磁區管理員
實體裝置 (Block Devices) --(pvcreate)-->Physical Volumes--(vgcreate)-->Volume Group--(lvcreate)-->Logical Volumes
實體裝置先用 pvcreate 將之轉換成實體磁區 (Physical Volumes),再由一個或多個實體磁區藉由 vgcreate 指令組成磁區群組 (Volume Group),當我們要使用磁區的時候,再由磁區群組中以 lvcreate 指令切割出邏輯磁區 (Logical Volumes) 使用。
==============================
建立實體磁區 (Physical Volumes)
先將磁碟分割的旗標設定為 LVM (8e):
# parted /dev/sdb print (先看一下原來的旗標)
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 旗標
1 32.3kB 107MB 107MB 主分割區
2 107MB 214MB 107MB 主分割區
3 214MB 321MB 107MB 主分割區
4 321MB 10.7GB 10.4GB 擴充分割區
5 321MB 421MB 100MB 邏輯分割區 raid
6 421MB 521MB 100MB 邏輯分割區 raid
7 521MB 621MB 100MB 邏輯分割區 raid
8 621MB 721MB 100MB 邏輯分割區 raid
9 721MB 821MB 100MB 邏輯分割區 raid
10 821MB 1000MB 179MB 邏輯分割區 raid
資訊: 如果必要,不要忘記更新 /etc/fstab。
# parted /dev/sdb set 5 lvm on (將之設定為 lvm)
# parted /dev/sdb set 6 lvm on
# parted /dev/sdb set 7 lvm on
# parted /dev/sdb set 8 lvm on
# parted /dev/sdb set 9 lvm on
# parted /dev/sdb set 10 lvm on
# partprobe (設好後 partprobe 一下,讓磁碟分割的資訊重載入記憶體)
# parted /dev/sdb print (看一下成果)
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 旗標
1 32.3kB 107MB 107MB 主分割區
2 107MB 214MB 107MB 主分割區
3 214MB 321MB 107MB 主分割區
4 321MB 10.7GB 10.4GB 擴充分割區
5 321MB 421MB 100MB 邏輯分割區 lvm
6 421MB 521MB 100MB 邏輯分割區 lvm
7 521MB 621MB 100MB 邏輯分割區 lvm
8 621MB 721MB 100MB 邏輯分割區 lvm
9 721MB 821MB 100MB 邏輯分割區 lvm
10 821MB 1000MB 179MB 邏輯分割區 lvm
資訊: 如果必要,不要忘記更新 /etc/fstab。
接下來將磁碟分割 5, 6, 7, 8, 9, 10 規劃成實體磁區 (Physical Volumes, PV):
# pvscan (先查看一下現在的 PV 狀況)
PV /dev/sda2 VG VolGroup00 lvm2 [19.88 GB / 0 free] (只有 /dev/sda2 被規劃成 PV)
Total: 1 [19.88 GB] / in use: 1 [19.88 GB] / in no VG: 0 [0 ]
# pvdisplay (另一種查詢 PV 狀況的工具指令)
— Physical volume —
PV Name /dev/sda2
VG Name VolGroup00
PV Size 19.89 GB / not usable 19.49 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 636
Free PE 0
Allocated PE 636
PV UUID 094knT-cjEq-QmX0-max9-02AA-6JhB-eHA5JL
將要設定成 LVM 的分割區設定成 PV:
# pvcreate /dev/sdb5
Physical volume “/dev/sdb5” successfully created
# pvcreate /dev/sdb6
Physical volume “/dev/sdb6” successfully created
# pvcreate /dev/sdb7
Physical volume “/dev/sdb7” successfully created
# pvcreate /dev/sdb8
Physical volume “/dev/sdb8” successfully created
# pvcreate /dev/sdb9
Physical volume “/dev/sdb9” successfully created
# pvcreate /dev/sdb10
Physical volume “/dev/sdb10” successfully created
# pvscan (再查詢一下 PV 狀況)
PV /dev/sda2 VG VolGroup00 lvm2 [19.88 GB / 0 free]
PV /dev/sdb5 lvm2 [95.54 MB]
PV /dev/sdb6 lvm2 [95.37 MB]
PV /dev/sdb7 lvm2 [95.37 MB]
PV /dev/sdb8 lvm2 [95.37 MB]
PV /dev/sdb9 lvm2 [95.37 MB]
PV /dev/sdb10 lvm2 [170.71 MB]
Total: 7 [20.51 GB] / in use: 1 [19.88 GB] / in no VG: 6 [647.72 MB]
# pvdisplay (再查詢一下 PV 狀況)
— Physical volume —
PV Name /dev/sda2
VG Name VolGroup00
PV Size 19.89 GB / not usable 19.49 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 636
Free PE 0
Allocated PE 636
PV UUID 094knT-cjEq-QmX0-max9-02AA-6JhB-eHA5JL
— NEW Physical volume —
PV Name /dev/sdb5
VG Name
PV Size 95.54 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID f7MtqR-cYWF-f1ML-nsgk-0zE3-Cp3x-wYPX41
— 略—
— 略—
— 略—
— NEW Physical volume —
PV Name /dev/sdb10
VG Name
PV Size 170.71 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 5csVCq-j2Q9-sfxI-OL2z-qra0-tKoC-8IaFsP
==============================
組成磁區群組 (Volume Group)
有了實體磁區 PV 後,就可以拿來組合成磁區群組 (Volume Group, VG):
# vgscan (查看一下現在 VG 的狀況)
Reading all physical volumes. This may take a while…
Found volume group “VolGroup00” using metadata type lvm2
# vgdisplay (另一種查看 VG 狀況的指令)
— Volume group —
VG Name VolGroup00
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.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 636 / 19.88 GB
Free PE / Size 0 / 0
VG UUID 9mRM1x-cj2z-cZjO-ogfs-yK58-o1na-vitn7z
開始建立 VG:
# vgcreate myVG /dev/sdb5 /dev/sdb6 /dev/sdb7 (建立一個名為 myVG 的磁區群組,加入 /dev/sdb5 /dev/sdb6 /dev/sdb7 為這個 VG 的成員)
Volume group “myVG” successfully created
# vgscan (看一下狀態)
Reading all physical volumes. This may take a while…
Found volume group “myVG” using metadata type lvm2
Found volume group “VolGroup00” using metadata type lvm2
# vgdisplay (看一下狀態)
— Volume group —
VG Name myVG
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 276.00 MB
PE Size 4.00 MB
Total PE 69
Alloc PE / Size 0 / 0
Free PE / Size 69 / 276.00 MB
VG UUID zVNzlO-cYJH-G3W1-a569-Eig7-b6JB-257ACY
— 略 —
如果要加入 VG 成員,就用 vgextend 來加入:
# vgextend myVG /dev/sdb8 /dev/sdb9 /dev/sdb10 (將 /dev/sdb8 /dev/sdb9 /dev/sdb10 加入到 myVG 這個群組中作為成員)
Volume group “myVG” successfully extended
# vgscan (看一下)
Reading all physical volumes. This may take a while…
Found volume group “myVG” using metadata type lvm2
Found volume group “VolGroup00” using metadata type lvm2
# vgdisplay (查詢一下)
— Volume group —
VG Name myVG
System ID
Format lvm2
Metadata Areas 6
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 6
Act PV 6
VG Size 628.00 MB
PE Size 4.00 MB
Total PE 157
Alloc PE / Size 0 / 0
Free PE / Size 157 / 628.00 MB
VG UUID zVNzlO-cYJH-G3W1-a569-Eig7-b6JB-257ACY
— 略 —
也可以用 vgreduce 將其中的 PV 成員從 VG 中拿掉:
# pvmove /dev/sdb10 (先用 pvmove 將 /dev/sdb10 裡的資料移動到其他磁區)
/dev/sdb10: Moved: 100.0%
# vgreduce myVG /dev/sdb10 (將 /dev/sdb10 從 myVG 中拿掉)
Removed “/dev/sdb10” from volume group “myVG”
# vgdisplay (查詢一下)
— Volume group —
VG Name myVG
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 5
Act PV 5
VG Size 460.00 MB
PE Size 4.00 MB
Total PE 115
Alloc PE / Size 0 / 0
Free PE / Size 115 / 460.00 MB
VG UUID zVNzlO-cYJH-G3W1-a569-Eig7-b6JB-257ACY
— 略 —
如果要將 myVG 移除,可用 vgremove 指令來達成:
# vgremove myVG (移除 myVG 這個磁區群組)
Volume group “myVG” successfully removed
# vgscan (看一下)
Reading all physical volumes. This may take a while…
Found volume group “VolGroup00” using metadata type lvm2 (myVG 不見了)
==============================
建立邏輯磁區 (Logical Volumes)
磁區群組 VG 建立之後,就可以在其上分割出想要的邏輯磁區 (Logical Volumes, LV):
# lvscan (先查看一下 LV 的狀況)
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
# lvcreate -L 200M -n myLV01 myVG (從 myVG 建立一個大小 -L 為 200M 名稱 -n 為 myLV01 的邏輯磁區)
Logical volume “myLV01” created
# lvscan (查詢一下建立結果)
ACTIVE ‘/dev/myVG/myLV01’ [200.00 MB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
# lvdisplay (用 lvdisplay 看一下細節)
— Logical volume —
LV Name /dev/myVG/myLV01
VG Name myVG
LV UUID qV5XcR-646V-PdNr-gvBv-6Rso-2QzT-lbpI1M
LV Write Access read/write
LV Status available
# open 0
LV Size 200.00 MB
Current LE 50
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:2
— 略 —
— 略 —
將 myLV01 製作成 ext3 檔案系統:
# mke2fs -j /dev/myVG/myLV01 (製作檔案系統成 ext3)
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
將之掛載起來:
# mkdir /mnt/lv01 (先建立掛載點)
# mount /dev/myVG/myLV01 /mnt/lv01 (掛載起來)
# touch /mnt/lv01/test.txt (新增一個檔案試試看)
# ls /mnt/lv01 (列出內容)
lost+found test.txt
# df -h (查詢一下磁碟狀況)
檔案系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
18G 5.8G 11G 35% /
/dev/sda1 99M 22M 72M 24% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/myVG-myLV01
194M 5.6M 179M 4% /mnt/lv01
當 myLV01 有擴充需求時,可用 lvextend 來作擴充,然後用 resize2fs 來為擴充的部份製作 ext3 檔案系統:
# lvextend -L +200M /dev/myVG/myLV01 (擴充 /dev/myVG/myLV01,+200M 為增加 200MB,如果沒有 + 號,則是成為指定為 200MB )
Extending logical volume myLV01 to 400.00 MB
Logical volume myLV01 successfully resized
# resize2fs /dev/myVG/myLV01 (為擴充部份製作檔案系統)
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/myVG/myLV01 is mounted on /mnt/lv01; on-line resizing required
Performing an on-line resize of /dev/myVG/myLV01 to 409600 (1k) blocks.
The filesystem on /dev/myVG/myLV01 is now 409600 blocks long.
# df -h (檢查一下磁碟內容)
檔案系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
18G 5.8G 11G 35% /
/dev/sda1 99M 22M 72M 24% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/myVG-myLV01
388M 6.3M 362M 2% /mnt/lv01
如果 LV 有減少磁碟空的需求時,可以用 lvreduce 來製作:
# umount /mnt/lv01 (先將掛載點 umount)
# e2fsck -f /dev/myVG/myLV01 (檔案系統要先檢查)
e2fsck 1.39 (29-May-2006)
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/myLV01: 12/102400 files (8.3% non-contiguous), 15238/409600 blocks
# resize2fs /dev/myVG/myLV01 300M (先用 resize2fs 來改變大小)
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/myVG/myLV01 to 307200 (1k) blocks.
The filesystem on /dev/myVG/myLV01 is now 307200 blocks long.
# lvreduce -L 300M /dev/myVG/myLV01 (再用 lvreduce 來設定 myLV01 的大小,其中磁碟大小要和 resize2fs 設定的一樣大)
WARNING: Reducing active logical volume to 300.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myLV01? [y/n]: y (確定減少)
Reducing logical volume myLV01 to 300.00 MB
Logical volume myLV01 successfully resized
# mount /dev/myVG/myLV01 /mnt/lv01/ (重新把它 mount 起來)
# df -h (檢查一下)
檔案系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
18G 5.8G 11G 35% /
/dev/sda1 99M 22M 72M 24% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/myVG-myLV01
291M 2.1M 274M 1% /mnt/lv01
# lvdisplay (看一下 LV 的狀態)
— Logical volume —
LV Name /dev/myVG/myLV01
VG Name myVG
LV UUID qV5XcR-646V-PdNr-gvBv-6Rso-2QzT-lbpI1M (LV 的編號並沒有改變)
LV Write Access read/write
LV Status available
# open 1
LV Size 300.00 MB
Current LE 75
Segments 3
Allocation inherit
Read ahead sectors 0
Block device 253:2
— 略 —
— 略 —
移除 LV 磁區,可使用 lvremove 指令:
# lvscan (先看一下內容)
ACTIVE ‘/dev/myVG/myLV01’ [300.00 MB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
# df -h (檢查一下是否有掛載)
檔案系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
18G 5.8G 11G 35% /
/dev/sda1 99M 22M 72M 24% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/myVG-myLV01
291M 11M 266M 4% /mnt/lv01
# umount /mnt/lv01 (將掛載的 LV 磁區卸載)
# lvremove /dev/myVG/myLV01 (將 LV 磁區移除)
Do you really want to remove active logical volume “myLV01”? [y/n]: y
Logical volume “myLV01” successfully removed
# df -h (檢查一下)
檔案系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
18G 5.8G 11G 35% /
/dev/sda1 99M 22M 72M 24% /boot
tmpfs 506M 0 506M 0% /dev/shm
# lvscan (檢查一下)
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
==============================
邏輯磁區管理員快照 (LVM Snapshots)
在 LVM 中可以使用快照功能,來作為備份、系統測試、或虛擬機器使用:
# touch /mnt/lv01/lv01.txt (先建一個文件以供測試用)
# ls -l /mnt/lv01 (看一下原來的磁碟內容)
總計 13
drwx—— 2 root root 12288 4月 5 20:55 lost+found
-rw-r–r– 1 root root 0 4月 5 21:19 lv01.txt
# lvscan (先看一下原來的 LV 內容)
ACTIVE ‘/dev/myVG/myLV01’ [300.00 MB] inherit (建一個快照和它一樣好了)
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
# lvcreate -L 300M -p r -s -n mySnapshots /dev/myVG/myLV01 (建立快照,-L 300M 設定快照的大小、-p r 指定權限 permission 為唯讀、-s 指定建立的是快照、-n MY_NAME 指定這個快照的名稱、最後帶上要建立快照的 LV 裝置)
Logical volume “mySnapshots” created
# lvscan (看一下建立後的 LV 內容)
ACTIVE Original ‘/dev/myVG/myLV01’ [300.00 MB] inherit (多了 Original 標籤)
ACTIVE Snapshot ‘/dev/myVG/mySnapshots’ [300.00 MB] inherit (增加了 Snapshot 這個 LV 磁區)
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit
將它掛載起來:
# mkdir /mnt/snap (建立一個掛載點)
# mount -o ro /dev/myVG/mySnapshots /mnt/snap/ (以 ro 唯讀的方式掛載)
# ls -l /mnt/snap (看一下快照磁區的內容,和原先的一樣)
總計 13
drwx—— 2 root root 12288 4月 5 20:55 lost+found
-rw-r–r– 1 root root 0 4月 5 21:19 lv01.txt
移除快照:
# umount /mnt/snap/ (先將之卸載)
# lvremove /dev/myVG/mySnapshots (移除 LV 快照裝置)
Do you really want to remove active logical volume “mySnapshots”? [y/n]: y
Logical volume “mySnapshots” successfully removed
# lvscan (檢查一下)
ACTIVE ‘/dev/myVG/myLV01’ [300.00 MB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00’ [17.94 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit