本文共 9467 字,大约阅读时间需要 31 分钟。
LVM:Logical Volume Manager(逻辑卷管理)
许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统分区不够用时可能不得不备份、删除相关数据,甚至被迫重新规划分区并重装操作系统,以满足应用系统的需要。
LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性。RHEL5默认安装的分区格式就是LVM逻辑卷的格式,需要注意的是/boot分区不能基于LVM创建,必须独立出来。
LVM包含:PV、VG、LV
PV:Physical Volume
这是LVM最低层设备,即多块物理磁盘。
VG:Volume group
使用多块物理磁盘来组成卷组,便于KVM做卷管理,但此时还不能直接使用,相当于扩展分区。
LV:logical Volume
LV则是将VG创建为逻辑卷,来供使用。在一个VG中可以创建多个逻辑卷。
因此,当LV不够用的时候,即可以在不超过VG边界的情况下扩展,而VG不够用的时候,则只需要在VG中添加PV即可。
PV管理命令
pvcreate:创建PV
pv在没有加入vg时,没有PE信息。
pvs:显示PV信息
pvdisplay /dev/vdb:显示pv详细信息
pvcsan:查看系统有多少pv
pvremove:清除pv上的数据
pvmove:移动pv上的数据到目标pv
VG管理命令
vgcreate:创建vg
-s #:创建时指定PE大小,默认为4M
vgremove:删除vg
vgs:显示vg信息
vgdisplay:显示vg详细信息
vgextend:扩展vg即增加pv进vg
vgreduce:缩减vg即删除pv
vgscan:查看系统上的vg
LV管理命令
lvcreate:创建lv
-n:指定lv名称
-L:指定lv大小
lvremove:删除lv
lvextend:扩展lv
lvreduce:缩减lv
lvresize:减小ls
lvs:显示lv信息
创建PV,3G、2G、4G的三个PV
#使用fdisk创建三个分区,需要把分区类型修改为8e[root@liang-study ~]# fdisk /dev/vdb/dev/vdb5 1 6243 3146409 83 Linux/dev/vdb6 6244 10405 2097616+ 83 Linux/dev/vdb7 10406 18728 4194760+ 83 Linux#修改分区类型Command (m for help): tPartition number (1-7): 5Hex code (type L to list codes): 8eChanged system type of partition 5 to 8e (Linux LVM) Device Boot Start End Blocks Id System/dev/vdb5 1 6243 3146409 8e Linux LVM/dev/vdb6 6244 10405 2097616+ 8e Linux LVM/dev/vdb7 10406 18728 4194760+ 8e Linux LVM#手动使内核重读文件系统[root@liang-study ~]# partprobe /dev/vdb[root@liang-study ~]# cat /proc/partitions 252 21 3146409 vdb5 252 22 2097616 vdb6 252 23 4194760 vdb7#创建PV[root@liang-study ~]# pvcreate /dev/vdb5 /dev/vdb6 /dev/vdb7 Physical volume "/dev/vdb5" successfully created Physical volume "/dev/vdb6" successfully created Physical volume "/dev/vdb7" successfully created[root@liang-study ~]# pvs PV VG Fmt Attr PSize PFree /dev/vdb5 lvm2 ---- 3.00g 3.00g /dev/vdb6 lvm2 ---- 2.00g 2.00g /dev/vdb7 lvm2 ---- 4.00g 4.00g
创建vg
#创建vg[root@liang-study ~]# vgcreate liangstudy /dev/vdb5 /dev/vdb6 Volume group "liangstudy" successfully created[root@liang-study ~]# vgs VG #PV #LV #SN Attr VSize VFree liangstudy 2 0 0 wz--n- 4.99g 4.99g[root@liang-study ~]# vgdisplay liangstudy --- Volume group --- VG Name liangstudy System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 4.99 GiB PE Size 4.00 MiB Total PE 1278 Alloc PE / Size 0 / 0 Free PE / Size 1278 / 4.99 GiB VG UUID 7E2oE3-j6hl-QAmF-tfhv-Qa89-KL40-Fs1D1l#移除vg[root@liang-study ~]# vgremove liangstudy Volume group "liangstudy" successfully removed#缩减vg[root@liang-study ~]# vgreduce liangstudy /dev/vdb6 Removed "/dev/vdb6" from volume group "liangstudy"#扩展vg[root@liang-study ~]# vgextend liangstudy /dev/vdb6 Volume group "liangstudy" successfully extended
创建lv
#创建lv[root@liang-study ~]# lvcreate -n lianglv -L 50M liangstudy Rounding up size to full physical extent 56.00 MiB Logical volume "lianglv" created.[root@liang-study ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lianglv liangstudy -wi-a----- 56.00m #格式化lv[root@liang-study ~]# mke2fs -j /dev/liangstudy/lianglv #挂载lv[root@liang-study ~]# mount /dev/liangstudy/lianglv /mnt[root@liang-study ~]# df -PhFilesystem Size Used Avail Use% Mounted on/dev/vda2 30G 3.3G 25G 12% /tmpfs 939M 0 939M 0% /dev/shm/dev/vda1 190M 58M 122M 33% /boot/dev/vda4 45G 210M 43G 1% /home/dev/mapper/liangstudy-lianglv 55M 5.0M 47M 10% /mnt#移除lv[root@liang-study ~]# lvremove /dev/liangstudy/lianglv Do you really want to remove active logical volume lianglv? [y/n]: y Logical volume "lianglv" successfully removed 注:移除前要求先卸载mount
扩展逻辑卷
扩展逻辑卷需要先扩展物理边界,再扩展逻辑边界。
lvextend
-L [+]#:[扩展了]扩展到#大小。
resize2fs -p
#挂载逻辑卷[root@liang-study ~]# mount /dev/mapper/liangstudy-lianglv /lvtest/#复制文件到挂载点,以便测试[root@liang-study ~]# cp /var/log/messages /lvtest/[root@liang-study ~]# cp /var/log/lastlog /lvtest/查看文件系统使用情况,将lvtest逻辑卷扩展到2G[root@liang-study lvtest]# df -PhFilesystem Size Used Avail Use% Mounted on/dev/vda2 30G 3.3G 25G 12% /tmpfs 939M 0 939M 0% /dev/shm/dev/vda1 190M 58M 122M 33% /boot/dev/vda4 45G 210M 43G 1% /home/dev/mapper/liangstudy-lianglv 55M 5.0M 47M 10% /lvtest#扩展逻辑卷,物理边界,扩展前一定要确认vg中剩余空间大于要扩展的空间[root@liang-study lvtest]# lvextend -L 2G /dev/liangstudy/lianglv Size of logical volume liangstudy/lianglv changed from 56.00 MiB (7 extents) to 2.00 GiB (256 extents). Logical volume lianglv successfully resized. 注:此时使用df -Ph查看lvtest,文件系统大小依然为55M。因为这里扩展的是物理边界,这里显示的不是lv的大小,此时通过lvs查看,即可看到大小已经添加[root@liang-study lvtest]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lianglv liangstudy -wi-ao---- 2.00g #扩展逻辑卷,逻辑边界[root@liang-study lvtest]# resize2fs -p /dev/liangstudy/lianglv resize2fs 1.41.12 (17-May-2010)Filesystem at /dev/liangstudy/lianglv is mounted on /lvtest; on-line resizing requiredold desc_blocks = 1, new_desc_blocks = 8Performing an on-line resize of /dev/liangstudy/lianglv to 2097152 (1k) blocks.The filesystem on /dev/liangstudy/lianglv is now 2097152 blocks long.[root@liang-study lvtest]# df -PHFilesystem Size Used Avail Use% Mounted on/dev/vda2 32G 3.5G 27G 12% /tmpfs 985M 0 985M 0% /dev/shm/dev/vda1 199M 61M 128M 33% /boot/dev/vda4 49G 220M 46G 1% /home/dev/mapper/liangstudy-lianglv 2.1G 7.0M 2.0G 1% /lvtest#查看之前复制的文件[root@liang-study lvtest]# tail -2 /lvtest/messages Dec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): using internal journalDec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): mounted filesystem with ordered data mode
缩减逻辑卷
缩减逻辑卷需要先缩减逻辑边界,再缩减物理边界。不能在线缩减,要先卸载。缩减操作非常危险
在缩减之前应该先检查文件系统
resize2fs -p
lvreduce
-L [-]#:[扩展了]扩展到#大小。
#卸载文件系统[root@liang-study ~]# umount /lvtest/#强行监测文件系统[root@liang-study ~]# e2fsck -f /dev/liangstudy/lianglv e2fsck 1.41.12 (17-May-2010)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/liangstudy/lianglv: 13/524288 files (7.7% non-contiguous), 72967/2097152 blocks#缩减lv的逻辑边界,将lvtest逻辑卷缩减至1G[root@liang-study ~]# resize2fs /dev/liangstudy/lianglv 1Gresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/liangstudy/lianglv to 1048576 (1k) blocks.The filesystem on /dev/liangstudy/lianglv is now 1048576 blocks long.#缩减lv物理边界[root@liang-study ~]# lvreduce -L 1G /dev/liangstudy/lianglv WARNING: Reducing active logical volume to 1.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce liangstudy/lianglv? [y/n]: y Size of logical volume liangstudy/lianglv changed from 2.00 GiB (256 extents) to 1.00 GiB (128 extents). Logical volume lianglv successfully resized.#重新挂在文件系统[root@liang-study ~]# mount /dev/liangstudy/lianglv /lvtest/#查看文件是否正常[root@liang-study lvtest]# tail -2 messages Dec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): using internal journalDec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): mounted filesystem with ordered data mode
逻辑卷快照
lvcreate:快照卷创建大小应该大于快照卷声明周期内数据增长量
-s:指定创建快照卷
-p r|w:指定快照卷权限,快照卷必须创建在原卷同一vg中,快照卷应该为只读的
#创建快照卷[root@liang-study /]# lvcreate -L 50M -n lvtest-snap -s -p r /dev/liangstudy/lianglv Rounding up size to full physical extent 56.00 MiB Logical volume "lvtest-snap" created.#查看快照卷[root@liang-study /]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lianglv liangstudy owi-aos--- 1.00g lvtest-snap liangstudy sri-a-s--- 56.00m lianglv 0.02 #挂在快照卷[root@liang-study /]# mount /dev/liangstudy/lvtest-snap /lvtest-snap/mount: block device /dev/mapper/liangstudy-lvtest--snap is write-protected, mounting read-only#对源文件进行修改[root@liang-study lvtest-snap]# echo "new line." >> /lvtest/messages #备份快照卷[root@liang-study lvtest-snap]# tar -zcf /tmp/lvtest.tar.gz lastlog messages #删除快照卷[root@liang-study ~]# lvremove /dev/liangstudy/lvtest-snap Do you really want to remove active logical volume lvtest-snap? [y/n]: y Logical volume "lvtest-snap" successfully removed#模拟源文件损坏[root@liang-study ~]# rm -rf /lvtest/lastlog /lvtest/messages #恢复快照[root@liang-study ~]# tar -zxvf /tmp/lvtest.tar.gz -C /lvtest/#查看文件恢复情况[root@liang-study lvtest]# tail -2 messages Dec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): using internal journalDec 27 17:59:21 liang-study kernel: EXT3-fs (dm-0): mounted filesystem with ordered data mode可以看到,恢复的状态即为快照备份的状态。之后的修改因为没有增量备份,因此不能恢复。
转载地址:http://dnkia.baihongyu.com/