VMware/安裝 Arch Linux 為虛擬機

出自 Arch Linux 中文维基

這篇文章是關於如何在 VMware 產品,比如 Workstation PlayerFusionWorkstation Pro 中安裝Arch Linux。

編譯進內核的驅動程序(模塊)[編輯 | 編輯原始碼]

注意: 下列模塊只有部分會被 Arch 的 Udev 自動檢測並啟用。如果某些需要的模塊沒有被自動檢測到(可通過 lsmod | grep modulename 進行確認),可以將其添加到 mkinitcpioMODULES 列表。例如:
/etc/mkinitcpio.conf
...
	
MODULES=(... vmw_balloon vmw_pvscsi vsock vmw_vsock_vmci_transport ...)


確保 重新生成 initramfs

  • vmw_balloon - 物理內存管理驅動。它可以像一個氣球一樣被「吹大」,在客戶機上申請內存,並通過監視器釋放掉這部分內存,使得這部分物理內存可以被分配給其它客戶機使用;也可以"放氣",以允許虛擬機使用更多物理內存。還支持將反分配狀態(deallocated)的虛擬機原本所占的內存釋放回主機,而無需徹底停用虛擬機。
  • vmw_pvscsi - VMware 平行虛擬化 SCSI(PVSCSI)的主機總線適配器(HBA)。
  • vmw_vmci - 虛擬機通信接口(VMCI)。VMCI 虛擬設備的作用是實現虛擬環境中的主機-客機間高速通信。
  • vmwgfx - 這是 VMware SVGA2 虛擬顯卡的 DRM 驅動,作用是 3D 加速。支持 KMS
  • vmxnet3 - VMware 的 vmxnet3 虛擬網卡所需模塊。
  • open-vm-tools 10.0 以上版本包含了一個基於 FUSE 實現的 HGFS 文件系統,支持 Linux 4.0 及以上版本內核,用於主機-客機間共享目錄。

如果你在某種監視器(比如 VMware vSphere Hypervisor)上運行 Arch Linux,那麼同時需要安裝下面這些模塊。當虛擬機間通信時,客戶機-伺服器類型的應用可以通過 VMCI 虛擬設備向 VMCI 套接字(vsock)接口進行寫入:

  • vsock - 虛擬套接字協議。其作用是允許虛擬機,主機或監視器間像 TCP/IP 協議一樣通信。
  • vmw_vsock_vmci_transport - 基於 VMCI 實現的虛擬套接字。

某些模塊,例如舊的 vmhgfs 目錄共享模塊,還需要額外地手工編譯,並手工從 systemd 啟用服務,才能正常運轉。

VMware Tools 與 Open-VM-Tools 方案對比[編輯 | 編輯原始碼]

2007 年,VMware 將 VMware Tools 中的大部分代碼以 LGPL 協議發布,這就是 Open-VM-Tools。官方的 VMware Tools 不再單獨向 Arch Linux 提供。

以往,VMware Tools 方案所提供的網絡與儲存驅動是最好的,而且還帶有時間同步等功能。然而網絡與 SCSI 驅動這部分代碼現已合併入 Linux 內核了。

VMware Tools 曾經有使用 Unity mode 功能的優勢,但由於使用的人不多且維護困難,於是從 VMWare Workstation 12 開始移除了 Linux 客戶機的 Unity mode 支持。詳情請閱 此跟帖 的答案。

Open-VM-Tools[編輯 | 編輯原始碼]

實用工具[編輯 | 編輯原始碼]

open-vm-tools 軟體包裡包括如下工具:

  • vmtoolsd - 負責匯報虛擬機狀態的服務。
  • vmware-checkvm - 用於檢測虛擬機中是否在運行著某程序的工具。
  • vmware-toolbox-cmd - 用於收集宿主系統信息的工具。
  • vmware-user - 用來在宿主機和虛擬機之間共享剪切板(複製/粘貼)的工具。
  • vmware-vmblock-fuse - 文件系統工具。基於 FUSE(Filesystem in Userspace)實現了宿主/客機之間拖拽文件的功能。
  • vmware-xferlogs - 向虛擬機的日誌文件輸出日誌與調試信息。
  • vmhgfs-fuse - 掛載 HGFS 共享目錄的工具。

