Mac OSX下VirtualBox直接使用物理硬盘作虚拟机磁盘

目前VirtualBox只能用命令行来建立磁盘才可以使用物理硬盘。

如果是USB磁盘的话,那么需要从"关于本机"->"概览"->"系统报告"->"USB"中找到磁盘的名字,比如"disk2".

我们假定VirtualBox安装在"
/Applications/VirtualBox.app/"这个目录下面,要在"~/VirtualBox\ VMs/Ubuntu/"目录下面生成文件,则执行如下命令:

$ diskutil umountDisk disk2
$ sudo chown `whoami` /dev/disk2
$ mkdir ~/VirtualBox\ VMs
$ mkdir ~/VirtualBox\ VMs/Ubuntu
$ sudo /Applications/VirtualBox.app/Contents/MacOS/VBoxManage internalcommands createrawvmdk -filename ~/VirtualBox\ VMs/Ubuntu/Ubuntu.vmdk -rawdisk /dev/disk2
$ sudo chown `whoami` ~/VirtualBox\ VMs/Ubuntu/Ubuntu.vmdk

/dev/disk2表示机器上的第二块硬盘,每次插入新磁盘后,就会出现类似/dev/disk*的一个路径名。

最后,新建一个虚拟机,然后指定使用刚刚创建的磁盘即可。

参考链接


How do I install Mavericks onto external HD but from inside VirtualBox

VirtualBox 5.0.X安装后Windows 10 下载文件频繁失败

操作系统


Windows 10 64位 专业版

VirtualBox版本


5.0.0 ~ 5.0.14(当前最新版)

故障现象


安装VirtualBox后,下载文件频繁失败,禁用网卡属性中的 VirtualBox NDIS6 Bridged Networking Driver 之后一切正常。

启动虚拟机,发现桥接模式下无适配器可选(因为被我们禁用了)。

网上搜索,得到的信息是5.0版对Windows 10的网络支持非常不好,工程师正全力解决Windows 10的兼容性问题,Windows 7,Windows 8也有人反应有网络相关的问题,问题回复中有人提供了继续使用NDIS5驱动的方法,试用后问题解决。

方法:下载VirtualBox 5.0.14版,卸载已安装的5.0版本,打开命令行,带参数执行安装程序:

$VirtualBox-5.0.14-105127-Win.exe -msiparams NETWORKTYPE=NDIS5

随后会打开正常的图形界面安装,装完后网卡属性,桥接驱动名中已经无NDIS6字样。

期待官方解决NDIS6的问题,早日用上更好的驱动。

参考


https://www.virtualbox.org/ticket/14457

https://forums.virtualbox.org/viewtopic.php?f=6&t=68980

Windows下VirtualBox直接使用物理硬盘作虚拟机磁盘

目前VirtualBox只能用命令行来建立磁盘才可以使用物理硬盘。

我们假定VirtualBox安装在“D:\Program Files\Oracle\VirtualBox”这个目录下面,要在“D:\Users\VirtualBox VMs\Ubuntu\”目录下面生成文件,则执行如下命令:

"D:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands createrawvmdk -filename "D:\Users\VirtualBox VMs\Ubuntu\Ubuntu.vmdk" -rawdisk \\.\PhysicalDrive1

\\.\PhysicalDrive1 表示机器上的第二块硬盘,\\.\PhysicalDrive0是第一块,\\.\PhysicalDrive2是第三块,以此类推。

在Windows 的磁盘管理中能看到硬盘的分区路径。如下图所示:ComputerManger

然后新建虚拟机,选择"使用已有的虚拟硬盘文件",找到刚刚创建的 Ubuntu.vmdk 即可。

对于Windows 8.1/10系统,以上的命令需要以管理员启动命令行的方式下启动并执行,否则会报告找不到磁盘。另外启动Virtual Box的时候,同样需要以管理员的身份运行,否则会出现VERR_ACCESS_DENIED错误,如下图所示:

VERR_ACCESS_DENIED

Ubuntu 13.10 运行VirtualBox虚拟机编译出现错误提示运行"/etc/init.d/vboxdrv setup"的问题

运行虚拟机提示:

The virtual machine 'XXX' has terminated unexpectedly during startup with exit code 1.

之后提示:

    Kernel driver not installed (rc=-1908)

    The VirtualBox Linux kernel driver (vboxdrv) is either not loaded or there is a permission problem with /dev/vboxdrv. Please reinstall the kernel module by executing

    '/etc/init.d/vboxdrv setup'

    as root. If it is available in your distribution, you should install the DKMS package first. This package keeps track of Linux kernel changes and recompiles the vboxdrv kernel module if necessary.

在终端下运行:

sudo /etc/init.d/vboxdrv setup

提示信息

Stopping VirtualBox kernel modules [ OK ]
* Recompiling VirtualBox kernel modules
* Look at /var/log/vbox-install.log to find out what went wrong

提示有错误,查看了一下错误日志

cat /var/log/vbox-install.log

内容如下

Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again。 停止。

根据提示应该是没有找到对应的源文件,查看了一下系统版本号:

uname -r

在进入/usr/src里发现果然没有对应的版本号头文件。

解决方法:

sudo apt-get install dkms build-essential linux-headers-generic
sudo /etc/init.d/vboxdrv setup

VirtualBox压缩VDI格式硬盘/调整磁盘大小

首先,看 vbox的官方文档:

http://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvdi

VBoxManage modifyhd         <uuid>|<filename>
                            [--type normal|writethrough|immutable|shareable|
                                    readonly|multiattach]
                            [--autoreset on|off]
                            [--compact]
                            [--resize <megabytes>|--resizebyte <bytes>]
  • With the --compact option, can be used to compact disk images, i.e. remove blocks that only contains zeroes. This will shrink a dynamically allocated 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. Executesdelete -z in the guest to zero the free disk space before compressing the virtual disk image. For Linux, use the zerofree utility which supports ext2/ext3 filesystems.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 allocated format. You can use this workaround until compacting is also supported for disk formats other than VDI.

关键之处正在于 sdelete 应该使用 -c -z  两个选项 ,而网上所以的方法都说是使用 -c 选项。

 Using SDelete

SDelete is a command line utility that takes a number of options. In any given use, it allows you to delete one or more files and/or directories, or to cleanse the free space on a logical disk. SDelete accepts wild card characters as part of the directory or file specifier.

usage: sdelete [-p passes] [-s] [-q] <file or directory> ...
       sdelete [-p passes] [-z|-c] [drive letter] ...
   -a         Remove Read-Only attribute
   -c         Clean free space
   -p passes  Specifies number of overwrite passes (default is 1)
   -q         Don't print errors (Quiet)
   -s or -r   Recurse subdirectories
   -z         Zero free space (good for virtual disk optimization)

所以,总结一下,正确的方法应该是这样:

  1. 在guest os 中清理系统, windows的话可以再硬盘碎片整理一下
  2. 在 guest os 中 Windows 执行 `sdelete -z -c` ; Linux/Debian/Ubuntu 启动到Recovery Mode执行 `zerofree /dev/sdaX`
  3. VBoxManage modifyhd <uuid>|<filename>  --compact

如果磁盘空间不足,使用如下命令调整(增大/加大)磁盘空间

# 调整到30G,由于默认单位是M,因此数字是 30960

$ VBoxManage modifymedium Debian.vdi --resize 30960 --compact

# 如果存在快照的话,需要删除全部快照,否则系统中看不到磁盘空间变化

# 系统中调整磁盘空间大小
$ sudo apt-get install gparted

$ sudo gparted

参考链接