LVM管理

梓沐發表於2017-02-03

一、簡介


LVM是邏輯盤卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁碟分割槽進行管理的一種機制,LVM是建立在硬碟和分割槽之上的一個邏輯層,來提高磁碟分割槽管理的靈活性。


LVM的工作原理其實很簡單,它就是通過將底層的物理硬碟抽象的封裝起來,然後以邏輯卷的方式呈現給上層應用。在傳統的磁碟管理機制中,我們的上層應用是直接訪問檔案系統,從而對底層的物理硬碟進行讀取,而在LVM中,其通過對底層的硬碟進行封裝,當我們對底層的物理硬碟進行操作時,其不再是針對於分割槽進行操作,而是通過一個叫做邏輯卷的東西來對其進行底層的磁碟管理操作。比如說我增加一個物理硬碟,這個時候上層的服務是感覺不到的,因為呈現給上層服務的是以邏輯卷的方式。


LVM最大的特點就是可以對磁碟進行動態管理。因為邏輯卷的大小是可以動態調整的,而且不會丟失現有的資料。如果我們新增加了硬碟,其也不會改變現有上層的邏輯卷。作為一個動態磁碟管理機制,邏輯卷技術大大提高了磁碟管理的靈活性。

基本的邏輯卷管理概念:


PVPhysical Volume- 物理卷

物理卷在邏輯卷管理中處於最底層,它可以是實際物理硬碟上的分割槽,也可以是整個物理硬碟,也可以是raid裝置。


VGVolumne Group- 卷組

卷組建立在物理卷之上,一個卷組中至少要包括一個物理卷,在卷組建立之後可動態新增物理捲到卷組中。一個邏輯卷管理系統工程中可以只有一個卷組,也可以擁有多個卷組。


LVLogical Volume- 邏輯卷

邏輯卷建立在卷組之上,卷組中的未分配空間可以用於建立新的邏輯卷,邏輯卷建立後可以動態地擴充套件和縮小空間。系統中的多個邏輯卷可以屬於同一個卷組,也可以屬於不同的多個卷組。

PEPhysical Extent- 物理塊

LVM 預設使用4MBPE區塊,而LVMLV最多僅能含有65534PE(lvm1的格式),因此預設的LVMLV最大容量為4M*65534/(1024M/G)=256GPE是整個LVM最小的儲存區塊,也就是說,其實我們的資料都是由寫入PE來處理的。簡單的說,這個PE就有點像檔案系統裡面的block大小。所以調整PE會影響到LVM的最大容量!不過,在CentOS 6.x以後,由於直接使用lvm2的各項格式功能,因此這個限制已經不存在了。


二、安裝LVM管理工具

2.1 檢查系統中是否安裝了LVM管理工具

# rpm -qa|grep lvm


2.2 如果未安裝,則使用yum方式安裝

# yum install lvm*

# rpm -qa|grep lvm


三、新建一個分割槽

[root@oracle ~]# fdisk -l


Disk /dev/sda: 128.8 GB, 128849018880 bytes

255 heads, 63 sectors/track, 15665 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x0000832f


Device Boot Start End Blocks Id System

/dev/sda1 * 1 26 204800 83 Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2 26 548 4194304 82 Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3 548 15666 121428992 83 Linux


Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x00000000


[root@oracle ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x1a091f57.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').


Command (m for help): n

Command action

e extended

p primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-652, default 1):

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):

Using default value 652


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@oracle ~]# fdisk -l


Disk /dev/sda: 128.8 GB, 128849018880 bytes

255 heads, 63 sectors/track, 15665 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x0000832f


Device Boot Start End Blocks Id System

/dev/sda1 * 1 26 204800 83 Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2 26 548 4194304 82 Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3 548 15666 121428992 83 Linux


Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x1a091f57


Device Boot Start End Blocks Id System

/dev/sdb1 1 652 5237158+ 83 Linux


四、建立PV

[root@oracle ~]# pvcreate /dev/sdb1

dev_is_mpath: failed to get device for 8:17

Physical volume "/dev/sdb1" successfully created


[root@oracle ~]# pvdisplay

"/dev/sdb1" is a new physical volume of "4.99 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb1

VG Name

PV Size 4.99 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID jqnJu3-zJIc-tLDU-RNhZ-Ixcf-5bXd-uAI0fK

還可以使用命令pvpvscan檢視簡略資訊


五、建立VG

[root@oracle ~]# vgdisplay

No volume groups found


[root@oracle ~]# vgcreate VolGroup00 /dev/sdb1

Volume group "VolGroup00" successfully created

[root@oracle ~]# vgdisplay

--- Volume group ---

VG Name VolGroup00

System ID

Format lvm2

Metadata Areas 1

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 1

Act PV 1

VG Size 4.99 GiB

PE Size 4.00 MiB

Total PE 1278

Alloc PE / Size 0 / 0

Free PE / Size 1278 / 4.99 GiB

VG UUID kyafXU-PWQU-FCam-pxkl-jNZX-qQbH-A2cRMn

還可以使用命令vgsvgscan檢視簡略資訊


[root@oracle ~]# vgextend VolGroup00 /dev/sdb1

Physical volume '/dev/sdb1' is already in volume group 'VolGroup00'

Unable to add physical volume '/dev/sdb1' to volume group 'VolGroup00'.


[root@oracle ~]# pvdisplay

--- Physical volume ---

PV Name /dev/sdb1

VG Name VolGroup00

