查看: 19124|回复: 31
收起左侧

[分享] 关于virtualbox Differencing(差分磁盘)的一些探索

[复制链接]
种过一豆
发表于 2011-5-17 10:47:33 | 显示全部楼层 |阅读模式
本帖最后由 种过一豆 于 2011-5-17 15:59 编辑

---------------------------->>>>>    以下的探索参考材料来源于手册    <<<<<---------------------------

       说到虚拟机的差分磁盘,大家一定首先想到的是windows 7下面的vhd,还有可能还会想到vmware的母盘克隆以及快照链接克隆功能


       virtualbox呢?-------似乎被大家遗忘了,貌似也没有人对vbox基于母盘的差分技术感兴趣,翻遍了kafan论坛虚拟机区的帖子,也只找到一篇关于问differencing按钮做什么用的帖子


       做这个帖子还有很大程度上是因为前几天有个momo0的人发了帖子问virtualbox的差分盘打开有报错的bug(http://bbs.kafan.cn/thread-982073-1-1.html),那个时候也没有跟那个人详细交流一番,趁这个机会干脆把自己详细接触vbox一年多来了解到的差分东西总结一下

       其实以前版本的virtualbox界面上是有differencing图标按钮的,正如我上面提到的那帖子说的一样确实是有,也不知道是为什么,在现在普遍使用的4.0.x版本上vbox直接就把differencing按钮给灭了,没有按钮就没有按钮吧,如果想实现virtualbox基于母盘的差分磁盘也不是非常麻烦的事情

        差分磁盘,有不少的好处,大多数人不想在原有虚拟机系统镜像上完整克隆出一份再导入到虚拟机中运行,这样耗时还凭空占用了相对宝贵的磁盘空间(虽然现在磁盘空间都很吓人了,但还是能省点就省点),克隆出另外一份能运行的系统通过差分盘技术就能很好地解决

---------===============  virtualbox 实现虚拟磁盘系统克隆的方法  ==============---------

vbox也没有简单的按钮供大家直接点击就实现克隆出一份镜像来,在这里也就不妨多罗嗦几句怎样实现系统的克隆

---- 第一种也许是最麻烦也是最笨的方法,就是在初次完成系统的安装后,通过菜单  file-----export 导出已经安装好的系统,再通过菜单 file-----inport  导入刚才导出的ova文件(也有可能是ovf格式之类的,这主要看自己导出的时候选择的是什么格式)

---- 第二种就是通过vboxmanage 这个内置的CUI命令来实现,具体其实在之前的帖子里面我已经说过,不过因为服务器磁盘故障问题直观图片挂了,在这里贴上步骤吧:

1)开始运行敲入cmd命令  打开命令提示符

2)连续敲入cd..命令退回到C盘根目录下

3)敲入cd命令进入vbox程序的安装目录  C:\>cd "Program Files\Oracle\VirtualBox"
如果没有修改安装路径的话vboxmanage.exe程序应该是在Program Files\Oracle\VirtualBox目录下

进入vboxmanage.exe 所在的目录后就可以着手虚拟机克隆了

4)敲入命令vboxmanage clonehd 原始镜像文件路径+名称 新的镜像文件路径+名称

----vboxmanage.exe命令多个后缀也应该可以

例如:我的虚拟机镜像文件路径是系统默认保存的,在C:\Documents and Settings\Administrator\VirtualBox VMs下
这时需要克隆一份windows 2003的系统,同时克隆的系统依然放在windows 2003的目录下,只需要进行如下操作

vboxmanage clonehd “C:\Documents and Settings\Administrator\VirtualBox VMs\windows 2003\windows 2003-disk1.vmdk” “C:\Documents and Settings\Administrator\VirtualBox VMs\windows 2003\windows 2003-disk2.vmdk”

这样之后一份一样的系统就被克隆出来了.

这时打开vbox主程序还是看不到这个被克隆的虚拟机的,需要新建一个虚拟机,名称与之前的windows 2003 要不一样,比如取名为2003,到创建硬盘镜像的时候选择已经存在的hard disk,找到新克隆的那个镜像下一步就可以了

----第三种方法,也就是我接下去通过英文手册相关资料跟自己实践摸出来的,可能会有些不对,希望高手指正

评分

参与人数 3经验 +50 人气 +2 收起 理由
kangzhen + 1 根据版规,加1分以示鼓励
詩、未詺 + 50 感谢支持,欢迎常来: )
107 + 1 感谢提供分享

查看全部评分

种过一豆
 楼主| 发表于 2011-5-17 10:47:41 | 显示全部楼层
本帖最后由 种过一豆 于 2011-5-17 15:56 编辑

具体的差分磁盘准确定义我就不想说了,我也说不出来,更懒得去刨根揪底追着名词代号跑

