之前遇到过一个linux服务器分区采用的是标准分区(非LVM),后来由于业务增长磁盘无法满足需求,但是磁盘分区除了boot和swap剩余就一个根分区,只能扩展根分区。
因为根目录会包含很多系统应用运行的配置等,肯定不能直接卸载,所以要采用一种既不破坏数据,又能扩容的方法:
情况说明:/dev/vda3挂载在/目录,是/dev/vda最后一个分区,刚好新增的空间跟vda3是连续的,在最后选择start和end的时候,刚好就能连接上,这是成功的前提!!!
一、磁盘配置
1.关机
2.设置–>扩展磁盘容量
3.开机
在VMware中,在虚拟机关机,并且无快照的情况下:
然后输入需要扩容的大小点击确定。
40G–>100G(扩展到100G大小)
最后启动虚拟机。
二、分区管理
使用xshell登录虚拟机,按以下步骤执行:
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda3 ext4 36G 34G 16M 100% /
tmpfs tmpfs 99G 23G 77G 23% /dev/shm
/dev/vda1 ext4 190M 39M 141M 22% /boot
/dev/vdb1 ext4 2.0T 1.7T 244G 88% /mnt/db1
[root@localhost ~]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes
16 heads, 63 sectors/track, 83220 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008e937
Device Boot Start End Blocks Id System
/dev/vda1 * 3 409 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 409 8731 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/vda3 8731 83221 37542912 83 Linux
Partition 3 does not end on cylinder boundary.
Disk /dev/vdb: 2199.0 GB, 2199023255552 bytes
16 heads, 63 sectors/track, 4260880 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x97b5a27a
Device Boot Start End Blocks Id System
/dev/vdb1 1 4260880 2147483488+ 83 Linux
由上可以发现,根目录上挂载的是/dev/vda3 这个分区,而且这个系统里的分区id为83,因此不能通过LVM进行逻辑卷管理。
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 40G 0 disk
├─vda1 252:1 0 200M 0 part /boot
├─vda2 252:2 0 4G 0 part [SWAP]
└─vda3 252:3 0 35.8G 0 part /
vdb 252:16 0 2T 0 disk
└─vdb1 252:17 0 2T 0 part /mnt/db1
通过这个命令可以知道,vda确实是已经加了容量了,之前的vda只有40G的。
(1) 现在我们就要把加了的容量添加到vda3并同步到根目录的文件系统中。
根据提示,先删除vda3根分区,然后重新创建vda3根分区
[root@localhost ~]# fdisk /dev/vda
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 //这里输入p,列出分区列表,记住下面的start和end,后续操作才能保证数据不会丢失。
Disk /dev/sda: 51.5 GB, 51539607552 bytes, 100663296 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: 0x000e780b
Device Boot Start End Blocks Id System
/dev/vda1 * 3 409 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 409 8731 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/vda3 8731 83221 37542912 83 Linux
Command (m for help): d //这里输入d,表示删除一个分区
Partition number (1-3, default 3): 3 //这里输入3,是因为之前的分区是/dev/sda3
Partition 3 is deleted
Command (m for help): n //删除完,输入n新建一个分区
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p //选择主分区
Partition number (3,4, default 3): 3 //还是/dev/sda3
First sector (3762176-100663295, default 3762176): //这里直接回车
Using default value 3762176
Last sector, +sectors or +size{K,M,G} (3762176-100663295, default 100663295): //这里明显可以看到,不仅包含了之前的sda3的start和end,而且远大于了,使用默认的将剩余的空间都给这个新建的分区。
Using default value 100663295
Partition 3 of type Linux and of size 46.2 GiB is set
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.
You have new mail in /var/spool/mail/root
2)重启系统
重启当前系统,可以选择 reboot 或者 init 6来执行。
//先重启,必须
[root@localhost ~]# reboot
(3)扩容文件系统
我们使用一下命令查看vda3的文件格式:
[root@db-phis01 ~]# df -Th /dev/vda3
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda3 ext4 36G 34G 16M 100% /
它的格式是ext4的,使用resize2fs命令,如果是xfs的,就要用xfs_growfs命令进行刷新文件系统容量
[root@localhost ~]# resize2fs /dev/vda3
然后我们再看根目录的大小:
[root@localhost ~]# df -hT
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 100G 13G 87G 13% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.9M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 297M 195M 103M 66% /boot
tmpfs 183M 0 183M 0% /run/user/0
发现从之前的40G升到了100G了,本次扩容成功。
由于我们的vda3挂载在根目录下,vda3是最后一个分区,所以刚好新增的空间跟vda3是连续的,在最后选择start和end的时候,刚好就能连接上,这也是成功的一个条件,如果不是最后一个分区挂载在根目录下,这个就比较麻烦,需要进一步研究。建议新建虚拟机的时候,选择用LVM来管理分区,这样就好好解决以上的问题了。