PV Size 4.99 GiB / not usable 2.41 MiB

Allocatable yes

PE Size 4.00 MiB

Total PE 1278

Free PE 1278

Allocated PE 0

PV UUID jqnJu3-zJIc-tLDU-RNhZ-Ixcf-5bXd-uAI0fK


六、建立LV

[root@oracle ~]# lvcreate -L 2G -n LogVol00 VolGroup00

Logical volume "LogVol00" created


[root@oracle ~]# lvdisplay

--- Logical volume ---

LV Path /dev/VolGroup00/LogVol00

LV Name LogVol00

VG Name VolGroup00

LV UUID LkyfTA-1uzY-ZLNx-106T-ZPEx-Yp6k-6yCm4n

LV Write Access read/write

LV Creation host, time oracle, 2017-02-03 10:50:14 +0800

LV Status available

# open 0

LV Size 2.00 GiB

Current LE 512

Segments 1

Allocation inherit

Read ahead sectors auto

- currently set to 256

Block device 253:0


[root@oracle ~]# vgdisplay

--- Volume group ---

VG Name VolGroup00

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 2

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 0

Max PV 0

Cur PV 1

Act PV 1

VG Size 4.99 GiB

PE Size 4.00 MiB

Total PE 1278

Alloc PE / Size 512 / 2.00 GiB

Free PE / Size 766 / 2.99 GiB

VG UUID kyafXU-PWQU-FCam-pxkl-jNZX-qQbH-A2cRMn


[root@oracle ~]# lvs

LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

LogVol00 VolGroup00 -wi-a----- 2.00g


七、格式化LV

7.1報錯

[root@oracle ~]# resize2fs /dev/VolGroup00/LogVol00

resize2fs 1.41.12 (17-May-2010)

resize2fs: Bad magic number in super-block while trying to open /dev/VolGroup00/LogVol00

Couldn't find valid filesystem superblock.

[root@oracle ~]# mkdir /sun



[root@oracle ~]# mount /dev/VolGroup00/LogVol00 /sun

mount: you must specify the filesystem type


7.2使用mkfs.ext4格式化

[root@oracle ~]# mkfs.ext4 /dev/VolGroup00/LogVol00

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

131072 inodes, 524288 blocks

26214 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=536870912

16 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

    32768, 98304, 163840, 229376, 294912


Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 23 mounts or

180 days, whichever comes first. Use tune2fs -c or -i to override.

[root@oracle ~]# resize2fs /dev/VolGroup00/LogVol00

resize2fs 1.41.12 (17-May-2010)

The filesystem is already 524288 blocks long. Nothing to do!


[root@oracle ~]# fdisk -l


Disk /dev/sda: 128.8 GB, 128849018880 bytes

255 heads, 63 sectors/track, 15665 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x0000832f


Device Boot Start End Blocks Id System

/dev/sda1 * 1 26 204800 83 Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2 26 548 4194304 82 Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3 548 15666 121428992 83 Linux


Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x1a091f57


Device Boot Start End Blocks Id System

/dev/sdb1 1 652 5237158+ 83 Linux


Disk /dev/mapper/VolGroup00-LogVol00: 2147 MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

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

Disk identifier: 0x00000000


八、掛載使用

[root@oracle ~]# mount /dev/VolGroup00/LogVol00 /sun

[root@oracle ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 114G 8.5G 100G 8% /

tmpfs 939M 0 939M 0% /dev/shm

/dev/sda1 194M 34M 151M 19% /boot

/dev/mapper/VolGroup00-LogVol00 2.0G 67M 1.9G 4% /sun


[root@oracle sun]# touch /sun/a.txt

[root@oracle sun]# cat /sun/a.txt

1234


[root@oracle ~]# vgs

VG #PV #LV #SN Attr VSize VFree

VolGroup00 1 1 0 wz--n- 4.99g 2.99g


九、繼續擴充套件2G分配給LVM

[root@oracle ~]# lvextend -L +2G -n /dev/VolGroup00/LogVol00

Extending logical volume LogVol00 to 4.00 GiB

Logical volume LogVol00 successfully resized


[root@oracle ~]# vgs

VG #PV #LV #SN Attr VSize VFree

VolGroup00 1 1 0 wz--n- 4.99g 1016.00m

[root@oracle ~]# lvs

LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

LogVol00 VolGroup00 -wi-ao---- 4.00g


[root@oracle ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 114G 8.5G 100G 8% /

tmpfs 939M 0 939M 0% /dev/shm

/dev/sda1 194M 34M 151M 19% /boot

/dev/mapper/VolGroup00-LogVol00 2.0G 67M 1.9G 4% /sun



[root@oracle sun]# resize2fs /dev/VolGroup00/LogVol00

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/VolGroup00/LogVol00 is mounted on /sun; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 1

Performing an on-line resize of /dev/VolGroup00/LogVol00 to 1048576 (4k) blocks.

The filesystem on /dev/VolGroup00/LogVol00 is now 1048576 blocks long.


[root@oracle sun]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 114G 8.5G 100G 8% /

tmpfs 939M 0 939M 0% /dev/shm

/dev/sda1 194M 34M 151M 19% /boot

/dev/mapper/VolGroup00-LogVol00 4.0G 68M 3.7G 2% /sun


[root@oracle sun]# cat a.txt

1234

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29812844/viewspace-2133023/,如需轉載,請註明出處,否則將追究法律責任。

相關文章