歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

LVM邏輯管理器(Logical volume Manager)

由於本人是linux小白,現在在自學linux目前學到了LVM就把LVM的實驗步驟整理了一下,有什麼錯誤請大家多多指點!注:以下內容借鑒鳥哥的linux私房菜 LVM的重點在於可以彈性的調整文件系統的容量!而並非在於性能與數據保全上面。需要文件的讀寫性能可以使用RAID。LVM可以整個多個物理分區,讓這些分區看起來就像一個磁盤一樣。而且還可以在將來的其他的物理分區或將其從這個LVM上刪除。

1.1 什麼是LVMPV,PE,VG,LV的意義LVM的全名是Logical Volume Manager ,中文可以翻譯為邏輯管理器。之所以成為“卷”可能是因為可以將文件系統向卷一樣伸長或縮短之故吧!LVM的做法是將幾個物理的分區(或磁盤)通過軟件組合成為一塊看起來獨立的大磁盤(VG),然後將這些大磁盤在經過分成可以使用的分區(LV),最終就就夠掛載使用了。但是為什麼這樣的系統可以進行擴充或縮小呢?其實與一個稱為PE的選項有關。Physical Volume,PV,物理卷

我們實際的分區需要調整系統標識符(system ID)成為8e(LVM的標識符),然後再通過pvcreate的命令將它轉化為LVM最底層的物理卷(PV),之後才能將這些PV加以利用,調整systemID的方法就是使用fdisk工具

Volume Group,VG,卷組

所謂的LVM大磁盤就是將許多PV整合成這個VG,所以VG就是LVM組合起來的大磁盤。這個大磁盤默認的最大容量是256GB,該結果與PE的大小有關,每個VG最多可以包含65534個PE,而每個PE的默認大小是4MB,一次默認的LVMVG會有4M*65534/(1024M/G)=256GB。PE是LVM中最小的存儲單位,我們的文件數據都是由PE來處理的,簡單地說PE就像文件系統裡的block大小。所以調整PE的大小會影響到VG的最大容量

Logical Volume,LV,邏輯卷

最終的VG還會被切成LV,這個LV就是最後可以被格式化使用的類型分區率。LV不可以隨意指定大小。既然PE是整個LVM對的最小存儲單位,那麼LV的大小就與再次LV內的PE總數有關。為了方便用戶利用LVM來管理其系統,因此LV的設備文件通常指定為“/dev/VGNAME/LVNAME 注:VGNAME是VG的名稱;LVNAME是LV的名稱”的樣式。

1.2 LVM的實現流程1、 先將磁盤通過fdisk命令進行分區

2、 將新建好的分區建為PV格式

3、 將全部的分區整個成一個VG,將VG的名稱設置為vbirdv;且PE大小為16MB

4、 最終將VG容量都丟給LV,LV的名稱設置為vbirdlv

5、 最終將LV格式化為ext3的文件系統,並掛載到/mnt/lvm下

1.3 實驗步驟× 創建分區

[root@localhost ~]# fdisk /dev/sdb 注:使用fdisk工具對/dev/sdb這塊磁盤進行分區,由於筆者電腦中只有兩塊磁盤,本次實驗將使用不同的分區代替磁盤。

Command(m for help): n

Command action l logical (5 or over) p primary partition (1-4)lFirst cylinder (1963-3917, default 1963):Using default value 1963Last cylinder, +cylinders or +size{K,M,G} (1963-3917, default 3917):+5G 如上示例創建sdb5、sdb6、sdb7、sdb8四個分區(可以根據自己的習慣創建分區筆者使用的是擴展分區)

更改system IDCommand(m for help): t

Partitionnumber (1-8): 5

Hex code(type L to list codes): 8e

Changedsystem type of partition 8 to 8e (Linux LVM)

Command(m for help): t

Partition number (1-8): 6Hex code(type L to list codes): 8e

Changedsystem type of partition 8 to 8e (Linux LVM)

Command(m for help): t

Partition number (1-8): 7Hex code(type L to list codes): 8e

Changedsystem type of partition 8 to 8e (Linux LVM)

Command(m for help): t

Partition number (1-8): 8Hex code(type L to list codes): 8e

Changedsystem type of partition 8 to 8e (Linux LVM)

注:將分區類型改為8e(其實沒有設置成8e也沒關系,不過某些LVM的檢測命令可能會檢測不到該分區。)

