Skip to content

Instantly share code, notes, and snippets.

@mkawserm
Last active August 29, 2015 14:00
Show Gist options
  • Save mkawserm/11114043 to your computer and use it in GitHub Desktop.
Save mkawserm/11114043 to your computer and use it in GitHub Desktop.
Mount partition or folder using python on any linux distro
#!/usr/bin/env python3
"""
Object Name : PyDriveMount
Author : kawser
Author Blog : http://blog.kawser.org
Date : 20/04/2014
Time : 1:40 PM
Objective : mount a drive or folder using python
"""
import os
import subprocess
#mount -t ntfs -o fmask=0022,dmask=0000,uid=1000,gid=1000
""" Py Drive Mount Object """
class PyDriveMount(object):
def __init__(self):
pass
def l_whoami(self):
c = subprocess.Popen(['whoami'], stdout=subprocess.PIPE).stdout.read().split()
if len(c) == 1:return str(c[0],"utf-8")
return None
def l_mount(self):
c = subprocess.Popen(['mount'], stdout=subprocess.PIPE).stdout.read()
return str(c,"utf-8")
def l_id(self):
"""get user id through linux command"""
c = subprocess.Popen(['id'], stdout=subprocess.PIPE).stdout.read().split()
#print( c )
if len(c) == 3:return c
return None
def isAdmin(self):
"""Check if the user has admin privileges"""
_id = str(self.l_id()[0],"utf-8") #get user id
if _id !=None:
if _id.find("root")!=-1:
return True
return False
def isRoot(self):return self.isAdmin()
def isDriveMounted(self,drive):
_mount = self.l_mount()
if _mount.find(drive)!=-1:return True
return False
def mountFolder(self,src,dst):
if self.isDriveMounted(src):
print( "%s already mounted"%src )
return False
elif self.isRoot():
#if directory is not exists create a new one
if not os.path.exists(dst):
os.makedirs(dst)
cmd_sequence = []
cmd_sequence.append("mount")
cmd_sequence.append("--bind")
cmd_sequence.append(src)
cmd_sequence.append(dst)
c = subprocess.Popen(cmd_sequence, stdout=subprocess.PIPE).stdout.read().split()
return True
else:
print ("Are you root?")
return False
def mountDrive(self,drive,drive_mount_point,ptype = "ntfs"):
if self.isDriveMounted(drive):
print( "%s drive already mounted"%drive )
return False
elif self.isRoot():
#if directory is not exists create a new one
if not os.path.exists(drive_mount_point):
os.makedirs(drive_mount_point)
cmd_sequence = []
cmd_sequence.append("mount")
cmd_sequence.append("-t")
cmd_sequence.append(ptype)
cmd_sequence.append("-o")
cmd_sequence.append("fmask=0022,dmask=0000,uid=1000,gid=1000")
cmd_sequence.append(drive)
cmd_sequence.append(drive_mount_point)
c = subprocess.Popen(cmd_sequence, stdout=subprocess.PIPE).stdout.read().split()
return True
else:
print ("Are you root?")
return False
####################################################################################################
if __name__ == "__main__":
drives = {
u"/dev/sda2" : u"WinHDD",
u"/dev/sda5" : u"DevHDD",
u"/dev/sda6" : u"EduHDD",
u"/dev/sda7" : u"MediaHDD"
}
folder = {
u"/media/kawser/DevHDD/WorkSpace" : u"/home/kawser/WorkSpace",
u"/media/kawser/MediaHDD/DownloadsHDD" : u"/home/kawser/Downloads",
}
pdm = PyDriveMount()
drive_root = u"/media/kawser"
if pdm.isRoot():
#mounting drives
for i in drives.keys():
_mount_point = "%s/%s" %( drive_root , drives[i] )
pdm.mountDrive( i , _mount_point )
#mounting folder
for src in folder.keys():
pdm.mountFolder(src,folder[src])
else:
print ("You don't have root privileges")
###################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment