LVM是一个多才多艺的硬盘系统工具。在Linux上非常的好用,传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。
首先是实际的物理磁盘及其划分的分区和其上的物理卷(PV)。一个或多个物理卷可以用来创建卷组(VG)。然后基于卷组可以创建逻辑卷(LV)。只要在卷组中有可用空间,就可以随心所欲的创建逻辑卷。文件系统就是在逻辑卷上创建的,然后可以在操作系统挂载和访问。
本文将介绍怎么在linux中创建和管理LVM卷。我们将会分成两个部分。第一个部分,我们首先要在一个硬盘上创建多个逻辑卷,然后将它们挂载在/lvm-mount目录。然后我们将要对创建好的卷调整大小。而第二部分,我们将会扩充所创建的LVM的大小。
1.准备磁盘分区
通过使用fdisk,创建磁盘分区。我们需要创建3个2G分区,注意,并不要求分区的大小一致。同样,分区需要使用‘8e’类型来使他们可用于LVM。
#新建3个分区,大小都为2G,并转换类型为"8e" [root@localhost ~]# fdisk /dev/sdb 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): m #查看帮助 Command action d delete a partition #删除一个分区 l list known partition types #列出所有的分区类型 m print this menu #打印帮助菜单 n add a new partition #新增分区 p print the partition table #打印分区表 q quit without saving changes #不保存退出 t change a partition's system id #修改分区系统ID w write table to disk and exit #保存退出 Command (m for help): n #新建分区 Command action e extended p primary partition (1-4) p #新建一个主分区 Partition number (1-4): 1 #指定分区号为1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G #指定分区大小为2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (525-2610, default 525): Using default value 525 Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G Command (m for help): p #打印分区表 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: 0x5f0f2678 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux /dev/sdb2 263 524 2104515 83 Linux /dev/sdb3 525 786 2104515 83 Linux Command (m for help): t #修改分区的系统ID Partition number (1-4): 1 Hex code (type L to list codes): 8e #LVM的系统ID为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): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p 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: 0x5f0f2678 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 8e Linux LVM /dev/sdb2 263 524 2104515 8e Linux LVM /dev/sdb3 525 786 2104515 8e Linux LVM Command (m for help): w #保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2.准备物理卷(PV)
刚创建的分区是用来储存物理卷的。LVM可以使用不同大小的物理卷。
#把/dev/sdb1分区创建成物理卷 [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created #把/dev/sdb2分区创建成物理卷 [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created #把/dev/sdb3分区创建成物理卷 [root@localhost ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created [root@localhost ~]# pvs #查看系统上的物理卷 PV VG Fmt Attr PSize PFree /dev/sdb2 lvm2 ---- 2.01g 2.01g /dev/sdb3 lvm2 ---- 2.01g 2.01g [root@localhost ~]# pvdisplay #查看系统上的物理卷 "/dev/sdb2" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Q4cbqD-Y3RV-pvMH-gk4t-Hlbh-31uH-tyDE54 "/dev/sdb3" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb3 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID waK6Gz-fo3F-MjzQ-e33P-tbsY-eP46-PYGrC8
3.准备卷组(VG)
下列命令用来创建名为’vg1′的卷组,使用/dev/sdb1, /dev/sdb2 和 /dev/sdb3创建。
#把/dev/sdb1创建成卷组 [root@localhost ~]# vgcreate /dev/vg1 /dev/sdb1 Volume group "vg1" successfully created #查看卷组 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 1 0 0 wz--n- 2.00g 2.00g #扩充卷组 [root@localhost ~]# vgextend /dev/vg1 /dev/sdb2 /dev/sdb3 Volume group "vg1" successfully extended #查看卷组 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 3 0 0 wz--n- 6.01g 6.01g #查看卷组的详细信息 [root@localhost ~]# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 6.01 GiB PE Size 4.00 MiB Total PE 1539 Alloc PE / Size 0 / 0 Free PE / Size 1539 / 6.01 GiB
从输出中,我们可以看见卷组的使用量/总量。物理卷给卷组提供空间。只要在这个卷组中还有可用空间,我们就可以随意创建逻辑卷。
4.创建逻辑卷(LV)
下列命令创建一个名为’lv0′、大小为4G的逻辑卷。这个逻辑卷使用之前创建的卷组的空间。
#在vg1的上创建逻辑卷lv0,指定大小为4G [root@localhost ~]# lvcreate -n lv0 -L 4G vg1 Logical volume "lv0" created. #查看所创建的逻辑卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 4.00g #逻辑卷可使用lvdisplay命令查看。 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv0 LV Name lv0 VG Name vg1 LV UUID KCLYti-wEGU-CupG-ucsk-UlF0-Pgpb-ij71xJ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-04-24 23:24:09 +0800 LV Status available # open 0 LV Size 4.00 GiB Current LE 1024 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
现在逻辑卷已经准备好了,我们可以格式化和挂载逻辑卷,就像其它ext2/3/4分区一样!
5.为逻辑卷创建文件系统,并挂载
#把所创建的逻辑卷格式化为ext分区 [root@localhost ~]# mkfs.ext4 /dev/vg1/lv0 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 262144 inodes, 1048576 blocks 52428 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 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. #创建挂载点/mnt/lv0 [root@localhost ~]# mkdir /mnt/lvo #把逻辑卷挂载到刚才所创建的挂载点上 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
一旦逻辑卷挂载,我们就可以到挂载点 /lvm-mount/ 上读写了。要创建和挂载其它的逻辑卷,我们重复这个过程。
6.扩展一个LVM卷
调整逻辑卷大小的功能是LVM最有用的功能。这个部分会讨论我们怎么样扩展一个存在的逻辑卷。下面,我们将会扩展先前创建的逻辑卷‘lv0’扩大到6G。
注意,调整逻辑卷大小之后,也需要对文件系统调整大小进行匹配。这个额外的步骤各不相同,取决于创建文件系统的类型。在本文中,我们使用’lv0′创建了ext4类型的文件系统,所以这里的操作是针对ext4文件系统的。(ext2/3文件系统也类同)。
#卸载掉lv0卷 [root@localhost ~]# umount /mnt/lv0 #可以看到已经安全卸载了 [root@localhost ~]# mount | grep lv0 #扩展前先检查文件系统(必须的步骤) [root@localhost ~]# e2fsck -f /dev/vg1/lv0 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/vg1/lv0: 11/262144 files (0.0% non-contiguous), 51278/1048576 blocks #把逻辑卷扩展到6G的大小 [root@localhost ~]# lvresize -L 6G /dev/vg1/lv0 Size of logical volume vg1/lv0 changed from 4.00 GiB (1024 extents) to 6.00 GiB (1536 extents). Logical volume lv0 successfully resized. #查看系统上的逻辑卷 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv0 LV Name lv0 VG Name vg1 LV UUID KCLYti-wEGU-CupG-ucsk-UlF0-Pgpb-ij71xJ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-04-24 23:24:09 +0800 LV Status available # open 0 LV Size 6.00 GiB Current LE 1536 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 #再次挂载逻辑卷 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
7.缩减一个LVM卷
现在介绍缩减LVM卷大小的方法。
注意减少逻辑卷的大小值若小于储存的数据大小,存储在后面的数据会丢失。
#卸载掉卷。 [root@localhost ~]# umount /mnt/lv0 #检查逻辑卷上的文件系统(必须的步骤) [root@localhost ~]# e2fsck -f /dev/vg1/lv0 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/vg1/lv0: 11/262144 files (0.0% non-contiguous), 51278/1048576 blocks [root@localhost ~]# resize2fs /dev/vg1/lv0 4G resize2fs 1.41.12 (17-May-2010) The filesystem is already 1048576 blocks long. Nothing to do! #查看逻辑卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 6.00g #缩减逻辑卷到4G [root@localhost ~]# lvreduce -L 4G /dev/vg1/lv0 WARNING: Reducing active logical volume to 4.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg1/lv0? [y/n]: y Size of logical volume vg1/lv0 changed from 6.00 GiB (1536 extents) to 4.00 GiB (1024 extents). #查看缩减后的逻辑卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 4.00g Logical volume lv0 successfully resized. #挂载到挂载点 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
现在可以使用所创建好的逻辑卷了
祝大家工作生活愉快!!
原创文章,作者:renpingsheng,如若转载,请注明出处:http://www.178linux.com/74662