Linux基础知识 - LVM(逻辑卷管理器)
LVM是一个用于逻辑卷管理的工具,用于分配磁盘,条带化,镜像和调整逻辑卷大小。
使用LVM,将硬盘驱动器或者硬盘驱动器分配给一个或者多个物理卷。
LVM物理卷可以放置在可能跨越两个或者多个磁盘的其他块设备上。
由于物理卷不能跨越多个驱动器,因此跨越多个驱动器,因此每个驱动器创建一个或者多个物理卷。
卷组可以分为逻辑卷,这些卷被分配为"/home"和'/'和文件系统类型,例如ext2或者ext3或者ext4.
当"分区"达到完全容量时,可以将来自卷组的可用空间添加到逻辑卷中以增加分区的大小。
当新的硬盘驱动器添加到系统中时,它可以添加到卷组,并且逻辑卷的分区的大小可以增加。
另一方面,如果系统与ext4文件系统分区,则硬盘驱动器分为定义大小的分区。
如果分区已满,则扩展分区的大小并不容易。
即使分区移动到另一个硬盘驱动器,也必须将原始硬盘驱动器空间重新分配为其他分区或者不使用。
在此操作文献中,让我们了解LVM命令的一些基础知识。
目标
在这个例子中,让我们,
- 创建3个100MB大小的分区。
- 将它们转换为物理卷。
- 将物理卷组合到卷组中。
- 最后从卷组创建逻辑卷。
创建分区
使用fdisk命令创建和管理分区。
要查看现有分区,请使用以下命令:
[Hyman@theitroad ~]# fdisk -l Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x0007b12c Device Boot Start End Blocks Id System Disk /dev/sda: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x000ac451 Device Boot Start End Blocks Id System /dev/sda1 * 1 128 1024000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 128 291 1310720 82 Linux swap/Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 291 1045 6052864 83 Linux
以上输出显示了我们两个物理硬盘。
/dev/sda包含三个分区,没有空格以创建其他分区。
而第二个驱动器/dev/sdb还包含任何分区。
所以让我们在本教程中使用第二个。
现在让我们使用fdisk命令创建每个大小100MB的三个分区。
[Hyman@theitroad ~]# 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1044, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +100M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (15-1044, default 15): Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-1044, default 1044): +100M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (29-1044, default 29): Using default value 29 Last cylinder, +cylinders or +size{K,M,G} (29-1044, default 1044): +100M
要检查是否已创建分段使用参数"P"。
Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x0007b12c Device Boot Start End Blocks Id System /dev/sdb1 1 14 112423+ 83 Linux /dev/sdb2 15 28 112455 83 Linux /dev/sdb3 29 42 112455 83 Linux
保存新创建的分区。
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
更新内核以保存更改而无需重新启动系统。
[Hyman@theitroad ~]# partprobe Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it Jan not reflect all of your changes until after reboot.
我们再次使用fdisk命令检查现有分区。
[Hyman@theitroad ~]# fdisk -l Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x0007b12c Device Boot Start End Blocks Id System /dev/sdb1 1 14 112423+ 83 Linux /dev/sdb2 15 28 112455 83 Linux /dev/sdb3 29 42 112455 83 Linux Disk /dev/sda: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x000ac451 Device Boot Start End Blocks Id System /dev/sda1 * 1 128 1024000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 128 291 1310720 82 Linux swap/Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 291 1045 6052864 83 Linux
上面的输出显示了/dev/sdb磁盘中创建了三个段。
如果FDISK -L没有显示输出重启以生效。
创建物理量
注意:如果我们在最小模式下安装了服务器,则找不到命令"PVCreate","LVCreate","VgCreate"等。
使用该命令首先安装"LVM2"包。
[Hyman@theitroad ~]# yum install lvm2 Loaded plugins: rhnplugin This system is not registered with RHN. RHN support will be disabled. Setting up Install ProcessThe Resolving Dependencies --> Running transaction check ---> Package lvm2.i686 0:2.02.72-8.el6 set to be updated --> Processing Dependency: lvm2-libs = 2.02.72-8.el6 for package: lvm2-2.02.72-8.el6.i686 --> Processing Dependency: libdevmapper-event.so.1.02(Base) for package: lvm2-2.02.72-8.el6.i686 --> Processing Dependency: libdevmapper-event.so.1.02 for package: lvm2-2.02.72-8.el6.i686 --> Running transaction check ---> Package device-mapper-event-libs.i686 0:1.02.53-8.el6 set to be updated ---> Package lvm2-libs.i686 0:2.02.72-8.el6 set to be updated --> Processing Dependency: device-mapper-event >= 1.02.53-8.el6 for package: lvm2-libs-2.02.72-8.el6.i686 --> Running transaction check ---> Package device-mapper-event.i686 0:1.02.53-8.el6 set to be updated --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: lvm2 i686 2.02.72-8.el6 localrepo 514 k Installing for dependencies: device-mapper-event i686 1.02.53-8.el6 localrepo 79 k device-mapper-event-libs i686 1.02.53-8.el6 localrepo 74 k lvm2-libs i686 2.02.72-8.el6 localrepo 565 k Transaction Summary ================================================================================ Install 4 Package(s) Upgrade 0 Package(s) Total download size: 1.2 M Installed size: 2.5 M Is this ok [y/N]: y Downloading Packages: ------------------------------------------------------------------------------- Total 11 MB/s | 1.2 MB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : device-mapper-event-libs-1.02.53-8.el6.i686 1/4 Installing : device-mapper-event-1.02.53-8.el6.i686 2/4 Installing : lvm2-libs-2.02.72-8.el6.i686 3/4 Installing : lvm2-2.02.72-8.el6.i686 4/4 Installed: lvm2.i686 0:2.02.72-8.el6 Dependency Installed: device-mapper-event.i686 0:1.02.53-8.el6 device-mapper-event-libs.i686 0:1.02.53-8.el6 lvm2-libs.i686 0:2.02.72-8.el6 Complete!
现在使用命令pvcreate创建物理卷。
[Hyman@theitroad ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created
要验证新创建的物理卷,请使用命令pvdisplay。
[Hyman@theitroad ~]# pvdisplay "/dev/sdb1" is a new physical volume of "109.79 MiB" --- NEW Physical volume -- PV Name /dev/sdb1 VG Name PV Size 109.79 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID jQl5F4-DyLj-SkHu-4lhZ-J3nQ-zax9-aT8sc4 "/dev/sdb2" is a new physical volume of "109.82 MiB" --- NEW Physical volume -- PV Name /dev/sdb2 VG Name PV Size 109.82 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID i4MHvw-8hYB-Fwz8-fxTL-G3mu-fl5E-zGYhDO "/dev/sdb3" is a new physical volume of "109.82 MiB" --- NEW Physical volume -- PV Name /dev/sdb3 VG Name PV Size 109.82 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 99qkNw-3oAw-vXwg-WE6U-zyKO-Ffs3-rDSqUY
创建卷组
使用命令vgcreate使用两个物理卷/dev/sdb1和/dev/sdb2创建名为VG1的新卷组。
[Hyman@theitroad ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 Volume group "vg1" successfully created
要验证卷组或者不使用命令VGDisplay。
[Hyman@theitroad ~]# vgdisplay --- Volume group -- VG Name vg1 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 216.00 MiB PE Size 4.00 MiB Total PE 54 Alloc PE/Size 0/0 Free PE/Size 54/216.00 MiB VG UUID ds3OtP-DMUx-33nN-HDar-eqNj-uIED-41gjqI
创建逻辑卷
要创建逻辑卷,请使用命令lvcreate。
让我们创建一个名为LV1的逻辑卷,大小为200MB。
[Hyman@theitroad ~]# lvcreate -L 200M vg1 -n lv1 Logical volume "lv1" created
验证是否使用命令lvdisplay创建逻辑卷。
[Hyman@theitroad ~]# lvdisplay --- Logical volume -- LV Name /dev/vg1/lv1 VG Name vg1 LV UUID dgLZ79-JZdn-NUSF-fUS1-YVFk-36qs-iuafhE LV Write Access read/write LV Status available # open 0 LV Size 200.00 MiB Current LE 50 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
格式化并安装逻辑卷
现在格式化新创建的逻辑卷并将其安装在/mnt目录中或者无论我们想要的地方。
[Hyman@theitroad ~]# mkfs.ext4 /dev/vg1/lv1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 51200 inodes, 204800 blocks 10240 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 25 block groups 8192 blocks per group, 8192 fragments per group 2048 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
并将逻辑卷安装在/mnt安装点中。
[Hyman@theitroad ~]# mount /dev/vg1/lv1 /mnt/
现在逻辑卷成功安装在/mnt中。
我们可以使用新的逻辑卷来存储数据。
[Hyman@theitroad ~]# cd /mnt/ [Hyman@theitroad mnt]# touch file1 file2 file3 [Hyman@theitroad mnt]# mkdir dir1 dir2 dir3 [Hyman@theitroad mnt]# ls dir1 dir2 dir3 file1 file2 file3 lost+found
扩展卷组大小
如果我们在逻辑卷中的空间中运行,则如果物理磁盘包含可用空间或者其他物理磁盘(硬盘),则可以轻松扩展它的大小。
例如,可以使用物理卷/dev/sdb3来扩展卷组VG1.
让我们将添加100MB添加到逻辑卷LV1.
[Hyman@theitroad mnt]# vgextend vg1 /dev/sdb3 Volume group "vg1" successfully extended
然后调整逻辑Vloume Lv1的大小。
[Hyman@theitroad mnt]# lvresize -L +100M /dev/vg1/lv1 Extending logical volume lv1 to 300.00 MiB Logical volume lv1 successfully resized
调整逻辑卷LV1文件系统的大小。
[Hyman@theitroad mnt]# resize2fs /dev/vg1/lv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg1/lv1 is mounted on /mnt; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/vg1/lv1 to 307200 (1k) blocks. The filesystem on /dev/vg1/lv1 is now 307200 blocks long.
现在验证逻辑卷Lv1的新大小。
[Hyman@theitroad mnt]# lvdisplay /dev/vg1/lv1 --- Logical volume -- LV Name /dev/vg1/lv1 VG Name vg1 LV UUID dgLZ79-JZdn-NUSF-fUS1-YVFk-36qs-iuafhE LV Write Access read/write LV Status available # open 1 LV Size 300.00 MiB Current LE 75 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
完成。
现在逻辑卷LV1的大小延长100MB。
删除逻辑卷
从/mnt挂载点中取出,卸载逻辑卷Lv1并使用命令Lvremove删除它。
[Hyman@theitroad mnt]# cd .. [Hyman@theitroad /]# umount /mnt/ [Hyman@theitroad /]# lvremove /dev/vg1/lv1 Do you really want to remove active logical volume lv1? [y/n]: y Logical volume "lv1" successfully removed
删除卷组
[Hyman@theitroad /]# vgremove /dev/vg1 Volume group "vg1" successfully removed
删除物理量
[Hyman@theitroad /]# pvremove /dev/sdb1 /dev/sdb2 /dev/sdb3 Labels on physical volume "/dev/sdb1" successfully wiped Labels on physical volume "/dev/sdb2" successfully wiped Labels on physical volume "/dev/sdb3" successfully wiped