调整 VirtualBox 虚拟机的磁盘大小
调整 VirtualBox 虚拟机的磁盘大小使用 VM VirtualBox 创建了一个 CentOS 虚拟机,划分了 40GB 的磁盘空间,使用了动态分配存储的方式,文件系统为 LVM。折腾了一段时间之后,却发现磁盘容量不够了。读了一些资料之后,终于给 VirtualBox 的虚拟机增加了磁盘容量,并让 CentOS 的 LVM 将其利用上。
先看看虚拟机中虚拟磁盘的的分区表:
# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e
Device Boot Start End Blocks IdSystem
/dev/sda1 * 1 64 512000 83Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8eLinux LVM
/dev/sda3 1045 5221 33549074+8eLinux LVM
Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
#
VirtualBox 的磁盘管理
调整虚拟磁盘大小的时候,虚拟机必须关闭!
下面使用 VirtualBox 的命令行将该虚拟机的磁盘分配从 40GB 调整为 50GB。
VirtualBox 的说明文档中明确给出了调整虚拟机磁盘大小的方法。
modifymedium 指令就是用来调整磁盘大小的。对象可以是虚拟机的 uuid,也可以直接指定要调整大小的虚拟机磁盘文件名(全路径)。需要注意的是,如果安装 VirtualBox 的时候没有将其路径添加到系统路径之中,运行命令行的时候需要指定路径或切换到安装路径,如,
上述指令将 F:\VirtualBoxImgs目录下的 CentOS6.3.vdi 这个虚拟机磁盘调整为(参数 –resize)51200,单位是 MB。
如果想查看对应的 uuid,可以使用指令:
以上指令在Win10+VirtualBox 5.0.10版本上验证可行。
2. CentOS 的 LVM 管理
# df -h
Filesystem SizeUsed Avail Use% Mounted on
/dev/mapper/vg_yuxuechengcentos6-lv_root
37G 12G 26G31% /
tmpfs 499M 76K499M 1% /dev/shm
/dev/sda1 477M 51M397M12% /boot
/dev/sr0 57M 57M 0 100% /media/VBOXADDITIONS_5.0.10_104061
#
重新启动该虚拟机。再来看看磁盘情况:
# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e
Device Boot Start End Blocks IdSystem
/dev/sda1 * 1 64 512000 83Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8eLinux LVM
/dev/sda3 1045 5221 33549074+8eLinux LVM
Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
#
现在,磁盘大小为 53680MB。磁盘占用情况为,可以看到总大小还是40G的配置。
# df -h
Filesystem SizeUsed Avail Use% Mounted on
/dev/mapper/vg_yuxuechengcentos6-lv_root
37G 12G 26G31% /
tmpfs 499M 76K499M 1% /dev/shm
/dev/sda1 477M 51M397M12% /boot
/dev/sr0 57M 57M 0 100% /media/VBOXADDITIONS_5.0.10_104061
#
试试看用dd命令创建一个30G大小的块文件:
# dd if=/dev/zero of=1.0G.img bs=1M count=30000
dd: writing `1.0G.img': No space left on device
26223+0 records in
26222+0 records out
27496136704 bytes (27 GB) copied, 104.717 s, 263 MB/s
#
提示说磁盘空间不足(No space left on device)。虽然我们给了虚拟机足够的空间,但是虚拟机里的系统(这里是 CentOS)并没有将这一部分空间利用上。可以看到,虚拟机里的磁盘空间只有 37G,原始分配的 50G 没有用完。这同时也说明,即使创建虚拟机的时候使用动态分配磁盘大小,虚拟机里的系统也并不会自动修改自己的磁盘。如果是正常使用,即使虚拟机的虚拟磁盘 足够大,还是可能会在虚拟机中遇到磁盘空间不足的警告。
下面调整虚拟机操作系统的文件系统,使之将所有空间利用上。
将空余磁盘创建为 sda4
重新启动虚拟机,登入 CentOS,使用 fdisk 将虚拟磁盘的空闲空间创建为一个新的分区。注意要使用代表 Linux LVM 的分区号 8e 来作为 ID。
# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (5222-6527, default 5222):
Using default value 5222
Last cylinder, +cylinders or +size{K,M,G} (5222-6527, default 6527):
Using default value 6527
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 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.
#
上述命令执行后,可以使用fdisk -l /dev/sda来查看结果,来查看 sda4 是否成功创建并标记为 LVM,如果没有,则需要重新启动。
# fdisk -l /dev/sda
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e
Device Boot Start End Blocks IdSystem
/dev/sda1 * 1 64 512000 83Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8eLinux LVM
/dev/sda3 1045 5221 33549074+8eLinux LVM
/dev/sda4 5222 6527 10490445 8eLinux LVM
#
执行完后重启。
调整 LVM 大小
先看看当前 Volume Group:
# vgdisplay
--- Volume group ---
VG Name vg_yuxuechengcentos6
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No5
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 39.50 GiB
PE Size 4.00 MiB
Total PE 10112
Alloc PE / Size 10112 / 39.50 GiB
FreePE / Size 0 / 0
VG UUID 8oim1v-kbuK-5RjL-MchZ-PLqp-3Uco-OCTXMu
#
名称为 vg_yuxuechengcentos6,可调整大小(resizable),当前大小为39.50GB。
查看活动的 LVM 卷:
# lvscan
ACTIVE '/dev/vg_yuxuechengcentos6/lv_root' inherit
ACTIVE '/dev/vg_yuxuechengcentos6/lv_swap' inherit
#
先将分配过来的新磁盘空间创建为一个新的物理卷:
# pvcreate /dev/sda4
Writing physical volume data to disk "/dev/sda4"
Physical volume "/dev/sda4" successfully created
#
然后使用新的物理卷来扩展 LVM 的 vg_yuxuechengcentos6:
# vgextend vg_yuxuechengcentos6 /dev/sda4
Volume group "vg_yuxuechengcentos6" successfully extended
#
然后扩展 LVM 的逻辑卷 vg_yuxuechengcentos6-lv_root:
# lvextend /dev/mapper/vg_yuxuechengcentos6-lv_root /dev/sda4
Extending logical volume lv_root to 47.53 GiB
Logical volume lv_root successfully resized
#
最后,调整逻辑卷文件系统的大小,
# resize2fs /dev/mapper/vg_yuxuechengcentos6-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_yuxuechengcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_yuxuechengcentos6-lv_root to 12460032 (4k) blocks.
The filesystem on /dev/mapper/vg_yuxuechengcentos6-lv_root is now 12460032 blocks long.
#
调整完后,看看效果:
# lvscan
ACTIVE '/dev/vg_yuxuechengcentos6/lv_root' inherit
ACTIVE '/dev/vg_yuxuechengcentos6/lv_swap' inherit
#
另外,几个有用的 LVM 指令(lvm2):
[*]lvmdiskscan
[*]vgdisplay
[*]pvdisplay
[*]lvdispay
[*]vgcreate
[*]pvcreate
[*]lvscan
[*]pvscan
[*]vgscan
我这里用的是 CentOS 系统。如果是 Windows 就更简单了,可通过 Windows 的磁盘管理来重新调整分区大小以占用所有可用磁盘空间。
页:
[1]