2010/10/13

解決Virtual Box的常見錯誤(2010/10/13更新)

Virtual Box是一個很好用的系統,在使用時非常的有彈性。但在使用時偶爾會因為操作錯誤,而產生錯誤。

NAT#0: configiguration query for ""HostPort: in failed(VERR_CFGM_VALUE_NOT_FOUND)
Unknown error creating VM(VERR_CFGM_VALUE_NOT_FOUND)

這篇文章不但會說明如何解決錯誤,也順便教你Windows版VirtualBox 的簡單操作。

開啟終端機


要設定Virtual Box時,需要先開啟Windows的終端機。開啟的方式是:
  1. 點「開始」->「執行」。
  2. 輸入 cmd 後點選「確定」。
  3. 進入Virtual Box的個人設定目錄(輸入>後的字串)

Microsoft Windows XP [版本 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\jose.sun>cd C:\Program Files\Oracle\VirtualBox

C:\Program Files\Oracle\VirtualBox>

顯示image設定


遇到問題時,如果要知道目前image的設定(範例中的image名稱是XP)則輸入。
C:\Program Files\Oracle\VirtualBox>VBoxManage getextradata XP enumerate
Oracle VM VirtualBox Command Line Management Interface Version 3.2.6
(C) 2005-2010 Oracle Corporation
All rights reserved.

Key: GUI/LastCloseAction, Value: powerOff
Key: GUI/LastGuestSizeHint, Value: 640,480
Key: GUI/LastWindowPostion, Value: 0,0,0,0,max
Key: GUI/MiniToolBarAlignment, Value: bottom
Key: GUI/SaveMountedAtRuntime, Value: yes
Key: GUI/ShowMiniToolBar, Value: yes
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol, Value: TCP

移除Port


我們發現上述的設定中,有一個奇怪的TCP設定。

如果要移除此項設定,可以使用以下的設定:
C:\Program Files\Oracle\VirtualBox> VBoxManage setextradata "XP" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol"
就可以把http的Protocol Config回覆成空值。

這時你再顯示設定時,就會發現這個VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol已經不見了。
C:\Program Files\Oracle\VirtualBox>VBoxManage getextradata XP enumerate
Oracle VM VirtualBox Command Line Management Interface Version 3.2.6
(C) 2005-2010 Oracle Corporation
All rights reserved.

Key: GUI/LastCloseAction, Value: powerOff
Key: GUI/LastGuestSizeHint, Value: 640,480
Key: GUI/LastWindowPostion, Value: 0,0,0,0,max
Key: GUI/MiniToolBarAlignment, Value: bottom
Key: GUI/SaveMountedAtRuntime, Value: yes
Key: GUI/ShowMiniToolBar, Value: yes
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol, Value: TCP

新增Port


我們有時候會在VirtualBox內安裝某個Service,並開放Port讓本機以外的使用者也能使用,這時就要設定本機端的Port要轉到image上的Service。設定中:
  • pcnet 表示網卡,因為VirtualBox預設的虛擬網下是 PCnet Fast III,如果你的image內的網卡不是pcnet,就需要做相應的變動。
  • 每一組設定有三項,每組設定都必須設定相同的image名稱,config的名稱也必須一致,通常config都是以Service的名稱為主,以避免搞錯,例如:http、mysql。
  • HostPort對應的是主機的Port,簡單地說是你的電腦開啟給image使用的port,GuestPort則是指image內相應的埠。有人說這兩個Port的值一定要相同,但我不這麼認為,不過因為我自己沒試過不同的值,所以我不確定此一說法是否正確。
  • 每次設定後都必須重啟VirtualBox,新的值才會生效。
  • 本機連入image內的Service,通常在瀏覽器內輸入127.0.0.1或是127.0.0.1:port,就可以連到Service了。
  • image內的網路連線模式預設值是 NAT模式,所以會自動取得 10.0.2.x 這種的虛擬 IP。
以下是常用的範例和設定值。
VBoxManage setextradata image名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol" TCP
VBoxManage setextradata image名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/GuestPort" 10080
VBoxManage setextradata image名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/HostPort" 10080

VBoxManage setextradata 名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/mysql/Protocol" TCP
VBoxManage setextradata 名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/mysql/GuestPort" 13306
VBoxManage setextradata 名稱 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/mysql/HostPort" 13306

設定xml檔


Windows版VirtualBox 的設定檔是C:\Documents and Settings\ID名稱\.VirtualBox內的VirtualBox.xml,直接點選就能開啟設定。

上述的設定如果要生效,都必須將值放在這個檔案內。

Windows共用資料匣 (share folder)


如果想要讓VirtualBox內的客體xp也共用機器上的某個資料匣,可以依據以下的步驟設定:
  1. 點選 VirtualBox選項 裝置 > 共用資料夾。

  2. 點選 加入共用資料夾
  3. 點選「資料夾名稱」、「資料夾路徑」設定資料夾。

  4. 點選 VirtualBox選項 裝置 > 安裝Guest Additions ,安裝後選擇重啟客體xp
  5. 進入「網路芳鄰」後輸入\\Vboxsvr 後,就會出現設定的資料匣了。

Linux共用資料匣


如果想要讓VirtualBox內的客體Linux也共用機器上的某個資料匣,可以依據以下的步驟設定:
  1. 點選 VirtualBox選項 裝置 > 安裝Guest Additions ,安裝後選擇重啟客體Linux(如果已經作過可以略過)
  2. 在客體Linux內開啟「終端機」
  3. 編輯 /etc/fstab 檔,加入
    VB_Folder /mnt/share vboxsf defaults 0 0

  4. 重啟客體Linux
  5. 分享的目錄會放在 /mnt/share 下。

請注意:範例中的:VB_Folde和 /mnt/share可以隨需要改變。
  1. Rondo's Cache -《圖解 VirtualBox 入門1234567
  2. My. Debian.-《My. Debian. » How to Access via ssh a Virtualbox Guest machine.

1 則留言:

  1. 您好我是路人
    這篇文章解決了我長久的困擾
    非常感謝您的分享~!

    回覆刪除