设为首页 收藏本站
查看: 859|回复: 0

[经验分享] VirtualBox Zero Free Unused HD Space

[复制链接]

尚未签到

发表于 2015-4-14 10:39:35 | 显示全部楼层 |阅读模式
How to shrink a dynamically-expanding guest virtualbox image
  http://dantwining.co.uk/2011/07/18/how-to-shrink-a-dynamically-expanding-guest-virtualbox-image/
  

Sometimes bigger isn’t always better. If your dynamically-expanding virtual machine images are growing out of control, then here’s how to trim them back…
  
Background
  I’m a big fan of VirtualBox, and use separate virtual machines (VMs) for the various separate bits and pieces I’ve got on the go (as I invariably end up messing something up, and can just trash the image and start again, without taking down whatever else it is I’m playing with at the time).
  All my VMs use a dynamically expanding image for their hard drive, where you set the maximum size of the disk, but the system will only grow to fill that space if required. By setting this nice and high, I can be sure that the hard drive space is there if I need it, without taking space away unnecessarily from the rest of the system.
  Unfortunately, whilst VirtualBox will dynamically expand the hard drive as it’s required, it won’t dynamically shrink it again if you free up space in the VM. This can be a problem, especially if, like me, the sum total of all those theoretical maximums exceeds the actual maximum capacity of the hard drive hosting all these VMs.
  The good news is that you can shrink those images back down again. The bad news is that a lot of the guides on the internet are out-of-date, and woefully misleading. Here’s what I did to get the job done…
1. Free up space in the client machine
  It’s a bit of an obvious first step, but you can only shrink down the client VM by the size of the available free space therein, so delete the files and uninstall the programs that you no longer need but are hogging your resources.
2. Zero out the free space
  VirtualBox only knows that the space is really free if it’s been set to zero, and standard deletion won’t do this.
If it’s an Ubuntu VM
  You’ll want to use zerofree:

  • install with sudo aptitude install zerofree
  • (if you don’t have aptitude, you can either use apt-get to install zerofree  (sudo apt-get install zerofree) or use apt-get to install aptitude (sudo apt-get install aptitude). I’d recommend getting hold of aptitude, as it does a great job of managing packages in Ubuntu)
  • Reboot the machine (sudo shutdown -r now). During boot, hold down the left shift key. A menu will appear, you need to select “recovery mode”; this should be the second item in the list.
  • You’ll get another menu, towards the bottom there should be the option to “Drop to root shell prompt”
  • Run df and look for the mount point that’s that the biggest – this is where all your files are, and is the one we’ll need to run zerofree against. For the rest of this guide, we’ll assume it’s /dev/sda1
  • The following three commands (thanks, VirtualBox forum!) stop background services that we can’t have running:

    • service rsyslog stop


    • service network-manager stop
    • killall dhclient

  • Once they’ve stopped, you can re-mount the partition as readonly (zerofree needs this)

    • mount -n -o remount,ro -t ext3 /dev/sda1 /

  • You can now run zerofree

    • zerofree -v /dev/sda1

  • Finally, shut down the VM

    • shutdown -h now

If it’s a Windows VM
  You’ll need to run sdelete; I’ve never done this, but there are instructions on that here:

  • http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06
3. Shrink the VM
  Quite a lot of the online guides say that you’ll have to clone the hard drive image to shrink it, as VirtualBox 2.2 and above dropped support for compacting the image. This isn’t true, certainly not for version 4.0.4, and you can shrink the image in-place with the following command:

  • VBoxManage modifyhd my.vdi –compact

where you replace my.vdi with the name of the vdi you’d like to shrink (for more information on this command, see the VirtualBox manual).That’s it!
  With any luck, you’ll now have plenty of disk space to fill will equally useless tat…

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-57025-1-1.html 上篇帖子: VirtualBox Android x86 网络配置 下篇帖子: virtualbox安装centos 6.4 server 网络连接问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表