Command(m for help): p

Disk/dev/sdb: 107.4 GB, 107374182400 bytes

255heads, 63 sectors/track, 13054 cylinders

Units =cylinders of 16065 * 512 = 8225280 bytes

Sectorsize (logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Diskidentifier: 0x74429a75

Device Boot Start End Blocks Id System

/dev/sdb1 1 3917 31463271 5 Extended

/dev/sdb5 1 393 3156709+ 8e Linux LVM

/dev/sdb6 394 1308 7349706 8e Linux LVM

/dev/sdb7 1309 1962 5253223+ 8e Linux LVM

/dev/sdb8 1963 2616 5253223+ 8e Linux LVM

Command(m for help): w

The partitiontable has been altered!

Callingioctl() to re-read partition table.

Syncingdisks.

使用p命令查看創建好的分區,確認無誤後按w保存並退出。

[root@localhost~]# partprobe 注:強制讓內核重新找一次分區表

[root@localhost~]# fdisk -l

Disk/dev/sdb: 107.4 GB, 107374182400 bytes

255heads, 63 sectors/track, 13054 cylinders

Units =cylinders of 16065 * 512 = 8225280 bytes

Sectorsize (logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Diskidentifier: 0x74429a75

Device Boot Start End Blocks Id System

/dev/sdb1 1 3917 31463271 5 Extended

/dev/sdb5 1 393 3156709+ 8e Linux LVM

/dev/sdb6 394 1308 7349706 8e Linux LVM

/dev/sdb7 1309 1962 5253223+ 8e Linux LVM

/dev/sdb8 1963 2616 5253223+ 8e Linux LVM

1.4、PV階段

pv:physical volume(實際額)

1)創建pv

pvcreate

2)刪除pv

pvremove

3)掃描pv

pvscan

4)查看pv

pvdisplay

5)移動pv

pvmove

檢查系統上是否存在PV,然後將/dev/sdb5~8新建成為PV格式[root@localhost~]# pvscan

PV /dev/sda2 VGVolGroup lvm2 [39.51 GiB / 0 free] Total:1 [39.51 GiB] / in use: 1 [39.51 GiB] / in no VG: 0 [0 ] 注:這個PV創建系統的時候自帶的,與本次實驗無關 創建PV[root@localhost ~]# pvcreate/dev/sdb{5,6,7,8} Physicalvolume "/dev/sdb5" successfully created Physicalvolume "/dev/sdb6" successfully created Physicalvolume "/dev/sdb7" successfully created Physicalvolume "/dev/sdb8" successfully created 注:這個命令可以一下創建多個PV比單個穿件簡便不少 [root@localhost ~]# pvscan PV/dev/sda2 VG VolGroup lvm2 [39.51 GiB / 0 free] PV/dev/sdb5 lvm2 [3.01GiB] PV/dev/sdb6 lvm2 [7.01GiB] PV/dev/sdb7 lvm2 [5.01 GiB] PV/dev/sdb8 lvm2 [5.01GiB]Total: 5[59.55 GiB] / in use: 1 [39.51 GiB] / in no VG: 4 [20.04 GiB]

這就分別顯示每個PV的信息與系統所有的PV信息。尤其是最有一行,顯示的是:整體PV的量/已經被使用到VG的量/剩余的PV量 更詳細的顯示PV信息[root@localhost~]# pvdisplay

[root@localhost ~]# pvdisplay

--- Physical volume ---

PV Name /dev/sda2

VG Name VolGroup

PV Size 39.51 GiB / not usable 3.00 MiB

Allocatable yes (but full)

PE Size 4.00 MiB

Total PE 10114

Free PE 0

Allocated PE 10114

PV UUID C8Soc4-4V1F-I3LI-YyJr-KnEP-urtx-xXpKjh

"/dev/sdb5" is a newphysical volume of "3.01 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb5

VG Name

PV Size 3.01 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID IYkvra-xyFA-mB1U-7wvs-fhf4-I4Qg-88C3mG

"/dev/sdb6" is a newphysical volume of "7.01 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb6 ←實際的分區設備名稱

VG Name ←因為尚未分配出去所以空白

PV Size 7.01 GiB ←就是容量的說明

Allocatable NO ←是否已被分配

PE Size 0 ←在此PV內的PE大小

Total PE 0 ←共分出幾個PE

Free PE 0 ←沒被LV用掉的PE

