最近安装虚拟机时老是发现磁盘空间不够,但是因为网上的方法感觉都不是很全,因此整理一份详细的攻略,以作留存。
虚拟机软件:Oracle VM VirtualBox v5
系统:centos 7
1.关闭虚拟机,找到虚拟机的安装目录
cmd 进入安装目录下
C:Usersft>d: D:>cd D:ft D:ft>
2.查看虚拟机硬盘状态,找到需要改变的虚拟机,我这边是salve1
D:ft>VBoxManage list hdds UUID: 43fe3803-36e7-4db7-9877-63f19dec6ee0 Parent UUID: base State: locked write Type: normal (base) Location: C:UsersftVirtualBox VMsmastermaster.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: f100d1ba-bd50-40ac-a608-8c5c4b428995 Parent UUID: base State: created Type: normal (base) Location: C:UsersftVirtualBox VMsslave2slave2.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: dc4a20c8-ef7a-4376-b535-7a586b25820d Parent UUID: base State: created Type: normal (base) Location: C:UsersftVirtualBox VMsslave1slave1.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled
slave1原本为8G,现在将其改为18G,记录下它的UUID
3.增加硬盘容量
D:fangtao>VBoxManage modifyhd dc4a20c8-ef7a-4376-b535-7a586b25820d --resize 18192
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
D:fangtao>
其中,dc开头的代码为需要修改虚拟机的UUID, 18192 为修改后的虚拟机大小,再次查看虚拟机硬盘状态,发现硬盘已经变大
D:ft>VBoxManage list hdds UUID: 43fe3803-36e7-4db7-9877-63f19dec6ee0 Parent UUID: base State: locked write Type: normal (base) Location: C:UsersftVirtualBox VMsmastermaster.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: f100d1ba-bd50-40ac-a608-8c5c4b428995 Parent UUID: base State: created Type: normal (base) Location: C:UsersftVirtualBox VMsslave2slave2.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: dc4a20c8-ef7a-4376-b535-7a586b25820d Parent UUID: base State: created Type: normal (base) Location: C:UsersftVirtualBox VMsslave1slave1.vdi Storage format: VDI Capacity: 18192 MBytes Encryption: disabled
4.创建分区
打开虚拟机(若原本是休眠状态的,需要重启)
[root@slave1 ~]# df -h #容量依旧是6G 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/cl-root 6.2G 5.8G 441M 94% / devtmpfs 481M 0 481M 0% /dev tmpfs 497M 156K 497M 1% /dev/shm tmpfs 497M 7.0M 490M 2% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/sda1 1014M 173M 842M 18% /boot tmpfs 100M 28K 100M 1% /run/user/1000
[root@slave1 ~]# fdisk -l /dev/sda # 查看磁盘分区表
磁盘 /dev/sda:19.1 GB, 19075694592 字节,37257216 个扇区 #新增的
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0000f022
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 16777215 7339008 8e Linux LVM
[root@slave1 ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (16777216-37257215,默认为 16777216):
将使用默认值 16777216
Last 扇区, +扇区 or +size{K,M,G} (16777216-37257215,默认为 37257215):
将使用默认值 37257215
分区 3 已设置为 Linux 类型,大小设为 9.8 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@slave1 ~]# fdisk -l /dev/sda
磁盘 /dev/sda:19.1 GB, 19075694592 字节,37257216 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0000f022
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 16777215 7339008 8e Linux LVM
/dev/sda3 16777216 37257215 10240000 83 Linux #新增
[root@slave1 ~]#
[root@slave1 ~]# partprobe #接收新的分区表信息 也可以直接重启
[root@slave1 ~]# mkfs.ext4 /dev/sda3 #将分区格式化为ext4格式
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
640848 inodes, 2560000 blocks
128000 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
79 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@slave1 ~]#
[root@slave1 ~]# vgdisplay #查看卷组名
--- Volume group ---
VG Name cl #记录下cl
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 7.00 GiB
PE Size 4.00 MiB
Total PE 1791
Alloc PE / Size 1791 / 7.00 GiB
Free PE / Size 0 / 0
VG UUID FCGIuD-oCl3-NOws-Gpot-eyHi-41zz-p4mxmG
[root@slave1 ~]# pvcreate /dev/sda3 #创建新物理卷
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
[root@slave1 ~]# vgextend cl /dev/sda3 #扩展到卷组
Volume group "cl" successfully extended
[root@slave1 ~]# lvdisplay #查看根分区
--- Logical volume ---
LV Path /dev/cl/swap
LV Name swap
VG Name cl
LV UUID 95DKbg-Uh4p-bc7P-e6ql-L4Zc-wLJV-McwpRQ
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-02-18 16:05:35 +0800
LV Status available
# open 2
LV Size 820.00 MiB
Current LE 205
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/cl/root #记录下
LV Name root
VG Name cl
LV UUID p7T17B-ozhs-Cqez-dG1W-LrHk-X1SM-lJhzwf
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-02-18 16:05:35 +0800
LV Status available
# open 1
LV Size 6.20 GiB
Current LE 1586
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@slave1 ~]# lvextend /dev/cl/root /dev/sda3 #扩展到容量逻辑分区
Size of logical volume cl/root changed from 6.20 GiB (1586 extents) to 15.96 GiB (4085 extents).
Logical volume cl/root successfully resized.
[root@slave1 ~]# resize2fs /dev/cl/root # 刷新逻辑分区
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 当尝试打开 /dev/cl/root 时
找不到有效的文件系统超级块.
[root@slave1 ~]# cat /etc/fstab #若报错,查看文件系统是不是xfs 的
#
# /etc/fstab
# Created by anaconda on Mon Feb 18 16:05:37 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=8307e61c-9c4f-4aa0-abe8-fe985cbe6a5d /boot xfs defaults 0 0
/dev/mapper/cl-swap swap swap defaults 0 0
[root@slave1 ~]# xfs_growfs /dev/cl/root #若是,则用该命令
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=406016 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1624064, 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 1624064 to 4183040
[root@slave1 ~]# df -h #查看根目录
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 16G 5.8G 11G 37% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 497M 84K 497M 1% /dev/shm
tmpfs 497M 7.0M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 100M 16K 100M 1% /run/user/42
tmpfs 100M 0 100M 0% /run/user/1000
tmpfs 100M 0 100M 0% /run/user/0
[root@slave1 ~]# reboot #重启