Skip to content

Commit 82dc63d

Browse files
committed
kvm/virtiofsd: New page
1 parent a3eca53 commit 82dc63d

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

docs/assets/virtiofsd.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
if test $# -ne 2; then
4+
echo "Need exactly 2 arguments" >&2
5+
exit 1
6+
fi
7+
8+
VMID="$1"
9+
PHASE="$2"
10+
11+
if [ "$VMID" -lt 1000 ]; then
12+
exit 0
13+
fi
14+
15+
case "$PHASE" in
16+
pre-start) systemctl restart "virtiofsd@$VMID".service ;;
17+
pre-stop) ;;
18+
post-start) ;;
19+
post-stop) ;;
20+
*) echo "Unknown phase $PHASE" >&2; exit 1;;
21+
esac

docs/assets/[email protected]

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[Unit]
2+
Description=VirtIO Filesystem Daemon for /opt/vlab (VMID: %i)
3+
4+
[Service]
5+
Type=exec
6+
WorkingDirectory=/run/virtiofsd
7+
RuntimeDirectory=virtiofsd
8+
Environment=SHARED_DIR=/opt/vlab
9+
ExecStart=/usr/libexec/virtiofsd \
10+
--syslog \
11+
--socket-path /run/virtiofsd/%i.sock \
12+
--shared-dir $SHARED_DIR \
13+
--sandbox namespace \
14+
--announce-submounts \
15+
--inode-file-handles=prefer
16+
17+
ReadOnlyPaths=/opt/vlab
18+
InaccessiblePaths=/boot /etc /home /var
19+
20+
SystemCallArchitectures=native
21+
LockPersonality=yes
22+
MemoryDenyWriteExecute=yes
23+
RestrictAddressFamilies=AF_UNIX
24+
25+
PrivateTmp=yes
26+
#PrivateDevices=yes
27+
ProtectClock=yes
28+
ProtectHome=yes
29+
ProtectHostname=yes
30+
ProtectKernelLogs=yes
31+
ProtectKernelModules=yes
32+
ProtectKernelTunables=yes
33+
ProtectSystem=yes

docs/kvm/virtiofsd.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 虚拟机 VirtIO FS 文件系统配置
2+
3+
[virtiofsd](https://virtio-fs.gitlab.io/) 是一种将主机上的文件系统共享进虚拟机的机制,类似 NFS,但不经过网络,而是使用本地的 Unix socket 和 FUSE 语义,可以提供比 NFS 更好的性能。
4+
5+
## 主机配置
6+
7+
为 virtiofsd 创建一个模板化的 systemd 服务,以便在主机上启动 virtiofsd 服务([文件下载](../assets/[email protected])):
8+
9+
```ini title="/etc/systemd/system/[email protected]"
10+
11+
```
12+
13+
然后编写一个 hook script 脚本([文件下载](../assets/virtiofsd.sh)):
14+
15+
```shell title="/mnt/vz/snippets/virtiofsd.sh"
16+
--8<-- "virtiofsd.sh"
17+
```
18+
19+
## 虚拟机配置
20+
21+
每个想要使用 `/opt/vlab` 的虚拟机都需要配置以下两点:
22+
23+
```yaml
24+
args: -chardev socket,id=virtfs0,path=/run/virtiofsd/9612.sock -device vhost-user-fs-pci,queue-size=1024,chardev=virtfs0,tag=vlab-software -object memory-backend-file,id=mem,size=6144M,mem-path=/dev/shm,share=on -numa node,memdev=mem
25+
hookscript: nfs-template:snippets/virtiofsd.sh
26+
```
27+
28+
其中 `args` 里有两点需要注意:
29+
30+
- `socket,[...],path=` 后面的路径需要和前面的 systemd service 中为 `virtiofsd` 指定的 socket 路径一致;
31+
- `memory-backend-file` 的 `size` 需要和虚拟机和内存大小一致。
32+
33+
除此之外,`vhost-user-fs-pci` 后面的 `tag` 参数就是在虚拟机内挂载的 source。以此处的 `tag=vlab-software` 为例,虚拟机内的挂载命令为:
34+
35+
```shell
36+
mount -t virtiofs vlab-software /opt/vlab
37+
```
38+
39+
对应的 fstab 写法为:
40+
41+
```shell
42+
vlab-software /opt/vlab virtiofs nofail 0 0
43+
```
44+
45+
## 参考资料
46+
47+
- [\[TUTORIAL\] virtiofsd in PVE 8.0.x](https://forum.proxmox.com/threads/virtiofsd-in-pve-8-0-x.130531/)

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ nav:
112112
- 相关记录: vlab-software/misc.md
113113
- KVM 虚拟机:
114114
- cloud-init 配置: kvm/cloud-init.md
115+
- VirtIO FS 配置: kvm/virtiofsd.md
115116
- 测试环境:
116117
- 简介: testing/index.md
117118
- Vlab VPN: testing/devvpn.md

0 commit comments

Comments
 (0)