Allocated PE 0 ←尚可分配出去的PE數量

PV UUID gErjmF-t2Wb-jFI4-qLZ4-4lze-1oU9-O7HdRb

"/dev/sdb7" is a newphysical volume of "5.01 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb7

VG Name

PV Size 5.01 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID CqHwxm-ZfFT-KHju-SkS0-bXNN-Q70X-hYkjb0

"/dev/sdb8" is a newphysical volume of "5.01 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb8

VG Name

PV Size 5.01 GiB

Allocatable NO

PE Size 0

TotalPE 0

Free PE 0

Allocated PE 0

PV UUID ty3uai-DtDD-h2QC-n01u-psKJ-7Blf-cvQAiL

注:由於PE是新建VG的時候才給予的參,因此在這裡看到的PV裡面PE會是0

而且也沒有多余的PE可供分配(allocatable)

1.5、VG階段

g:volume group(卷組)

1)創建vg

Vgcreate 2)刪除vg

vgremove

3)掃描vg

vgscan

4)查看vg

vgdisplay

5)移動vg

gmove

6)擴展vg

vgextend

7)縮減vg

vgreduce

與VG不同,PV的名稱是自定義的!我們知道PV的名稱其實就是分區的設備文件名,但是這個VG名稱則可以隨便你自己取。在下面例子當中將VG取名為vbirdvg。新建這個VG的流程是這樣的。 創建VG[root@localhost ~]# vgcreate -s 16Mvbirdvg /dev/sdb{5,6,7}

Volumegroup "vbirdvg" successfully created

以上字段的意思為將/dev/sdb5-7創建為一個VG,且指定PE大小為16MB。

注:-s後面跟的是PE的大小(size),單位可以是M、G、T(大小寫均可)。

[root@localhost ~]# vgscan

Readingall physical volumes. This may take awhile...

Foundvolume group "vbirdvg" using metadata type lvm2

Foundvolume group "VolGroup" using metadata type lvm2

注:以上信息可以看出vbirdvg已經創建成功

[root@localhost ~]# pvscan

PV/dev/sdb5 VG vbirdvg lvm2 [3.00 GiB / 3.00 GiB free]

PV/dev/sdb6 VG vbirdvg lvm2 [7.00 GiB / 7.00 GiB free]

PV/dev/sdb7 VG vbirdvg lvm2 [5.00 GiB / 5.00 GiB free]

PV/dev/sda2 VG VolGroup lvm2 [39.51 GiB / 0 free]

PV/dev/sdb8 lvm2 [5.01GiB]

Total:5 [59.52 GiB] / in use: 4 [54.51 GiB] / in no VG: 1 [5.01 GiB]

注:從以上信息中可以看出有三個PV被占用,有一個空閒(/dev/sdb8)

更詳細的顯示出VG的信息[root@localhost ~]# vgdisplay

---Volume group ---

VGName vbirdvg

SystemID

Format lvm2

MetadataAreas 3

MetadataSequence No 1

VGAccess read/write

VGStatus resizable

MAXLV 0

CurLV 0

OpenLV 0

MaxPV 0

CurPV 3

ActPV 3

VGSize 15.00 GiB ←整體的VG容量有這麼大

PESize 16.00 MiB ←內部每個PE的大小

Total PE 960 ←總共的PE數量共有這麼多Alloc PE / Size 0 / 0

Free PE / Size 960 / 15.00 GiBVGUUID QaQ6X4-U9RP-wW7m-bgWA-UJvY-4Kg5-6jmvye

注:深色三行指的是PE能夠使用的情況!由於尚未創建LV,因此所有的PE均可自由使用。

---Volume group ---

VGName VolGroup

SystemID

Format lvm2

MetadataAreas 1

MetadataSequence No 3

VGAccess read/write

VGStatus resizable

MAXLV 0

CurLV 2

OpenLV 2

MaxPV 0

CurPV 1

ActPV 1

VGSize 39.51 GiB

PESize 4.00 MiB

TotalPE 10114

AllocPE / Size 10114 / 39.51 GiB

Free PE / Size 0 / 0

VG UUID madnL1-qOMP-r9EC-36w9-0bO0-KeyZ-z0znMh

注:VolGroup是創建系統時自帶,本實驗將無視該內容

這樣我們就創建一個VG了,假設我們要增加這個VG的容量,我們可以使用剛才保留的/dev/sdb8增加VG容量。

