Linux_LVM逻辑卷管理
文章目录
- LVM的优点及逻辑概念
- LVM的组成部分
- 创建LVM的步骤描述
- LVM实验需求
- 创建LVM实验步骤
LVM的优点及逻辑概念
- LVM最大的好处是可以随时调整分区的大小,且在调整分区大小过程中,分区中现有数据不会丢失,不需要卸载分区和停止服务
- LVM是在硬盘分区之上建立一个逻辑层,这个逻辑层让多个硬盘或分区看起来像一块逻辑硬盘,然后将这块逻辑硬盘分成逻辑卷使用,大大提高了分区的灵活性
LVM的组成部分
- 物理卷(PV):真正的物理硬盘或者服务器上的现有分区
- 卷组(VG):将多块物理卷合起来就组成了卷组
- 逻辑卷(LV):卷组是一块逻辑硬盘,硬盘必须分区后才能使用,在卷组层面上分区称为逻辑卷;逻辑卷可以被格式化和写入数据。可将逻辑卷理解为分区
- 物理扩展(PE):PE是逻辑卷用来保存数据的最小单元,数据实际上都是写入PE中的。PE大小可以配置,默认为4MB
创建LVM的步骤描述
- 把物理硬盘分成几个物理分区或者是整块物理硬盘
- 把物理分区建立为物理卷(PV),也可以把整块物理硬盘建立为物理卷(PV)
- 把物理卷整合为卷组(VG),卷组就可以动态的调整大小,可以把物理卷加入到卷组中,也可以把物理卷从卷组中剔除
- 把卷组再划分为逻辑卷(LV),当然逻辑卷也是可以直接调整大小的
- 把逻辑卷(LV)格式化后,进行挂载使用
LVM实验需求
- 创建一个至少有两个PV组成的大小为6G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录。
创建LVM的实验步骤
1、在服务器上挂载一块10G的硬盘
lsblk #查看服务器硬盘信息,包括未挂载的硬盘
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 19.5G 0 part ├─centos-root 253:0 0 17.5G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk #此硬盘为本次实验的对象 sr0 11:0 1 4G 0 rom
2、硬盘分区
fdsik /dev/sdb #对硬盘sdb进行分区操作,/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-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +3G Partition 1 of type Linux and of size 3 GiB is set Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): Using default response p Partition number (2-4, default 2): First sector (6293504-20971519, default 6293504): Using default value 6293504 Last sector, +sectors or +size{K,M,G} (6293504-20971519, default 20971519): +3G Partition 2 of type Linux and of size 3 GiB is set Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): e Partition number (3,4, default 3): First sector (12584960-20971519, default 12584960): Using default value 12584960 Last sector, +sectors or +size{K,M,G} (12584960-20971519, default 20971519): Using default value 20971519 Partition 3 of type Extended and of size 4 GiB is set Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (12587008-20971519, default 12587008): Using default value 12587008 Last sector, +sectors or +size{K,M,G} (12587008-20971519, default 20971519): +3G Partition 5 of type Linux and of size 3 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
partprobe #分区后需要重新读取分区表,否则需要重启系统才生效
3、转变分区ID为8e
fdisk /dev/sdb #同样使用分区命令进入分区编辑环境改变分区ID
[root@abc ~]# 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): t Partition number (1-3,5, default 5): Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): t Partition number (1-3,5, default 5): 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): t Partition number (1-3,5, default 5): 2 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@abc ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
4、建立物理卷
[root@abc ~]# pvcreate /dev/sdb1 #把分区/dev/sdb1转换为物理卷 Physical volume "/dev/sdb1" successfully created. [root@abc ~]# pvcreate /dev/sdb2 #把分区/dev/sdb2转换为物理卷 Physical volume "/dev/sdb2" successfully created. [root@abc ~]# pvcreate /dev/sdb5 #把逻辑分区/dev/sdb5转换为物理卷 Physical volume "/dev/sdb5" successfully created.
pvscan #用于查询系统中那些硬盘或者分区是物理卷
pvdisplay #查看更详细的物理卷状态
pvremove 分区设备文件名 #此命令为删除物理卷操作命令
5、建立卷组
vgcreate -s 16MB testvg /dev/sdb1 /dev/sdb2 #此命令是将物理卷/dev/sdb1和/dev/sdb2建立为名为testvg的卷组,且卷组中PE大小指定为16MB
Volume group "testvg" successfully created
vgscan #此命令主要用于查看系统中是否有卷组
vgdisplay #此命令则用于查看卷组的详细信息
vgextend testvg[卷组名] [分区设备文件名]
vgreduce [卷组名] [分区设备文件名]
例如:
vgreduce testvg /dev/sdb1
vgremove [卷组名]
6、建立逻辑卷
lvcreate -L 5G -n testlv testvg #在卷组testvg上创建一个5G的testlv逻辑卷
Logical volume "testlv" created.
lvscan #此命令只能看到系统中是否拥有逻辑卷
lvdisplay #此命令可看到逻辑卷的详细信息
[root@abc ~]# lvresize -L 5.5G /dev/testvg/testlv #此命令将逻辑卷的大小改为5.5G;【注意】不建议减少逻辑卷的空间,以防数据丢失 Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 5.50 GiB (352 extents). Logical volume testvg/testlv successfully resized.
lvremove [逻辑卷的设备文件名]
tg:
lvremove /dev/testvg/testlv #删除卷组/testvg上的testlv逻辑卷
7、格式化
mkfs -t ext4 /dev/testvg/testlv #格式化逻辑卷testlv
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 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 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 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
8、挂载
mount /dev/testvg/testlv /users #将逻辑卷testlv挂载到/users目录下
9、效果展示
lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 19.5G 0 part ├─centos-root 253:0 0 17.5G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk ├─sdb1 8:17 0 3G 0 part │ └─testvg-testlv 253:2 0 5G 0 lvm /users ├─sdb2 8:18 0 3G 0 part │ └─testvg-testlv 253:2 0 5G 0 lvm /users ├─sdb3 8:19 0 1K 0 part └─sdb5 8:21 0 3G 0 part sr0 11:0 1 4G 0 rom

浙公网安备 33010602011771号