2007年1月21日 星期日

[LVM2] 調整分割區大小

我想有接觸 Linux 的人應該對 LVM 不陌生吧? 以下是 LVM 的一段英文介紹(原文出處):

LVM is an implementation of a logical volume manager for the Linux kernel. It was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.
The installers for the Fedora, SuSE and Debian distributions are LVM-aware and can install a bootable system with a root filesystem on an logical volume (LV).

The LVM can:
  • Resize volume groups (VG) online by absorbing new physical volumes (PV) or ejecting existing ones.
  • Resize LVs online by concatenating extents onto them or truncating extents from them.
  • Create read-only snapshots of LVs (LVM1).
  • Create read-write snapshots of LVs (LVM2).
  • Stripe whole or parts of LVs across multiple PVs, in a fashion similar to RAID0.
  • Move online logical volumes between PVs.
  • Split or merge VGs in situ (as long as no LVs span the split). This can be useful when migrating whole LVs to or from offline storage.

It cannot:

  • Mirror whole or parts of LVs, in a fashion similar to RAID1 or RAID5 mirroring of LVs. For this, it is recommended that one use the Linux software RAID driver to mirror the underlying PVs to achieve redundancy.


其實簡單來說,如果在系統中裝有多顆磁碟機的話,用 LVM 可以將數個實體空間整合為一個虛擬的加總空間,不僅如此,還可在不重新開機的情況下,隨時動態進行調整,實在是一個很方便的工具,只是在損壞復原上似乎有點麻煩,這個之後有研究到再來寫好了......

關於 LVM2,網路上有許多相關的資訊,在中文的部分,推薦下面這個連結給大家參考:

http://linux.vbird.org/somepaper/20050621-LVM2.pdf

裡面有將 LVM 的原理說明的很清楚,包括 Physical Vloume、Volume Group、Physical Extent、Logical Vloume、Logical extent....等觀念,瞭解這些觀念之後,在設定 LVM 的過程中才不會手忙腳亂不知所措。

在我實驗的環境中,所使用的是 Ubuntu,而上述連結中所介紹的是 RHEL,雖然兩者在實做的部分有些許不同,但是 LVM 的觀念還是不變的。

當然在調整容量之前,要檢查一下目前磁碟中是否還有空間可以進行擴充,下達以下指令可進行檢查:
# 檢查目前 PV 情形,藍字的部分是還有使用的空間(顯示sdb1磁碟分割中還有空間可用)
[root@nat ~]# pvscan
PV /dev/sda2 VG VG_root lvm2 [186.03 GB / 0 free]
PV /dev/sdb1 VG VG_root lvm2 [298.06 GB / 203.75 GB free]
Total: 2 [484.09 GB] / in use: 2 [484.09 GB] / in no VG: 0 [0 ]

# 檢查目前 LV 情形,藍字的部分是 LV 的名稱,到時候擴充空間時會用到
[root@nat ~]# lvscan
ACTIVE '/dev/VG_root/LV_root' [3.91 GB] inherit
ACTIVE '/dev/VG_root/LV_dw' [275.44 GB] inherit
ACTIVE '/dev/VG_root/LV_swap' [1.00 GB] inherit

以下分別介紹一下在兩種不同的環境下,調整分割區大小的方式:

以下為假設情形:

欲調整 /home 的分割區,將其加大 5GB,假設 /home 的分割區的 LV 位置為 /dev/VG_root/LV_home


RHEL

# 加大 LV(LV_home)容量 5GB
lvextend -L +5000M /dev/VG_root/LV_home

# 調整剛設定好的容量大小
ext2online /dev/VG_root/LV_home


Ubuntu

Ubuntu 的部分就比較困擾一點了,因為系統不允許直接針對已將掛載的分割區進行調整....(這是螢幕上顯示的訊息,搞不好可以,只是我沒找到相關資料....)

當時所顯示的錯誤訊息如下:
/dev/VG_root/LV_home is mounted; can’t resize a mounted filesystem!

因此,我的作法是先到 /etc/fstab 中,把 /home 分割區的掛載先作註解,重新開機後,再進行以下動作:

sudo lvextend -L +5000M /dev/VG_root/LV_home

# 會強制要求進行磁碟檢查
sudo e2fsck -f /dev/VG_root/LV_home

# 此處的指令跟 RHEL 就有落差了
sudo resize2fs /dev/VG_root/LV_home


經過上述的動作就大功告成囉!! 下 "df -h" 就可以看見 /home 分割區增加了 5GB 的空間了!


參考資料

沒有留言:

張貼留言