Use a service for virtiofs for better control.

This commit is contained in:
Andreas B. Mundt 2023-09-04 07:59:43 +02:00
parent 51cb0a2804
commit 7fd6c76996
4 changed files with 24 additions and 25 deletions

View file

@ -40,4 +40,4 @@ sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}"
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}" sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}"
# set virtiofs-Socket # set virtiofs-Socket
sed -i "s:VIRTIOFSSOCKET:${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock:" "${VM_XML}" sed -i "s:VIRTIOFSSOCKET:/run/virtiofsd/${VM_NAME}-clone.sock:" "${VM_XML}"

View file

@ -10,27 +10,7 @@ if [[ $# -ne 1 ]]; then
fi fi
VM_NAME="$1" VM_NAME="$1"
XDG_RUNTIME_DIR="/run/user/${SUDO_UID}" ## Make sure VMs can read the base directory:
chgrp 1010 "/lmn/media/${SUDO_USER}"
if ! [[ -d "${XDG_RUNTIME_DIR}/virtiofs" ]]; then systemctl restart virtiofs@${VM_NAME}-clone:${SUDO_USER}.service
mkdir "${XDG_RUNTIME_DIR}/virtiofs/"
chown "${SUDO_UID}" "${XDG_RUNTIME_DIR}/virtiofs/"
fi
if [[ -S "${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock" ]] ; then
/usr/bin/rm "${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock"
fi
#sudo /usr/lib/qemu/virtiofsd --socket-path="${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock" \
# --socket-group="${SUDO_USER}" --announce-submounts -o source="/media/${USER}" > /dev/null &
#sudo /usr/lib/qemu/virtiofsd --socket-path="${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock" \
# --socket-group="${SUDO_USER}" -o source="/media/${USER}" > /dev/null &
/usr/lib/qemu/virtiofsd --socket-path="${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock" \
-o source="/lmn/media/${SUDO_USER}/" &
# Wait until socket-File exists and chown to user
until [[ -S "${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock" ]]; do
echo "waiting for ${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock"
sleep 0.1
done
/usr/bin/chown "${SUDO_USER}" "${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock"

View file

@ -0,0 +1,14 @@
[Unit]
Description=share home folders via virtiofsd
## $I is split into two parameters: VM:USER
[Service]
Type=exec
ExecStart=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; exec /usr/lib/qemu/virtiofsd --socket-path="/run/virtiofsd/$${soc}.sock" -o source="/lmn/media/$${usr}"'
ExecStartPost=chmod 755 /run/virtiofsd/
ExecStartPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; chown "$$usr" "/run/virtiofsd/$${soc}.sock"'
ExecStopPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; rm "/run/virtiofsd/$${soc}.sock"'
SuccessExitStatus=1
[Install]
WantedBy=multi-user.target

View file

@ -137,9 +137,14 @@
dest: /etc/systemd/system/ dest: /etc/systemd/system/
owner: root owner: root
group: root group: root
mode: '0755' mode: '0644'
notify: "enable vmimage-torrent.service" notify: "enable vmimage-torrent.service"
- name: Deploy virtiofs service
copy:
src: virtiofs@.service
dest: /etc/systemd/system/
- name: Deploy bridge.conf needed for qemu session mode - name: Deploy bridge.conf needed for qemu session mode
lineinfile: lineinfile:
path: /etc/qemu/bridge.conf path: /etc/qemu/bridge.conf