背景:
前段时间装了一个虚拟机,准备装一个好几百g大小的软件,首先我把安装介质传到了虚拟机里面,用了100GB空间,然而我的安装介质明明只有50g,怎么回事呢?经过我用du -sh命令查询到底那里占了空间,最后锁定了
[root@lyg VMwareDnD]# pwd
/tmp/VMwareDnD
这个目录下有一个跟我的安装介质相同大小的文件,但是问题是,系统没有自动删除,最后我把这个目录下所有文件都手动删除了。
接着上面的问题,虽然虚拟机里已经删除了,也有了剩余空间。但是,我安装软件的时候,一直使用的是我宿主机的硬盘的空间,也就是说,我虚拟机里面的空闲的空间一直是那么多,就是不使用。。。这下麻烦了。。随着宿主机空间越来越小,一会宿主机就没磁盘了,还有就是我建虚拟机的时候给虚拟硬盘的空间是200G,没想到的是,虚拟机自己会扩张这个硬盘的大小,最后比200G还大很多。。
最后,我决定想办法收缩一下硬盘。。。
Compact a Virtual Disk
Compacting a virtual disk reclaims(回收再利用) unused space in the virtual disk. If a disk has empty space, this process reduces the amount of space the virtual disk occupies on the host drive(这个进程操作降低了虚拟磁盘占用宿主机磁盘的空间)
Make sure the following prerequisites are met:
• |
The virtual machine is powered off.(先关机) |
• |
The virtual disk is not mapped (on Windows guests) or mounted (on Linux guests). You cannot compact a virtual disk while it is mapped or mounted.(没被挂载) |
• |
The disk space is not preallocated (被预先分配)for the virtual hard disk. Use the virtual machine settings editor to view the disk information for this virtual hard disk. If the disk space was preallocated, you cannot compact the disk. |
• |
If the virtual hard disk is an independent disk, it is in persistent mode. |
To change the mode, see Exclude a Virtual Disk from Snapshots for a description of independent disks.
To compact a virtual disk
1 |
Select VM > Settings. |
2 |
On the Hardware tab, select Hard Disk for the virtual hard disk you want to compact. |
3 |
Select Utilities > Compact |
4 |
Click OK after the disk compacting process is complete. |
我按照上面的步骤进行了compact,很遗憾,不知道为什么,花了好几个小时,进行了compact,竟然硬盘没有任何的变化,这个时候我都快崩溃了。。。。
从网上找到了这个连接,貌似这个网友与我碰到了一样的问题:
在这个问题的答案下面,有网友说,shrink应该管用,于是,我去找了shrink的相关资料:
http://www.vmware.com/support/ws5/doc/ws_disk_shrink.html(这是官方的文档)
Shrinking Virtual Disks
If you have a virtual disk that grows as data is added, you can shrink it as described in this section.If you allocated all the space for your virtual disk at the time you created it, you
cannot shrink it.(就是说,如果你选择了在建立虚拟机的时候一次性分配硬盘空间,那就不能收缩了)
Note: The maximum benefit occurs when you defragment
a virtual disk before you shrink it. See Defragmenting
Virtual Disks.(最好碎片整理一下)
Shrinking a virtual disk reclaims unused space in the virtual disk. If there is empty space in the disk, this process reduces the amount of space the virtual disk occupies on the host drive.(这就是我要的目的啊啊啊啊)
Shrinking a virtual disk is a convenient way to convert a virtual disk to the format supported by Workstation. Virtual disks created in the new format can be recognized only by VMware Workstation 3.0 and higher.
This section describes the following topics:
Unsupported
and Disabled Partitions
Restrictions and Requirements
Shrinking requires free disk space on the host equal to the size of the virtual disk you are shrinking.(收缩空间时,必须硬盘空间有和虚拟机磁盘一样大的空间,当时我就泪奔了。。。尼玛。。我收缩就是因为硬盘没空间了啊。。不过我在绝望的时候又做了一次defragment,然后compatc了一下,这次没报希望的动作居然成功的把物理硬盘空间腾出来30G,这样一来,我的磁盘空余空间就大于虚拟磁盘空间了)
Shrinking applies only to virtual disks. You cannot shrink physical disks or CD-ROMs.
The shrink feature is not enabled if the virtual machine
Contains a snapshot(有快照存在的时候不能shrink)
Is a parent of a linked clone
Is a linked clone
The shrink feature is not enabled for a virtual machine if any of its virtual disks are
Preallocated when created(建虚拟机时分配 好了的磁盘不能收缩)
Not used in independent-persistent mode(意思是 只有independent-persistent模式的才可以收缩)
Legacy disks that are not in persistent mode
Booted as independent disks
Note: You can
change the mode of a virtual disk before the virtual machine is powered on. See Excluding
Disks from Snapshots for a discussion of independent disks.
The Shrinking Process
Shrinking a disk is a two-step process:
In the first step, called wiping, VMware Tools reclaims all unused portions of disk partitions (such as deleted files) and prepares them for shrinking. Wiping takes place in the guest operating system.(第一步,被称为清理,VMware
Tools要求收回所有磁盘分区中未使用的部分(诸如删除的文件)并为收缩作好准备。清理工作在客户机操作系统中进行。 )
The second step is the shrinking process itself, which takes place on the host. Workstation reduces the size of the disk's files by the amount of disk space reclaimed in the wipe process.(第二步,收缩虚拟磁盘,它在宿主机中进行。Workstation通过清理程序回收磁盘空间来减少虚拟磁盘文件的大小。
)
When a virtual machine is powered on, you shrink its virtual disks from the VMware Tools control panel. You cannot shrink virtual disks if a snapshot exists. To remove the snapshot if one exists, choose VM
> Snapshot > Snapshot Manager > Delete. See Unsupported
and Disabled Partitions.
In a Linux or FreeBSD guest operating system, to prepare virtual disks for shrinking, you should run VMware Tools as the root user. This way, you ensure the whole virtual disk is shrunk. Otherwise, if you shrink disks as a nonroot user you cannot wipe the parts
of the virtual disk that require root-level permissions.
To shrink a virtual disk:
1. Launch the control panel.
Windows guest — double-click the VMware Tools icon in the system tray,
or choose Start > Settings > Control Panel, then double-click VMware
Tools.
Linux or FreeBSD
guest — become root (su -), then run vmware-toolbox
.#/usr/bin/vmware-toolbox
2. Click the Shrink tab.
3. Select the virtual disks you want to shrink, then click Prepare to Shrink.
A dialog box tracks the progress of the wiping process.
Note: If you deselect some partitions, the whole disk is still shrunk.
However, those partitions are not wiped for shrinking, and the shrink process does not reduce the size of the virtual disk as much as it could with all partitions selected.
4. Click Yes when VMware Tools finishes wiping the selected disk partitions.
A dialog box tracks the progress of the shrinking process. Shrinking disks may take considerable time.
5. Click OK to finish.
Unsupported and Disabled Partitions
In some configurations, it is not possible to shrink virtual disks. If your virtual machine uses such a configuration, the Shrink tab displays information explaining why you cannot shrink your virtual disks.
For example, you cannot shrink a virtual disk if
You preallocated disk space when you created the disk. Preallocating disk space is the default option for both typical and custom virtual machine creation paths.
The virtual machine has any snapshots. To delete a snapshot, choose VM >
Snapshot >Snapshot Manager > Delete.
The virtual machine contains physical disks.
The virtual disk is not an independent disk in persistent mode.
The virtual disk is stored on a CD-ROM
执行完shrink之后,我的host主机的硬盘可用空间从190G增加到了240G,可见,容量收缩了50多个个G。。。
3、vmware-vdiskmanager
虚拟机在使用过程中,虚拟磁盘的大小会不断变大。即使你删除了磁盘中的文件,虚拟磁盘的大小仍然不会缩小。VMWare在VMWare Tools中推出了Shrink这个功能。在安装VMWare Tools后,在没有Snapshot的情况下,在Guest操作系统为Windows的情况下,能有效缩小虚拟磁盘大小。但如果在Guest操作系统为Linux时,此方法效果就不好了,而且有些挂载点无法Shrink。
VMWare还推出了vmware-vdiskmanager工具,也能Shrink虚拟磁盘。在Guest操作系统为Linux时,单独用此工具没有什么效果。需要先在Guest系统中把未使用的 空间清零,在使用vmware-vdiskmanager,效果比较好。可以通过以下步骤有些缩小虚拟磁盘。
1、cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill
在Shell中运行以上命令,能对未使用空间清零,需要较大的空闲空间(跳过这步也可以,只是压缩效果没有这样好)
2、关闭Guest操作系统,进入VMWare安装目录运行:
vmware-vdiskmanager.exe -k "f:/vmware/Fedora11/Fedora11.vmdk"
就可以有效缩小虚拟磁盘的大小,基本达到你用了多少占用多少的效果。
用此方法分别对Guest系统为Fedora11和OpenSolaris10的VMDK文件进行Shrink,效果明显。