Red Hat Training
A Red Hat training course is available for Red Hat Enterprise Linux
4.4. 邏輯卷冊管理
此部份描述了執行可進行各種邏輯卷冊管理的各項指令。
4.4.1. 建立邏輯卷冊
若要建立邏輯卷冊,請使用
lvcreate
指令。您可建立線性卷冊(linear volumes)、等量卷冊(striped volumes)以及鏡像卷冊(mirrored volumes),如下列小部份中所示。
若您不為邏輯卷冊指定一組名稱的話,預設的
lvol#
就會被使用,並且 # 為邏輯卷冊的內部號碼。
下列部份提供了您可透過 LVM 建立的三種邏輯卷冊的邏輯卷冊建立範例。
4.4.1.1. 建立線性卷冊
當您建立邏輯卷冊時,邏輯卷冊會由一個使用實體卷冊上可用扇區所組成的卷冊群組來形成。一般來講,邏輯卷冊會使用掉基本實體卷冊上的所有可用空間。修改邏輯卷冊可空出和重新分配實體卷冊中的空間。
下列指令將會在
vg1
卷冊群組中建立大小為 10GB 的邏輯卷冊。
lvcreate -L 10G vg1
下列指令將會在
testvg
卷冊群組中建立一個 1500MB、名為 testlv
的線性卷冊,並建立 /dev/testvg/testlv
這個區塊裝置。
lvcreate -L1500 -n testlv testvg
下列指令會由
vg0
卷冊群組中的可用扇區建立一個 50GB、名為 gfslv
的邏輯卷冊。
lvcreate -L 50G -n gfslv vg0
您可使用
lvcreate
指令的 -l
引數來指定扇區中的邏輯卷冊大小。您也能使用此引數來指定使用於邏輯卷冊的卷冊群組比例。下列指令將會建立一個名為 mylv
的邏輯卷冊,它使用了卷冊群組 testvol
中 60% 的總空間。
lvcreate -l 60%VG -n mylv testvg
您也能使用
lvcreate
指令的 -l
引數來將卷冊群組中的剩下的可用空間比例作為邏輯卷冊的大小。下列指令將會建立一個名為 yourlv
的邏輯卷冊,它將會使用卷冊群組 testvol
中所有未分配的空間。
lvcreate -l 100%FREE -n yourlv testvg
You can use
-l
argument of the lvcreate
command to create a logical volume that uses the entire volume group. Another way to create a logical volume that uses the entire volume group is to use the vgdisplay
command to find the "Total PE" size and to use those results as input to the the lvcreate
command.
下列指令將會建立一個名為
mylv
的邏輯卷冊,該邏輯卷冊會將名為 testvg
的卷冊群組填滿。
#vgdisplay testvg | grep "Total PE"
Total PE 10230 #lvcreate -l 10230 testvg -n mylv
The underlying physical volumes used to create a logical volume can be important if the physical volume needs to be removed, so you may need to consider this possibility when you create the logical volume. For information on removing a physical volume from a volume group, see 節 4.3.6, “由卷冊群組中移除實體卷冊”.
若要建立一個由卷冊群組中的實體卷冊所分配的邏輯卷冊,請在
lvcreate
指令列後方指定實體卷冊或卷冊。下列指令將會在 testvg
卷冊群組中建立一個由實體卷冊 /dev/sdg1
分配並稱為 testlv
的邏輯卷冊。
lvcreate -L 1500 -ntestlv testvg /dev/sdg1
您可指定實體卷冊的哪些扇區可用來作為邏輯卷冊。下列範例透過了卷冊群組
testvg
中的實體卷冊 /dev/sda1
中的扇區 0 至 25,以及實體卷冊 /dev/sdb1
中的扇區 50 至 125 建立了一個線性邏輯卷冊。
lvcreate -l 100 -n testlv testvg /dev/sda1:0-25 /dev/sdb1:50-125
下列範例由
/dev/sda1
實體卷冊的扇區 0 至 25 建立了一個線性邏輯卷冊,然後在扇區 100 繼續編排邏輯卷冊。
lvcreate -l 100 -n testlv testvg /dev/sda1:0-25:100-
The default policy for how the extents of a logical volume are allocated is
inherit
, which applies the same policy as for the volume group. These policies can be changed using the lvchange
command. For information on allocation policies, see 節 4.3.1, “建立卷冊群組”.
4.4.1.2. 建立等量卷冊
For large sequential reads and writes, creating a striped logical volume can improve the efficiency of the data I/O. For general information about striped volumes, see 節 2.3.2, “等量邏輯卷冊”.
當您建立等量邏輯卷冊時,您可透過
lvcreate
指令的 -i
引數來指定磁條的數量。這可決定邏輯卷冊需要被分散至多少實體卷冊上。磁條的數量不可大於卷冊群組中的實體卷冊數量(除非使用了 --alloc anywhere
引數)。
若構成等量邏輯卷冊的基本實體裝置的大小不同的話,等量卷冊的最大大小便會取決於最小的基本裝置。比方說,在一個 two-legged 的磁條中,最大大小將會等於較小裝置大小的兩倍。在一個 three-legged 的磁條中,最大大小則等於最小裝置大小的三倍。
下列指令將會在兩個實體卷冊上建立等量的邏輯卷冊,並且磁帶為 64KB。邏輯卷冊大小為 50GB、名為
gfslv
,並由卷冊群組 vg0
中所分割出。
lvcreate -L 50G -i2 -I64 -n gfslv vg0
就與 linear 卷冊相同,您可指定您將會使用於磁條的實體卷冊的扇區。下列指令將會在兩個實體卷冊上建立一個大小為 100 扇區的等量卷冊,名為
stripelv
並位於卷冊群組 testvg
中。該磁條會使用 /dev/sda1
的磁區 0-50 以及 /dev/sdb1
的磁區 50-100。
# lvcreate -l 100 -i2 -nstripelv testvg /dev/sda1:0-50 /dev/sdb1:50-100
Using default stripesize 64.00 KB
Logical volume "stripelv" created
4.4.1.3. 建立鏡像卷冊
當您要建立鏡像卷冊時,您需要以
lvcreate
指令的 -m
引數來指定複製的資料數量。指定 -m1
的話會建立一個鏡像,並產生出兩份檔案系統:一個 linear 邏輯卷冊和一個副本。相同地,指定了 -m2
的話會建立兩個鏡像,並產生出三份檔案系統。
下列指令會建立一個單鏡像的鏡像的鏡像邏輯卷冊。卷冊大小為 50GB,名為
mirrorlv
,並由卷冊群組 vg0
中所分割出:
lvcreate -L 50G -m1 -n mirrorlv vg0
LVM 鏡像會將要被複製的裝置分為不同的區域,就預設值大小會是 512KB。您可使用
lvcreate
指令的 -R
引數來以 MB 為單位指定區域大小。您亦可藉由編輯 lvm.conf
檔案中的 mirror_region_size
設定來更改預設的區域大小。
注意
基於叢集架構中的限制,大於 1.5TB 的叢集鏡像無法以 512KB 的預設區域大小來建立。需要較大鏡像的使用者應將預設大小增加。若沒有增加區域大小的話將會造成 LVM 的建立程序停擺,並且可能會使其它 LVM 指令也停止運作。
就一般指導綱要來說,當要為大於 1.5TB 的鏡像指定區域大小時,您可將您的鏡像大小以 TB 來進行二進位的四捨五入,並使用這組數字來作為
lvcreate
指令的 -R
引數。比方說,若您的鏡像大小為 1.5TB,您可指定 -R 2
。若您的鏡像大小為 3TB,您可指定 -R 4
。當鏡像大小為 5TB 時,您則能夠指定 -R 8
。
下列指令將會建立區域大小為 2MB 的鏡像邏輯卷冊:
lvcreate -m1 -L 2T -R 2 -n mirror vol_group
LVM 含有一個小型的日誌,它會使用該日誌來記錄哪個區域和鏡像已同步化。就預設值,這個日誌會被存放在磁碟上,使其在系統重新啟動後依然可保留設定。您亦可透過
--corelog
引數來指定將此日誌存放在記憶體中;這可省略使用額外的日誌裝置。不過若要如此,每當系統重新啟動時,整個鏡像就必須被重新同步化。
下列指令將會由
bigvg
卷冊群組建立鏡像邏輯卷冊。該邏輯卷冊名為 ondiskmirvol
並且含有單鏡像。卷冊大小為 12MB,並且會將 mirror log 存放在記憶體中。
# lvcreate -L 12MB -m1 --corelog -n ondiskmirvol bigvg
Logical volume "ondiskmirvol" created
mirror log 會被建立在一個與任何 mirror leg 都會被建立於的裝置區隔開的裝置上。不過您亦可透過使用
vgcreate
指令的 --alloc anywhere
引數來在和其中一個 mirror leg 的裝置相同的裝置上建立一個 mirror log。這可能會使效能降低,不過它卻能讓您建立鏡像,儘管您只有兩個基本裝置。
下列指令會建立一個單鏡像的鏡像邏輯卷冊,它的 mirror log 位於與其中一個 mirror leg 相同的裝置上。在此範例中,
vg0
這個卷冊群組只包含著兩個裝置。這項指令會在 vg0
卷冊群組中建立大小為 500MB、名為 mirrorlv
的鏡像卷冊。
lvcreate -L 500M -m1 -n mirrorlv -alloc anywhere vg0
當鏡像被建立後,鏡像區域會被同步化。對於大型的鏡像元件來說,同步程序可能會花上許多時間。當您要建立一個無須再生的新鏡像時,您可指定
nosync
引數來顯示來自於第一個裝置的初始同步化是非必要的。
您可指定使用來作為 mirror log 和日誌的裝置為何,以及該使用裝置的哪個扇區。若要強制將日誌存放在某個特定磁碟上,請在磁碟上確切指定一個要存放的扇區。LVM 並不一定會遵照指令列中所列出的裝置順序。若有任何實體卷冊被列出的話,那將會是唯一被進行分配的空間。任何包含在清單中並且已被分配的實體扇區都會被忽略掉。
下列指令會建立一個含有單獨鏡像的鏡像邏輯卷冊。卷冊大小為 500MB,名為
mirrorlv
,並且是由卷冊群組 vg0
中所切割出來的。鏡像的第一個 leg 位於 /dev/sda1
裝置上,鏡像的第二個 leg 位於 /dev/sdb1
裝置上,mirror logs 則位於 /dev/sdc1
上。
lvcreate -L 500M -m1 -n mirrorlv vg0 /dev/sda1 /dev/sdb1 /dev/sdc1
下列指令會建立一個含有單獨鏡像的鏡像邏輯卷冊。卷冊大小為 500MB,名為
mirrorlv
,並且是由卷冊群組 vg0
中所切割出來的。鏡像的第一個 leg 位於 /dev/sda1
裝置的扇區 0 至 499 上,鏡像的第二個 leg 位於 /dev/sdb1
裝置的扇區 0 至 499 上,並且 mirror log 則是由 /dev/sdc1
裝置的扇區 0 上起始。這些為大小 1MB 的扇區。若有任何指定的扇區早已被分配的話,它們將會被忽略掉。
lvcreate -L 500M -m1 -n mirrorlv vg0 /dev/sda1:0-499 /dev/sdb1:0-499 /dev/sdc1:0
注意
As of the RHEL 5.3 release, mirrored logical volumes are supported in a cluster. Creating a mirrored LVM logical volume in a cluster requires the same commands and procedures as creating a mirrored LVM logical volume on a single node. However, in order to create a mirrored LVM volume in a cluster the cluster and cluster mirror infrastructure must be running, the cluster must be quorate, and the locking type in the
lvm.conf
file must be set correctly to enable cluster locking. For an example of creating a mirrored volume in a cluster, see 節 5.5, “在叢集中建立鏡像 LVM 邏輯卷冊”.
4.4.1.4. 更改鏡像卷冊配置
您可透過
lvconvert
指令來將一個邏輯卷冊由鏡像卷冊轉換為線性卷冊,或由線性卷冊轉換為鏡像卷冊。您亦可使用這項指令來重新配置現有邏輯卷冊的其它鏡像參數,例如 corelog
。
當您將邏輯卷冊轉換為鏡像卷冊時,您基本上就是在為一個現有的卷冊建立 mirror leg。這代表您的卷冊群組必須包含著 mirror leg 以及 mirror log 的裝置與空間。
If you lose a leg of a mirror, LVM converts the volume to a linear volume so that you still have access to the volume, without the mirror redundancy. After you replace the leg, you can use the
lvconvert
command to restore the mirror. This procedure is provided in 節 6.3, “由 LVM 鏡像錯誤中復原”.
下列指令會將線性邏輯卷冊
vg00/lvol1
轉換為鏡像邏輯卷冊。
lvconvert -m1 vg00/lvol1
下列指令會將鏡像邏輯卷冊
vg00/lvol1
轉換為線性邏輯卷冊,並移除 mirror leg。
lvconvert -m0 vg00/lvol1