扩容分区
最近因为买的vps总共60g硬盘,结果只给我了5g系统盘,剩下的需要我自己挂载,但挂载后发现系统最后还是因为磁盘空间满了,而无法运行,就连基本的vim打开文件都成问题。因些学习给给分区扩容的方法。
为了熟练应用,我先在自己虚拟机上试验,已经成功扩容了500M空间,还有500M没分配,下面是过程
初始状态
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 3.4G 14G 20% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 378M 0 378M 0% /run/user/0
[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: 0x000b1260
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: 1073 MB, 1073741824 bytes, 2097152 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: 0xc54f72f4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 8e Linux LVM
Disk /dev/mapper/centos-root: 18.8 GB, 18769510400 bytes, 36659200 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
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID 5cv5pM-y4pl-smPG-ua5w-PtyA-wuuo-JPvfoj
--- Physical volume ---
PV Name /dev/sdb1
VG Name centos
PV Size 500.00 MiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 124
Free PE 0
Allocated PE 124
PV UUID xrcueS-HaOs-v2vh-c6D2-5rU1-1QI1-W6PDzG
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 19.48 GiB
PE Size 4.00 MiB
Total PE 4987
Alloc PE / Size 4987 / 19.48 GiB
Free PE / Size 0 / 0
VG UUID KsvH41-BVX8-3bAx-PXoC-V0JJ-71xR-dWFKK9
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID hSKD4d-pF2b-9Xd9-tRYq-eerg-gj3r-4ffiU9
LV Write Access read/write
LV Creation host, time localhost, 2019-12-03 06:51:55 -0500
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 2V789n-VcGr-GE8l-vaGe-cjjb-ZqBr-5SNCJC
LV Write Access read/write
LV Creation host, time localhost, 2019-12-03 06:51:56 -0500
LV Status available
# open 1
LV Size 17.48 GiB
Current LE 4475
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
由fdisk -l
可以看到我新加的硬盘是:/dev/sdb 共1073M ,我已经扩容一sdb1 500M
pv.lv.vg的关系如下:
pv是指物理磁盘,vg相当于磁盘类似于win中的c盘,d盘这种概念,而lv则是逻辑分区。这里的pv有两个,一个是新建虚拟机时分配的20g,另一个sdb1则是我新添加的1g硬盘中分配出来的500M.
创建分区
[root@localhost ~]# fdisk /dev/sdb # 新加的1g盘 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 # n表示新建
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p # 主分区,最多只能建4个
Partition number (2-4, default 2): 2 # 因为我已经建过一个sdb1,这里只能从2开始
First sector (1026048-2097151, default 1026048): # 默认
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-2097151, default 2097151): # 默认
Using default value 2097151
Partition 2 of type Linux and of size 523 MiB is set
Command (m for help): t # 修改磁盘格式
Partition number (1,2, default 2): 2 # 选择第二块
Hex code (type L to list all codes): 8e # 更忙为Lvm格式
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p # 查看下格式信息,可以看到都是lvm
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xc54f72f4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 8e Linux LVM
/dev/sdb2 1026048 2097151 535552 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/kpartx命令来刷新分区表。 在没有这些操作之前我们来查看下:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 3.4G 14G 20% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 378M 0 378M 0% /run/user/0
[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: 0x000b1260
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: 1073 MB, 1073741824 bytes, 2097152 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: 0xc54f72f4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 8e Linux LVM
/dev/sdb2 1026048 2097151 535552 8e Linux LVM
Disk /dev/mapper/centos-root: 18.8 GB, 18769510400 bytes, 36659200 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
可以看到sdb2虽然可以看到,但系统容量仍然没有变化
我们运行命令:partprobe
[root@localhost ~]# partprobe # 这里后面有解决办法
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# partprobe /dev/sdb2
再次查看:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 3.4G 14G 20% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 378M 0 378M 0% /run/user/0
[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: 0x000b1260
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: 1073 MB, 1073741824 bytes, 2097152 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: 0xc54f72f4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 8e Linux LVM
/dev/sdb2 1026048 2097151 535552 8e Linux LVM
Disk /dev/mapper/centos-root: 18.8 GB, 18769510400 bytes, 36659200 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
[root@localhost ~]# partx /dev/sdb# 查看sdb硬盘使用情况
NR START END SECTORS SIZE NAME UUID
1 2048 1026047 1024000 500M
2 1026048 2097151 1071104 523M
这么看好像仍然没有变化,因为我们只是完成了分区,还没扩容 。
扩容
如果vg为空,我们可以创建:vgcreate命令
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 19.48 GiB
PE Size 4.00 MiB
Total PE 4987
Alloc PE / Size 4987 / 19.48 GiB
Free PE / Size 0 / 0
VG UUID KsvH41-BVX8-3bAx-PXoC-V0JJ-71xR-dWFKK9
我们有个vgroup: centos
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID 5cv5pM-y4pl-smPG-ua5w-PtyA-wuuo-JPvfoj
--- Physical volume ---
PV Name /dev/sdb1
VG Name centos
PV Size 500.00 MiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 124
Free PE 0
Allocated PE 124
PV UUID xrcueS-HaOs-v2vh-c6D2-5rU1-1QI1-W6PDzG
而pvolumn仍然是只有原来的一块和sdb1 再看看lvolumn
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID hSKD4d-pF2b-9Xd9-tRYq-eerg-gj3r-4ffiU9
LV Write Access read/write
LV Creation host, time localhost, 2019-12-03 06:51:55 -0500
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 2V789n-VcGr-GE8l-vaGe-cjjb-ZqBr-5SNCJC
LV Write Access read/write
LV Creation host, time localhost, 2019-12-03 06:51:56 -0500
LV Status available
# open 1
LV Size 17.48 GiB
Current LE 4475
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
很显示和原来的一样,只有一个交换分区和主分区,我们要扩展到这个主分区上。
创建物理空间sdb2
[root@localhost ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
而空间组只有centos,我们就将它扩展到这个centos上
[root@localhost ~]# vgextend centos /dev/sdb2
Volume group "centos" successfully extended
扩展到逻辑分区
这里用的是小写的-l参数, +100%FREE表示将所有空间的空间都扩展到/dev/centos/root这个逻辑分区上
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 17.48 GiB (4475 extents) to <17.99 GiB (4605 extents).
Logical volume centos/root successfully resized.
可以看到它从17.48g扩展到了17.99g。 但此时你查看磁盘仍然看不出来变化 :
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 3.4G 14G 20% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 378M 0 378M 0% /run/user/0
重设分区大小
[root@localhost ~]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.
我这是centos7系统,更改为使用:xfs_growfs
[root@localhost ~]# xfs_growfs /dev/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 4715520
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 18G 3.4G 15G 19% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 378M 0 378M 0% /run/user/0
[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: 0x000b1260
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: 1073 MB, 1073741824 bytes, 2097152 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: 0xc54f72f4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 8e Linux LVM
/dev/sdb2 1026048 2097151 535552 8e Linux LVM
Disk /dev/mapper/centos-root: 19.3 GB, 19314769920 bytes, 37724160 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
df -h
命令中即可看到centos-root已经由原来的17g变成了18g.
本博客参考链接: ref:https://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html
错误及解决办法
遇到的错误及错误解决的链接:
1 lvextend -L 500M /dev/centos/root
New size given (125 extents) not larger than existing size (4351 extents)
原因:本来磁盘有18.2g, 我在此基础上加了500m,所以报错
解决办法:-l注意这里的l是小写
lvextend -l +100%FREE /dev/centos/root
ref:https://www.golinuxcloud.com/new-size-given-extents-not-larger-than-existing-size-extents/
2 partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
原因: sr0为光驱,光驱为read only
解决办法:指定路径
partprobe /dev/sdb
ref:https://www.cnblogs.com/kerrycode/p/5634981.html
3 resize2fs /dev/centos/root
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.
原因:centos7 默认的文件系统是xfs,命令不对
解决办法:
xfs_growfs /dev/centos/root
ref:https://stackoverflow.com/questions/26305376/resize2fs-bad-magic-number-in-super-block-while-trying-to-open