一、逻辑卷的创建
查看磁盘
[root@centos7 ~ ]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 147G 0 lvm /home sdb 8:16 0 10G 0 disk //新加入的磁盘sdb sdc 8:32 0 5G 0 disk //新加入的磁盘sdc sr0 11:0 1 8.8G 0 rom
为磁盘分区
[root@centos7 ~ ]#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-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): t //选择标签 Selected partition 1 Hex code (type L to list all codes): 8e // 8e Linux LVM 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.
同理分出(sdc)分区
[root@centos7 ~ ]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 147G 0 lvm /home
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
sdc 8:32 0 5G 0 disk
└─sdc1 8:33 0 5G 0 part
sr0 11:0 1 8.8G 0 rom
创建物理卷(PV)pvcreate /dev/sdb1
[root@centos7 ~ ]#pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created.
查看物理卷信息(pvdisplay;pvs)
[root@centos7 ~ ]#pvdisplay
--- Physical volume ---PV Name /dev/sdb1VG Name vg0PV Size <10.00 GiB / not usable 3.00 MiBAllocatable yes PE Size 4.00 MiBTotal PE 2559Free PE 1279Allocated PE 1280PV UUID arm45N-isin-be4N-ob5a-Xwj1-i7Wc-Nwf73e
创建卷组(VG) vgcreate vg0 /dev/sdb1
[root@centos7 ~ ]#vgcreate vg0 /dev/sdb1Volume group "vg0" successfully created
查看卷组信息(vgdisplay;vgs)
[root@centos7 ~ ]#vgsVG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- <199.00g 4.00mvg0 1 0 0 wz--n- <10.00g <10.00g
创建逻辑卷(LV)这里创建了名称为lv0,大小5GB的逻辑卷分区;lvcreate -L 5G -n /dev/vg0/lv0 vg0
[root@centos7 ~ ]#lvcreate -L 5G -n /dev/vg0/lv0 vg0Logical volume "lv0" created.
查看逻辑卷信息(lvdisplay;lvs)
[root@centos7 ~ ]#lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos -wi-ao---- 146.99g root centos -wi-ao---- 50.00g swap centos -wi-ao---- 2.00g lv0 vg0 -wi-ao---- 5.00g
使用 mkfs.xfs 格式化为 lv0 的xfs文件系统:
[root@centos7 ~ ]#mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
挂载到/data目录下(你可以挂载到自己需要的目录下)
[root@centos7 ~ ]#mount /dev/vg0/lv0 /data
[root@centos7 ~ ]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 147G 0 lvm /home
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part └─vg0-lv0 253:3 0 5G 0 lvm /data
sdc 8:32 0 5G 0 disk
└─sdc1 8:33 0 5G 0 part
sr0 11:0 1 8.8G 0 rom
设置开机自动挂载,编辑 /etc/fstab 文件,加入最后一行 vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Aug 14 16:09:13 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=936ee62c-93ff-4f19-ae07-1112add772b6 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=8e7e026a-4025-4cee-ac6d-b3766fef9993 /data xfs defaults 0 0
二、逻辑卷扩容
先使用vgs查看卷组剩余大小,有剩余空间在使用lvextend进行扩容,没有则要增加磁盘。
[root@centos7 ~ ]#vgdisplay--- Volume group ---VG Name vg0System ID Format lvm2Metadata Areas 1Metadata Sequence No 2VG Access read/writeVG Status resizableMAX LV 0Cur LV 1Open LV 1Max PV 0Cur PV 1Act PV 1VG Size <10.00 GiBPE Size 4.00 MiBTotal PE 2559Alloc PE / Size 1280 / 5.00 GiBFree PE / Size 1279 / <5.00 GiBVG UUID 501kCS-IukG-st2J-5wUs-R7vQ-JdC9-wPjq0S
方法1:lvextend -L +1G /dev/vg0/lv0
[root@centos7 ~ ]#lvextend -l +100%FREE /dev/vg0/lv0Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to <10.00 GiB (2559 extents).Logical volume vg0/lv0 successfully resized.
* 使用resize2f调整ext4格式文件系统大小 resize2fs /dev/vg0/lv0
* 如果是xfs格式,使用xfs_growfs调整xfs格式文件系统大小 xfs_growfs /dev/vg0/lv0
方法2:合并一条命令 lvextend -r -l +100%FREE /dev/vg0/lv0
[root@centos7 ~ ]#lvextend -r -l +100%FREE /dev/vg0/lv0Size of logical volume vg0/lv0 unchanged from <10.00 GiB (2559 extents).Logical volume vg0/lv0 successfully resized.
meta-data=/dev/mapper/vg0-lv0 isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1310720, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 1310720 to 2620416
查看分区
[root@centos7 ~ ]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:3 0 147G 0 lvm /home
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part └─vg0-lv0 253:2 0 10G 0 lvm /data
sdc 8:32 0 5G 0 disk
└─sdc1 8:33 0 5G 0 part
sr0 11:0 1 8.8G 0 rom
三、缩减逻辑卷(ext4格式)
1. 逻辑卷回缩不能在线进行,所以先卸载已经挂载的逻辑卷并检测文件系统
使用umount卸载 umount /dev/vg0/lv0
[root@centos7 ~ ]#umount /dev/vg0/lv0
使用e2fsck检测文件系统 e2fsck -f /dev/vg0/lv0
[root@centos7 ~ ]#e2fsck -f /dev/vg0/lv0
e2fsck 1.42.9 (28-Dec-2013)
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/vg0/lv0: 11/655360 files (0.0% non-contiguous), 83137/2620416 blocks
2. 使用resize2fs缩小文件系统为5G resize2f /dev/app/app_lv 5G
[root@centos7 ~ ]#resize2fs /dev/vg0/lv0 1G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 262144 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 262144 blocks long.
3. 使用lvreduce 缩小逻辑卷,上面要缩小到1G,原先是10G,所以这里减少9G lvreduce -L 1G /dev/vg0/lv0
[root@centos7 ~ ]#lvreduce -L 1G /dev/vg0/lv0 WARNING: Reducing active logical volume to 1.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: ySize of logical volume vg0/lv0 changed from <10.00 GiB (2559 extents) to 1.00 GiB (256 extents).Logical volume vg0/lv0 successfully resized.[root@centos7 ~ ]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:3 0 147G 0 lvm /home
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part └─vg0-lv0 253:2 0 1G 0 lvm
sdc 8:32 0 5G 0 disk
└─sdc1 8:33 0 5G 0 part
sr0 11:0 1 8.8G 0 rom
4. 挂载逻辑卷 mount /dev/vg0/lv0 /data
[root@centos7 ~ ]#mount /dev/vg0/lv0 /data
四、逻辑卷删除
1. 先卸载你要删除的逻辑卷 umount /dev/vg0/lv0
[root@centos7 ~ ]#umount /dev/vg0/lv0
2. 使用lvremove删除逻辑卷LV lvremove /dev/vg0/lv0
[root@centos7 ~ ]#lvremove /dev/vg0/lv0
Do you really want to remove active logical volume vg0/lv0? [y/n]: yLogical volume "lv0" successfully removed
3. 验证逻辑卷是否删除 lvdisplay | grep "/dev/vg0/lv0"
[root@centos7 ~ ]#lvdisplay | grep "/dev/vg0/lv0"
4. 先使用vgchange关闭VG vgchange -a n vg0
[root@centos7 ~ ]#vgchange -a n vg00 logical volume(s) in volume group "vg0" now active
5.使用vgremove删除卷组VG vgremove /dev/vg0
[root@centos7 ~ ]#vgremove /dev/vg0Volume group "vg0" successfully removed
6.使用pvremove删除物理卷PV pvremove /dev/sdb1
[root@centos7 ~ ]#pvremove /dev/sdb1
___ Labels on physical volume "/dev/sdb1" successfully wiped.
转载于:https://blog.51cto.com/13854765/2160019