17-在Linux中通过LVM管理 扩容和缩小磁盘空间
了解LVM了解PV、LV、VGPV(Physical Volume):物理卷是LVM的“逻辑存储块”,与硬盘分区是逻辑的对应关系。多个PV可以组合或拆分,从而实现容量的增加或缩减。LV(Logical Volume):逻辑卷是LVM上的“逻辑分区”(类似Windows文件系统中的NTFS分区)。LV的容量从VG中提取然后进行创建,最后对LV进行格式化并挂载。LV是分配给到Linux系统中使用的。V
一、了解LVM
- PV(Physical Volume):物理卷是LVM的“逻辑存储块”,与硬盘分区是逻辑的对应关系。多个PV可以组合或拆分,从而实现容量的增加或缩减。
- LV(Logical Volume):逻辑卷是LVM上的“逻辑分区”(类似Windows文件系统中的NTFS分区)。LV的容量从VG中提取然后进行创建,最后对LV进行格式化并挂载。LV是分配给到Linux系统中使用的。
- VG(Volume Groups):卷组是多个PV的组合,LVM逻辑上的“磁盘设备”。卷组的大小取决于PV的容量和个数。
- PE(Physical extent): 物理长度。在将物理卷组合为卷组,在LVM逻辑磁盘上的“最小存储单元”,默认是4MB(值可修改,一般不必要)。
注:如通过lvcreate –l 200 –n test_lv centos #使用200个最小存储单元创建1个名为test_lv逻辑卷,容量大小为200*4MB,且存储空间从centos卷组中提取。
了解LVM的基本命令
注:扩容或缩小LVM后,记得使用xfs_growfs(适用xfs文件格式)或resize2fs(适用ext4文件格式)更新文件系统
二、查看当前虚拟机的硬盘容量
用df –Th查看磁盘使用情况,Size空间大约在17G
用fdisk –l查看磁盘设备情况
用ls /dev/sd*查看分区数量,可以看到,现有系统有一块硬盘sda,共20G,并分成2个分区,其中/dev/sda1是分配给boot分区使用,/dev/sda2是分配给根目录,交由LVM来管理。
三、给/根目录扩容(加硬盘方式)
3.1 关闭虚拟机系统,并添加一块新的硬盘
3.2 用fdisk –l查看磁盘分区情况,可以看到新加的硬盘已被识别,但还未进行分区。
[root@localhost ~]# fdisk -l
Disk /dev/sda: 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: 0x000c8cd0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
3.3 用lsblk查看硬盘设备
[root@ntp ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 4.4G 0 rom
3.4 使用fdisk /dev/sdb对硬盘设备进行分区
[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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x70c9290d.
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): 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 # change a partition's system id
Selected partition 1 #指定分区编号
Hex code (type L to list all codes): 8e #指定分区标签为LVM
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
3.5 使用partprobe /dev/sdb更新分区表,也可以重启系统
注:如果无法识别到硬盘分区信息,可使用此命令不用重启。
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# fdisk -l
Disk /dev/sda: 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: 0x000c8cd0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x70c9290d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20971519 10484736 8e Linux LVM
------------------省略部分信息------------------
3.6 使用pvcreate将/dev/sdb1支持LVM
注物理卷与硬盘设备的分区是逻辑上一一对应的关系。
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
3.7 查看系统中的PV物理卷
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- <10.00g <10.00g #此PV还未加入任何卷组中
3.8 使用vgextend命令将/dev/sdb物理卷加到当前系统的centos卷组中,扩容卷组容量。
[root@localhost ~]# vgextend /dev/centos /dev/sdb1
Volume group "centos" successfully extended
3.9 查看当前系统的vg卷组,
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 28.99 GiB
PE Size 4.00 MiB
Total PE 7422
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 2559 / <10.00 GiB
VG UUID EVvYM0-Gw4t-Hs0Z-hTet-iywX-lw87-qMPuf7
3.10 df –h查看系统磁盘信息
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.2G 16G 7% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
3.11 给根目录/dev/mapper/centos-root扩展逻辑卷容量
注:扩容/根目录时提示目录忙碌。最后跳过卸载/dev/centos/root根目录,直接扩容centos-root逻辑卷也能成功,这里只是实验对/dev/mapper/centos-root目录进行扩展,但是平时的维护应该避免对/目录进行扩容,而是选择扩容其他目录下(通过建立逻辑卷,然后与该目录建立mount关系),同时为了保证数据安全,在扩容或缩小容量一定要记得卸载挂载点。
卸载/目录
[root@localhost ~]# umount /dev/mapper/centos-root
umount: /: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
安装fuser工具
yum install psmisc -y
查看/目录下那些进程占用了目录
[root@localhost ~]# fuser -m -v /
强制杀死/目录下所有进程,结果导致xshell连接中断
3.12 使用lvextend对/dev/mapper/centos-root逻辑卷进行扩容,centos表示vg卷组,root表示逻辑卷。
[root@localhost ~]# lvextend -L +5G /dev/mapper/centos-root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <22.00 GiB (5631 extents).
Logical volume centos/root successfully resized.
3.13 使用xfs_growfs命令更新文件系统
如果逻辑卷是使用ext4文件系统格式,使用resize2fs命令更新文件系统,在更新前有必要检查硬盘完整性,如e2fsck –f /dev/centos/root,检查前需要卸载设备和挂载点的关联。
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, 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 4455424 to 5766144
3.14 最后再df –h查看
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 22G 1.2G 21G 6% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
3.15 重启系统,验证能否登录
[root@localhost ~]# shutdown -r now
3.16 使用vgdisplay查看卷组的仔细信息
案例4 创建逻辑卷并分配给/lv_test目录使用
注:新建逻辑卷,分三步走,一:从卷组中提取空间用于创建逻辑卷;二:将逻辑卷格式化并挂载到系统目录下;三:将挂载信息写入/etc/fstab配置文件中。不需要使用xfs_growfs或resize2fs更新文件系统信息(只有在扩容或缩小容量时使用),特别是缩小容量操作要慎重,容易导致数据丢失或系统无法启动,每操作一个步骤都要仔细确认并确保做好数据备份。
4.1 将卷组中剩下的4G空间分配出来,建立一个ext4格式的逻辑卷,并挂载到/lv_test目录下
#创建/lv-test目录
[root@localhost ~]# mkdir /lv_test
[root@localhost ~]# ls /
bin dev home lib64 media opt root sbin sys usr
boot etc lib lv_test mnt proc run srv tmp var
4.2 创建逻辑卷lvtest
[root@localhost ~]# lvcreate -n lvtest -L 4G centos
Logical volume "lvtest" created.
4.3 查看vgs
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 3 0 wz--n- 28.99g 1020.00m
4.4 查看lvs
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvtest centos -wi-a----- 4.00g
root centos -wi-ao---- <22.00g
swap centos -wi-ao---- 2.00g
4.5 格式化并挂载
[root@localhost ~]# mkfs.ext4 /dev/centos/lvtest
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
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 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
[root@localhost ~]# mount /dev/centos/lvtest /lv_test
4.6 写入/etc/fstab
4.7 df –h检查文件系统的占用情况
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 22G 1.4G 21G 7% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/centos-lvtest 3.9G 16M 3.6G 1% /lv_test
案例五 扩容和缩小逻辑卷容量
注1:xfs格式的LVM不支持缩小容量,ext4的LVM支持缩小容量!安装系统时,如果选择系统自动分区且使用LVM文件系统,默认采用xfs文件系统格式。
注2:缩小逻辑卷容量有可能会删除逻辑卷上的数据
5.1 扩容逻辑卷lvtest的容量
5.1.1 卸载挂载点
[root@localhost ~]# umount /lv_test/
5.1.2 检查文件系统
[root@localhost ~]# e2fsck -f /dev/centos/lvtest
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/centos/lvtest: 11/262144 files (0.0% non-contiguous), 53326/1047552 blocks
5.1.3 将剩余空间全部扩容到lvtest逻辑卷
[root@localhost ~]# lvextend -L +1G /dev/centos/lvtest
Size of logical volume centos/lvtest changed from <4.00 GiB (1023 extents) to <5.00 GiB (1279 extents).
Logical volume centos/lvtest successfully resized.
或者
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/lvtest
Size of logical volume centos/lvtest changed from 724.00 MiB (181 extents) to <5.00 GiB (1279 extents).
Logical volume centos/lvtest successfully resized.
5.1.4 使用resize2fs /dev/centos/lvtest
[root@localhost ~]# resize2fs /dev/centos/lvtest
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/centos/lvtest to 1309696 (4k) blocks.
The filesystem on /dev/centos/lvtest is now 1309696 blocks long.
5.1.5 重新挂载
[root@localhost ~]# mount -a
5.1.6 df –h查看文件系统占用情况
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 22G 1.4G 21G 7% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/centos-lvtest 4.8G 16M 4.7G 1% /lv_test
5.2 缩小逻辑卷lvtest的容量
5.2.1 缩小lvtest逻辑卷容量300M
注:执行缩容前一定要先卸载文件系统与设备的关联,这里选择卸载硬盘设备上的逻辑卷
[root@localhost ~]# umount /dev/centos/lvtest
5.2.2 检查文件系统的完整性,确保硬盘数据正常
[root@localhost ~]# e2fsck -f /dev/centos/lvtest
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/centos/lvtest: 11/327680 files (0.0% non-contiguous), 57438/1309696 blocks
5.2.3 通知文件系统减少容量信息(重置硬盘容量300M)
[root@localhost ~]# resize2fs /dev/centos/lvtest 300M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/centos/lvtest to 76800 (4k) blocks.
The filesystem on /dev/centos/lvtest is now 76800 blocks long.
5.2.4 把逻辑卷lvtest容量减少300M
[root@localhost ~]# lvreduce -L -300M /dev/centos/lvtest
WARNING: Reducing active logical volume to 4.70 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/lvtest? [y/n]: y
Size of logical volume centos/lvtest changed from <5.00 GiB (1279 extents) to 4.70 GiB (1204 extents).
Logical volume centos/lvtest successfully resized.
5.2.5 使用resize2fs /dev/centos/lvtest更新文件系统
[root@localhost ~]# resize2fs /dev/centos/lvtest
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/centos/lvtest to 1232896 (4k) blocks.
The filesystem on /dev/centos/lvtest is now 1232896 blocks long.
5.2.6 mount –a重新挂载
5.2.7 lvs查看逻辑卷
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvtest centos -wi-ao---- 4.70g
root centos -wi-ao---- <22.00g
swap centos -wi-ao---- 2.00g
5.2.8 lsblk查看硬盘设备信息,并且显示硬盘设备之间的依赖关系
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 22G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
├─centos-root 253:0 0 22G 0 lvm /
└─centos-lvtest 253:2 0 4.7G 0 lvm /lv_test
sdc 8:32 0 5G 0 disk
sr0 11:0 1 4.4G 0 rom
5.2.9 df –h查看文件系统信息
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 22G 1.4G 21G 7% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/centos-lvtest 4.6G 16M 4.4G 1% /lv_test
附:Linux磁盘分区格式
更多推荐
所有评论(0)