refactor: mount smb by puttings credentials in temporary file (#396)
This commit is contained in:
parent
2ab37e6b67
commit
51d2ffad17
1 changed files with 20 additions and 10 deletions
|
|
@ -40,16 +40,22 @@ const mount = async (config: BackendConfig, path: string) => {
|
||||||
|
|
||||||
const source = `//${config.server}/${config.share}`;
|
const source = `//${config.server}/${config.share}`;
|
||||||
const { uid, gid } = os.userInfo();
|
const { uid, gid } = os.userInfo();
|
||||||
const options = [`user=${config.username}`, `pass=${password}`, `port=${config.port}`, `uid=${uid}`, `gid=${gid}`];
|
const credentialsDir = await fs.mkdtemp(`${os.tmpdir()}/zerobyte-smb-`);
|
||||||
|
const credentialsPath = `${credentialsDir}/credentials`;
|
||||||
|
const credentialsLines = [`username=${config.username}`, `password=${password}`];
|
||||||
|
|
||||||
|
if (config.domain) {
|
||||||
|
credentialsLines.push(`domain=${config.domain}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.writeFile(credentialsPath, credentialsLines.join("\n"), { mode: 0o600 });
|
||||||
|
|
||||||
|
const options = [`credentials=${credentialsPath}`, `port=${config.port}`, `uid=${uid}`, `gid=${gid}`];
|
||||||
|
|
||||||
if (config.vers && config.vers !== "auto") {
|
if (config.vers && config.vers !== "auto") {
|
||||||
options.push(`vers=${config.vers}`);
|
options.push(`vers=${config.vers}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.domain) {
|
|
||||||
options.push(`domain=${config.domain}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.readOnly) {
|
if (config.readOnly) {
|
||||||
options.push("ro");
|
options.push("ro");
|
||||||
}
|
}
|
||||||
|
|
@ -60,11 +66,15 @@ const mount = async (config: BackendConfig, path: string) => {
|
||||||
logger.info(`Executing mount: mount ${args.join(" ")}`);
|
logger.info(`Executing mount: mount ${args.join(" ")}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await executeMount(args);
|
try {
|
||||||
} catch (error) {
|
await executeMount(args);
|
||||||
logger.warn(`Initial SMB mount failed, retrying with -i flag: ${toMessage(error)}`);
|
} catch (error) {
|
||||||
// Fallback with -i flag if the first mount fails using the mount helper
|
logger.warn(`Initial SMB mount failed, retrying with -i flag: ${toMessage(error)}`);
|
||||||
await executeMount(["-i", ...args]);
|
await executeMount(["-i", ...args]);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await fs.rm(credentialsPath, { force: true });
|
||||||
|
await fs.rmdir(credentialsDir).catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`SMB volume at ${path} mounted successfully.`);
|
logger.info(`SMB volume at ${path} mounted successfully.`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue