首页 > 代码库 > 磁盘管理之LVM
磁盘管理之LVM
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,从而提高磁盘分区管理的灵活性。接下来通过实验来详细了解LVM的使用.
-------------------------------------------------------------------------------------------------------
1.实验环境准备.添加一块未使用过的磁盘/dev/sdb,使用fdisk从其中分出两个5G的分区,并且将其分区类型该为8e
[root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x0f199ec4 Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 8e Linux LVM /dev/sdb2 655 1308 5253255 8e Linux LVM
2.创建物理卷:
[root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@localhost ~]# pvcreate /dev/sdb2 dev_is_mpath: failed to get device for 8:18 Physical volume "/dev/sdb2" successfully created [root@localhost ~]# pvscan PV /dev/sda2 VG vg0 lvm2 [59.99 GiB / 7.99 GiB free] PV /dev/sdb1 lvm2 [5.01 GiB] PV /dev/sdb2 lvm2 [5.01 GiB] Total: 3 [70.01 GiB] / in use: 1 [59.99 GiB] / in no VG: 2 [10.02 GiB]
3.在物理卷的基础上创建卷组,将/dev/sdb1加入卷组
[root@localhost ~]# vgcreate test /dev/sdb1 Volume group "test" successfully create [root@localhost ~]# vgscan #可以看到卷组test已经创建成功 Reading all physical volumes. This may take a while... Found volume group "test" using metadata type lvm2 Found volume group "vg0" using metadata type lvm2
4.在卷组test中创建一个大小为4G的逻辑卷test1
[root@localhost ~]# lvcreate -L 4G -n test1 test Logical volume "test1" created [root@localhost ~]# lvscan ACTIVE ‘/dev/test/test1‘ [4.00 GiB] inherit #test1已经创建成功 ACTIVE ‘/dev/vg0/root‘ [20.00 GiB] inherit ACTIVE ‘/dev/vg0/swap‘ [2.00 GiB] inherit ACTIVE ‘/dev/vg0/usr‘ [10.00 GiB] inherit ACTIVE ‘/dev/vg0/var‘ [20.00 GiB] inherit
5.接下来就可以按照普通分区,对test1进行格式化,并且挂载使用了
[root@localhost ~]# mkfs.ext4 /dev/test/test1 mke2fs 1.41.12 (17-May-2010) ...... This filesystem will be automatically checked every 34 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkdir /test [root@localhost ~]# mount /dev/test/test1 /test/ [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 428M 19G 3% / tmpfs 246M 0 246M 0% /dev/shm /dev/sda1 194M 30M 155M 16% /boot /dev/mapper/vg0-usr 9.9G 2.3G 7.2G 24% /usr /dev/mapper/vg0-var 20G 361M 19G 2% /var /dev/mapper/test-test1 4.0G 136M 3.7G 4% /test #逻辑卷成功创建
6.在/test下放入一个文件,测试可否使用
[root@localhost test]# echo "lv test" >a.txt [root@localhost test]# cat a.txt lv test
7.当我们使用完一段时间后,可能4G的空间不够用了,这种时候,我们可以对test1进行扩展.LVM的灵活性就展现出来了.例如我们想扩展到7G,但是经过查看,卷组中的空间已经不够使用,需要先对卷组进行扩展
[root@localhost test]# vgs test VG #PV #LV #SN Attr VSize VFree test 1 1 0 wz--n- 5.01g 1.01g [root@localhost test]# vgextend test /dev/sdb2 Volume group "test" successfully extended [root@localhost test]# vgs test VG #PV #LV #SN Attr VSize VFree test 2 1 0 wz--n- 10.02g 6.02g
8.扩展逻辑卷test1(首先扩展物理边界,再扩展文件系统边界)
[root@localhost test]# vgextend test /dev/sdb2 Volume group "test" successfully extended [root@localhost test]# vgs test VG #PV #LV #SN Attr VSize VFree test 2 1 0 wz--n- 10.02g 6.02g [root@localhost test]# lvextend -L 7G /dev/test/test1 #扩展物理边界 Extending logical volume test1 to 7.00 GiB Logical volume test1 successfully resized [root@localhost test]# resize2fs /dev/test/test1 #扩展文件系统边界 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/test/test1 is mounted on /test; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/test/test1 to 1835008 (4k) blocks. The filesystem on /dev/test/test1 is now 1835008 blocks long. [root@localhost test]# df -h #test1成功扩展到7G大小 Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 428M 19G 3% / tmpfs 246M 0 246M 0% /dev/shm /dev/sda1 194M 30M 155M 16% /boot /dev/mapper/vg0-usr 9.9G 2.3G 7.2G 24% /usr /dev/mapper/vg0-var 20G 361M 19G 2% /var /dev/mapper/test-test1 6.9G 137M 6.5G 3% /test
9.对测试后的test1访问,查看文件是否被破坏
[root@localhost test]# cat a.txt #文件未被破坏 lv test
10.快照卷创建(用于文件系统备份等场景使用)
[root@localhost test]# lvcreate -n snap-test1 -L 2G -s /dev/test/test1 -p r Logical volume "snap-test1" created [root@localhost test]# mkdir /test.bak [root@localhost test]# mount /dev/test/snap-test1 /test.bak/ mount: block device /dev/mapper/test-snap--test1 is write-protected, mounting read-only
11.查看test1及其快照卷的文件列表及a.txt的文件内容
[root@localhost /]# ls /test a.txt lost+found [root@localhost /]# ls /test.bak/ a.txt lost+found [root@localhost test]# cat /test/a.txt /test.bak/a.txt lv test lv test
12.修改test1中的文件,查看在其快照卷中是否发生了改动
[root@localhost test]# echo "This is a new line" >>/test/a.txt [root@localhost test]# cat /test/a.txt lv test This is a new line [root@localhost test]# cat /test.bak/a.txt #快照卷的文件内容未发生改变 lv test
本文出自 “淡淡” 博客,请务必保留此出处http://dddbk.blog.51cto.com/6837943/1440531
磁盘管理之LVM