创建LVM,并挂载
1.对磁盘进行分区
fdisk /dev/sdb
[root@testdb ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x3b1b6c9d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): 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: 0x3b1b6c9d
Device Boot Start End Blocks Id System
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):
Using default value 1044
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
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: 0x3b1b6c9d
Device Boot Start End Blocks Id System
/dev/sdb1 1 1044 8385898+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
注意:上述交互中分别输入了:n,p,1,回车,回车,t,8e,p,w
输入8e 的意思是选择类型为:Linux LVM 。
2.创建物理卷PV
pvcreate /dev/sdb1
( 如果新加的磁盘全部加入扩展的vg,则前面1步可省略,将整个盘新建为pv:
pvcreate /dev/sdc 原文链接:https://www.linuxidc.com/Linux/2018-09/154432.htm)
3.创建卷组VG
vgcreate lgj_vg_01 /dev/sdb1
(如果有多个PV,则 vgcreate lgj_vg_01 /dev/sdb1 /dev/sdb1 )
如果要把新的PV /dev/sdd1加入已存在的VG,方法:
vgextend lgj_vg_01 /dev/sdd1
4.创建LV
lvcreate -L 1G -n lgj_lv_01 lgj_vg_01
5.创建文件系统,设定格式为ext4
mkfs -t ext4 /dev/sdc1
6.创建目录,并挂载
mkdir /data
mount /dev/lgj_vg_01/lgj_lv_01 /data
7.设置LV大小
lvextend -L 10G /dev/mapper/lgj_vg_01-lgj_lv_01
9.使用resize2fs设置大小
resize2fs /dev/mapper/lgj_vg_01-lgj_lv_01
9.最后确认
lvs , df -h
LVM 扩容
前提:/dev/sdc 是新添加的盘。
步骤:
1.对磁盘进行分区
fdisk /dev/sdc
2.创建文件系统,设定格式为ext4
mkfs -t ext4 /dev/sdc1
3.创建物理卷PV
pvcreate /dev/sdc1
( 如果新加的磁盘全部加入扩展的vg,则前面两步可省略,将整个盘新建为pv:
pvcreate /dev/sdc 原文链接:https://www.linuxidc.com/Linux/2018-09/154432.htm)
4.创建卷组VG
vgextend lgj_vg_01 /dev/sdc1
5.设置LV大小
lvextend -L 10G /dev/mapper/lgj_vg_01-lgj_lv_01
6.使用resize2fs设置大小
resize2fs /dev/mapper/lgj_vg_01-lgj_lv_01
7.最后确认
lvs , df -h
LVM缩减
1.先卸载逻辑卷
umount /dev/mapper/lgj_vg_01-lgj_lv_01
2.然后通过e2fsck命令检测逻辑卷上空余的空间
e2fsck -f /dev/mapper/lgj_vg_01-lgj_lv_01
3.使用resize2fs将文件系统减少到3G
resize2fs /dev/mapper/lgj_vg_01-lgj_lv_01 3G
4.再使用lvreduce命令将逻辑卷减少到3G
lvreduce -L 3G /dev/mapper/lgj_vg_01-lgj_lv_01
5.最后再挂载和验证
mount /dev/mapper/lgj_vg_01-lgj_lv_01 /data
如何确定磁盘映射关系?
dmsetup ls
more /proc/partitions
ll /dev/mapper/
ll /dev/*vg*
pvdisplay