@sentry0/qemu-hook-manager
Version:
A simple QEMU hook manager.
3 lines (2 loc) • 1.86 kB
JavaScript
import{argv as d,env as e,exit as s}from"node:process";import{join as a,sep as f}from"node:path";import{exec as w}from"node:child_process";import g from"node:util";import{readdir as k}from"node:fs/promises";import c from"prompts";import D from"yargs/yargs";import{logger as r}from"./src/libs/logger.js";import{enablePassthrough as H,disablePassthrough as O,fetchActiveDomains as _,installScript as v,filterAnswers as I,dirExists as x}from"./src/libs/general.js";import{buildQuestions as q}from"./src/libs/questions.js";import{installHook as M,QEMU_HOOK_DIR as h,makeHookDirectories as R}from"./src/libs/hooks.js";export const asyncExec=g.promisify(w),DEFAULT_HOOKS_DIR=a(import.meta.dirname,"qemu_hook_skeleton");try{const t=await _();c.override(D(d.slice(2)).argv);const l=await c(await q(),{onCancel:()=>{r.info("User cancelled the installation"),s(0)}}),o=await I(l);o.useOwnHooks?(await x(o.hooksDir)||(r.error(`Hooks directory ${o.hooksDir} does not exist`),s(3)),e.QHM_HOOKS_DIR=o.hooksDir):e.QHM_HOOKS_DIR=DEFAULT_HOOKS_DIR,o.iommuGroups.length<=0&&(r.info("No valid IOMMU groups specified, exiting"),s(2)),r.info("Installing the script 'qemu' which calls our hooks"),await v(a(DEFAULT_HOOKS_DIR,"qemu.sh"),h,"qemu"),r.info("Installing hooks");for(const i of o.iommuGroups){const p=o.useOwnHooks?e.QHM_HOOKS_DIR:a(e.QHM_HOOKS_DIR,"hooks");for(const m of await k(p,{recursive:!0}))if(m.endsWith(".sh")){const n=m.split(f),u=n.pop();await R(a(h,"qemu.d",".qhm-passthrough",...n)),await M(`0000:${i}`,n.join(f),u,o.useOwnHooks)}}for(const i of t)o.domains.includes(i)||(r.info(`Disabling hardware passthrough for domain: ${i}`),await O(i));for(const i of o.domains)t.includes(i)||(r.info(`Enabling hardware passthrough for domain: ${i}`),await H(i));r.info("qemu-hook-manager has completed.")}catch(t){r.error(t.message),s(1)}s(0);