不过大概要知道是怎么回事:就是在以一个母本为基础的前提上通过各种方法实现多个独立运行,各独立系统内的操作不影响其它系统,跟vhd vmware的母盘一样,virtualbox中那个最基础的母盘也应该要保持独立,如果母盘都出现被改动了那将是一个毁灭性的后果,当然这个直接修改成只读好像是不行的,我做了试验,最后还是通过手册里面的磁盘格式转换实现的

说下我的初步想法(当然这个初步的想法也被实践证实是不可行的)

直接新建一个虚拟机 a,在选择磁盘的时候直接指定原来已经存在的xp虚拟磁盘系统,期间没有做任何额外的设置


服务器磁盘故障的缘故,现在对上传图片有种恐惧症(怕等下又什么都看不见了),就不传太多的图了,有兴趣的就当“天书”蛮看吧

启动后先运行原来的xp,后运行a,发现两个都可以运行,不过也碰到了先启动后面新建的虚拟机a 然后启动xp无法运行的现象(直接提示报错了),而且如果先启动xp的虚拟机,在里面随便创建一个文档的话,打开基于母盘的虚拟机a会发现a里面也有这个文档


----------------相互之间有影响了------------------------

>>>>--------------很明显,这不是我想要的结果-----------


细心的你可能发现了我上面那张图的虚拟磁盘系统为 normal 格式

没错,翻了下手册第八章
VBoxManage modifyhdWith the modifyhd command, you can    change the characteristics of a disk image after it has been    created:
VBoxManage modifyhd         <uuid>|<filename>                            [--type normal|writethrough|immutable|shareable|                                    readonly|multiattach]                            [--autoreset on|off]                            [--compact]                            [--resize <megabytes>|--resizebyte <bytes>]NoteDespite the "hd" in the subcommand name, the command works with        all disk images, not only hard disks. For compatibility with earlier        versions of VirtualBox, the "modifyvdi" command is also supported and        mapped internally to the "modifyhd" command.

The following options are available:
  • With the --type argument, you          can change the type of an existing image between the normal,          immutable, write-through and other modes; see the section called “Special image write modes” for details.
  • For immutable (differencing) hard disks only, the          --autoreset on|off option          determines whether the disk is automatically reset on every VM          startup (again, see the section called “Special image write modes”). The default          is "on".
  • With the --compact option,          can be used to compact disk images, i.e. remove blocks that only          contains zeroes. This will shrink a dynamically expanding image          again; it will reduce the physical size of the          image without affecting the logical size of the virtual disk.          Compaction works both for base images and for diff images created as          part of a snapshot.
    For this operation to be effective, it is required that free          space in the guest system first be zeroed out using a suitable          software tool. For Windows guests, you can use the          sdelete tool provided by Microsoft.          Execute sdelete -c in the guest to          zero the free disk space before compressing the virtual disk          image.
    Please note that compacting is currently only available for          VDI images. A similar effect can be achieved by zeroing out free          blocks and then cloning the disk to any other dynamically expanding          format. You can use this workaround until compacting is also          supported for disk formats other than VDI.
  • The --resize option allows you to expand the capacity of an          existing image; this increases the logical size          of a virtual disk without affecting the physical size much.[32] This currently works only for the VDI and VHD formats,          and only for the dynamically expanding variants. For example, if you          originally created a 10G disk which is now full, you can use the          --resize command to add more space to the virtual disk without          having to create a new image and copy all data from within a virtual          machine.

然后跳到第五章


  • Next, immutable images
    only        remember write accesses temporarily while the virtual machine is        running; all changes are lost when the virtual machine is powered on        the next time. As a result, as opposed to "normal" images, the same        immutable image can be used with several virtual machines without        restrictions.
    Creating an immutable image makes little        sense since it would be initially empty and lose its contents with        every machine restart (unless you really want to have a disk that is        always unformatted when the machine starts up). As a result, normally,        you would first create a "normal" image and then, when you deem its        contents useful, later mark it immutable.
    If you take a snapshot of a machine with immutable images, then        on every machine power-up, those images are reset to the state of the        last (current) snapshot (instead of the state of the original        immutable image).
    NoteAs a special exception, immutable images are          not reset if they are attached to a machine          whose last snapshot was taken while the machine was running (a          so-called "online" snapshot). As a result, if the machine's current          snapshot is such an "online" snapshot, its immutable images behave          exactly like the "normal" images described previously. To re-enable          the automatic resetting of such images, delete the current snapshot          of the machine.

    Again, technically, VirtualBox never writes to an immutable        image directly at all. All write operations from the machine will be        directed to a differencing image; the next time the VM is powered on,        the differencing image is reset so that every time the VM starts, its        immutable images have exactly the same content.[25] The differencing image is only reset when the machine is        powered on from within VirtualBox, not when you reboot by requesting a        reboot from within the machine. This is also why immutable images        behave as described above when snapshots are also present, which use        differencing images as well.
    If the automatic discarding of the differencing image on VM        startup does not fit your needs, you can turn it off using the        autoreset parameter of        VBoxManage modifyhd; see the section called “VBoxManage modifyhd” for details.


