挂载磁盘
https://jingyan.baidu.com/album/a681b0de5b46db3b184346fc.html?picindex=2
扩展根目录:
https://blog.csdn.net/qq_38685754/article/details/85135264
https://www.jianshu.com/p/bf4ba81c9877
https://www.cnblogs.com/luck123/p/7994096.html (据说亲测有效--注意坑,别挂root下面??)
强制卸载命令 umount -l
https://blog.csdn.net/u014237598/article/details/83026286 为centos6的
https://blog.csdn.net/bigtree_3721/article/details/86646551 (扩展Centos7 逻辑卷 物理卷 卷组 实践,,非根分区扩展,经测比较有效)
Centos挂载硬盘(查看、分区、格式化、挂载)磁盘
https://www.fujieace.com/linux/centos-mount.html
http://www.360doc.com/content/18/0316/10/670256_737454830.shtml
https://blog.csdn.net/bigtree_3721/article/details/86646551
删除逻辑卷
https://blog.51cto.com/junongs/2330676?source=dra
强制卸载命令:
https://blog.csdn.net/qq_19175749/article/details/51154529
##########################################################################################################
https://blog.csdn.net/bigtree_3721/article/details/86646551 (扩展Centos7 逻辑卷 物理卷 卷组 实践,,非根分区扩展,经测比较有效)
第一章: 相关概念
1.物理存储介质(PhysicalStorageMedia):指系统的物理存储设备==>磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元。
2.物理卷(Physical Volume,PV):指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
3.卷组(Volume Group,VG):是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个逻辑卷。
4.逻辑卷(Logical Volume,LV):类似于非LVM系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。
第二章 实践与例子
1 创建物理卷
物理卷(Physical Volume,PV):就是指硬盘分区,也可以是整个硬盘或已创建的软RAID,是LVM的基本存储设备。在创建逻辑卷之前需要先创建物理卷,然后将多个物理卷组合创建卷组,最后在卷组的基础上创建逻辑卷。
将空白硬盘(或者分区)加入主机,系统识别到新的硬盘sdb后,使用pvcreate对硬盘创建PV:
代码实现:
# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdc 253:32 0 500G 0 disk
#fdisk /dev/sdb
....
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e # 修改系统ID为 Linux LVM
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6200a0ac
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
//注,如果是第一次建立的新磁盘,则应该先分区。命令顺序如下:
# fdisk /dev/sdb
>n
然后选择p(主分区)
>p
然后一路回车
>t
后面的命令和上面一样了。
# 创建物理卷
# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
# pvdisplay
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
需要注意的是,如果针对空闲分区创建PV,需要重新指定分区的系统ID。在上面的代码中Hex code (type L to list all codes): 8e指定分区的系统ID为Linux LVM。空白为分区磁盘直接使用pvcreate /dev/sdb命令,系统ID自动转成Linux LVM。
2 创建卷组
卷组(Volume Group,VG):是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个逻辑卷。现在使用vgcreate命令对上文创建的PV(单个物理卷)创建卷组。
代码实现:
[root@Centos7T ~]#vgcreate vg0 /dev/sdb1 # 创建卷组vg0,指定物理块PE大小10M,并将pv:/dev/sdb1加入vg0.
Volume group "vg0" successfully created
[root@Centos7T ~]#vgdisplay # 查看所有卷组的信息.后面接具体卷组名,可查看指定卷组信息
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 9.99 GiB
PE Size 4.00 MiB
Total PE 1023
Alloc PE / Size 0 / 0
Free PE / Size 1023 / 9.99 GiB
VG UUID 46Z4tB-d71l-nfir-t6sB-cZPo-SBfl-eFCDIj
[root@Centos7T ~]#pvdisplay # 查看所当前物理卷的信息.后面可接具体物理卷设备,可查看指定物理卷信息
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0 # 已加入卷组vg0
PV Size 10.00 GiB / not usable 10.00 MiB
Allocatable yes
PE Size 10.00 MiB # pe大小
Total PE 1023 # 当前物理卷的pe总数
Free PE 1023 # 当前物理卷的未被划分逻辑卷的pe总数
Allocated PE 0
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
3 创建逻辑卷
逻辑卷(Logical Volume,LV):类似于非LVM系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。可以使用lvcreate命令创建逻辑卷
代码实现:
# lvcreate -n lv0 -l 100%vg vg0 # 使用vg0的全部空间创建逻辑卷lv0
Logical volume "lv0" created.
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID zGNz6W-N4QC-m3ZF-ey3h-mP7i-gFJ3-DjGMX1
LV Write Access read/write
LV Creation host, time Centos7.magedu.com, 2017-08-19 21:27:34 +0800
LV Status available
# open 0
LV Size 9.99 GiB
Current LE 1023
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
# lsblk -f /dev/sdb
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0
此时会在/dev目录下生成一个新的设备文件:
# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Aug 19 21:27 /dev/vg0/lv0 -> ../dm-0 # 软链接文件
#ll /dev/dm-0
brw-rw----. 1 root disk 253, 0 Aug 19 21:27 /dev/dm-0
4 创建文件系统
在逻辑卷lv0创建完成后话需要创建文件系统并挂载到制定目录才能够使用,当然还需要将挂载信息写入/etc/fstab文件
代码实现:
# mkfs.xfs /dev/vg0/lv0 # 创建文件系统
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=654720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2618880, 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
# lsblk /dev/sdb -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a
# lsblk /dev/vg0/lv0 -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a
# mkdir /mnt/lv0 && mount /dev/vg0/lv0 /mnt/lv0 # 将逻辑卷挂载到制定目录
[root@Centos7T ~]#df -T # 查看挂载点
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 xfs 52403200 4605416 47797784 9% /
/dev/sda1 xfs 1038336 172116 866220 17% /boot
/dev/sda5 xfs 52403200 376288 52026912 1% /app
/dev/sr0 iso9660 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 xfs 10465280 32944 10432336 1% /mnt/lv0
# blkid /dev/vg0/lv0
/dev/vg0/lv0: UUID="549cfbaa-a6dc-4161-a765-5f55af78131a" TYPE="xfs"
#df -T /dev/vg0/lv0
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg0-lv0 xfs 10465280 32944 10432336 1% /mnt/lv0
# echo "UUID=549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0 xfs defaults 0 0" >> /etc/fstab
# cat /etc/fstab | egrep "UUID"
UUID=806ef114-dff2-4c17-aff7-c95bc67faf40 / xfs defaults 0 0
UUID=3df87bb6-2f6f-4d3f-af94-5ec47d2a9796 /app xfs defaults 0 0
UUID=1e7ba91a-85c8-442b-b105-bc61c00ddbee /boot xfs defaults 0 0
UUID=cc0e9678-d369-4bb9-9039-fd2f322df8b4 swap swap defaults 0 0
UUID=549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0 xfs defaults 0 0
至此,逻辑卷创建完毕并可投入使用。
5 扩展逻辑卷
任何分区在使用过程中难免会遇到空间不足的情况,逻辑卷也不例外。但我们可以很方便的多逻辑卷进行扩展,不论是使用新的空白磁盘还是已有的空白分区。其大致实现原理为:对空白分区创建物理卷,然后将物理卷加入到需要扩展逻辑卷所在的卷组,最后扩大逻辑卷空间。如果逻辑卷所在的卷组还有空闲的空间(PE)可被使用且足够大,则甚至不需要另外的分区或者磁盘。本文还是以新的分区为例扩展逻辑卷lv0.
代码实现:
修改系统ID并创建PV
[root@Centos7T ~]#lsblk -f /dev/sdc
NAME FSTYPE LABEL UUID MOUNTPOINT
sdc
└─sdc1 xfs 6ef26358-ce07-4b0c-b28b-07caa90838bd
[root@Centos7T ~]#fdisk /dev/sdc
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): p
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe20e9d16
Device Boot Start End Blocks Id System
/dev/sdc1 2048 62914559 31456256 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe20e9d16
Device Boot Start End Blocks Id System
/dev/sdc1 2048 62914559 31456256 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos7T ~]#pvcreate /dev/sdc1 # 在指定分区上创建物理卷
WARNING: xfs signature detected on /dev/sdc1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdc1.
Physical volume "/dev/sdc1" successfully created.
[root@Centos7T ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 9.99g 0
/dev/sdc1 lvm2 --- 30.00g 30.00g
将PV加入到指定VG
[root@Centos7T ~]#vgextend vg0 /dev/sdc1 # 将物理卷加入已有的卷组
Volume group "vg0" successfully extended
[root@Centos7T ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 39.98g 29.99g # VFREE为30G,即为新增物理卷的空间
[root@Centos7T ~]#pvdisplay /dev/sdc1
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 30.00 GiB / not usable 9.00 MiB
Allocatable yes
PE Size 10.00 MiB
Total PE 3071
Free PE 3071
Allocated PE 0
PV UUID Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
可知,将新建的PV加入到vg0后,vg0也有了30G的可用空间。同时/dev/sdc1的PE大小自动与/dev/sdb1同步为10M。
扩展逻辑卷LV
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 9.99g
[root@Centos7T ~]#lvextend -l 100%free /dev/vg0/lv0 # 扩展逻辑卷,使用卷组的全部可用空间
Size of logical volume vg0/lv0 changed from 9.99 GiB (1023 extents) to 29.99 GiB (3071 extents).
Logical volume vg0/lv0 successfully resized.
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 29.99g
[root@Centos7T ~]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID zGNz6W-N4QC-m3ZF-ey3h-mP7i-gFJ3-DjGMX1
LV Write Access read/write
LV Creation host, time Centos7.magedu.com, 2017-08-19 21:27:34 +0800
LV Status available
# open 1
LV Size 29.99 GiB
Current LE 3071
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@Centos7T ~]#lsblk -f /dev/sd{b,c}
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0
sdc
└─sdc1 LVM2_member Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0
逻辑卷扩展成功。
5 删除逻辑卷
本文由于更多是演示,删除逻辑卷会卷组、物理卷一并彻底删除。但实际生产中可能只是单独删除逻辑卷,卷组、物理卷还会保留。当然,实际生产中很少需要删除逻辑卷。在删除逻辑卷之前,所有有用的资料都需要备份转移。
代码实现:
[root@Centos7T ~]#sed -i '/lv0/d' /etc/fstab && umount /mnt/lv0/ # 删除配置信息
[root@Centos7T ~]#cat /etc/fstab | egrep "UUID"
UUID=806ef114-dff2-4c17-aff7-c95bc67faf40 / xfs defaults 0 0
UUID=3df87bb6-2f6f-4d3f-af94-5ec47d2a9796 /app xfs defaults 0 0
UUID=1e7ba91a-85c8-442b-b105-bc61c00ddbee /boot xfs defaults 0 0
UUID=cc0e9678-d369-4bb9-9039-fd2f322df8b4 swap swap defaults 0 0
[root@Centos7T ~]#df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 xfs 52403200 4605388 47797812 9% /
/dev/sda1 xfs 1038336 172116 866220 17% /boot
/dev/sda5 xfs 52403200 376288 52026912 1% /app
/dev/sr0 iso9660 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 29.99g
[root@Centos7T ~]#lvremove /dev/vg0/lv0 # 移除逻辑卷
Do you really want to remove active logical volume vg0/lv0? [y/n]: y
Logical volume "lv0" successfully removed
[root@Centos7T ~]#lvs
[root@Centos7T ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- 39.98g 39.98g
[root@Centos7T ~]#vgremove vg0 # # 移除卷组
Volume group "vg0" successfully removed
[root@Centos7T ~]#vgs
[root@Centos7T ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 10.00g 10.00g
/dev/sdc1 lvm2 --- 30.00g 30.00g
[root@Centos7T ~]#pvremove /dev/sdb1 /dev/sdc1 # 移除物理卷
Labels on physical volume "/dev/sdb1" successfully wiped.
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@Centos7T ~]#pvs
[root@Centos7T ~]#lsblk -f /dev/sd{b,c}
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1
sdc
└─sdc1 LVM2_member Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
至此,逻辑卷删除完成。当然,/dev/sdc1显示LVM2_member(实际上sdb1也是的),这是由于我们前面将/dev/sdc1的系统ID改为8e导致的,重新将系统ID改回83即可。