@sentry0/qemu-hook-manager
Version:
A simple QEMU hook manager.
2 lines (1 loc) • 2.18 kB
JavaScript
import{EOL as f}from"node:os";import{join as i,sep as y}from"node:path";import{rm as g,copyFile as v,chown as x,chmod as A,opendir as E,access as p}from"node:fs/promises";import{asyncExec as a}from"../../index.js";import{logger as u}from"./logger.js";import{QEMU_HOOK_DIR as m}from"./hooks.js";import{domainExists as O}from"./virsh.js";const k=async()=>{const o="find /sys/kernel/iommu_groups/*/devices/* -maxdepth 0 2>/dev/null | sort -t/ -k5 -n",{stdout:s}=await a(o),r=s.split(f).filter(e=>e.length>0),t=new Map;for(const e of r){const n=e.match(/\/iommu_groups\/(\d+)\/devices\/(.+)$/);if(!n)continue;const c=parseInt(n[1],10),d=n[2],{stdout:h}=await a(`lspci -nns ${d}`),w=h.trim();t.has(c)||t.set(c,[]),t.get(c).push({pciAddress:d,description:w})}return t},$=async()=>{const o=["ls",i("/etc","libvirt","hooks","qemu.d")];try{const{stdout:s}=await a(o.join(" "));return s.split(f).filter(r=>r.length>0)}catch{return[]}},j=async o=>{const s=["ln","-s",i(m,"qemu.d",".qhm-passthrough"),i(m,"qemu.d",o)],{stdout:r,stderr:t}=await a(s.join(" "));if(t)throw new Error(t);return r},q=async o=>{const s=["unlink",i(m,"qemu.d",o)],{stdout:r,stderr:t}=await a(s.join(" "));if(t)throw new Error(t);return r},G=async(o,s,r=void 0)=>{r=r||o.split(y).pop();const t=i(s,r);await l(t)&&(u.info(`Removing existing script: ${t}`),await g(t,{force:!0,recursive:!0})),await v(o,t),await x(t,0,0),await A(t,"755")},l=async o=>{try{return await p(o),!0}catch{return!1}},I=async o=>{try{return await p(o),!0}catch{return!1}},P=async o=>{try{return await E(o)}catch{return{}}},b=async o=>{const s={iommuGroups:[],domains:[]};for(const[r,t]of Object.entries(o))if(r==="iommuGroups"){const e=Array.isArray(t)?t:[t];for(const n of e)/^[\da-f]{2}:[\da-f]{2}\.[\da-f]$/.test(n)?s.iommuGroups.push(n):u.warn(`Invalid IOMMU group: ${n}`)}else if(r==="domains"){const e=Array.isArray(t)?t:[t];for(const n of e)await O(n)?s.domains.push(n):u.warn(`Domain ${n} does not exist`)}else r==="useOwnHooks"?s[r]=t==="yes":s[r]=t;return s};export{k as fetchIommuGroups,$ as fetchActiveDomains,j as enablePassthrough,q as disablePassthrough,G as installScript,l as fileExists,I as dirExists,P as ls,b as filterAnswers};