在这里找到了解决这个问题的思路





评分

参与人数 2人气 +2 收起 理由
FreeEquFraT + 1 膜拜一下大作
107 + 1 。。。

查看全部评分

种过一豆
 楼主| 发表于 2011-5-17 10:47:58 | 显示全部楼层
本帖最后由 种过一豆 于 2011-5-17 16:21 编辑

问题就在于在刚才直接新建a虚拟机之后基于的那个母盘格式是 normal 的,如果我们把这个基础的母盘搞成 immutable (这个格式在启动基于母盘的系统之后关闭虚拟机,之前所作的一切更改都会直接被母盘pass掉)就应该不会出现上述直接选存在虚拟磁盘系统的问题


--------很好,这个格式正是我们理想中那个情况(似乎vhd之类的就是基于这样类似格式的母盘的吧,我很少用vpc、vmware之类的,所以了解不是很多,如出言有不对之处请指正)


接下来就是通过一些方法把那个vmdk后缀(也就是想用来作母盘的那个文件)转换成immutable格式的

通过面板也是没办法的,因为没这些个按钮



vboxmanage 命令

就是这个内置而且强大的 cui 命令可以帮助我们完成预定的目标



ps:底下因为我没有在windows下操作,所以不能按照windows的目录路径来给有兴趣的人谈我的探索
如果你是在windows下面的话请参考我上面如何克隆镜像的步骤,需要先进入vboxmanager.exe程序的目录然后再操作


进入虚拟机系统保存的路径

我自己机器上的话找到window xp sp2-disk1.vmdk 这个文件

敲入命令 vboxmanage modifyhd window xp sp2-disk1.vmdk --type immutable

此时会报错



error的错误提示大概就是这个vmdk文件已经被attach到系统上了,这个提示在windows下面也应该可以显示,不依赖于是什么平台的系统

跟着提示做,慢慢就能解决问题

既然提示已经被attach到系统上了那么我们就应该把它从系统中释放出来

释放出来也很简单

找到xp虚拟机的设置,然后找到磁盘选项那边右键先释放掉它





这个时候再敲入 vboxmanage modifyhd window xp sp2-disk1.vmdk --type immutable

没报错了

用vboxmanage showhdinfo  window xp sp2-disk1.vmdk

你会看到在Type类型中已经由原来的normal 变成了 immutable 这样就可以实现每次关闭虚拟机后自动回滚到母盘的初始状态

种过一豆
 楼主| 发表于 2011-5-17 10:48:15 | 显示全部楼层
本帖最后由 种过一豆 于 2011-5-18 07:51 编辑

其实做到这里已经接近于尾声了,因为预想中的immutable磁盘格式已经被我们成功地转换出来

(  ------------------------                                           ---------------------------------------------------------
在linux下面习惯了把已经做好不需要修改的东西权限改一下,我额外做了一点点小小的工作

chmod 400 window xp sp2-disk1.vmdk

直接把已经转换成immutable格式的
window xp sp2-disk1.vmdk 文件设置成只读,不允许再被写Windows下应该可以直接通过右键选择只读状态来修改,具体修改后还能不能再次载入运行我就没在windows下面做试验了

----------------这里可以不要做,没什么影响,immutable已经是自动回滚开启了-------------------------

下面开始创建新的虚拟磁盘系统了,假设为b(这名字取得b  算了,都建好了就将就点吧)

----->>>>省略好多步骤,一直到你选择磁盘那的步骤时,选择刚才转换好格式的磁盘




这时可以看到我们载入的母盘确实是被转换成immutable的格式了,开心吧,不过别高兴得太早了

这个时候你启动b这个虚拟机,进入b虚拟机的文件夹你会发现在Snapshots里面会有一个很长串以uuid命名的vmdk文件
不要以为我在跟你说废话,这个uuid文件是有玄机的[:27:]


不信你直接启动b虚拟机,然后在里面创建一个文件,关掉b再重启b,你那个文件没有了


一开始也卡在这里面很久,觉得纳闷,我已经是在母盘上链接出一个快照文件来了阿,怎么继承了母盘的回滚功能呢???

好了,另外一个关键点就在于我说的那个b/Snapshots文件夹下那个{uuid}.vmdk文件身上
进去看看到底怎么回事

vboxmanage showhdinfo
{uuid}.vmdk
..................
..................
Auto-Reset:            on

看到最后一行了吧,问题就出在这里,

回到英文手册第八章
VBoxManage modifyhd         <uuid>|<filename>                            [--type normal|writethrough|immutable|shareable|                                    readonly|multiattach]                            [--autoreset on|off]                            [--compact]        


再次阅读了一下相关信息,发现autoreset 是用来设置虚拟磁盘自动回滚属性的

根据字面意思理解,这个快照文件夹里面的此属性应该搞成off

那么就用命令修改吧

vboxmanage modifyhd  
{uuid}.vmdk --autoreset off

ok,到现在为止就搞定了,重启一下b虚拟机搞点小破坏再重启,里面的残留还是会存在的




关于autoreset这里我还做了其它的测试,比如我一开始不把这个属性的值设置成off,直接在新建好的b虚拟机上面创建快照,诶,好像......也是可以实现文件的保存的,官方手册对这个地方的解释是还原到距离最近的那个快照状态(没耐心仔细看了,只看了点大概意思),有兴趣的不妨去仔细研读

创建c d e f 虚拟机的时候步骤跟b一样,这些虚拟机都是基于我们转换好的immutable格式母盘,所以即便开很多虚拟机体积也很小,这不就实现了virtualbox传说中的基于母盘差分么

乱七八糟的码了那么多字,自己都看得头晕了


最后搞张高清无码大图,证明没骗耐心读下来的你









总结一下步骤:

1)进入相对应虚拟机系统磁盘设置里面弹出虚拟磁盘系统的文件(vdi 或者vmdk格式的)

