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