LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。
相关概念:
物理卷PV:是指硬盘分区或者从逻辑上看起来和硬盘分区类似的设备(比如RAID设备)。
逻辑卷LV:一个或者多个物理卷组成一个逻辑卷。
卷组VG:一个或者多个逻辑卷组成一个卷组。卷组把多个逻辑卷组合在一起,形成一个可管理的单元。
物理块PE:物理卷按大小相等的“块”为单位存储,块的大小与卷组中逻辑卷块的大小相同。
逻辑块LE:逻辑卷按“块”为单位存储,在一卷组中的所有逻辑卷的块大小是相同的。
- 创建物理分区
- 在使用LVM之前,需要首先划分磁盘分区,也就是用fdisk命令划分磁盘分区,需要指定分区类型为linux LVM,对应的ID为8e(其实LVM也能识别linux默认的分区类型83)
- [root@rhel5 ~]# fdisk -l
- Disk /dev/sda: 21.4 GB, 21474836480 bytes
- 255 heads, 63 sectors/track, 2610 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 78 522112+ 82 Linux swap / Solaris
- /dev/sda3 79 1383 10482412+ 8e Linux LVM
- /dev/sda4 1384 2610 9855877+ 5 Extended
- /dev/sda5 1384 1994 4907826 83 Linux
- [root@rhel5 ~]# fdisk -t 8e /dev/sda5
- 创建物理卷PV
- 创建物理卷的命令是pvcreate,通过该命令可以将希望添加到卷组(VG)的所有磁盘分区或者整个磁盘创建为物理卷。使用格式为:
- pvcreate 磁盘分区或整个磁盘
- [root@rhel5 ~]# pvcreate /dev/sda5
- Physical volume “/dev/sda5” successfully created
- 创建/激活卷组VG
- 创建卷组的命令是vgcreate,使用格式为:
- vgcreate 卷组名 物理卷
- [root@rhel5 ~]# vgcreate iscsi /dev/sda5
- Volume group“iscsi” successfully created
- 卷组创建完毕后,可以通过vgchange命令激活卷组,而无需重启系统。Vgchange使用格式如下:
- vgchange -a y 卷组名 (激活卷组)
- vgchange -a n 卷组名 (停用卷组)
- 创建逻辑卷LV
- 创建逻辑卷的命令是lvcreate,常用的使用格式为:
- lvcreate [-L 逻辑卷大小| -l PE数 ] –n 逻辑卷名称 所属的卷组名
- 其中:
- -L:后面接逻辑卷的大小,可以用K、M、G表示。例如100M、10G等
- -l:用PE数来计算逻辑卷的大小。
- [root@rhel5 ~]# lvcreate -n sharedisk -L 2G iscsi
- Logical volume “sharedisk” created
- 显示PV、VG、LV的属性信息
- [root@rhel5 ~]# pvs
- PV VG Fmt Attr PSize PFree
- /dev/sda3 rootvg lvm2 a- 9.97G 0
- /dev/sda5 iscsi lvm2 a- 4.68G 4.68G
- [root@rhel5 ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- iscsi 1 0 0 wz–n- 4.68G 4.68G
- rootvg 1 1 0 wz–n- 9.97G 0
- [root@rhel5 ~]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- sharedisk iscsi -wi-ao 2.00G
- rootlv rootvg -wi-ao 9.97G
- 相应的也可通过pvdisplay、vgdisplay、lvdisplay查看更详细的信息
- 格式化逻辑卷,创建文件系统
- [root@rhel5 ~]# mkfs.ext3 /dev/iscsi/sharedisk
- mke2fs 1.39 (29-May-2006)
- Filesystem label=
- OS type: Linux
- Block size=4096 (log=2)
- Fragment size=4096 (log=2)
- 262144 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
- 16384 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 38 mounts or
- 180 days, whichever comes first. Use tune2fs -c or -i to override.
- [root@rhel5 ~]# tune2fs -c 0 -i 0 /dev/iscsi/sharedisk
- tune2fs 1.39 (29-May-2006)
- Setting maximal mount countto -1
- Setting interval between checks to 0 seconds
- [root@rhel5 ~]#mkdir /data
- [root@rhel5 ~]# mount /dev/iscsi/sharedisk /data/
- [root@rhel5 ~]# df -h
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/mapper/rootvg-rootlv 9.7G 7.6G 1.7G 83% /
- /dev/sda1 99M 14M 80M 15% /boot
- tmpfs 322M 0 322M 0% /dev/shm
- /dev/mapper/iscsi-sharedisk 2.0G 68M 1.9G 4% /data
================================================================================
- 添加新的PV到VG
- 紧接着上面的讲解实例,通过fdisk新建一个磁盘分区,对应的分区为/dev/sda6,现在要把新增的硬盘分区加入到卷组iscsi,操作如下:
- [root@rhel5 ~]# vgdisplay iscsi | grep -i free //首先检查iscsi卷组中的空闲空间
- Free PE / Size 686 / 2.68 GB
- [root@rhel5 ~]# pvcreate /dev/sda6 //先将/dev/sda6转换为物理卷
- Physical volume “/dev/sda6” successfully created
- [root@rhel5 ~]# vgextend iscsi /dev/sda6 //将新增物理卷添加到卷组iscsi中
- Volume group“iscsi” successfully extended
- [root@rhel5 ~]# vgdisplay iscsi | grep -i free //此时剩余卷组空间还有4.55G,刚好是新增物理卷的大小
- Free PE / Size 1164 / 4.55 GB
- 在线扩展LV的大小
- LVM最主要的功能就是能动态的调整分区的大小,其实也就是修改逻辑卷的大小
- lvresize [-L (+size) –l (+PE数)] 逻辑卷名称
- 扩展逻辑卷sharedisk的大小到5G,执行以下命令:
- [root@rhel5 ~]# lvresize -L 5G /dev/iscsi/sharedisk
- [root@rhel5 ~]# resize2fs /dev/iscsi/sharedisk
- resize2fs 1.39 (29-May-2006)
- resize2fs: Bad magic number in super-block while trying toopen /dev/iscsi/sharedisk
- Couldn’t find valid filesystem superblock.
- 缩小LV的大小
- 缩小LV必须先缩小LV所在的文件系统(resize2fs)才能进一步缩小LV(lvresize)
- 缩小逻辑卷sharediak的大小为3G
- [root@rhel5 ~]# umount /dev/iscsi/sharedisk
- [root@rhel5 ~]# e2fsck -f /dev/iscsi/sharedisk //检查文件系统的准确性
- e2fsck 1.39 (29-May-2006)
- Couldn’t find ext2 superblock, trying backup blocks…
- 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/iscsi/sharedisk: ***** FILE SYSTEM WAS MODIFIED *****
- /dev/iscsi/sharedisk: 11/262144 files (9.1% non-contiguous), 25406/524288 blocks
- [root@rhel5 ~]# resize2fs /dev/iscsi/sharedisk 3G //缩小文件系统的大小为3G
- resize2fs 1.39 (29-May-2006)
- Resizing the filesystem on /dev/iscsi/sharedisk to 786432 (4k) blocks.
- The filesystem on /dev/iscsi/sharedisk is now 786432 blocks long.
- [root@rhel5 ~]# lvresize -L 3G /dev/iscsi/sharedisk //缩小逻辑卷sharedisk的大小为3G
- WARNING: Reducing active logical volume to 3.00 GB
- THIS MAY DESTROY YOUR DATA (filesystem etc.)
- Do you really want to reduce sharedisk? [y/n]: y
- Reducing logical volume sharedisk to 3.00 GB
- Logical volume sharedisk successfully resized
- [root@rhel5 ~]# mount /dev/iscsi/sharedisk /data/
- 删除LV、VG、PV
- 删除物理卷的命令是pvremove,将物理卷从卷组移除的命令是vgreduce,删除卷组的命令是vgremove,同理,删除逻辑卷的命令是lvremove。
- 删除一个卷组的顺序是:卸载逻辑卷分区–>删除卷组上所有逻辑卷–>删除卷组
- [root@rhel5 ~]# umount /dev/iscsi/sharedisk
- [root@rhel5 ~]# lvremove /dev/iscsi/sharedisk
- Do you really want to remove active logical volume sharedisk? [y/n]: y
- Logical volume “sharedisk” successfully removed
- [root@rhel5 ~]# vgchange -a n iscsi //停用卷组iscsi
- 0 logical volume(s) in volume group“iscsi” now active
- [root@rhel5 ~]# vgreduce iscsi /dev/sda6 //从卷组iscsi 中移除一个/dev/sda6的物理卷
- Removed “/dev/sda6”from volume group“iscsi”
- [root@rhel5 ~]# vgs iscsi
- VG #PV #LV #SN Attr VSize VFree
- iscsi 1 0 0 wz–n- 4.68G 4.68G
- [root@rhel5 ~]# vgremove iscsi /dev/sda5
- Volume group“iscsi” successfully removed
- [root@rhel5 ~]# pvremove /dev/sda5
- Labels on physical volume “/dev/sda5” successfully wiped
- [root@rhel5 ~]# pvremove /dev/sda6
- Labels on physical volume “/dev/sda6” successfully wiped
LVM快照:
LVM快照是基于存储块基本的,而不是文件系统级别的,所以当一个snapshot创建的时候,仅拷贝原始卷里数据的元数据(meta-data)。创建的时候,并不会有数据的物理拷贝,因此snapshot的创建几乎是实时的,当原始卷上有写操作执行时,snapshot跟踪原始卷块的改变,这个时候原始卷上将要改变的数据在改变之前被拷贝到snapshot预留的空间里,因此这个原理的实现叫做写时复制(copy-on-write)。
在写操作写入块之前,CoW将原始数据移动到 snapshot空间里,这样就保证了所有的数据在snapshot创建时保持一致。而对于snapshot的读操作,如果是读取数据块是没有修改过的,那么会将读操作直接重定向到原始卷上,如果是要读取已经修改过的块,那么就读取拷贝到snapshot中的块。
采取CoW实现方式时,snapshot的大小并不需要和原始卷一样大,其大小仅仅只需要考虑两个方面:从shapshot创建到释放这段时间内,估计块的改变量有多大;数据更新的频率。一旦 snapshot的空间记录满了原始卷块变换的信息,那么这个snapshot立刻被释放,从而无法使用,从而导致这个snapshot无效。
- [root@rhel5 ~]# pvcreate /dev/sda{5,6}
- Physical volume “/dev/sda5” successfully created
- Physical volume “/dev/sda6” successfully created
- [root@rhel5 ~]# vgcreate datavg /dev/sda{5,6}
- Volume group“datavg” successfully created
- [root@rhel5 ~]# lvcreate -n datalv -L 500M datavg
- Logical volume “datalv” created
- [root@rhel5 ~]# mkfs.ext3 /dev/datavg/datalv
- mke2fs 1.39 (29-May-2006)
- Filesystem label=
- OS type: Linux
- Block size=1024 (log=0)
- Fragment size=1024 (log=0)
- 128016 inodes, 512000 blocks
- 25600 blocks (5.00%) reserved for the super user
- First data block=1
- Maximum filesystem blocks=67633152
- 63 block groups
- 8192 blocks per group, 8192 fragments per group
- 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 filesystem accounting information: done
- This filesystem will be automatically checked every 27 mounts or
- 180 days, whichever comes first. Use tune2fs -c or -i to override.
- [root@rhel5 ~]# mount /dev/datavg/datalv /data
- [root@rhel5 ~]# dd if=/dev/zero of=/data/test.img bs=10M count=10
- 10+0 records in
- 10+0 records out
- 104857600 bytes (105 MB) copied, 4.79465 seconds, 21.9 MB/s
- [root@rhel5 ~]# lvcreate -L 400M -n snaplv -s /dev/datavg/datalv
- Logical volume “snaplv” created
- [root@rhel5 ~]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- datalv datavg owi-ao 500.00M
- snaplv datavg swi-a- 400.00M datalv 0.00
- rootlv rootvg -wi-ao 9.97G
- [root@rhel5 ~]# mount /dev/datavg/snaplv /data_snap/
- [root@rhel5 ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/rootvg-rootlv
- 9.7G 3.4G 5.8G 38% /
- /dev/sda1 99M 14M 80M 15% /boot
- tmpfs 212M 0 212M 0% /dev/shm
- /dev/mapper/datavg-datalv
- 485M 111M 354M 24% /data
- /dev/mapper/datavg-snaplv
- 485M 111M 354M 24% /data_snap
- [root@rhel5 ~]# ls /data /data_snap/
- /data:
- lost+found test.img
- /data_snap/:
- lost+found test.img
- [root@rhel5 ~]# dd if=/dev/zero of=/data/test2.img bs=10M count=10
- 10+0 records in
- 10+0 records out
- 104857600 bytes (105 MB) copied, 6.50751 seconds, 16.1 MB/s
- [root@rhel5 ~]#
- [root@rhel5 ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/rootvg-rootlv
- 9.7G 3.4G 5.8G 38% /
- /dev/sda1 99M 14M 80M 15% /boot
- tmpfs 212M 0 212M 0% /dev/shm
- /dev/mapper/datavg-datalv
- 485M 212M 254M 46% /data
- /dev/mapper/datavg-snaplv
- 485M 111M 354M 24% /data_snap
- [root@rhel5 ~]# ls /data /data_snap/
- /data:
- lost+found test2.img test.img
- /data_snap/:
- lost+found test.img
- [root@rhel5 ~]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- datalv datavg owi-ao 500.00M
- snaplv datavg swi-ao 400.00M datalv 24.76
- rootlv rootvg -wi-ao 9.97G
- [root@rhel5 ~]# rm -rf /data/*
- [root@rhel5 ~]# dd if=/dev/zero of=/data/snap.img bs=40M count=10 //此时datalv上的变化量将超过快照snaplv的大小400M,则snaplv将失效
- [root@rhel5 ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/rootvg-rootlv
- 9.7G 3.4G 5.8G 38% /
- /dev/sda1 99M 14M 80M 15% /boot
- tmpfs 212M 0 212M 0% /dev/shm
- /dev/mapper/datavg-datalv
- 485M 400M 85M 82% /data
- /dev/mapper/datavg-snaplv
- 485M 111M 354M 24% /data_snap
- [root@rhel5 ~]# lvs
- /dev/datavg/snaplv: read failed after 0 of 2048 at 0: Input/output error
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- datalv datavg owi-ao 500.00M
- snaplv datavg Swi-Io 400.00M datalv 100.00
- rootlv rootvg -wi-ao 9.97G
- [root@rhel5 ~]# rm -rf /data_snap/test.img //快照失效,里面的文件已无法读取
- rm: cannot remove `/data_snap/test.img’: Read–only file system
- [root@rhel5 ~]# mount -o remount,rw /data_snap/
- mount: block device /dev/mapper/datavg-snaplv is write-protected, mounting read–only
- [root@rhel5 ~]# lvremove /dev/datavg/snaplv //快照失效,只能移除LV
- /dev/cdrom: open failed: Read–only file system
- /dev/datavg/snaplv: read failed after 0 of 4096 at 0: Input/output error
- Do you really want to remove active logical volume snaplv? [y/n]: y
- Logical volume “snaplv” successfully removed
可以将LVM快照应用在KVM虚拟机中,创建一个LV作为原始映像,为该原始映像创建LV快照作为虚拟机的磁盘映像,这样可以迅速克隆系统(创建快照的时间常常不超过几秒)且节省磁盘空间(guest共享原始映像的大多数数据)。即使系统出现问题,只要移除快照��可。