详解Linux LVM逻辑卷配置过程
什么是LVM?
LVM是Linux Logical Volume Manager(逻辑卷管理器)的缩写。它是一种为Linux操作系统提供磁盘管理的方法,它允许用户将多个硬盘分区或整个硬盘组合成一个逻辑卷,从而为用户提供更加灵活的磁盘空间管理方式。
LVM的组成部分
LVM主要由三个部分组成:
-
物理卷(PV):即硬盘上的分区或整个硬盘。
-
卷组(VG):将多个物理卷组合起来构成一个卷组。
-
逻辑卷(LV):在卷组内创建的逻辑卷,用户可以对逻辑卷进行格式化和挂载等操作。
LVM逻辑卷的配置过程
1. 创建物理卷
操作步骤:
-
找到要创建物理卷的目标设备,并将设备分区(例如/dev/sdb1)。
-
将分区转换为物理卷,使用以下命令:
pvcreate /dev/sdb1
示例说明:
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
2. 创建卷组
操作步骤:
-
找到已创建的物理卷。
-
创建卷组,使用以下命令:
vgcreate [卷组名] /dev/sdb1
示例说明:
[root@localhost ~]# vgcreate vg_test /dev/sdb1
Volume group "vg_test" successfully created
3. 创建逻辑卷
操作步骤:
-
找到已创建的卷组。
-
创建逻辑卷,并指定逻辑卷的大小,使用以下命令:
lvcreate -L [大小] -n [逻辑卷名] [卷组名]
示例说明:
[root@localhost ~]# lvcreate -L 2G -n lv_test vg_test
Logical volume "lv_test" created.
4. 格式化并挂载逻辑卷
操作步骤:
-
找到已创建的逻辑卷。
-
使用以下命令格式化逻辑卷(ext4格式为例):
mkfs.ext4 /dev/vg_test/lv_test
- 创建挂载点:
mkdir /mnt/lv_test
- 挂载逻辑卷:
mount /dev/vg_test/lv_test /mnt/lv_test
示例说明:
[root@localhost ~]# mkfs.ext4 /dev/vg_test/lv_test
mke2fs 1.42.9 (28-Dec-2013)
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
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mkdir /mnt/lv_test
[root@localhost ~]# mount /dev/vg_test/lv_test /mnt/lv_test
5. 增加逻辑卷空间
操作步骤:
-
关闭以前的挂载点。
-
拓展逻辑卷的大小,并修改文件系统大小,使用以下命令:
lvextend -L [新大小] [逻辑卷名]
resize2fs [文件系统名]
示例说明:
[root@localhost ~]# umount /mnt/lv_test
[root@localhost ~]# lvextend -L 3G /dev/vg_test/lv_test
Size of logical volume vg_test/lv_test changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
Logical volume vg_test/lv_test successfully resized.
[root@localhost ~]# resize2fs /dev/vg_test/lv_test
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg_test/lv_test is mounted on /mnt/lv_test; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg_test/lv_test is now 786432 (4k) blocks long.
6. 减少逻辑卷空间
操作步骤:
-
关闭已挂载的文件系统。
-
缩小文件系统大小。
-
缩小逻辑卷大小,使用下面的命令:
lvreduce -L [新大小] [逻辑卷名]
- 修改文件系统大小:
resize2fs [文件系统名]
示例说明:
[root@localhost ~]# umount /mnt/lv_test
[root@localhost ~]# resize2fs /dev/vg_test/lv_test 1G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg_test/lv_test to 262144 (4k) blocks.
The filesystem on /dev/vg_test/lv_test is now 262144 blocks long.
[root@localhost ~]# lvreduce -L 2G /dev/vg_test/lv_test
WARNING: Reducing active logical volume to 2.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_test? [y/n]: y
Size of logical volume vg_test/lv_test changed from 3.00 GiB (768 extents) to 2.00 GiB (512 extents).
Logical volume vg_test/lv_test successfully resized.
[root@localhost ~]# resize2fs /dev/vg_test/lv_test
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg_test/lv_test to 524280 (4k) blocks.
The filesystem on /dev/vg_test/lv_test is now 524280 blocks long.
7. 删除逻辑卷
操作步骤:
-
关闭已挂载的文件系统。
-
删除逻辑卷,使用以下命令:
lvremove [逻辑卷名]
示例说明:
[root@localhost ~]# umount /mnt/lv_test
[root@localhost ~]# lvremove /dev/vg_test/lv_test
Do you really want to remove active logical volume lv_test? [y/n]: y
Logical volume "lv_test" successfully removed
8. 删除卷组
操作步骤:
-
关闭已挂载的文件系统。
-
删除卷组,使用以下命令:
vgremove [卷组名]
示例说明:
[root@localhost ~]# umount /mnt/lv_test
[root@localhost ~]# vgremove vg_test
Volume group "vg_test" successfully removed
以上就是Linux LVM逻辑卷配置过程的详细说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Linux LVM逻辑卷配置过程(创建,增加,减少,删除,卸载) - Python技术站