VirtualBox使用物理硬盘作为虚拟机硬盘
# run shell as root
# [for linux]
# create vmdk
VBoxManage internalcommands createrawvmdk -filename ~/mydisk.vmdk -rawdisk /dev/sda
# list partitions info
VBoxManage internalcommands listpartitions -rawdisk /dev/sda
# [for windows]
# create vmdk
VBoxManage internalcommands createrawvmdk -filename mydisk.vmdk -rawdisk \\.\PhysicalDrive0
# list partitions info
VBoxManage internalcommands listpartitions -rawdisk \\.\PhysicalDrive0
VirtualBox 使用物理硬盘作为虚拟机硬盘
有的时候需要将一整个物理硬盘都当作虚拟的硬盘,其实这样做的用途很广的。比如你安装了双系统,Windows在第一分区上,Linux在第4分区,那么你可以在启动Windows之后创建一个虚拟机,并把整块硬盘作为虚拟机的硬盘。虚拟机启动之后进入到你的Linux系统,这样就可以同时运行你同一块硬盘的两个分区上的系统了。或者你也可以进入Linux之后创建虚拟机来启动你的Windows,这样一来你的两个系统就互通。
当然,有点不足之处就是,你在Linux下不能挂载你的Windows系统分区,因为如果你挂载的话那么虚拟机就没法用这个分区了,也就没法启动那个分区的系统了。
创建VMDK文件
VMDK文件其实是一硬盘描述文件(如果你有兴趣可以用记事本打开),VBox的虚拟机可以直接使用该文件作为它的硬盘。
VBox的帮助文档这么描述VMDK的:
While this variant is the simplest to set up, you must be aware that this will give a guest operating system direct and full access to an entire physical disk. If your host operating system is also booted from this disk, please take special care to not access the partition from the guest at all. On the positive side, the physical disk can be repartitioned in arbitrary ways without having to recreate the image file that gives access to the raw disk.
在Linux下的话你可以使用下面的命令来创建VMDK:
VBoxManage internalcommands createrawvmdk -filename ~/mydisk.vmdk -rawdisk /dev/sda
这样就创建了一个和/dev/sda硬盘对应的虚拟硬盘文件mydisk.vmdk。
如果是在Windows下的话需要用下面的命令,记得先把VBox的安装路径添加到PATH环境变量里面,或者切换到VBox的安装目录下:
VBoxManage internalcommands createrawvmdk -filename mydisk.vmdk -rawdisk \\.\PhysicalDrive0
其中\\.\PhysicalDrive0是第一个物理硬盘,如果你不确定是不是这个硬盘的话你可以用下面的命令检查一下分区情况:
windows
1、查看当前Windows物理硬盘信息,在Windows cmd中运行:wmic diskdrive list brief
C:\WINDOWS\system32>wmic diskdrive list brief
Caption DeviceID Model Partitions Size
WDS100T3X0C-00SJG0 \\.\PHYSICALDRIVE3 WDS100T3X0C-00SJG0 1 1000202273280
INTEL SSDSC2KW480H6 \\.\PHYSICALDRIVE1 INTEL SSDSC2KW480H6 1 480101368320
KINGSTON SHFS37A240G \\.\PHYSICALDRIVE2 KINGSTON SHFS37A240G 2 240054796800
ST2000DL003-9VT166 \\.\PHYSICALDRIVE0 ST2000DL003-9VT166 1 2000396321280
2、创建物理硬盘映射出的虚拟硬盘: VBoxManage internalcommands createrawvmdk -filename "</path/to/your/virtual/disk/file>" -rawdisk "<DeviceID>"
C:\work\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands createrawvmdk -filename "test.vmdk" -rawdisk "\\.\PhysicalDrive3"
The 'createrawvdk' subcommand is deprecated. The equivalent functionality is
available using the 'VBoxManage createmedium' command and should be used
instead. See 'VBoxManage help createmedium' for details.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: 7ce0c0d0-7371-47cb-b19a-c2de56dacf31
3、查看指定物理硬盘分区信息:
# 磁盘正在使用时无法查看
C:\work\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands listpartitions -rawdisk "\\.\PhysicalDrive3"
VBoxManage.exe: error: Cannot open the raw disk: VERR_SHARING_VIOLATION
# 查看未使用磁盘分区信息
C:\work\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands listpartitions -rawdisk "\\.\PhysicalDrive2"
Number Type StartCHS EndCHS Size (MiB) Start (Sect)
1 0x07 0 /32 /33 1023/254/63 228353 2048
2 0x27 1023/254/63 1023/254/63 580 467671040
通过修改最后的编号就可以看到不同硬盘的分区情况了。创建了VMDK之后就可以新建一个虚拟机把这个vmdk文件指定为它的硬盘了。
ubuntu
创建物理磁盘映射文件/home/dd/sdb2.vmdk
chmod 666 /dev/sdb2
vboxmanage internalcommands createrawvmdk -filename /home/dd/sdb2.vmdk -rawdisk /dev/sdb2 -relative
Install Ubuntu
检查磁盘分区情况
bob@bob-virtual-machine:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.5G 0 1.5G 0% /dev
tmpfs 295M 8.7M 287M 3% /run
/dev/sda1 110G 3.4G 101G 4% / <- Check the conpacity of root fs
tmpfs 1.5G 164K 1.5G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
tmpfs 295M 84K 295M 1% /run/user/1000
添加主机的共享文件夹