安裝[編輯 | 編輯原始碼]

安裝 open-vm-tools。然後 啟動啟用 vmtoolsd.servicevmware-vmblock-fuse.service

如果主機和客戶機間複製粘貼功能無法正常工作,請嘗試手動安裝 gtkmm3

官方的 VMware Tools[編輯 | 編輯原始碼]

模塊[編輯 | 編輯原始碼]

  • vmblock - 文件系統驅動。支持在主機-客機間拖拽文件。已被 vmware-vmblock-fuse 取代
  • vmci - 主機-客戶機間的高性能通信接口。
  • vmmon - 虛擬機監視器。
  • vmnet - 網絡驅動。
  • vsock - VMCI 套接字。
注意: vmware-vmblock-fuse 這一組件不是以 內核 模塊的形式實現的;且除非你禁用了 fusevmblock 已被從內核中移除。如果需要啟用這些功能,需要手動啟用 systemd 服務,具體操作見下文。

在客戶機安裝[編輯 | 編輯原始碼]

安裝依賴項:base-devel包組(用於編譯模塊),net-tools(提供 ifconfig 供安裝程序調用)和 linux-headers (提供內核頭文件)。另外需要 devtools 包中的 pkgctl 來簽出 open-vm-tools

然後為安裝程序創建假 init 目錄:

# for x in {0..6}; do mkdir -p /etc/init.d/rc${x}.d; done

掛載安裝程序:

# mount /dev/cdrom /mnt

解壓(以解壓到 /root 為例):

# tar xf /mnt/VMwareTools*.tar.gz -C /root

開始安裝:

# perl /root/vmware-tools-distrib/vmware-install.pl

若安裝時出現如下的錯誤,都可以安全忽略:

  • VMXNET 3 虛擬網卡
  • "Warning: This script could not find mkinitrd or update-initramfs and cannot remake the initrd file!"
  • 在系統中找不到 Fuse 組件

啟用 vmware-vmblock-fuse systemd 服務(請確保你手動安裝了依賴或使用了 -s 參數)。The open-vm-tools source code should be checked out using the Arch build system.

 $ pkgctl repo clone open-vm-tools
 $ cd open-vm-tools
 $ makepkg -s --asdeps
 # cp vm* /usr/lib/systemd/system

啟用 vmware-vmblock-fuse.servicevmtoolsd.service

接下來重新啟動虛擬機。

登錄並啟動 VMware Tools:

# /etc/init.d/rc6.d/K99vmware-tools start

另外,可以通過創建 /etc/systemd/system/vmwaretools.service 文件來在啟動時自動啟動 vmware-tools

/etc/systemd/system/vmwaretools.service
[Unit]
Description=VMWare Tools daemon

[Service]
ExecStart=/etc/init.d/vmware-tools start
ExecStop=/etc/init.d/vmware-tools stop
PIDFile=/var/lock/subsys/vmware
TimeoutSec=0
RemainAfterExit=yes
 
[Install]
WantedBy=multi-user.target

然後啟用 vmwaretools.service

提示:在 GitHub 上有個項目 [1] 嘗試將這些步驟全自動處理。

Xorg 配置[編輯 | 編輯原始碼]

注意: 要在虛擬機中使用 Xorg,至少需要為其分配 32MB 的顯存。

需要安裝以下依賴:xf86-input-vmmousexf86-video-vmware,以及 mesa

應該只需要這幾個包就可以啟動至 graphical target 了。接下來 /etc/xdg/autostart/vmware-user.desktop 會自動啟動,並負責完成在虛擬機裡運行的相關必要配置。

然而如果你先啟動至了 multi-user.target,或者你的環境不太常規(比如用了多個顯示器),那麼你需要手動啟用 vmtoolsd.service 服務,並且還需要讓 Xorg 以根權限運行以加載相關驅動。

提示與技巧[編輯 | 編輯原始碼]

通過 vmhgfs-fuse 共享目錄[編輯 | 編輯原始碼]

注意: 這一功能需要的最低軟體版本是 open-vm-tools v.10.x 與 Linux 內核 4.x,同時需要 VMware Workstation / Fusion。

在菜單中選擇 Edit virtual machine settings > Options > Shared Folders > Always enabled,即可建立共享目錄。

在客機裡運行如下命令可以列出共享目錄:

