def _mountCIFS(self, ctx:ThaniyaBackupContext, localDirPath:str, cifsHostAddress:str, #cifsPort:int, cifsShareName:str, cifsLogin:str, cifsPassword:str, cifsVersion:str) -> bool: assert isinstance(localDirPath, str) assert os.path.isdir(localDirPath) localDirPath = os.path.abspath(localDirPath) mounter = jk_mounting.Mounter() mip = mounter.getMountInfoByMountPoint(localDirPath) if mip is not None: raise Exception("Directory " + repr(localDirPath) + " already used by mount!") credentialFilePath = ctx.privateTempDir.writeTextFile( "username="******"\npassword="******"\n" ) options = [ "user="******",credentials=", credentialFilePath, ",rw", ] if cifsVersion: options.append(",vers=") options.append(cifsVersion) cmd = [ BackupConnectorMixin_mountCIFS.MOUNT_PATH, "-t", "cifs", "-o", "".join(options), "//" + cifsHostAddress + "/" + cifsShareName, localDirPath, ] # print(" ".join(cmd)) p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write((cifsPassword + "\n").encode("utf-8")) (stdout, stderr) = p.communicate(timeout=3) if p.returncode != 0: returnCode1 = p.returncode stdOutData1 = stdout.decode("utf-8") stdErrData1 = stderr.decode("utf-8") print("Mount attempt:") print("\tcmd =", cmd) print("\treturnCode =", returnCode1) print("\tstdOutData =", repr(stdOutData1)) print("\tstdErrData =", repr(stdErrData1)) raise Exception("Failed to mount device!") return False else: return True
def _mountSSH(self, localDirPath: str, sshHostAddress: str, sshPort: int, sshLogin: str, sshPassword: str, sshDirPath: str) -> bool: assert isinstance(localDirPath, str) assert os.path.isdir(localDirPath) localDirPath = os.path.abspath(localDirPath) mounter = jk_mounting.Mounter() mip = mounter.getMountInfoByMountPoint(localDirPath) if mip is not None: raise Exception("Directory " + repr(localDirPath) + " already used by mount!") cmd = [ BackupConnectorMixin_mountSFTP.SSHFS_PATH, "-p", str(sshPort), "-o", "password_stdin", "-o", "reconnect", sshLogin + "@" + sshHostAddress + ":" + sshDirPath, localDirPath ] p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write((sshPassword + "\n").encode("utf-8")) (stdout, stderr) = p.communicate(timeout=3) if p.returncode != 0: returnCode1 = p.returncode stdOutData1 = stdout.decode("utf-8") stdErrData1 = stderr.decode("utf-8") p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write(("yes\n" + sshPassword + "\n").encode("utf-8")) (stdout, stderr) = p.communicate(timeout=3) if p.returncode != 0: returnCode2 = p.returncode stdOutData2 = stdout.decode("utf-8") stdErrData2 = stderr.decode("utf-8") print("Mount attempt 1:") print("\tcmd =", cmd) print("\treturnCode =", returnCode1) print("\tstdOutData =", repr(stdOutData1)) print("\tstdErrData =", repr(stdErrData1)) print("Mount attempt 2:") print("\treturnCode =", returnCode2) print("\tstdOutData =", repr(stdOutData2)) print("\tstdErrData =", repr(stdErrData2)) raise Exception("Failed to mount device!") return False else: return True else: return True
def performBackup(self, ctx: ThaniyaBackupContext): if self.__ensureNotMounted: mi = jk_mounting.Mounter().getMountInfoByFilePath( self.__devicePath) if mi is not None: raise Exception("The device is still mounted: " + repr(self.__devicePath)) ThaniyaIO.copyDevice( ctx=ctx, sourceDevicePath=self.__devicePath, targetFileOrDirectoryPath=ctx.absPath(self.__targetFileName), )
def cmd_diskfree(cfg: dict, log): print() mounter = jk_mounting.Mounter() mi = mounter.getMountInfoByFilePath(cfg["wwwWikiRootDir"]) stdout, stderr, exitcode = jk_sysinfo.run(None, "/bin/df -BK") ret = jk_sysinfo.parse_df(stdout, stderr, exitcode)[mi.mountPoint] print("Mount point:", mi.mountPoint) fBlock = (ret["spaceTotal"] - ret["spaceFree"]) / ret["spaceTotal"] barLength = min(jk_console.Console.width(), 140) - 20 iBlock = int(round(fBlock * barLength)) text = "{0} {1:.1f}% filled".format( "#" * iBlock + ":" * (barLength - iBlock), fBlock * 100) print(text) print(_formatGBytes((ret["spaceTotal"] - ret["spaceFree"]) / 1073741824), "of", _formatGBytes(ret["spaceTotal"] / 1073741824), "used.")
def mountSSH(self, dirPath:str): assert isinstance(dirPath, str) assert os.path.isdir(dirPath) dirPath = os.path.abspath(dirPath) mounter = jk_mounting.Mounter() mip = mounter.getMountInfoByMountPoint(self._ssh_dirPathRoot) if mip is not None: raise Exception("Directory " + repr(self._ssh_dirPathRoot) + " already used by mount!") cmd = [ BackupClient_ThaniyaSSH.SSHFS_PATH, "-p", str(self._ssh_port), "-o", "password_stdin", "-o", "reconnect", self._ssh_login + "@" + self._ssh_hostAddress + ":" + self._ssh_dirPathRoot, dirPath ] p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write((self._ssh_password + "\n").encode("utf-8")) (stdout, stderr) = p.communicate(timeout=3) if p.returncode != 0: returnCode1 = p.returncode stdOutData1 = stdout.decode("utf-8") stdErrData1 = stderr.decode("utf-8") p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write(("yes\n" + self._ssh_password + "\n").encode("utf-8")) (stdout, stderr) = p.communicate(timeout=3) if p.returncode != 0: returnCode2 = p.returncode stdOutData2 = stdout.decode("utf-8") stdErrData2 = stderr.decode("utf-8") print("Mount attempt 1:") print("\treturnCode =", returnCode1) print("\tstdOutData =", repr(stdOutData1)) print("\tstdErrData =", repr(stdErrData1)) print("Mount attempt 2:") print("\treturnCode =", returnCode2) print("\tstdOutData =", repr(stdOutData2)) print("\tstdErrData =", repr(stdErrData2)) raise Exception("Failed to mount device!") self.__mountPoint = dirPath
def __perform_deinitialize(self, bd2: BD2): with ProcessingContext(text="Disconnecting and cleaning up", bd2=bd2, bMeasureDuration=False, statsDurationKey=None) as ctx: mountDirPath = None mounter = None if self.__backupConnector.performsMountUnmount: mountDirPath = self.__backupConnector.mountDirPath mounter = jk_mounting.Mounter() assert mounter.isMounted(mountDirPath) # terminate connection with ctx.descend("Terminating connection ...") as ctx2: self.__backupConnector.deinitialize(ctx2) # verify that a mounted directory has been unmounted as expected if bd2.mountDirPath: mounter.refresh() if mounter.isMounted(bd2.mountDirPath): ctx.log.error( "DEINITIALIZATION FAILED! Directory is still mounted: " + bd2.mountDirPath) ctx.log.error( "This is a bug! Please contact [email protected] and report this bug!" ) #try: #if self.__backupConnector.performsMountUnmount: # ThaniyaIO.removeEmptyDir(ctx, self.__mountDirPath) #except Exception as ee: # bError = True # ---- ctx.log.notice("Done.")
#!/usr/bin/env python3 import os import sys import jk_mounting mounter = jk_mounting.Mounter() mo = jk_mounting.MountOptions() mo.noatime = True mo.nodiratime = True print(mo) mounter.mount("/dev/xyzabc", "/mnt", options=mo) #mounter.unmount("/mnt")