在KVM主机和虚拟机之间共享目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在KVM主机和虚拟机之间共享目录相关的知识,希望对你有一定的参考价值。

执行以下步骤:

(1)在host OS上面创建新的目录和在这个目录里面创建一个文件。

[email protected]:~# mkdir /tmp/shared
[email protected]:~# touch /tmp/shared/file

(2)在停止KVM的实例后,添加下面的配置

[email protected]:~# virsh edit kvm1
 ...
 <devices>
   ...
   <filesystem type=‘mount‘ accessmode=‘passthrough‘>
     <source dir=‘/tmp/shared‘/>
     <target dir=‘tmp_shared‘/>
   </filesystem>
   ...
 </devices>
 ...
Domain kvm1 XML configuration edited.

(3)启动VM虚拟机:

[email protected]:~# virsh start kvm1
Domain kvm1 started

(4)执行以下命令连接控制台

[email protected]:~# virsh console kvm1
Connected to domain kvm1
Escape character is ^]

Debian GNU/Linux 8 debian ttyS0

debian login: root
Password:
...

(5)确保9p和virtio内存驱动已经加载。

[email protected]:~# lsmod | grep 9p
9pnet_virtio 17006 0
9pnet 61632 1 9pnet_virtio
virtio_ring 17513 3 virtio_pci,virtio_balloon,9pnet_virtio
virtio 13058 3 virtio_pci,virtio_balloon,9pnet_virtio

(6)挂载共享的目录到/mnt

[email protected]:~# mount -t 9p -o trans=virtio tmp_shared /mnt

(7)列出刚刚挂载的东西

[email protected]:~# mount | grep tmp_shared
tmp_shared on /mnt type 9p (rw,relatime,sync,dirsync,trans=virtio)

(8)查看下我们在第一步创建的文件是否能看的到。

[email protected]:~# ls -la /mnt/
total 8
drwxr-xr-x 2  root root 4096 Mar 23 11:25 .
drwxr-xr-x 22 root root 4096 Mar 22 16:28 ..
-rw-r--r-- 1  root root 0    Mar 23 11:25 file

以上是关于在KVM主机和虚拟机之间共享目录的主要内容,如果未能解决你的问题,请参考以下文章

基于NFS共享存储kvm虚拟机动态迁移操作梳理

kvm虚拟化学习笔记(十四)之kvm虚拟机静态迁移

kvm_虚拟机迁移

kvm虚拟机静态迁移

KVM虚拟化笔记(十四)------kvm虚拟机动态迁移

KVM虚拟化笔记(十三)------kvm虚拟机静态迁移