virt-manager设置主机和虚拟机之间文件共享

向右看齐 2022-09-11 07:23 658阅读 0赞

文章目录

    • 前言
    • 设置共享文件
    • 报错处理

前言

前几天,我开始使用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框架启动分析。

  1. cat /sys/kernel/security/lsm
  2. lockdown,capability,yama,apparmor

设置共享文件

  1. 修改libvirt runs VMs的权限。(不知道这样修改的具体原因,但在共享文件件的设置上,确实有效)

    1. sudo gedit /etc/libvirt/qemu.conf
    2. # By default libvirt runs VMs as non-root and uses AppArmor profiles
    3. # to provide host protection and VM isolation. While AppArmor
    4. # continues to provide this protection when the VMs are running as
    5. # root, /dev/vhost-net, /dev/vhost-vsock and /dev/vhost-scsi access is
    6. # allowed by default in the AppArmor security policy, so malicious VMs
    7. # running as root would have direct access to this file. If changing this
    8. # to run as root, you may want to remove this access from
    9. # /etc/apparmor.d/abstractions/libvirt-qemu. For more information, see:
    10. # https://launchpad.net/bugs/1815910
    11. # https://www.redhat.com/archives/libvir-list/2019-April/msg00750.html
    12. # Some examples of valid values are:
    13. #
    14. # user = "qemu" # A user named "qemu"
    15. # user = "+0" # Super user (uid=0)
    16. # user = "100" # A user named "100" or a user with uid=100
    17. #
    18. user = "root" <-----
    19. # The group for QEMU processes run by the system instance. It can be
    20. # specified in a similar way to user.
    21. group = "root" <-----
  2. 双击虚拟机,进入虚拟机的设置界面。
    在这里插入图片描述
  3. 使用添加硬件,添加一个文件系统。其中mnt/data/linux是我希望共享的文件夹,其目录与子目录中所有文件的权限为777。linux为挂在标签。mapped使用管理程序(QEMU 进程)的权限访问源。
    在这里插入图片描述
  4. 进入虚拟机,在虚拟机中创建一个挂载点,并挂载KVM主机系统的共享文件夹。

    1. sudo mkdir /usr/src/linux
    2. sudo mount -t 9p -o trans=virtio linux /usr/src/linux
  5. 测试共享文件夹。保证共享文件夹可读可写。

报错处理

问题一:如果共享文件夹中存在符号链接。当虚拟机访问共享文件夹中的符号链接文件时,会报错:too many levels of symbolic links

查看共享文件夹中是否存在符号链接文件,可运行下面命令。

  1. ls -alR | grep ^l

解决一:参考Sharing folder with VM through libvirt, 9p, permission denied,我们将上面的mapped模式修改为Squash模式。

至于passthroughmappedSquash这三个模式,我不清楚区别。可自行参考9p (libvirt/QEMU) share modes。

发表评论

表情:
评论列表 (有 0 条评论,658人围观)

还没有评论,来说两句吧...

相关阅读