高级硬盘管理LVM
LVM(Logical Volum e Manager,逻辑卷管理器)是一种可选的子系统,它提供了硬盘分区的一种超强版本。它能让你将多个单独的硬盘组成“卷组(volume group)”。卷组的总容量接着可以分配给逻辑卷(logical volume),后者按照普通的块设备来访问。
配置LVM的例子:
一,创建一个LVM:
1.)对磁盘进行格式化`fidisk /dev/sda`,创建4个LAM分区分为/dev/sda5 /dev/sda6 /dev/sda7
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
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
二.重新分配储存空间
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表
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`
--- 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
fstab lost+found messages restoresymtable