$ vmware-hgfsclient

然後以如下方式掛載:

# mkdir <shared folders root directory>
# vmhgfs-fuse -o allow_other -o auto_unmount .host:/<shared_folder> <shared folders root directory>

如果碰到了如下報錯:fusermount: option allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf,需將 /etc/fuse.conf 中的這一行取消注釋:

user_allow_other

欲了解 vmhgfs-fuse 的其他掛載參數,可以用 -h 參數調用:

# vmhgfs-fuse -h
注意: In case I/O operations (such as cp, mv or cat) result in an "Input/output error" when accessing the shared folder then setting the mount option max_write can serve as a workaround (observed with open-vm-tools version 11.1.0 and higher; see this bug report: https://github.com/vmware/open-vm-tools/issues/437)).

fstab[編輯 | 編輯原始碼]

每個共享目錄的掛載都需要寫如下的一行配置:

/etc/fstab
.host:/<shared_folder> <shared folders root directory> fuse.vmhgfs-fuse nofail,allow_other 0 0

然後創建並掛載目錄:

# mkdir <shared folders root directory>
# mount <shared folders root directory>

Systemd[編輯 | 編輯原始碼]

創建如下 .service 文件:

/etc/systemd/system/<shared folders root directory>-<shared_folder>.service
[Unit]
Description=Load VMware shared folders
Requires=vmware-vmblock-fuse.service
After=vmware-vmblock-fuse.service
ConditionPathExists=.host:/<shared_folder>
ConditionVirtualization=vmware

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/vmhgfs-fuse -o allow_other -o auto_unmount .host:/<shared_folder> <shared folders root directory>

[Install]
WantedBy=multi-user.target

如果客機裡的 <shared folders root directory> 目錄還不存在,你需要手動提前創建:

# mkdir -p <shared folders root directory>

然後啟用 <shared folders root directory>-<shared_folder>.service 掛載目標。

刪掉 <shared_folder> 的部分可以一次性掛載所有共享目錄。

使用 vmhgfs 模塊的舊式目錄共享[編輯 | 編輯原始碼]

注意: 該功能只在 VMware Workstation 和 Fusion 中可用

在菜單中選擇 Edit virtual machine settings > Options > Shared Folders > Always enabled,即可建立共享目錄。

確保已加載 vmhgfs 驅動:

# modprobe vmhgfs

可以通過以下命令查看共享目錄清單:

$ vmware-hgfsclient

然後可以通過如下命令掛載目錄:

# mkdir /home/user1/shares
# mount -n -t vmhgfs .host:/<shared_folder> /home/user1/shares

在啟動時啟用[編輯 | 編輯原始碼]

按照如下編輯 mkinitcpio.conf 文件:

/etc/mkinitcpio.conf
...
MODULES=(... vmhgfs)
...

然後重新生成 initramfs

fstab[編輯 | 編輯原始碼]

每個共享目錄的掛載都需要寫如下的一行配置:

/etc/fstab
.host:/<shared_folder> /home/user1/shares vmhgfs defaults 0 0

然後創建並掛載目錄:

# mkdir /home/user1/shares
# mount /home/user1/shares
Systemd[編輯 | 編輯原始碼]

共享目錄需用到 vmhgfs 驅動。參考如下案例創建 .service 文件:

/etc/systemd/system/<shared folders root directory>-<shared_folder>.mount
[Unit]
Description=Load VMware shared folders
ConditionPathExists=.host:/<shared_folder>
ConditionVirtualization=vmware

[Mount]
What=.host:/<shared_folder>
Where=<shared folders root directory>/<shared_folder>
Type=vmhgfs
Options=defaults,noatime

[Install]
WantedBy=multi-user.target
/etc/systemd/system/<shared folders root directory>-<shared_folder>.automount
[Unit]
Description=Load VMware shared folders
ConditionPathExists=.host:/<shared_folder>
ConditionVirtualization=vmware

[Automount]
Where=<shared folders root directory>/<shared_folder>

[Install]
WantedBy=multi-user.target

如果客戶機內還沒有 <shared folders root directory> 目錄,你需要手動提前創建:

# mkdir -p <shared folders root directory>

然後啟用 mnt-hgfs.automount 掛載目標。

刪掉 <shared_folder> 的部分可以一次性掛載所有共享目錄。

Prune mlocate DB[編輯 | 編輯原始碼]

When using mlocate, it is pointless to index the shared directories in the locate DB. Therefore, add the directories to PRUNEPATHS in /etc/updatedb.

3D 加速[編輯 | 編輯原始碼]

如果在創建客戶機時沒有選擇 3D 加速功能,可以勾選: 編輯虛擬機設置 > 硬體 > 顯示器 > 加速 3D 圖形

注意: 啟用 3D 加速之後,Xorg 的性能可能會很差。有些時候通過 llvmpipe 來實現軟體渲染的性能可能更好。

OpenGL 與 GLSL 支持[編輯 | 編輯原始碼]

用戶可以自行更新實現 OpenGL 的 GLSL 內核模塊,覆蓋 Arch 自帶的版本。

本文成文時,OpenGL 3.3 和 GLSL 3.30 都得到了支持。參閱 https://bbs.archlinux.org/viewtopic.php?id=202713 可以了解更多細節。

時間同步[編輯 | 編輯原始碼]

為虛擬機配置時間同步很重要,因為虛擬機比物理機更容易出現時間波動現象。主要原因就在於 CPU 是被共用的。

有兩種方案可以實現同步:與宿主機同步,或是通過外部伺服器同步。

與宿主機同步時間[編輯 | 編輯原始碼]

先確保 vmtoolsd.service 服務已啟動,然後用如下命令啟用時間同步功能:

# vmware-toolbox-cmd timesync enable

宿主系統休眠後,用如下的命令來使客機間同步時間:

# hwclock --hctosys --localtime

與外部伺服器同步時間[編輯 | 編輯原始碼]

參閱 NTP

故障排除[編輯 | 編輯原始碼]

客戶機網絡速度減慢[編輯 | 編輯原始碼]

Arch Linux 和其它 Linux 客戶機一樣,當使用 NAT 模式的時候也許會碰到網絡速度減慢的問題。為了解決這個問題,請在宿主機下把對應的客戶機的網絡模式切換為 橋接模式,在必要時修改客戶機網絡的配置文件。有關配置的詳細信息請參閱 網絡配置 。如果在 Windows 宿主機下,使用正確的客戶機網絡配置仍然無法正確連接網絡, 可使用 管理員 運行 虛擬網絡編輯器 並點擊左下方的 還原默認設置 按鈕。

聲音問題[編輯 | 編輯原始碼]

如果虛擬機發出了惱人的巨響,那有可能是 PC 揚聲器的原因。在客戶機系統裡禁用 PC 揚聲器即可解決:

滑鼠問題[編輯 | 編輯原始碼]

虛擬機可能會出現下列滑鼠問題:

  • 自動獲取/失去焦點的功能可能會在滑鼠箭頭移入窗口時失效
  • 按鍵無響應
  • 卡頓、延遲
  • 在個別軟體中點擊無響應
  • 箭頭在移入/移出虛擬機時跳躍
  • 箭頭會跳躍到從虛擬機移出的位置

可以先嘗試卸載 xf86-input-vmmouse 包,xf86-input-vmmousexf86-input-libinput 應足以處理滑鼠和鍵盤輸入。

為解決滑鼠箭頭跳躍到從虛擬機移出的位置的問題,可以試試在 .vmx 配置文件裡添加如下配置:

~/vmware/<Virtual Machine name>/<Virtual Machine name>.vmx
mouse.vusb.enable = "TRUE"
mouse.vusb.useBasicMouse = "FALSE"

VMware 還會為遊戲做自動的滑鼠優化。如果這一優化產生了問題,可以在這裡將其禁用:Edit > Preferences > Input > Optimize mouse for games: Never

再者,嘗試在 60-libinput.conf禁用 catchall 事件也可能有用:

/usr/share/X11/xorg.conf.d/60-libinput.conf
#Section "InputClass"
#        Identifier "libinput pointer catchall"
#        MatchIsPointer "on"
#        MatchDevicePath "/dev/input/event*"
#        Driver "libinput"
#EndSection

啟動故障[編輯 | 編輯原始碼]

啟動速度慢[編輯 | 編輯原始碼]

如果 VMware 開啟了內存熱擴容功能,那麼有可能會出現如下錯誤:

  • add_memory failed
  • acpi_memory_enable_device() error

可以在 .vmx 配置文件裡寫入 mem.hotadd = "FALSE" 來禁用內存熱擴容功能。

~/vmware/<Virtual Machine name>/<Virtual Machine name>.vmx
mem.hotadd = "FALSE"

關機/重啟時卡住不動[編輯 | 編輯原始碼]

試著降低 vmtoolsd 服務的超時閾值(默認是 90 秒):

/etc/systemd/system/vmtoolsd.service.d/timeout.conf
[Service]
TimeoutStopSec=1

窗口解析度自動適配[編輯 | 編輯原始碼]

自動適配的意思是,當你在宿主機裡縮放 VMware 窗口之後,Arch 作為客戶機系統,應該自動根據主系統窗口的新尺寸來調整解析度。

方案 1[編輯 | 編輯原始碼]

確保在設置裡開啟了自動適配。

VMware Worksation 的這一設置位於:View -> Autosize -> Autofit Guest

方案 2[編輯 | 編輯原始碼]

出於某些原因,解析度自動適配的功能依賴於 gtkmmgtk2 軟體包,所以需確保客戶機上已安裝這兩個包。如果客戶機沒有安裝 X windows 或使用的桌面環境不依賴於 GTK(例如 KDE),那麼你需要手動安裝這兩個包。

方案 3[編輯 | 編輯原始碼]

可能需要向 mkinitcpio.conf 添加相關模塊:

/etc/mkinitcpio.conf
MODULES=(vsock vmw_vsock_vmci_transport vmw_balloon vmw_vmci vmwgfx)

不要忘了重新生成 initramfs

方案 4[編輯 | 編輯原始碼]

啟用 vmtoolsd.service

如果不起效,需確保 vmtoolsd.service 已正常重啟

方案 5[編輯 | 編輯原始碼]

If GNOME is running on Wayland, install xf86-video-vmware (FS#57473).

See [2].

方案 6[編輯 | 編輯原始碼]

Make sure that Stretch Mode is disabled. Follow VM > Settings > Display > Display Scaling and untick the option Stretch mode.

拖拽與複製粘貼[編輯 | 編輯原始碼]

提示:這些功能與 gtkmm3 有著未指明的依賴關係,並會導致這些功能靜默失敗。此問題在 FS#43159有詳述。

為了確保拖拽與複製粘貼功能正常工作,需要安裝 open-vm-toolsgtkmm3 這兩個包。

使 vmware-userX11 之後運行:

  • 確保 /etc/xdg/autostart/vmware-user.desktop 存在,如果文件不存在,請運行:
# cp /etc/vmware-tools/vmware-user.desktop /etc/xdg/autostart/vmware-user.desktop

Wayland 暫不支持複製粘貼功能,但使用 Xwayland 的應用可以正常使用該功能。

在 VMware Workstation 11 版上運行共享 VM[編輯 | 編輯原始碼]

Workstation 11 有個 bug:當 Arch 客戶機以共享 VM 模式運行,且啟動了 vmtoolsd 服務時,vmware-hostd 會崩潰。open-vm-tools 有個補丁來繞過這一問題。

Virtual Network Editor Wayland[編輯 | 編輯原始碼]

Running GUI applications as root -- some of the following is copied from

  • When running VMWare workstation on a wayland host the Virtual Network Editor will not launch under Wayland this because

Trying to run a graphical application as root via su, sudo or pkexec in a Wayland session (e.g. GParted or Gedit), will fail with an error similar to this:

$ sudo vmware-netcfg
No protocol specified
Unable to init server: Could not connect: Connection refused

(gedit:2349): Gtk-WARNING **: cannot open display: :0

This is a security feature of Wayland they reason GUI applications should not be run as root. This can be achieved using the following in the terminal

Using xhost[編輯 | 編輯原始碼]

A more versatile —though much less secure— workaround is to use xhost to temporarily allow the root user to access the local user's X session[3]. To do so, execute the following command as the current (unprivileged) user:

$ xhost si:localuser:root

To remove this access after the application has been closed:

$ xhost -si:localuser:root

Using sudo -E[編輯 | 編輯原始碼]

Launch vmware-netcfg with:

$ sudo -E vmware-netcfg