feat: edit volume

This commit is contained in:
Nicolas Meienberger
2025-09-03 21:42:18 +02:00
parent ca4bd4a619
commit 91020e6f23
17 changed files with 790 additions and 319 deletions

View File

@@ -15,7 +15,7 @@ export const createVolumeBackend = (volume: Volume): VolumeBackend => {
return makeNfsBackend(config, path);
}
case "directory": {
return makeDirectoryBackend();
return makeDirectoryBackend(config, path);
}
default: {
throw new Error(`Backend ${config.backend} not implemented`);

View File

@@ -1,14 +1,17 @@
import * as fs from "node:fs/promises";
import type { BackendConfig } from "@ironmount/schemas";
import type { VolumeBackend } from "../backend";
const mount = async () => {
const mount = async (_config: BackendConfig, path: string) => {
console.log("Mounting directory volume...");
await fs.mkdir(path, { recursive: true });
};
const unmount = async () => {
console.log("Cannot unmount directory volume.");
};
export const makeDirectoryBackend = (): VolumeBackend => ({
mount,
export const makeDirectoryBackend = (config: BackendConfig, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount,
});

View File

@@ -1,4 +1,5 @@
import { exec } from "node:child_process";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import type { BackendConfig } from "@ironmount/schemas";
import type { VolumeBackend } from "../backend";
@@ -13,6 +14,8 @@ const mount = async (config: BackendConfig, path: string) => {
return;
}
await fs.mkdir(path, { recursive: true });
const source = `${config.server}:${config.exportPath}`;
const options = [`vers=${config.version}`, `port=${config.port}`];
const cmd = `mount -t nfs -o ${options.join(",")} ${source} ${path}`;
@@ -21,7 +24,7 @@ const mount = async (config: BackendConfig, path: string) => {
exec(cmd, (error, stdout, stderr) => {
console.log("Mount command executed:", { cmd, error, stdout, stderr });
if (error) {
console.error(`Error mounting NFS volume: ${stderr}`);
// console.error(`Error mounting NFS volume: ${stderr}`);
return reject(new Error(`Failed to mount NFS volume: ${stderr}`));
}
console.log(`NFS volume mounted successfully: ${stdout}`);
@@ -30,11 +33,28 @@ const mount = async (config: BackendConfig, path: string) => {
});
};
const unmount = async () => {
console.log("Unmounting nfs volume...");
const unmount = async (path: string) => {
if (os.platform() !== "linux") {
console.error("NFS unmounting is only supported on Linux hosts.");
return;
}
const cmd = `umount -f ${path}`;
return new Promise<void>((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
console.log("Unmount command executed:", { cmd, error, stdout, stderr });
if (error) {
console.error(`Error unmounting NFS volume: ${stderr}`);
return reject(new Error(`Failed to unmount NFS volume: ${stderr}`));
}
console.log(`NFS volume unmounted successfully: ${stdout}`);
resolve();
});
});
};
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount,
unmount: () => unmount(path),
});