搭建 VMware 共享文件夹

0x00 前言

趁有时间整理一下关于搭建 VMware 共享文件夹的相关步骤

本文所使用的环境:

  • VMware: Workstation 16 Pro & 16.2.0 build-18760230

  • host: Windows 11 Pro & 22000.282

  • guest: ArchLinux & 5.14.14-arch1-1

0x01 开启 host 共享文件夹

  • 在Win11上创建一个空文件夹(名字随意),例如:

    E:\hostshare

  • 开启 VMware host共享文件夹:

    settings > Options > Shared Folders > Always enabled

    Folders > add > select "E:\hostshare"

    sharefolder-enable

    select-sharefolder

    最后确认即可。
    注意:后面涉及的 ".host:/" 指的并不是绝对路径,而单纯就是 host 共享文件夹的名字,不管它上层有多少个文件夹。

0x02 在 guest 中安装并配置相关软件

In-kernel drivers

  1. 添加内核模块

/etc/mkinitcpio.conf

MODULES=(... vmw_balloon vmw_pvscsi vsock vmw_vsock_vmci_transport ...)
  1. 重新编译内核
mkinitcpio -P

Open-VM-Tools

  1. 安装 open-vm-tools
 pacman -S --noconfirm open-vm-tools
  1. start & enable service
## Service responsible for the Virtual Machine status report.
systemctl start vmtoolsd.service
systemctl enable vmtoolsd.service
## Filesystem utility. Enables drag & drop functionality between host and guest through FUSE (Filesystem in Userspace).
systemctl start vmware-vmblock-fuse.service
systemctl enable vmware-vmblock-fuse.service
  1. 安装 gtkmm3 (To enable copy and paste between host and guest gtkmm3 is required.)
pacman -S --noconfirm gtkmm3
  1. 检查共享文件夹是否启用
vmware-hgfsclient

如果有输出:hostshare,则表示已启用,否则回到 步骤0x01 检查是否有误。

Xorg configuration

  1. 安装依赖
pacman -S --noconfirm xf86-input-vmmouse xf86-video-vmware mesa
  1. if booting into multi-user.target or using an uncommon setup (e.g. multiple monitors)

    to give permission for loading drivers.

echo "needs_root_rights=yes" > /etc/X11/Xwrapper.config

Shared Folders with vmhgfs-fuse utility

  1. 在ArchLinux的家目录下创建一个guest共享文件夹(路径与名字随意)
mkdir -p /home/skillf/guestshare
  1. /etc/fuse.conf中取消下面一行的注释,表示允许其他用户访问
user_allow_other
  1. 将host共享文件夹挂载到guest共享文件夹(临时挂载),如果guest共享文件夹是非空文件夹,则要加参数 -o nonempty,另外挂载之后guest共享文件夹里的东西将被隐藏一直到卸载,所以guest共享文件夹最好在未挂载时,保持为空文件夹
vmhgfs-fuse -o allow_other -o auto_unmount .host:/hostshare /home/skillf/guestshare

注意:第3步为临时挂载,在重启后需要再次手动挂载,所以我们应该让它一开机自动挂载

这里推荐两种开机自动挂载方式:

  • 方法一:在 /etc/fstab 在最后添加一行即可:
.host:/hostshare /home/skillf/guestshare fuse.vmhgfs-fuse nofail,allow_other 0 0
  • 方法二:使用Systemd,创建一个开机自启服务(名字随意,但必须.service结尾) 例如:hostshare-guestshare.service,将挂载命令丢入服务中即可:
[Unit]
Description=Load VMware shared folders
Requires=vmware-vmblock-fuse.service
After=vmware-vmblock-fuse.service
ConditionPathExists=.host:/hostshare
ConditionVirtualization=vmware

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/vmhgfs-fuse -o allow_other -o auto_unmount .host:/hostshare /home/skillf/guestshare

[Install]
WantedBy=multi-user.target