擴展VG[root@localhost~]# vgextend vbirdvg /dev/sdb8

ume group"vbirdvg" successfully extended

注:這個命令可以將PV(/dev/sdb8)丟給vbirdvg。

[root@localhost ~]# vgdisplay --- Volume group --- VG Name vbirdvg System ID Format lvm2 Metadata Areas 4 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 4 Act PV 4 VG Size 20.00 GiB PE Size 16.00 MiB Total PE 1280 ←可以看出PE數已經增加 Alloc PE / Size 0 / 0 Free PE / Size 1280 / 20.00 GiB VG UUID QaQ6X4-U9RP-wW7m-bgWA-UJvY-4Kg5-6jmvye --- Volume group --- VG Name VolGroup 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 39.51 GiB PE Size 4.00 MiB Total PE 10114 Alloc PE / Size 10114 /39.51 GiB Free PE / Size 0 / 0 VG UUID madnL1-qOMP-r9EC-36w9-0bO0-KeyZ-z0znMh 1.6.、LV階段創建出VG這個大磁盤之後,再來就是要新建分區。這個分區就是所謂的LV。假設我們將剛才那個vbirdvg磁盤分成vbirdlv,整個VG的容量都會被分配到vbirdlv裡面去。 LV命令:1)創建LV

lvcreate

2)刪除lv

lvremove

3)掃描lv

lvscan

4)查看lv

lvdisplay

5)移動lv

lvmove

6)縮減lv

lvreduce

7)擴展lv

lvextend

創建LV[root@localhost ~]#lvcreate -L 20G -n vbirdlv vbirdvg Logical volume "vbirdlv" created注:-L後面接容量,容量的單位可以是M、G、T等,需要注意的是最小單位為PE,因此這個數量必須要是PE的倍數,若不相符,系統會自行計算最相近的容量。lvcreate後面也可以跟小寫的“-l”若使用小寫的-l則需要跟PE的“個數”,而不是數量,若要這麼做,得要自行計算PE數。 -n:後面接的是LV的名稱。 從vgdisplay可以看出VG共有20G,所以-L後面跟20G 查看LV的詳細信息

[root@localhost~]# lvdisplay --- Logical volume --- LV Path /dev/vbirdvg/vbirdlv LV Name vbirdlv VG Name vbirdvg LV UUID p89ztk-RQFc-YG5j-20Xc-QtJ9-uYdR-DU5bfZ LV Write Access read/write LV Creation host, time localhost, 2016-06-27 22:10:32 +0800 LV Status available # open 0 LV Size 20.00 GiB ←這個LV的容量這麼大 Current LE 1280 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID JNU6kK-z5rp-xkTU-lB9F-98yj-WcZc-E1AXrH LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-04-07 06:36:29 +0800 LV Status available # open 1 LV Size 37.57 GiB Current LE 9618 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID w1qlH9-6odx-Q4Es-tVAt-n3gn-JOjx-1v7QQY LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-04-07 06:36:32 +0800 LV Status available # open 1 LV Size 1.94 GiB Current LE 496 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 注:我們整個分區已經准備好,接下來就是針對LV進行處理。要注意的是,VG的名稱是vbirdvg,但是LV的名稱必須使用全名!即/dev/vbirdvg/vbirdlv。後續的處理都是這樣的!筆者就犯過這樣的錯誤,不注意的話很容易搞錯。 7、 文件系統階段× 格式化、掛載、查看LV

[root@localhost ~]#mkfs -t ext3 /dev/vbirdvg/vbirdlv

mke2fs 1.41.12 (17-May-2010)文件系統標簽=操作系統:Linux塊大小=4096 (log=2)分塊大小=4096 (log=2)Stride=0 blocks, Stripe width=0blocks1310720 inodes, 5242880 blocks262144 blocks (5.00%) reserved forthe super user第一個數據塊=0Maximum filesystem blocks=4294967296160 block groups32768 blocks per group, 32768fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768,98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成Writing superblocks and filesystemaccounting information: 完成 This filesystem will be automaticallychecked every 29 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkdir /mnt/lvm ←/mnt下創建一個名為lvm的文件夾[root@localhost ~]# mount/dev/vbirdvg/vbirdlv /mnt/lvm ←將LV分區掛載到新建的文件夾下 [root@localhost ~]# dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/VolGroup-lv_root 38776280 5019072 31787444 14% /tmpfs 243140 72 243068 1% /dev/shm/dev/sda1 495844 34907 435337 8% /boot/dev/mapper/vbirdvg-vbirdlv 20642428 176196 19417656 1% /mnt/lvm 注:其實Lv的名稱構成為/dev/vbirdvg/vbirdlv是為了讓用戶直觀德找到我們所需要的數據,實際上LVM的設備是放置到/dev/mapper/目錄下!此時LV已經建好了,可以自有應用/mnt/lvm中的所有資源。 1.8、 放大LV容量剛才和大家說到LVM最大的特色就是彈性調整磁盤容量,下面我們將 演示如何放大LVM容量,具體步驟如下:1) 創建新的分區,將system ID改為8e;

