'Node.js manage SMB mounts

How could I check on an ubuntu system the mounted drives from a Node.js app?

I would like to check if a specific drive is mounted, and if not mount it. The command I would use in the terminal would be:

mount.cifs //<ip>/<source_path> <target_path> -o user=guest,pass=,uid=guest

Could be there a solution, that works on both WINDOWS and UBUNTU systems?



Solution 1:[1]

I managed to solve this issue with execSync, and it works quite well. The problem with this solution, is that you have to write different scenarios for every platform.

Check if the target SMB is mounted on Ubuntu system:

 try {
        let mountGrepResponse = execSync(
          `mount | grep ${config.sourceFolderPath}`
        ).toString("utf-8");

        mountedPath = mountGrepResponse.split("type")[0].split("on")[1].trim();

        logger.info(`Target folder is mounted at: ${mountedPath}`);
      } catch (error) {
        logger.error("Drive isn't mounted, trying to mount it...");
      }

Mount the drive if not mounted:

 if (mountedPath.length === 0) {
        try {
          execSync(
            `sudo mount -t cifs -o username=guest,password= ${config.sourceFolderPath} ${config.mountFolderPath}`
          );
        } catch (error) {
          console.log(
            "Host unavailable, trying again in" + config.timeout + " seconds..."
          );
          await sleep(config.timeout * 1000) //stop the excution for the given seconds;
        }
      }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eogabor