feat(runtime): stage VM runtime artifacts and writable drives in per-VM ephemeral storage by default
This commit is contained in:
+33
-2
@@ -26,6 +26,8 @@ export class SmartVM {
|
||||
constructor(options: ISmartVMOptions = {}) {
|
||||
this.options = {
|
||||
dataDir: options.dataDir || '/tmp/.smartvm',
|
||||
runtimeDir: options.runtimeDir || this.getDefaultRuntimeDir(),
|
||||
ephemeralWritableDrives: options.ephemeralWritableDrives ?? true,
|
||||
arch: options.arch || 'x86_64',
|
||||
bridgeName: options.bridgeName || 'svbr0',
|
||||
subnet: options.subnet || '172.30.0.0/24',
|
||||
@@ -57,6 +59,30 @@ export class SmartVM {
|
||||
});
|
||||
}
|
||||
|
||||
private getDefaultRuntimeDir(): string {
|
||||
const tmpfsDir = '/dev/shm';
|
||||
try {
|
||||
if (plugins.fs.existsSync(tmpfsDir) && plugins.fs.statSync(tmpfsDir).isDirectory()) {
|
||||
return plugins.path.join(tmpfsDir, '.smartvm', 'runtime');
|
||||
}
|
||||
} catch {
|
||||
// Fall back to os.tmpdir() below.
|
||||
}
|
||||
return plugins.path.join(plugins.os.tmpdir(), '.smartvm', 'runtime');
|
||||
}
|
||||
|
||||
public getRuntimeDir(): string {
|
||||
return this.options.runtimeDir!;
|
||||
}
|
||||
|
||||
private sanitizePathPart(value: string): string {
|
||||
const sanitized = value.replace(/[^a-zA-Z0-9._-]/g, '_');
|
||||
if (!sanitized || sanitized === '.' || sanitized === '..') {
|
||||
return 'item';
|
||||
}
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the Firecracker binary is available.
|
||||
* Downloads it if not present.
|
||||
@@ -110,8 +136,9 @@ export class SmartVM {
|
||||
// Generate VM ID if not provided
|
||||
const vmId = config.id || plugins.smartunique.uuid4();
|
||||
|
||||
// Generate socket path
|
||||
const socketPath = this.imageManager.getSocketPath(vmId);
|
||||
// Keep per-VM runtime artifacts in tmpfs by default.
|
||||
const vmRuntimeDir = plugins.path.join(this.options.runtimeDir!, this.sanitizePathPart(vmId));
|
||||
const socketPath = plugins.path.join(vmRuntimeDir, 'firecracker.sock');
|
||||
|
||||
// Create MicroVM instance
|
||||
const vm = new MicroVM(
|
||||
@@ -120,6 +147,10 @@ export class SmartVM {
|
||||
this.firecrackerBinaryPath!,
|
||||
socketPath,
|
||||
this.networkManager,
|
||||
{
|
||||
runtimeDir: this.options.runtimeDir,
|
||||
ephemeralWritableDrives: this.options.ephemeralWritableDrives,
|
||||
},
|
||||
);
|
||||
|
||||
// Register in active VMs
|
||||
|
||||
Reference in New Issue
Block a user