Skip to content

Instantly share code, notes, and snippets.

@DoubNAT
Last active January 4, 2024 10:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DoubNAT/504649730113fcf93f7183b8031db47f to your computer and use it in GitHub Desktop.
Save DoubNAT/504649730113fcf93f7183b8031db47f to your computer and use it in GitHub Desktop.
Proxmox-VirtioFS

Proxmox-VirtioFS

Goal

Create a shared file system between host and VM programmatically without network

Script Results

  • Starts and stops the virtiofsd daemon on socket automatically (pre-start vm)
  • Args added to each vm conf automatically
  • Creates custom shared folder with folder structure "vmname (vmid)"

Snippet add

qm set <vm number> -hookscript local:snippets/virtiofs.pl

Check / Kill Processes

ps aux | grep virtiofsd killall virtiofsd

File Locations

/var/lib/vz/snippets/launch-virtio-daemon.sh /var/lib/vz/snippets/virtiofs.pl /etc/pve/nodes/pve/qemu-server/<vm number>.conf

Resources

#!/usr/bin/bash
vmid=$1
matchword="name: "
match="$(qm config $vmid | grep $matchword)"
vmname="${match/$matchword/}"
matchword="memory: "
match="$(qm config $vmid | grep $matchword)"
vmmemory="${match/$matchword/}"
sharepath="/mnt/share/$vmname (vm$vmid)"
socketpath="/var/run/vm$vmid-vhost-fs.sock"
vmconfpath="/etc/pve/nodes/pve/qemu-server"
vmconf="$vmconfpath/$vmid.conf"
vmconftemp="$vmconf.tmp"
if [ -a "$vmconf" ]
then
grep -v "args: -chardev socket,id=char0,path=/var/run/vm" $vmconf > $vmconftemp
mv $vmconftemp $vmconf
echo "args: -chardev socket,id=char0,path=/var/run/vm"$vmid"-vhost-fs.sock -device vhost-user-fs-pci,queue-size=1024,chardev=char0,tag=vmshare -object memory-backend-memfd,id=mem,size="$vmmemory"M,share=on -numa node,memdev=mem" >> $vmconf
fi
if [ ! -d "$sharepath" ]
then
mkdir "$sharepath"
fi
sleep 3
nohup /usr/lib/kvm/virtiofsd --syslog --daemonize --socket-path="$socketpath" -o source="$sharepath" -o cache=always &> /dev/null &
#!/usr/bin/perl
use strict;
use warnings;
print "GUEST HOOK: " . join(' ', @ARGV). "\n";
# First argument is the vmid
# Second argument is the phase
my $vmid = shift;
my $phase = shift;
if ($phase eq 'pre-start') {
print "$vmid is starting, doing preparations.\n";
system("/var/lib/vz/snippets/launch-virtio-daemon.sh", "$vmid");
} elsif ($phase eq 'post-start') {
print "$vmid started successfully.\n";
} elsif ($phase eq 'pre-stop') {
print "$vmid will be stopped.\n";
} elsif ($phase eq 'post-stop') {
print "$vmid stopped. Doing cleanup.\n";
} else {
die "got unknown phase '$phase'\n";
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment