高級硬盤管理LVM
LVM(Logical Volum e Manager,邏輯卷管理器)是一種可選的子系統,它提供了硬盤分區的一種超強版本。它能讓你將多個單獨的硬盤組成「卷組(volume group)」。卷組的總容量接着能夠分配給邏輯卷(logical volume),後者按照普通的塊設備來訪問。
配置LVM的例子:
一,建立一個LVM:
1.)對磁盤進行格式化`fidisk /dev/sda`,建立4個LAM分區分爲/dev/sda5 /dev/sda6 /dev/sda7
/dev/sda8,它們的文件系統類型設定爲8e,大小爲1G,且不要對它們格式化。
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 5235 41945715 8e Linux LVM
/dev/sda3 5236 5366 1052257+ 82 Linux swap / Solaris
/dev/sda4 5367 19581 114181987+ 5 Extended
/dev/sda5 5367 5489 987966 8e Linux LVM
/dev/sda6 5490 5612 987966 8e Linux LVM
/dev/sda7 5613 5735 987966 8e Linux LVM
/dev/sda8 5736 5858 987966 8e Linux LVM
2.)分別對/dev/sda5 /dev/sda6 /dev/sda7 /dev/sda8建立成爲物理卷`pvcreate /dev/sda{5,6,7,8}`,建立後可用`pvdisplay`顯示物理卷信息。
--- Physical volume ---
PV Name /dev/sda5
VG Name vgtest
PV Size 964.81 MB / not usable 830.00 KB
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 241
Free PE 0
Allocated PE 241
PV UUID p3ConK-2MWg-Rplv-bDC2-37ra-RQgq-abo9Sf
3.)對上述設備建立一個卷組,其命令爲`vgcreate vgtest(卷組名) /dev/sda{5,6,7,8}`,建立後可用顯示卷組信息。
--- Volume group ---
VG Name vgtest
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size 3.77 GB
PE Size 4.00 MB
Total PE 964
Alloc PE / Size 256 / 1.00 GB
Free PE / Size 708 / 2.77 GB
VG UUID MibgUv-PV8g-3SVk-Mqi5-DJYW-E2ub-C9QA5D
4.)建立一個邏輯卷:`lvcreate -L 1G -n lvtest vgtest`,建立後可經過`lvdisplay`可查看邏輯卷信息。
--- Logical volume ---
LV Name /dev/vgtest/lvtest
VG Name vgtest
LV UUID ttUzjZ-Ga2L-UGO0-fa9J-GUL2-PK4T-wMJvS7
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
5.)邏輯卷lvtest經過格式化`mkfs.ext3 /dev/vgtest/lvtest`,經過掛在到/mnt目錄下`mount
/dev/vgtest/lvtest /mnt`,便可使用。
二.從新分配儲存空間
1.)1.1把/dev/sda7從lvtest邏輯卷中移除 `vgreduce vgtest /dev/sda7`
Removed "/dev/sda7" from volume group "vgtest"
--- Volume group ---
VG Name vgtest
VG Size 2.82 GB
1.2再把/dev/sda7加入邏輯卷lvtest `vgextend vgtest /dev/sda7`
Volume group "vgtest" successfully extended
--- Volume group ---
VG Name vgtest
VG Size 3.77 GB
2.)擴展邏輯卷lvtest,它須要兩個個步驟:`lvextend -L +1G /dev/vgtest/lvtest`和`resize2fs -p /dev/vgtest/lvtest`,輸入命令順序不得改變,表示把邏輯卷的大小增長了1G。
Extending logical volume lvtest to 2.00 GB
Logical volume lvtest successfully resized
和:
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vgtest/lvtest to 524288 (4k) blocks.
Begin pass 1 (max = 8)
Extending the inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vgtest/lvtest is now 524288 blocks long.
3.)減少邏輯卷lvtest的大小,它須要卸載/dev/vgtest/lvtest並且要檢查LVM邏輯卷:其命令爲:`umount /dev/vgtest/lvtest` 和 `fsck -f /dev/vgtest/lvtest`;正常卸載和檢查後,可以使用命令`resize2fs /dev/vgtest/lvtest 512M`和`lvreduce -L 512M /dev/vgtest/lvtest`,輸入命令順序不得改變,表示把邏輯卷的大小調整到512M。
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vgtest/lvtest to 131072 (4k) blocks.
The filesystem on /dev/vgtest/lvtest is now 131072 blocks long.
和:
WARNING: Reducing active logical volume to 512.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvtest? [y/n]: y
Reducing logical volume lvtest to 512.00 MB
Logical volume lvtest successfully resized
4.)使用`df -lh`命令來查看LVM邏輯卷的狀態信息。
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vol0-root 20G 3.5G 15G 19% /
/dev/mapper/vol0-home 465M 11M 431M 3% /home
/dev/sda1 99M 21M 74M 22% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/mapper/vgtest-lvtest 504M 33M 456M 7% /lvtest
5.)若是想刪除物理卷,可用命令:`pvremove /dev/sda7`。
三.對LVM建立快照
1.)`mkdir -v /lvtest`建立目錄/lvtest ;`mount /dev/vgtest/lvtest /lvtest`掛載LVM邏輯卷`cd
/lvtest`到/lvtest目錄下,拷貝任意幾個文件到此目錄下,如/etc/fstab,/var/log/messages等文件。
2.)建立快照`lvcreate -L 12M -p r -s -n lvtest.backup /dev/vgtest/lvtest`,其中選項:-L表
示快照的大小;-p表示快照的權限;-s制定建立快照;-n指定快照名。可用`lvdisplay
/dev/vgtest/lvtest`命令顯示此快照文件信息。
--- Logical volume ---
LV Name /dev/vgtest/lvtest.backup
VG Name vgtest
LV UUID zvZ7R6-e7qv-VThO-PfBD-M4R2-ql6E-V02ozq
LV Write Access read only
LV snapshot status active destination for /dev/vgtest/lvtest
LV Status available
# open 0
LV Size 512.00 MB
Current LE 128
COW-table size 12.00 MB
COW-table LE 3
Allocated to snapshot 0.10%
Snapshot chunk size 4.00 KB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
3.)對快照備份`dump -0u -f /root/lv20100728.dump /dev/vgtest/lvtest.backup`,其中-0表明備份級別;u用來更新/etc/dumpdatas記錄備份信息;-f用來指定備份目標。備份完成以後輸入`lvremove /dev/vdtest/lvtest.backup`命令刪除快照文件。
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 138 blocks.
DUMP: Volume 1 started with block 1 at: Wed Jul 28 11:04:57 2010
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing /root/lvtest20100728.dump
DUMP: Volume 1 completed at: Wed Jul 28 11:04:57 2010
DUMP: Volume 1 140 blocks (0.14MB)
DUMP: 140 blocks (0.14MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Wed Jul 28 11:04:56 2010
DUMP: Date this dump completed: Wed Jul 28 11:04:57 2010
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
4.)格式化LVM邏輯卷或`cd /lvtest`轉到/lvtest目錄刪除裏面的文件,使數據受到破壞。
5.)`cd /lvtest`轉到/lvtest目錄,使用命令`restore -rf /root/lv20100728.dump`恢復數據。使用`ls`
命令可看到損壞的數據又恢復了。 fstab lost+found messages restoresymtable