---------------------------------------------------
1,概述 1
2,基本原理 1
3,建立邏輯卷 1
建立物理卷 1
建立卷組 2
建立邏輯卷 3
4,動(dòng)態(tài)增加和減小邏輯卷大小 4
動(dòng)態(tài)增加邏輯卷容量 4
動(dòng)態(tài)減小邏輯卷大小 6
5,結(jié)束語(yǔ) 7
正文:
----------------------------------------------------
邏輯卷管理(Logic Volume Manager),簡(jiǎn)稱LVM,它是與傳統(tǒng)的靜態(tài)分區(qū)完全不同的磁盤(pán)管理方式,LVM是把實(shí)際的物理磁盤(pán)數(shù)據(jù)影射到一個(gè)簡(jiǎn)單而靈活的虛擬性的邏輯存儲(chǔ)視圖上,借以控制磁盤(pán)資源。這樣,也就是重新考慮了管理文件系統(tǒng)和卷的方法,系統(tǒng)在文件系統(tǒng)管理中增加了一個(gè)額外的抽象層,不需要深入細(xì)節(jié)的了解,可以實(shí)現(xiàn)虛擬分區(qū)或者動(dòng)態(tài)建立一個(gè)邏輯卷以及動(dòng)態(tài)更改卷的大小,并且實(shí)現(xiàn)了允許文件系統(tǒng)跨越磁盤(pán)等功能。
·物理卷
物理卷 physical volume(PV),通常一個(gè)物理卷就是一塊硬盤(pán),或者是一個(gè)磁盤(pán)的某一個(gè)分區(qū),或者看起來(lái)象一塊硬盤(pán)的設(shè)備,如軟件raid設(shè)備。
·卷組
卷組 Volume Group(VG),卷組是lvm里的最高抽象級(jí)。它把邏輯卷和物理卷集中在一個(gè)管理單位里面。
·邏輯卷
邏輯卷(Logical Volume(LV):等同于非lvm系統(tǒng)下的分區(qū),lv可以看作標(biāo)準(zhǔn)的塊設(shè)備,可以包含一個(gè)文件系統(tǒng)(如/home)。
·物理盤(pán)區(qū)
物理盤(pán)區(qū)(Physical Extents(PE)):每個(gè)物理卷被劃分為稱為PE(PhysicalExtents)的基本單元,具有唯一編號(hào)的PE是可以被LVM尋址的最小單元。PE的大小是可以設(shè)置的,默認(rèn)為4MB。
·邏輯盤(pán)區(qū)
邏輯盤(pán)區(qū)(logical extent(LE)):邏輯卷也被劃分為被稱為LE(LogicalExtents)的可被尋址的基本單位。在同一個(gè)卷組中,LE的大小和PE是相同的,并且一一對(duì)應(yīng)。
思路:建立物理卷--à建立卷組--à建立邏輯卷--à格式化--àmount (刪除的話剛好反過(guò)去)
要把分區(qū)做成LVM分區(qū),首先應(yīng)先把我們的物理設(shè)備加入到物理卷中,我們這里有sdc,和sdd兩個(gè)分區(qū),我們現(xiàn)在用pvcreate命令分別將這兩個(gè)分區(qū)加入到一個(gè)pv(物理卷中),如圖-1:
# pvcreate /dev/sdb /dev/sdc
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
# pvdisplay
--- Physical volume ---
PV Name /dev/sdc
VG Name vg_bkeep
PV Size 8.00 GB / not usable 4.00 MB
Allocatable yes
PE Size (KByte) 4096
Total PE 2047
Free PE 2047
Allocated PE 0
PV UUID SFLz2x-NEa0-sjHa-KxKd-kQWg-NYSJ-V8qWmB
--- Physical volume ---
PV Name /dev/sdb
VG Name vg_bkeep
PV Size 8.00 GB / not usable 4.00 MB
Allocatable yes
PE Size (KByte) 4096
Total PE 2047
Free PE 2047
Allocated PE 0
PV UUID gvuO0s-0p8c-PpL1-in75-uaaQ-vvB6-NEv0sm
然后再將sdc和sdd分別加入到一個(gè)名叫vg_bkeep的卷組里,并用vgdisplay命令查看已經(jīng)添加好的名為vg_bkeep的卷組。
# pvcreate /dev/sdb /dev/sdc
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
# vgcreate vg_bkeep /dev/sdc /dev/sdb
Volume group "vg_bkeep" successfully created
# vgdisplay
--- Volume group ---
VG Name vg_bkeep
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 15.99 GB
PE Size 4.00 MB
Total PE 4094
Alloc PE / Size 0 / 0
Free PE / Size 4094 / 15.99 GB
VG UUID dCZcOf-nnfz-O5Ea-lW0C-dcIA-rnkt-pbXdCp
當(dāng)物理磁盤(pán)已經(jīng)加入物理卷,并且我們將加入物理卷的兩個(gè)磁盤(pán)創(chuàng)建成一個(gè)名叫vg_bkeep的卷組后,我們就可以在vg_bkeep卷組上劃分邏輯卷了。上圖可以看到目前我們
的VG size是1.99GB,我們用lvcreate命令劃分一個(gè)1G大小,名字叫data的邏輯卷,并且格式化然后掛在到根目錄下的data目錄中。
# lvcreate -n data -L 4G vg_bkeep
/dev/cdrom: open failed: Read-only file system
Logical volume "data" created
# lvdisplay
--- Logical volume ---
LV Name /dev/vg_bkeep/data
VG Name vg_bkeep
LV UUID Bvb0DG-tMaP-5uVS-8GrZ-KAPv-ZLSs-hfdmr9
LV Write Access read/write
LV Status available
# open 0
LV Size 4.00 GB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
從上圖我們可以看到,已經(jīng)成功劃分一個(gè)名為data的邏輯卷。lvcreate的具體用法可以用man lvcreate來(lái)查看。然后我們執(zhí)行mkfs.ext3 /dev/vg_bkeep/data
將vg_bkeep卷組中的data格式化為ext3文件系統(tǒng)。再執(zhí)行mount /dev/vg_bkeep/data /data,執(zhí)行df -h 命令就可以看到我們建立的邏輯卷已經(jīng)掛載到了/data目錄
下大小為1000MB。
# mkfs.ext3 /dev/vg_bkeep/data
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
524288 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks):
done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
# mount /dev/vg_bkeep/data /data
# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 3.8G 1.9G 1.8G 53% /
/dev/sda3 34G 3.1G 30G 10% /home
/dev/sda1 46M 11M 34M 24% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.9G 2.9G 0 100% /media/RHEL_5.2 i386 DVD
/dev/mapper/vg_bkeep-data 4.0G 137M 3.7G 4% /data
OK,到了重要的環(huán)節(jié)。我們建立邏輯卷的目的其實(shí)就是為了在分區(qū)空間快滿時(shí)或者分區(qū)空間分多了需要?jiǎng)討B(tài)增加或者減小時(shí)用到。比如我的home分區(qū)和var分區(qū)都
是掛在邏輯卷上。有一天home不夠用了,那我們只需要?jiǎng)討B(tài)將它放大。下面我們還以data目錄為例來(lái)增大邏輯卷data的容量到1.5G。
# vgdisplay
--- Volume group ---
VG Name vg_bkeep
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 15.99 GB
PE Size 4.00 MB
Total PE 4094
Alloc PE / Size 1024 / 4.00 GB
Free PE / Size 3070 / 11.99 GB
VG UUID dCZcOf-nnfz-O5Ea-lW0C-dcIA-rnkt-pbXdCp
# lvextend -L +2G /dev/vg_bkeep/data
/dev/cdrom: open failed: Read-only file system
Extending logical volume data to 6.00 GB
Logical volume data successfully resized
# lvdisplay
--- Logical volume ---
LV Name /dev/vg_bkeep/data
VG Name vg_bkeep
LV UUID Bvb0DG-tMaP-5uVS-8GrZ-KAPv-ZLSs-hfdmr9
LV Write Access read/write
LV Status available
# open 1
LV Size 6.00 GB
Current LE 1536
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 3.8G 1.9G 1.8G 53% /
/dev/sda3 34G 3.1G 30G 10% /home
/dev/sda1 46M 11M 34M 24% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.9G 2.9G 0 100% /media/RHEL_5.2 i386 DVD
/dev/mapper/vg_bkeep-data 4.0G 137M 3.7G 4% /data
如上圖我們看到的,我們用 ”lvextend -L +500M /dev/vg_bkeep/data “這個(gè)命令給data增加了500MB的空間,并且lvdisplay也看到/dev/vg_bkeep/data的大小確實(shí)是1.49GB。但是我們”df -h“看到的/data目錄大小仍然是1008MB。這里說(shuō)點(diǎn)小常識(shí),我們剛剛的命令只是增加了/dev/vg_bkeep/data這個(gè)邏輯卷的大小,但是在mkfs.ext3格式化data時(shí)程序已經(jīng)默認(rèn)按照1G的大小將/data格式化成了ext3文件系統(tǒng)。所以我們還要用到resize2fs這個(gè)命令將文件系統(tǒng)的大小也放大到1.5G左右。
# resize2fs /dev/vg_bkeep/data 6G
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg_bkeep/data is mounted on /data; on-line resizing required
Performing an on-line resize of /dev/vg_bkeep/data to 1572864 (4k) blocks.
The filesystem on /dev/vg_bkeep/data is now 1572864 blocks long.
# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 3.8G 1.9G 1.8G 53% /
/dev/sda3 34G 3.1G 30G 10% /home
/dev/sda1 46M 11M 34M 24% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.9G 2.9G 0 100% /media/RHEL_5.2 i386 DVD
/dev/mapper/vg_bkeep-data 6.0G 137M 5.5G 3% /data
如上圖所示我們已經(jīng)成功將文件系統(tǒng)的大小也改到了1.5GB,df -h 看到現(xiàn)在我們的/data 目錄大小已經(jīng)變成了1.5G。這樣我們就能繼續(xù)使用data目錄而不需要
重新卸載個(gè)掛載文件系統(tǒng)了。
減小邏輯卷就沒(méi)有增大那么容易了,因?yàn)橄到y(tǒng)在經(jīng)過(guò)長(zhǎng)期的刪除增加文件以后,文件系統(tǒng)的block的順序已經(jīng)比較混亂了。所以不能像增加容量那樣在線的動(dòng)態(tài)增加。必須要先將文件系統(tǒng)卸載掉,然后用"fsck -f /dev/vg_bkeep/data" 強(qiáng)制檢查和整理一下文件系統(tǒng)的inode和block.然后再減小容量。如圖-7:
# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 3.8G 1.9G 1.7G 53% /
/dev/sda3 34G 3.1G 30G 10% /home
/dev/sda1 46M 11M 34M 24% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.9G 2.9G 0 100% /media/RHEL_5.2 i386 DVD
/dev/mapper/vg_bkeep-data 3.0G 69M 2.8G 3% /data
# umount /data //這一步很重要哦!
# fsck -f /dev/vg_bkeep/data
fsck 1.39 (29-May-2006)
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/vg_bkeep/data: 11/393216 files (9.1% non-contiguous), 29902/786432 blocks
# resize2fs /dev/vg_bkeep/data 1G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg_bkeep/data to 262144 (4k) blocks.
The filesystem on /dev/vg_bkeep/data is now 262144 blocks long.
# lvreduce -L -2G /dev/vg_bkeep/data
/dev/cdrom: open failed: Read-only file system
WARNING: Reducing active logical volume to 1.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
Reducing logical volume data to 1.00 GB
Logical volume data successfully resized
# mount /dev/vg_bkeep/data /data
# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 3.8G 1.9G 1.7G 53% /
/dev/sda3 34G 3.1G 30G 10% /home
/dev/sda1 46M 11M 34M 24% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.9G 2.9G 0 100% /media/RHEL_5.2 i386 DVD
/dev/mapper/vg_bkeep-data 1008M 68M 900M 8% /data
如上圖所見(jiàn),我們lvdisplay后data卷大小已經(jīng)變成了1G,并且我們?cè)賿燧d/dev/vg_bkeep/data到/data,然后用“df -h”查看我們的/data目錄也減小1008MB。以上減小邏輯卷容量的動(dòng)作,邏輯卷上的數(shù)據(jù)是不會(huì)丟失的。注:這里必須要嚴(yán)格按照上面的步驟來(lái)做,順序如果反了話邏輯卷里面的數(shù)據(jù)將會(huì)損壞或者丟失。
聯(lián)系客服