2) 利用pvcreate構建PV;

3) 利用vgextend將PV加入我們的vbirdvg;

4) 通過resize2fs將文件系統的容量卻是增加;

創建新分區

[root@localhost lvm]# fdisk /dev/sdbswitch off the mode (command 'c') and change display units tosectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x74429a75

Device Boot Start End Blocks Id System/dev/sdb1 1 3917 31463271 5 Extended/dev/sdb5 1 393 3156709+ 8e Linux LVM/dev/sdb6 394 1308 7349706 8e Linux LVM/dev/sdb7 1309 1962 5253223+ 8e Linux LVM/dev/sdb8 1963 2616 5253223+ 8e Linux LVM

Command (m for help): nCommand action l logical (5 or over) p primary partition (1-4)lFirst cylinder (2617-3917, default 2617): Using default value 2617G

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x74429a75

Device Boot Start End Blocks Id System/dev/sdb1 1 3917 31463271 5 Extended/dev/sdb5 1 393 3156709+ 8e Linux LVM/dev/sdb6 394 1308 7349706 8e Linux LVM/dev/sdb7 1309 1962 5253223+ 8e Linux LVM/dev/sdb8 1963 2616 5253223+ 8e Linux LVM/dev/sdb9 2617 3009 3156741 83 Linux

Command (m for help): tPartition number (1-9): 9Hex code (type L to list codes): 8eChanged system type of partition 9 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x74429a75

Device Boot Start End Blocks Id System/dev/sdb1 1 3917 31463271 5 Extended/dev/sdb5 1 393 3156709+ 8e Linux LVM/dev/sdb6 394 1308 7349706 8e Linux LVM/dev/sdb7 1309 1962 5253223+ 8e Linux LVM/dev/sdb8 1963 2616 5253223+ 8e Linux LVM/dev/sdb9 2617 3009 3156741 8e Linux LVM

Command (m for help): wThe partition table has been altered!

Calling ioctl() to re-read partition table.

源忙.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.

[root@localhostlvm]# partprobe 創建新的PV

[root@localhost ~]# pvcreate /dev/sdb9

Physical volume"/dev/sdb9" successfully created [root@localhost ~]# pvscan

PV /dev/sdb5 VG vbirdvg lvm2 [3.00 GiB / 0 free]

PV /dev/sdb6 VG vbirdvg lvm2 [7.00 GiB / 0 free]

PV /dev/sdb7 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb8 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb9 lvm2 [3.00 GiB / 0 free]PV /dev/sda2 VG VolGroup lvm2 [39.51 GiB / 0 free]

Total: 6 [62.51 GiB] / inuse: 6 [62.51 GiB] / in no VG: 0 [0 ] 注:可以看出/dev/sdb9是新加入尚未使用的

加大VG使用vgextend功能