最后将 hostshare-guestshare.service 复制到 /etc/systemd/system/,并启用服务:

cp hostshare-guestshare.service /etc/systemd/system/
systemctl start hostshare-guestshare.service
systemctl enable hostshare-guestshare.service

Time synchronization

  • guest中时间常常与host中的时间不一致,所以需要以host作为同步时间源,进行同步
vmware-toolbox-cmd timesync enable
hwclock --hctosys --localtime

至此手动开启共享文件的步骤已整理完毕,再进一步写成名为 vmwareshare.sh 的脚本吧:

#!/bin/bash
###
 # @Author: skillf
 # @Date: 2021-11-02 21:20:10
 # @LastEditTime: 2021-11-03 09:45:18
 # @FilePath: \archlinuxInstall\vmware.sh
###

# Print the command. The script ends when the command fails.
# -o pipefail : As soon as a subcommand fails, the entire pipeline command fails and the script terminates.
set -euo pipefail
# Please uncomment it to see how it works
#set -x

# The shared folder in host
hostshare="host"
# The shared folder in guest
guestshare="/home/skillf/guest"
# ##*/ Cut the string after the last '/' from left to right
service=$hostshare-$(echo ${guestshare##*/}).service

# Check VMware share folder
pacman -Sy
pacman -S --noconfirm open-vm-tools
if ! vmware-hgfsclient | grep $hostshare > /dev/null; then
    echo -e "\033[031mERROR: The VMware shared folder \"$hostshare\" is not enabled !\033[0m"
    exit 0
fi

# In-kernel drivers
sed -i 's/^MODULES=()/MODULES=(vmw_balloon vmw_pvscsi vsock vmw_vsock_vmci_transport)/' /etc/mkinitcpio.conf
mkinitcpio -P

# Open-VM-Tools
pacman -S --noconfirm gtkmm3
## Service responsible for the Virtual Machine status report.
systemctl start vmtoolsd.service
systemctl enable vmtoolsd.service
## Filesystem utility. Enables drag & drop functionality between host and guest through FUSE (Filesystem in Userspace).
systemctl start vmware-vmblock-fuse.service
systemctl enable vmware-vmblock-fuse.service

# Xorg configuration
pacman -S --noconfirm xf86-input-vmmouse xf86-video-vmware mesa
echo "needs_root_rights=yes" > /etc/X11/Xwrapper.config

if df | grep $guestshare$ | grep vmhgfs-fuse > /dev/null; then
    systemctl stop $service
    systemctl disable $service
    rm -rf  /etc/systemd/system/$service
    rm -rf $guestshare
fi
mkdir -p $guestshare
# Change the owner permission of a shared folder
chown -R skillf:skillf $guestshare

sed -i 's/^#user_allow_other/user_allow_other/' /etc/fuse.conf
cat > /etc/systemd/system/$service <

然后在 $HOME/.xinitrc 文件中添加:
注意:如果是使用的是WM,比如:dwm 那么应该在其之前的行添加

##  Tool to enable clipboard sharing (copy/paste) between host and guest.
vmware-user &

最后使用脚本:

sudo chmod a+x vmwareshare.sh
sudo ./vmwareshare.sh

注意:如果是在Windows下编辑的代码,那么在Linux运行之前需要转换一下格式,否则会报错:
-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory

pacman -S --noconfirm dos2unix
dos2unix vmwareshare.sh

Tips:

  • 卸载 guest 共享文件夹:
    • 如果是在命令行直接挂载,则可以使用 umount 卸载:
    sudo umount -l /home/skillf/guestshare
    
    • 如果是使用 Systemd 启用服务的方式挂载,则移除服务之后自动卸载:
    systemctl stop hostshare-guestshare.service
    systemctl disable hostshare-guestshare.service
    
  • 往虚拟机中拖拽文件,将被保存在目录:
~/.cache/vmware/drag_and_drop/

你可能感兴趣的:(搭建 VMware 共享文件夹)