virt-manager设置主机和虚拟机之间文件共享
文章目录
- 前言
- 设置共享文件
- 报错处理
前言
前几天,我开始使用virt-manager管理虚拟机。现在遇到的第一个问题是,如何在主机和虚拟机之间共享文件。
我曾设置过qemu虚拟机和主机之间共享文件。所以通过virt-manager设置主机和虚拟机之间共享文件也并非难事。
参考网上的相关资料:在KVM主机和来宾之间设置共享文件夹、QEMU/KVM - Virt-Manager | Folder sharing and USB Redirection - YouTube、9p (libvirt/QEMU) share modes-askubuntu
这里补充下,我当前使用的机器并没有SElinux活动安全模块,所以可以省却SElinux的相关配置。如何查看当前主机的活动安全模块,可以运行下方命令。详细内容见残缺的LSM框架启动分析。
➜ cat /sys/kernel/security/lsm
lockdown,capability,yama,apparmor
设置共享文件
修改libvirt runs VMs的权限。(不知道这样修改的具体原因,但在共享文件件的设置上,确实有效)
sudo gedit /etc/libvirt/qemu.conf
# By default libvirt runs VMs as non-root and uses AppArmor profiles
# to provide host protection and VM isolation. While AppArmor
# continues to provide this protection when the VMs are running as
# root, /dev/vhost-net, /dev/vhost-vsock and /dev/vhost-scsi access is
# allowed by default in the AppArmor security policy, so malicious VMs
# running as root would have direct access to this file. If changing this
# to run as root, you may want to remove this access from
# /etc/apparmor.d/abstractions/libvirt-qemu. For more information, see:
# https://launchpad.net/bugs/1815910
# https://www.redhat.com/archives/libvir-list/2019-April/msg00750.html
# Some examples of valid values are:
#
# user = "qemu" # A user named "qemu"
# user = "+0" # Super user (uid=0)
# user = "100" # A user named "100" or a user with uid=100
#
user = "root" <-----
# The group for QEMU processes run by the system instance. It can be
# specified in a similar way to user.
group = "root" <-----
- 双击虚拟机,进入虚拟机的设置界面。
- 使用添加硬件,添加一个文件系统。其中
mnt/data/linux
是我希望共享的文件夹,其目录与子目录中所有文件的权限为777。linux
为挂在标签。mapped
使用管理程序(QEMU 进程)的权限访问源。 进入虚拟机,在虚拟机中创建一个挂载点,并挂载KVM主机系统的共享文件夹。
sudo mkdir /usr/src/linux
sudo mount -t 9p -o trans=virtio linux /usr/src/linux
- 测试共享文件夹。保证共享文件夹可读可写。
报错处理
问题一:如果共享文件夹中存在符号链接。当虚拟机访问共享文件夹中的符号链接文件时,会报错:too many levels of symbolic links
。
查看共享文件夹中是否存在符号链接文件,可运行下面命令。
ls -alR | grep ^l
解决一:参考Sharing folder with VM through libvirt, 9p, permission denied,我们将上面的mapped
模式修改为Squash
模式。
至于passthrough
,mapped
,Squash
这三个模式,我不清楚区别。可自行参考9p (libvirt/QEMU) share modes。
还没有评论,来说两句吧...