[root@localhost ~]#vgextend vbirdvg /dev/sdb9 Volumn group “vbirdvg”successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name vbirdvg System ID Format lvm2 Metadata Areas 5 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 5 Act PV 5 VG Size 23.00 GiB PE Size 16.00 MiB Total PE 1472 Alloc PE / Size 1280 / 23.00 GiB Free PE / Size 192/ 3GB VG UUID QaQ6X4-U9RP-wW7m-bgWA-UJvY-4Kg5-6jmvye --- Volume group --- VG Name VolGroup 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 39.51 GiB PE Size 4.00 MiB Total PE 10114 Alloc PE / Size 10114 / 39.51 GiB Free PE / Size 0 / 0 VG UUID madnL1-qOMP-r9EC-36w9-0bO0-KeyZ-z0znMh root@localhost ~]# lvresize –l +192 /dev/vbirdvg/vbirdlv Extending logical volume vbirdlvto 23 GB Logical volume vbirdlvsuccessfully resized注:本次使用-l只是為了給大家演示-l的用法這樣就增加LV了。Lvresize的用法很簡單,基本上同樣通過-l或-L來增加。 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vbirdvg/vbirdlv LV Name vbirdlv VG Name vbirdvg LV UUID p89ztk-RQFc-YG5j-20Xc-QtJ9-uYdR-DU5bfZ LV Write Access read/write LV Creation host, timelocalhost, 2016-06-27 22:10:32 +0800 LV Status available # open 0 LV Size 23.00 GiB Current LE 1472 Segments 5 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID JNU6kK-z5rp-xkTU-lB9F-98yj-WcZc-E1AXrH LV Write Access read/write LV Creation host, timelocalhost.localdomain, 2016-04-07 06:36:29 +0800 LV Status available # open 1 LV Size 37.57 GiB Current LE 9618 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID w1qlH9-6odx-Q4Es-tVAt-n3gn-JOjx-1v7QQY LV Write Access read/write LV Creation host, timelocalhost.localdomain, 2016-04-07 06:36:32 +0800 LV Status available # open 1 LV Size 1.94 GiB Current LE 496 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 [root@localhost ~]# df -h /mnt/lvmFilesystem Size Used Avail Use% Mounted on/dev/mapper/vbirdvg-vbirdlv 20G 173M 19G 1% /mnt/lvm注:可以看出最終結果中LV真的有放大到23G,但文件系統沒有增加。而且,我們的LVM可以在線直接處理,並不需要給它umount。

使用resize2fs

[root@localhost lvm]# dumpe2fs /dev/vbirdvg/vbirdlvdumpe2fs 1.41.12 (17-May-2010)Filesystem volume name: <none>Last mounted on: <not available>Filesystem UUID: b0560ee1-443e-4230-a3bd-0d13d4d39891Filesystem magic number: 0xEF53Filesystem revision #: 1 (dynamic)Filesystem features: has_journal ext_attr resize_inodedir_index filetype needs_recovery sparse_super large_fileFilesystem flags: signed_directory_hash Default mount options: (none)Filesystem state: cleanErrors behavior: ContinueFilesystem OS type: LinuxInode count: 1310720Block count: 5242880 這個文件系統的block總數Reserved block count: 262144Free blocks: 5116557Free inodes: 1310708First block: 0Block size: 4096Fragment size: 4096Reserved GDT blocks: 1022Blocks per group: 32768 多少個block設置成為一個blockFragments per group: 32768Inodes per group: 8192Inode blocks per group: 512Filesystem created: Mon Jun 27 22:17:05 2016Last mount time: Mon Jun 27 22:41:45 2016Last write time: Mon Jun 27 22:41:45 2016Mount count: 2Maximum mount count: 29Last checked: Mon Jun 27 22:17:05 2016Check interval: 15552000 (6 months)Next check after: Sat Dec 24 22:17:05 2016Reserved blocks uid: 0 (user root)Reserved blocks gid: 0 (group root)First inode: 11Inode size: 256Required extra isize: 28Desired extra isize: 28Journal inode: 8Default directory hash: half_md4Directory Hash Seed: 624a67f4-a148-425e-bb32-59ad7534feeaJournal backup: inode blocksJournal features: (none)日志大小: 128MJournal length: 32768Journal sequence: 0x0000000eJournal start: 1 Group 0: (Blocks 0-32767) ←括號內為block的號碼 主 superblock at 0, Group descriptors at 1-2 保留的GDT塊位於 3-1024 Block bitmap at 1025 (+1025), Inode bitmap at 1026 (+1026) Inode表位於 1027-1538 (+1027) 31222 free blocks, 8180 free inodes, 2 directories 可用塊數: 1545-6151, 6153-32767 可用inode數: 12-13, 15-8192 ….(中間省略)……. Group 159: (Blocks 5210112-5242879) 這是本系統中最後一個group Block bitmap at 5210112 (+0), Inode bitmap at 5210113 (+1) Inode表位於 5210114-5210625 (+2) 32254 free blocks, 8192 free inodes, 0 directories 可用塊數: 5210626-5242879 可用inode數: 1302529-1310720注:以上信息是文件系統內的superblock的記錄情況。 [root@localhostlvm]# resize2fs -f /dev/vbirdvg/vbirdlv

