CentOS 7 磁盘扩容
扩容步骤
- 增加虚拟机磁盘容量,从 40G 增加到 120G;
- 创建磁盘分区;
- 创建 pv;
- 扩容 vg;
- 扩容 lv;
详细步骤
登录系统,使用
fdisk
或者parted
创建新分区,这里使用fdisk
命令1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62[root@jenkins ~]# fdisk /dev/sda
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 (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (83886080-251658239, default 83886080):
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-251658239, default 251658239):
Using default value 251658239
Partition 3 of type Linux and of size 80 GiB is set
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x0000df14
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 251658239 83886080 83 Linux
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x0000df14
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 251658239 83886080 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.创建完新分区后,需要使用
partprobe
命令重新加载分区表,否则无法找到新建的分区1
2[root@jenkins ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.创建玩新分区后,使用
pvcreate
创建新的 PV1
2[root@jenkins ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.扩容 VG 或者新建一个 VG,这里使用扩容命令 vgextend 扩容 VG centos
1
2[root@jenkins ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended查看 VG 信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21[root@jenkins ~]# 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 118.99 GiB
PE Size 4.00 MiB
Total PE 30462
Alloc PE / Size 9982 / 38.99 GiB
Free PE / Size 20480 / 80.00 GiB
VG UUID 6im9xc-9k0x-tUq7-DHWd-ER4r-WiNs-j2Szfi查看 LV 信息
1
2
3
4[root@jenkins ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <35.12g
swap centos -wi-ao---- <3.88g扩容 LV
1
2
3
4
5
6
7
8[root@jenkins ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <35.12 GiB (8990 extents) to <115.12 GiB (29470 extents).
Logical volume centos/root successfully resized.
[root@jenkins ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <115.12g
swap centos -wi-ao---- <3.88g重新加载逻辑卷的大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# ext4 文件系统的方法
[root@jenkins ~]# resize2fs /dev/centos/root
# xfs 文件系统的方法
[root@jenkins ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2301440 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=9205760, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4495, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9205760 to 30177280查看最新的挂载信息
1
2
3
4
5
6
7
8
9[root@jenkins ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 116G 3.0G 113G 3% /
/dev/sda1 xfs 1014M 137M 878M 14% /boot
tmpfs tmpfs 396M 0 396M 0% /run/user/1000
LVM 磁盘管理
创建磁盘分区
- 使用 fdisk 命令创建新分区,并将分区格式设置为 Linux LVM 类型
1 | [root@qdmwms ~]# fdisk /dev/sdb |
PV 管理
使用 pvcreate 创建物理卷
1
2[root@qdmwms ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.使用 pvs 或者 pvdisplay 查看 PV 信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28[root@qdmwms ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 centos lvm2 a-- 444.90g 4.00m
/dev/sdb1 lvm2 --- <1.82t <1.82t
[root@qdmwms ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name centos
PV Size <444.91 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 113895
Free PE 1
Allocated PE 113894
PV UUID UvPihc-JF7t-mgQM-co5Q-NmNp-iCxg-xL2GKL
"/dev/sdb1" is a new physical volume of "<1.82 TiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <1.82 TiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ar3v7h-IE6S-heih-zwBU-a1Ky-l9rr-8YFy93删除 PV 命令为 pvremove,命令格式如下
1
[root@qdmwms ~]# pvremove /dev/sdb1
VG 管理
使用 vgcreate 创建卷组
1
2[root@qdmwms ~]# vgcreate data /dev/sdb1
Volume group "data" successfully created使用 vgs 或者 vgdisplay 命令查看卷组信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46[root@qdmwms ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- 444.90g 4.00m
data 1 0 0 wz--n- <1.82t <1.82t
[root@qdmwms ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 444.90 GiB
PE Size 4.00 MiB
Total PE 113895
Alloc PE / Size 113894 / <444.90 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID W1i9gV-0y9J-19ps-eYP9-cBai-sf00-i4qiFm
--- Volume group ---
VG Name data
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 <1.82 TiB
PE Size 4.00 MiB
Total PE 476598
Alloc PE / Size 0 / 0
Free PE / Size 476598 / <1.82 TiB
VG UUID 33pj0j-EFZr-A1qq-OT9o-So3N-T7nJ-N9WduY可以使用 vgremove 命令删除卷组,命令格式如下
1
[root@qdmwms ~]# vgremove data
LV 管理
创建指定大小空间的逻辑卷,参数为
-L
:1
2[root@qdmwms ~]# lvcreate -L 100G -n lv1 data
Logical volume "lv1" created.使用卷组的百分比创建逻辑卷,参数为
-l
:1
2[root@qdmwms ~]# lvcreate -l +100%VG -n lvm-data data
Logical volume "lvm-data" created.使用卷组的可用空间百分比创建逻辑卷
1
2[root@qdmwms ~]# lvcreate -l +100%Free -n lvm-data data
Logical volume "lvm-data" created.使用 lvs 或者 lvdisplay 命令查看逻辑卷信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75[root@qdmwms ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <340.90g
root centos -wi-ao---- 100.00g
swap centos -wi-ao---- 4.00g
lv1 data -wi-a----- 100.00g
[root@qdmwms ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 03iJRb-bm9Q-50Hq-8Bqo-bpvy-bjbA-HSCvrc
LV Write Access read/write
LV Creation host, time qdmwms, 2021-05-21 17:17:40 +0800
LV Status available
# open 1
LV Size 100.00 GiB
Current LE 25600
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID rktF4n-rxnw-b36X-e7cY-GBsN-P0lu-1qrUpQ
LV Write Access read/write
LV Creation host, time qdmwms, 2021-05-21 17:17:40 +0800
LV Status available
# open 1
LV Size <340.90 GiB
Current LE 87270
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID fPeOkm-O87N-sHdw-S3C4-NFCd-pjcM-nSDXzN
LV Write Access read/write
LV Creation host, time qdmwms, 2021-05-21 17:17:41 +0800
LV Status available
# open 2
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
--- Logical volume ---
LV Path /dev/data/lv1
LV Name lv1
VG Name data
LV UUID J45Veo-16Gm-eYaW-unCe-jPTA-ovfJ-O4z38O
LV Write Access read/write
LV Creation host, time qdmwms, 2021-05-22 08:45:40 +0800
LV Status available
# open 0
LV Size 100.00 GiB
Current LE 25600
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3使用 lvremove 命令删除逻辑卷
1
2
3[root@qdmwms ~]# lvremove /dev/data/lv1
Do you really want to remove active logical volume data/lv1? [y/n]: y
Logical volume "lv1" successfully removed
挂载逻辑卷
使用 mkfs.xxx 命令格式话逻辑卷,然后使用 mount 命令挂载逻辑卷
1
2[root@qdmwms ~]# mkfs.ext4 /dev/data/data
[root@qdmwms ~]# mount /dev/data/data /data
在线缩容(ext格式)
重新调整 LV 大小,将 data 的尺寸调整为 200G
1
lvreduce -L 200G /dev/ol/data
将腾出的 VG 剩余空间全部给 root
1
lvextend -l +100%FREE /dev/ol/root
刷新 root 的容量信息
1
resize2fs /dev/ol/root