/** * Permission and system checks */ import { $ } from 'bun'; import { logger } from '../utils/logger.js'; /** * Check if running as root */ export function isRoot(): boolean { return process.getuid?.() === 0; } /** * Check if user has sudo access */ export async function hasSudoAccess(): Promise { try { await $`sudo -n true`.quiet(); return true; } catch { return false; } } /** * Check if usbip kernel modules are loaded */ export async function checkModulesLoaded(): Promise { try { const result = await $`lsmod`.text(); return result.includes('vhci_hcd') || result.includes('usbip_core'); } catch { return false; } } /** * Create escalation script for installing udev rules */ export function generateEscalationScript(): string { return `#!/bin/bash # USB/IP Supervisor - Permission Escalation Script echo "This script will install udev rules and notification scripts for USB/IP Supervisor" echo "Root access is required." echo # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Please run with sudo:" echo " sudo $0" exit 1 fi # Install udev rules echo "Installing udev rules..." bunx @siwats/usbip-supervisor server --install-udev # Load kernel modules echo "Loading kernel modules..." modprobe vhci-hcd modprobe usbip-core echo echo "Installation complete!" echo "You can now run the supervisor without root access." `; } /** * Save escalation script */ export async function saveEscalationScript(path: string): Promise { const script = generateEscalationScript(); await Bun.write(path, script); await $`chmod +x ${path}`.quiet(); logger.info(`Saved escalation script to ${path}`); } /** * Verify system requirements */ export async function verifySystemRequirements(): Promise<{ usbipInstalled: boolean; modulesLoaded: boolean; hasPermissions: boolean; }> { const checks = { usbipInstalled: false, modulesLoaded: false, hasPermissions: false, }; // Check if usbip is installed try { await $`which usbip`.quiet(); checks.usbipInstalled = true; } catch { logger.error('usbip is not installed. Please install usbip-utils package.'); } // Check if modules are loaded checks.modulesLoaded = await checkModulesLoaded(); if (!checks.modulesLoaded) { logger.warn('USBIP kernel modules are not loaded. Will attempt to load them.'); } // Check permissions checks.hasPermissions = isRoot() || (await hasSudoAccess()); if (!checks.hasPermissions) { logger.warn('No root/sudo access. Some operations may fail.'); } return checks; }