resize2fs1.41.12 (17-May-2010)zingrequiredolddesc_blocks = 2, new_desc_blocks = 2ocks.Thefilesystem on /dev/vbirdvg/vbirdlv is now 6029312 blocks long. 注:

-f:強制進行resize的操作;

-f後面跟設備名還可以在設備名後面跟大小,可以加也可以不加,如果加上size的話必須給定一個單位(M、G等)如果沒有size的話默認使用整個分區量來處理。 [root@localhost ~]# df -h /mnt/lvmFilesystem Size Used Avail Use% Mounted on/dev/mapper/vbirdvg-vbirdlv 23G 173M 22G 1% /mnt/lvm [root@localhost ~]# ls -l/mnt/lvm/總用量 20drwx------. 2 root root 16384 6月 27 22:17 lost+found-rwxr-xr-x. 1 root root 23 6月 2722:29 test.sh 注:Lvm中的數據依然存在。如果你已經有填數據在lvm扇區中,這個數據是不會死掉的,還會在以前原本的扇區當中。1.9、 縮小LV容量[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sdb5

VG Name vbirdvg

PV Size 3.01 GiB / not usable 10.75 MiB

Allocatable yes (but full)

PE Size 16.00 MiB

Total PE 192

Free PE 0

Allocated PE 192

PV UUID IYkvra-xyFA-mB1U-7wvs-fhf4-I4Qg-88C3mG注:從以上信息得知/dev/sdb5有多大,而且含有192個PE的量。那如果要使用resize2fs是,則減去7.01GB就可以了。 [root@localhost ~]# pvscan

PV /dev/sdb5 VG vbirdvg lvm2 [3.00 GiB / 0 free]

PV /dev/sdb6 VG vbirdvg lvm2 [7.00 GiB / 0 free]

PV /dev/sdb7 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb8 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb9 VG vbirdvg lvm2 [3.00 GiB / 0 free]

PV /dev/sda2 VG VolGroup lvm2 [39.51 GiB / 0 free]

Total: 6 [62.51 GiB] / in use: 6 [62.51 GiB] /in no VG: 0 [0 ] 注:從以上信息可以看出去除/dev/sdb5剩余容量為20GB(7+5+5+3=20)卸載/mnt/lvm

[root@localhost ~]# umount/mnt/lvm

進行磁盤檢查

[root@localhost ~]# e2fsck -f/dev/vbirdvg/vbirdlv

e2fsck 1.41.12 (17-May-2010)

第一步: 檢查inode,塊,和大小

第二步: 檢查目錄結構

第3步: 檢查目錄連接性

Pass 4: Checking reference counts

第5步: 檢查簇概要信息

/dev/vbirdvg/vbirdlv:12/1507328 files (0.0% non-contiguous), 138659/6029312 blocks

降低文件系統的容量

[root@localhost ~]# resize2fs/dev/vbirdvg/vbirdlv 20Gresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on/dev/vbirdvg/vbirdlv to 5242880 (4k) blocks.The filesystem on/dev/vbirdvg/vbirdlv is now 5242880 blocks long.注:有可能出現容量不能夠寫小數點為的時候大家可以把G換算成M就可以了。 降低LV的容量

[root@localhost ~]# lvresize -l-192 /dev/vbirdvg/vbirdlv WARNING: Reducing active and open logical volume to 20.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reducevbirdlv? [y/n]: y ←會有警告信息,但是我們的實際數據量還是比16G要小,所以就按y Reducing logical volume vbirdlv to 20.00 GiB Logical volume vbirdlv successfully resized注:降低LV的量,同時我們知道/dev/sdb5有192個PE [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vbirdvg/vbirdlv LV Name vbirdlv VG Name vbirdvg LV UUID p89ztk-RQFc-YG5j-20Xc-QtJ9-uYdR-DU5bfZ LV Write Access read/write LV Creation host, time localhost, 2016-06-27 22:10:32 +0800 LV Status available # open 1 LV Size 20.00 GiB Current LE 1280 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID JNU6kK-z5rp-xkTU-lB9F-98yj-WcZc-E1AXrH LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-04-07 06:36:29 +0800 LV Status available # open 1 LV Size 37.57 GiB Current LE 9618 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID w1qlH9-6odx-Q4Es-tVAt-n3gn-JOjx-1v7QQY LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-04-07 06:36:32 +0800 LV Status available # open 1 LV Size 1.94 GiB Current LE 496 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 注:這樣就將LV縮小了,接下來就是要將/dev/sdb5移出vbirdvg這個VG之外。我們在移出之前我們要先確定/dev/sdb9裡面的PE完全不被使用後才能將/dev/sdb5抽離 /dev/sdb9移出vbirdvg

