LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。分为lvm,lvm2两个版本。
lvm2使用dm(device mapper模块),将一个或多个底层设备组织成一个逻辑的工具
底层可以是任何块设备,可以是分区、raid
pe:物理盘区,pysical extent
存储空间边界:
物理边界:
逻辑边界:
逻辑卷:
扩展:先物理,后逻辑
缩减:先逻辑,后物理
缩减不能少于已经存储的数据空间大小
快照卷也可以挂载到某个访问点
快照卷在刚创建时没有数据,访问快照卷其实是映射到原卷上了,其实是访问原卷的第二个通路。
如果改变原卷的数据,原卷上的数据在改之前,会复制到快照卷,所以快照卷不必和原卷一样大
快照卷没有永存的目的,只是备份
pv:
pvcreate,pvs,pvdisplay,pvremove,pvmove,pvscan,
vg:
vgcreate,vgs,vgdisplay,vgremove,vgextend,vgreduce,vgscan
lv:
lvcreate,lvs,lvdisplay,lvremove,lvextend,lvreduce,lvscan
实验:创建10G的vg
[root@node1 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x6f1459ef. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): p Disk /dev/sdb: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x6f1459ef Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-15665, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-15665, default 15665): +3G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (394-15665, default 394): Using default value 394 Last cylinder, +cylinders or +size{K,M,G} (394-15665, default 15665): +7G Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
查看分区:
[root@node1 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 10240000 sda2 8 3 512000 sda3 8 4 1 sda4 8 5 114870272 sda5 8 16 125829120 sdb 8 17 3156741 sdb1 8 18 7349737 sdb2
创建pv:
[root@node1 ~]# pvcreate /dev/sdb{1,2} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [root@node1 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 3.01g 3.01g /dev/sdb2 lvm2 --- 7.01g 7.01g [root@node1 ~]# pvdisplay "/dev/sdb1" is a new physical volume of "3.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 3.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID f3ko3z-2YZ8-ghkh-jPOE-C3RI-AKVi-Z5Hjkm "/dev/sdb2" is a new physical volume of "7.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 7.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID KsP0wm-X0KJ-ZkwP-3l3G-lVAA-peS0-UjmRoN
创建vg:
vgcreate [-s N[mgt]] VG名称 物理设备路径,-s指定pe大小
[root@node1 ~]# vgcreate myvg /dev/sdb1 Volume group "myvg" successfully created [root@node1 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 1 0 0 wz--n- 3.01g 3.01g [root@node1 ~]# pvdisplay /dev/sdb1 --- Physical volume --- PV Name /dev/sdb1 VG Name myvg PV Size 3.01 GiB / not usable 2.75 MiB Allocatable yes PE Size 4.00 MiB Total PE 770 Free PE 770 Allocated PE 0 PV UUID f3ko3z-2YZ8-ghkh-jPOE-C3RI-AKVi-Z5Hjkm
扩展vg:
[root@node1 ~]# vgextend myvg /dev/sdb2 Volume group "myvg" successfully extended [root@node1 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 10.02g 10.02g
lvcreate:
-n:lv_name
-L:size[mMgGtT]
VG_NAME
[root@node1 ~]# lvcreate -L 2G -n mylv myvg Logical volume "mylv" created [root@node1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 2.00g
逻辑卷被映射为一个设备:
[root@node1 ~]# ll /dev/mapper/ total 0 crw-rw---- 1 root root 10, 58 Dec 17 17:52 control lrwxrwxrwx 1 root root 7 Dec 17 18:09 myvg-mylv -> ../dm-0
逻辑卷的2个访问路径:
/dev/vg_name/lv_name
/dev/mapper/vg_name-lv_name
此二者均为符号链接,指向的文件为/dev/dm-#
[root@node1 ~]# ll /dev/mapper/myvg-mylv lrwxrwxrwx 1 root root 7 Dec 17 18:09 /dev/mapper/myvg-mylv -> ../dm-0 [root@node1 ~]# ll /dev/myvg/mylv lrwxrwxrwx 1 root root 7 Dec 17 18:09 /dev/myvg/mylv -> ../dm-0
分区、格式化:
[root@node1 ~]# mke2fs -t ext4 /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 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 8192 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 37 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@node1 ~]# mkdir /data [root@node1 ~]# mount /dev/myvg/mylv /data [root@node1 ~]# cd /data [root@node1 data]# ls lost+found [root@node1 data]# mount /dev/sda5 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) /dev/sda2 on /home type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/myvg-mylv on /data type ext4 (rw)
查看:
[root@node1 data]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 2.00g [root@node1 data]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 10.02g 8.02g
复制数据:
[root@node1 data]# cp /etc/rc.d/rc.sysinit /data [root@node1 data]# ls lost+found rc.sysinit
扩展:
1、先确定扩展的目标大小,并确保对应的卷组中有足够的空闲空间可用
2、扩展物理边界lvextend
3、扩展逻辑边界resize2fs
[root@node1 data]# lvextend -L 4G /dev/myvg/mylv Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents). Logical volume mylv successfully resized [root@node1 data]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 108G 1.3G 101G 2% / tmpfs 84M 0 84M 0% /dev/shm /dev/sda1 190M 25M 156M 14% /boot /dev/sda2 9.5G 22M 9.0G 1% /home /dev/mapper/myvg-mylv 2.0G 3.1M 1.9G 1% /data [root@node1 data]# resize2fs /dev/myvg/mylv resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /data; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv to 1048576 (4k) blocks. The filesystem on /dev/myvg/mylv is now 1048576 blocks long.
缩减:很危险,生产环境一般不缩减,缩减不能在线缩减。
1、先确定缩减后的目标大小,并确保对应的目标逻辑卷大小中有足够的空间可容纳原所有数据
2、先卸载文件系统,并要执行强制检测(e2fsck -f)
3、缩减逻辑边界resize2fs device 3G
4、缩减物理边界lvreduce
[root@node1 data]# cd [root@node1 ~]# umount /data [root@node1 ~]# e2fsck -f /dev/myvg/mylv 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/mylv: 12/262144 files (0.0% non-contiguous), 33875/1048576 blocks [root@node1 ~]# resize2fs /dev/myvg/mylv 3G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 786432 (4k) blocks. The filesystem on /dev/myvg/mylv is now 786432 blocks long. [root@node1 ~]# lvreduce -L 3G /dev/myvg/mylv WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y Size of logical volume myvg/mylv changed from 4.00 GiB (1024 extents) to 3.00 GiB (768 extents). Logical volume mylv successfully resized [root@node1 ~]# mount /dev/myvg/mylv /data [root@node1 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda5 108G 1.3G 101G 2% / tmpfs 84M 0 84M 0% /dev/shm /dev/sda1 190M 25M 156M 14% /boot /dev/sda2 9.5G 22M 9.0G 1% /home /dev/mapper/myvg-mylv 2.9G 3.1M 2.8G 1% /data [root@node1 ~]# cd /data [root@node1 data]# ls lost+found rc.sysinit
快照卷:
一般只能读
lvcreate:
-L:size
-n:name
-s:
-p:r
快照卷是对某逻辑卷进行的,因此必须跟目标逻辑卷在同一卷组中,所以无须指明卷组
注意:确保创建时,原卷没有修改
[root@node1 data]# mount -o remount,ro /dev/myvg/mylv /data [root@node1 data]# lvcreate -L 512M -n mylv-snap -p r -s /dev/myvg/mylv Logical volume "mylv-snap" created [root@node1 data]# mount -o remount,rw /dev/myvg/mylv /data [root@node1 data]# mkdir /snap [root@node1 data]# mount /dev/myvg/mylv-snap /snap mount: block device /dev/mapper/myvg-mylv--snap is write-protected, mounting read-only
修改原卷:
[root@node1 data]# vim rc.sysinit [root@node1 data]# tail -5 rc.sysinit if [ -x /bin/plymouth ]; then /bin/plymouth --sysinit fi new line
快照卷:
[root@node1 data]# cd /snap [root@node1 snap]# ls lost+found rc.sysinit [root@node1 snap]# tail -5 rc.sysinit # Let rhgb know that we're leaving rc.sysinit if [ -x /bin/plymouth ]; then /bin/plymouth --sysinit fi
复制文件到原卷并查看:
[root@node1 data]# cp /etc/fstab /data [root@node1 data]# ls fstab lost+found rc.sysinit
查看快照卷:
[root@node1 snap]# ls lost+found rc.sysinit
移除快照卷、lv、vg、pv:
[root@node1 snap]# cd [root@node1 ~]# umount /snap [root@node1 ~]# lvremove /dev/myvg/mylv-snap Do you really want to remove active logical volume mylv-snap? [y/n]: y Logical volume "mylv-snap" successfully removed [root@node1 ~]# umount /data [root@node1 ~]# lvremove myvg Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed [root@node1 ~]# vgremove myvg Volume group "myvg" successfully removed [root@node1 ~]# pvremove /dev/sdb{1,2} Labels on physical volume "/dev/sdb1" successfully wiped Labels on physical volume "/dev/sdb2" successfully wiped
做快照卷备份时,即把快照卷内的数据和原卷中的数据一起备份,然后再卸载、移除快照卷,卸载原卷并格式化它,重新挂载原卷,并还原备份。
原创文章,作者:黑白子,如若转载,请注明出处:http://www.178linux.com/63914