一 什么是LVM
逻辑卷轴管理器(英语:LogicalVolume Manager,缩写为LVM),又译为逻辑卷宗管理器、逻辑磁区管理器、逻辑磁碟管理器,是Linux核心所提供的逻辑卷轴管理(Logical volume management)功能。它在硬碟的硬盘分区之上,又建立一个逻辑层,以方便系统管理硬碟分割系统。
最先由IBM开发,在AIX系统上实作,OS/2 作业系统与 HP-UX也支援这个功能。在1998年,Heinz Mauelshagen 根据在 HP-UX 上的逻辑卷轴管理器,写作出第一个 Linux 版本的逻辑卷轴管理器。
#LVM:把磁盘合并
#物理卷:PV 划分为LVM的声明
#卷组:VG 把多个磁盘分到一组
#逻辑卷:LV 化分一部分使用
二 物理卷相关操作
#动态添加和减少
#坏了:磁盘更换,数据丢失
#安装pvcreate命令
[root@serv01 ~]# yum install /sbin/pvcreate-y
#创建物理卷
[root@serv01 ~]# pvcreate /dev/sdb1/dev/sdc1 /dev/sdd1
Physical volume “/dev/sdb1” successfully created
Physical volume “/dev/sdc1” successfully created
Physical volume “/dev/sdd1” successfully created
#显示物理卷
[root@serv01 ~]# pvdisplay
“/dev/sdb1” is a new physical volume of “2.00 GiB”
—NEW Physical volume —
PVName /dev/sdb1
VGName
PVSize 2.00 GiB
Allocatable NO
PESize 0
Total PE 0
Free PE 0
Allocated PE 0
PVUUID pz5yiv-EiGe-ggUF-PBuz-DGg7-Q7TN-wcRtDF
“/dev/sdc1” is a new physical volume of “2.00 GiB”
—NEW Physical volume —
PVName /dev/sdc1
VGName
PVSize 2.00 GiB
Allocatable NO
PESize 0
Total PE 0
Free PE 0
Allocated PE 0
PVUUID Ktxmtw-CXKV-EfUF-NU8q-dxBJ-ptFG-J61jGl
“/dev/sdd1” is a new physical volume of “2.00 GiB”
—NEW Physical volume —
PVName /dev/sdd1
VGName
PVSize 2.00 GiB
Allocatable NO
PESize 0
Total PE 0
Free PE 0
Allocated PE 0
PVUUID RGu6DT-qQut-fnAU-Hdvu-NKT4-uggp-QtFNRs
三 卷组相关操作
#显示卷组
[root@serv01 ~]# vgdisplay
Novolume groups found
#创建卷组
[root@serv01 ~]# vgcreate myvg /dev/sdb1/dev/sdc1 /dev/sdd1
Volume group “myvg” successfully created
[root@serv01 ~]# vgdisplay
—Volume group —
VGName myvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VGAccess read/write
VGStatus resizable
MAXLV 0
CurLV 0
Open LV 0
MaxPV 0
CurPV 3
ActPV 3
VGSize 5.99 GiB
PESize 4.00 MiB
Total PE 1533
Alloc PE / Size 0 / 0
Free PE / Size 1533 / 5.99 GiB
VGUUID Yai04O-2UGc-LoZ3-iVHA-L7yO-26eZ-CI82tb
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2013-11/93165p2.htm
相关阅读:
磁盘管理——RAID 0 http://www.linuxidc.com/Linux/2013-08/88577.htm
磁盘管理——RAID 1 http://www.linuxidc.com/Linux/2013-08/88616.htm
磁盘管理——RAID 10 http://www.linuxidc.com/Linux/2013-08/89039.htm
磁盘管理——RAID 5 http://www.linuxidc.com/Linux/2013-08/89040.htm
LVM磁盘管理之扩展与缩小LV http://www.linuxidc.com/Linux/2013-03/81262.htm
四 逻辑卷相关操作
#创建逻辑卷
[root@serv01 ~]# lvcreate -L 500M -n mylv01myvg
Logical volume “mylv01” created
[root@serv01 ~]# lvdisplay
— Logical volume —
LVName /dev/myvg/mylv01
VGName myvg
LVUUID zjfjIV-S3Kv-dkHZ-xKOC-wPV7-nG3H-QuaKGU
LVWrite Access read/write
LVStatus available
#open 0
LVSize 500.00 MiB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
-currently set to 256
Block device 253:0
五 LVM使用
#格式化
[root@serv01 ~]# mkfs.ext4 /dev/myvg/mylv01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the superuser
First data block=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments pergroup
2032 inodes per group
Superblock backups stored on blocks:
8193,24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystemaccounting information: done
This filesystem will be automaticallychecked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
#挂载
[root@serv01 ~]# mount /dev/myvg/mylv01/web/
[root@serv01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/mapper/myvg-mylv01
485M 11M 449M 3% /web
[root@serv01 ~]# cd /boot
[root@serv01 boot]# cd /web/
[root@serv01 web]# ls
lost+found
#拷贝文件
[root@serv01 web]# cp /boot/* ./ -rf
[root@serv01 web]# ls
config-2.6.32-131.0.15.el6.x86_64 initramfs-2.6.32-131.0.15.el6.x86_64.img System.map-2.6.32-131.0.15.el6.x86_64
efi lost+found vmlinuz-2.6.32-131.0.15.el6.x86_64
grub symvers-2.6.32-131.0.15.el6.x86_64.gz
#追加文件到/etc/fstab
[root@serv01 web]# echo”/dev/myvg/mylv01 /web ext4 defaults 1 2″ >> /etc/fstab
[root@serv01 web]# tail -n 1 /etc/fstab
/dev/myvg/mylv01 /web ext4 defaults 1 2
[root@serv01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/mapper/myvg-mylv01
485M 29M 431M 7% /web
/dev/sr0 3.4G 3.4G 0 100% /iso
#重启,到/web目录下查看文件
[root@serv01 web]# reboot
[root@larrywen disk]# ssh 192.168.1.11
root@192.168.1.11’s password:
Last login: Thu Aug 1 21:48:24 2013 from 192.168.1.1
[root@larrywen disk]# cd /web
config-2.6.32-131.0.15.el6.x86_64 lost+found System.map-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
[root@serv01 web]# lvextend —help
六 增加逻辑卷
[root@serv01 web]# lvextend -L +500M/dev/myvg/mylv01
Extending logical volume mylv01 to 1000.00 MiB
Logical volume mylv01 successfully resized
#lvdisplay
[root@serv01 web]# lvdisplay
—Logical volume —
LVName /dev/myvg/mylv01
VGName myvg
LVUUID zjfjIV-S3Kv-dkHZ-xKOC-wPV7-nG3H-QuaKGU
LVWrite Access read/write
LVStatus available
#open 1
LVSize 1000.00 MiB
Current LE 250
Segments 1
Allocation inherit
Read ahead sectors auto
-currently set to 256
Block device 253:0
#查看磁盘发现大小没变
[root@serv01 web]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/mapper/myvg-mylv01
485M 29M 431M 7% /web
/dev/sr0 3.4G 3.4G 0 100% /iso
[root@serv01 web]# cd /
#卸载
[root@serv01 /]# umount /web
#执行resize2fs命令更新
[root@serv01 /]# resize2fs /dev/myvg/mylv01
resize2fs 1.41.12 (17-May-2010)
Please run ‘e2fsck -f /dev/myvg/mylv01’first.
#发现有提示,先执行e2fsck -f/dev/myvg/mylv01
[root@serv01 /]# e2fsck -f /dev/myvg/mylv01
e2fsck 1.41.12 (17-May-2010)
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/myvg/mylv01: 16/128016 files (0.0%non-contiguous), 45237/512000 blocks
#再次执行上述命令,再次mount,再次查看磁盘空间,发现磁盘变大
[root@serv01 /]# resize2fs /dev/myvg/mylv01
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv01to 1024000 (1k) blocks.
The filesystem on /dev/myvg/mylv01 is now1024000 blocks long.
[root@serv01 /]# mount /dev/myvg/mylv01/web
[root@serv01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/mapper/myvg-mylv01
969M 29M 891M 4% /web
七 减少空间
#减少空间——危险操作,很可能数据被破坏
[root@serv01 /]# lvreduce -L -700M/dev/myvg/mylv01
WARNING: Reducing active and open logical volume to 300.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv01? [y/n]:y
Reducing logical volume mylv01 to 300.00 MiB
Logical volume mylv01 successfully resized
[root@serv01 /]# lvdisplay
—Logical volume —
LVName /dev/myvg/mylv01
VGName myvg
LVUUID zjfjIV-S3Kv-dkHZ-xKOC-wPV7-nG3H-QuaKGU
LVWrite Access read/write
LVStatus available
#open 1
LVSize 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
-currently set to 256
Block device 253:0
[root@serv01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/mapper/myvg-mylv01
969M 29M 891M 4% /web
[root@serv01 /]# resize2fs /dev/myvg/mylv01
resize2fs 1.41.12 (17-May-2010)
Please run ‘e2fsck -f /dev/myvg/mylv01’first.
[root@serv01 /]# e2fsck -f /dev/myvg/mylv01
e2fsck 1.41.12 (17-May-2010)
The filesystem size (according to thesuperblock) is 1024000 blocks
The physical size of the device is 307200blocks
Either the superblock or the partitiontable is likely to be corrupt!
Abort<y>? Yes
[root@serv01 /]# resize2fs /dev/myvg/mylv01300
resize2fs 1.41.12 (17-May-2010)
resize2fs: New size smaller than minimum(34402)
[root@serv01 /]# mount /dev/myvg/mylv01/web
mount: wrong fs type, bad option, badsuperblock on /dev/mapper/myvg-mylv01,
missing codepage or helper program, or other error
In some cases useful info is found in syslog – try
dmesg | tail or so
[root@serv01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
[root@serv01 /]# lvremove /dev/myvg/mylv01
[root@serv01 /]# vgremove myvg
[root@serv01 /]# vgcreate myvg/dev/sd[bcd]1
Volume group “myvg” successfully created
[root@serv01 /]# lvcreate -L 1000M -nmylv01 myvg
Logical volume “mylv01” created
[root@serv01 /]# lvreduce -L -700M/dev/myvg/mylv01^C
[root@serv01 /]# umount /web
umount: /web: not mounted
[root@serv01 /]# lvreduce -L 700M/dev/myvg/mylv01
WARNING: Reducing active logical volume to 700.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv01? [y/n]:y
Reducing logical volume mylv01 to 700.00 MiB
Logical volume mylv01 successfully resized
[root@serv01 /]# resize2fs -f/dev/myvg/mylv01 17323
八 添加磁盘
[root@serv01 /]# pvcreate /dev/sde1
Physical volume “/dev/sde1” successfully created
[root@serv01 /]# vgextend myvg /dev/sde1
Volume group “myvg” successfully extended
[root@serv01 /]# lvextend -L +2G/dev/myvg/mylv01
Extending logical volume mylv01 to 2.68 GiB
Logical volume mylv01 successfully resized
[root@serv01 /]# resize2fs /dev/myvg/mylv01
[root@serv01 /]# resize2fs /dev/myvg/mylv01
resize2fs 1.41.12 (17-May-2010)
resize2fs: Bad magic number in super-blockwhile trying to open /dev/myvg/mylv01
Couldn’t find valid filesystem superblock.
九 减少(移除)磁盘
#减少(移除)磁盘:减少的磁盘不影响存储
[root@serv01 /]# pvmove /dev/sdb1
[root@serv01 /]# vgreduce myvg /dev/sdb1
[root@serv01 /]# pvremove /dev/sdb1
[root@serv01 /]# df -h
十 销毁磁盘
[root@serv01 /]# lvremove /dev/myvg/mylv01
[root@serv01 /]# vgremove myvg
[root@serv01 /]# pvremove /dev/sdc1
[root@serv01 /]# pvremove /dev/sdd1
[root@serv01 /]# pvremove /dev/sde1