[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sdb5 VG Name vbirdvg PV Size 3.01 GiB /not usable 10.72 MiB Allocatable yes (butfull) PE Size 16.00 MiB Total PE 192 Free PE 0 Allocated PE 192 PV UUID IYkvra-xyFA-mB1U-7wvs-fhf4-I4Qg-88C3mG --- Physical volume --- PV Name /dev/sdb6 VG Name vbirdvg PV Size 7.01 GiB /not usable 9.45 MiB Allocatable yes (butfull) PE Size 16.00 MiB Total PE 448 Free PE 0 Allocated PE 448 PV UUID gErjmF-t2Wb-jFI4-qLZ4-4lze-1oU9-O7HdRb --- Physical volume --- PV Name /dev/sdb7 VG Name vbirdvg PV Size 5.01 GiB /not usable 10.10 MiB Allocatable yes (butfull) PE Size 16.00 MiB Total PE 320 Free PE 0 Allocated PE 320 PV UUID CqHwxm-ZfFT-KHju-SkS0-bXNN-Q70X-hYkjb0 --- Physical volume --- PV Name /dev/sdb8 VG Name vbirdvg PV Size 5.01 GiB /not usable 10.10 MiB Allocatable yes (butfull) PE Size 16.00 MiB Total PE 320 Free PE 0 Allocated PE 320 PV UUID ty3uai-DtDD-h2QC-n01u-psKJ-7Blf-cvQAiL --- Physical volume --- PV Name /dev/sdb9 VG Name vbirdvg PV Size 3.01 GiB /not usable 10.75 MiB Allocatable yes PE Size 16.00 MiB Total PE 192 Free PE 192 Allocated PE 0 PV UUID PaBXJr-hpTK-hcvU-8Uxh-odDw-QNEX-dTeL0V --- Physical volume --- PV Name /dev/sda2 VG Name VolGroup PV Size 39.51 GiB /not usable 3.00 MiB Allocatable yes (butfull) PE Size 4.00 MiB Total PE 10114 Free PE 0 Allocated PE 10114 PV UUID C8Soc4-4V1F-I3LI-YyJr-KnEP-urtx-xXpKjh 注:先確認/dev/sdb5是否將PE都刪除了 通過查看上述信息可以看出在/dev/sdb9上有沒有使用的PE剛好和/dev/sdb5上的PE一樣大,我們需要將PE從/dev/sdb5轉移到/dev/sdb9

轉移PE

[root@localhost ~]# pvmove/dev/sdb5 /dev/sdb9 /dev/sdb5: Moved: 0.0% /dev/sdb5: Moved: 26.0% /dev/sdb5: Moved: 52.1% /dev/sdb5: Moved: 78.1% /dev/sdb5: Moved: 100.0% 注:Pvmove 來源pv 目標pv,可以將/dev/sdb5中的數據全部移動到/dev/sdb9中。/dev/sdb5移出vbirdvg[root@localhost ~]# vgreduce vbirdvg/dev/sdb5 Removed "/dev/sdb5" from volumegroup "vbirdvg"

[root@localhost~]# pvscanPV /dev/sdb6 VG vbirdvg lvm2 [7.00 GiB / 0 free] PV /dev/sdb7 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb8 VG vbirdvg lvm2 [5.00 GiB / 0 free]

PV /dev/sdb9 VG vbirdvg lvm2 [3.00 GiB / 0 free]

PV /dev/sda2 VG VolGroup lvm2 [39.51 GiB / 0 free]

PV /dev/sdb5 lvm2 [3.01 GiB]

Total: 6 [62.52 GiB] / in use: 5 [59.51 GiB]/ in no VG: 1 [3.01 GiB] [root@localhost ~]# pvremove /dev/sdb5

Labels on physical volume"/dev/sdb5" successfully wiped注:/dev/sdb5已經空出來了,可以去讓他做別的事情了!基本上是完成了,LVM快照請參考文章http://bella41981.blog.51cto.com/10603572/1795009

Copyright © Linux教程網 All Rights Reserved