bob@bob-virtual-machine:~/dev$ ll /mnt/hgfs/V/
total 31
dr-xr-xr-x 1 root root 0 9月 28 12:00 ./
dr-xr-xr-x 1 root root 4192 9月 28 14:48 ../
---------- 1 root root 8907 9月 27 22:09 .bash_history
-r--r--r-- 1 root root 220 4月 9 2014 .bash_logout
-r--r--r-- 1 root root 3637 4月 9 2014 .bashrc
d--------- 1 root root 0 9月 27 10:00 .cache/
-r--r--r-- 1 root root 617 9月 27 10:46 .gitconfig
d--------- 1 root root 0 9月 28 10:54 .jack-server/
---------- 1 root root 132 9月 28 10:54 .jack-settings
-r--r--r-- 1 root root 675 4月 9 2014 .profile
dr-xr-xr-x 1 root root 0 9月 27 11:49 .repoconfig/
-r--r--r-- 1 root root 892 9月 27 11:56 .repo_.gitconfig.json
-r--r--r-- 1 root root 851 9月 27 11:49 .repopickle_.gitconfig
---------- 1 root root 1024 9月 28 14:16 .rnd
d--------- 1 root root 0 9月 27 11:40 .ssh/
---------- 1 root root 6989 9月 28 12:00 .viminfo
dr-xr-xr-x 1 root root 0 9月 28 14:27 work/
Ubuntu 安装 OS X 10.11 虚拟机
Errors
[Windows]virtualbox剪切板失效:
- virtualbox xp guest 粘帖文字到virtualbox剪切板失效
- virtualbox xp guest 粘帖文字到ubuntu 10.04 host 有时会失效
很早前就发现virtualbox xp guest 粘帖文字到ubuntu 10.04 host有时候会贴不过去,但ubuntu 10.04 host复制的东西能贴进xp guest里面,但在网上一直没找到解决的办法,后来发现只要重启virtualbox里的 xp就能恢复,所以就怀疑应该是xp里的virtualbox相关服务进程有问题,重启这个进程也许就能解决问题,于是就发现有VBoxTray.exe这个进程,把其kill掉后再重启果然就能解决问题了。每次这样手动处理太麻烦,网上找了个xp重启一个进程的方法,是个vbs脚本,如下:
strPName = "VBoxTray.exe"
strComputer = "."
Set objShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strPName & "'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
WScript.Sleep 4000
objshell.Run "VBoxTray.exe"
objshell.Run "VBoxTray.exe"
保存为xxx.vbs后双击就能运行。
(注:脚本里objshell.Run “VBoxTray.exe” 这个写了两遍是发现运行一次VBoxTray.exe启动不起来,运行两次也只有一个进程)
ubuntu下vbox挂载共享文件夹
挂载共享文件夹
进入虚拟Ubuntu,在命令行终端下输入:
sudo mkdir /mnt/shared
sudo mount -t vboxsf ${share_folder_name} /mnt/hgfs
其中${share_folder_name}是之前创建的共享文件夹的名字。OK,现在Ubuntu和主机可以互传文件了。
假如您不想每一次都手动挂载,可以在/etc/fstab中添加一项
gongxiang /mnt/shared vboxsf rw,gid=100,uid=1000,auto 0 0
卸载的话使用下面的命令:
sudo umount -f /mnt/shared
FAQ
镜像下载资源网站
| 标题 | 说明 | 网址 |
|---|---|---|
| 官方网站 | VirtualBox 官方下载地址 | Downloads – Oracle VM VirtualBox |
| OSBoxes | OSBoxes offers you ready-to-use Linux/Unix guest operating systems. If you don’t want to install secondary OS alongside with your main OS but still want to use/try it, then you can use VirtualBox or VMware on your host operating system to run virtual machine. | VirtualBox Images (osboxes.org) |
重置任务栏图标脚本
保存文件:issue-reset_box_tray-01.vbs
strPName = "VBoxTray.exe"
strComputer = "."
Set objShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strPName & "'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
WScript.Sleep 4000
objshell.Run "VBoxTray.exe"
objshell.Run "VBoxTray.exe"

3985

被折叠的 条评论
为什么被折叠?