2)用vboxmanage 命令把弹出的虚拟磁盘从normal转换成手册里面提到的immutable
       vboxmanage modifyhd <xxxxx.vmdk> --type immutable

3) 新建虚拟机,基于immutable的母盘

4)进入新建虚拟机比如b文件夹内Snapshots,把里面的vmdk文件自动回滚关闭
      vboxmanage modifyhd <*******.vmdk> --autoreset off



还是在这里说一下吧,步骤总结上面的那些可以不去看阿,毕竟只是我的一个思路,每个人的思路都是不一样的,所以看起来有些人会觉得很迷糊,敲了一些不知所云的命令(不过107版主也说了嘛,其实vbox的命令才是最强大的),而我自己又慢慢开始习惯敲那些简单的命令了,对着步骤有不明白的地方可以反到上面相对应的步骤去参考一下就好

小提示:有兴趣做实验的请先备份好自己的虚拟系统,不要急匆匆进去就刷刷刷命令一通,出了问题可就比较麻烦了,其实最后就这总结的几个步骤,很容易的,只要不是对命令行特别反感,略微知道一些vboxmanage的命令,做这些应该不是特别费解的事情

啊~~~~~~~~好累阿,码字也很累的,给点安慰吧




评分

参与人数 2人气 +2 收起 理由
hj5abc + 1 慢慢看,RQ安慰
fh123 + 1 膜拜豆子

查看全部评分

fh123
发表于 2011-5-17 11:00:48 | 显示全部楼层
本帖最后由 fh123 于 2011-5-17 11:02 编辑

好。学习学习豆子的方法
107
发表于 2011-5-17 11:07:30 | 显示全部楼层
vboxmanager是最强大的,命令符是最强大的,vbox的界面没有实现vboxmanager这么多功能
kangzhen
发表于 2011-5-17 11:23:33 | 显示全部楼层
今天没人气了,明天补上~~~
zby_1991
发表于 2011-5-17 12:14:33 | 显示全部楼层
乍看一遍,不懂

慢慢来~~
种过一豆
 楼主| 发表于 2011-5-17 12:15:11 | 显示全部楼层
本帖最后由 种过一豆 于 2011-5-17 12:32 编辑
107 发表于 2011-5-17 11:07
vboxmanager是最强大的,命令符是最强大的,vbox的界面没有实现vboxmanager这么多功能

我想改成原创引砖头来砸

在这里补上一点,如果还想用原来我们释放掉vmdk文件的虚拟机,必须要反其道而行之

具体的说就是跟释放attach那步反过来,在设置  磁盘   那边加上转换好的那个immutable格式文件

此时在xp名称的虚拟机下面也会有个Snapshots文件夹,进去一样把autoreset属性设置成off就行了

总之就是让母盘自动autoreset

而基于母盘的那些派生vmdk文件不要autoreset
种过一豆
 楼主| 发表于 2011-5-17 12:21:09 | 显示全部楼层
fh123 发表于 2011-5-17 11:00
好。学习学习豆子的方法

提供一种思路罢了,实际操作起来很是麻烦,但没办法,vbox精髓还是在命令
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-3-29 08:23 , Processed in 0.128232 second(s), 19 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

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