Thursday, March 10, 2016

script to mount disk to define mount point

sometimes you want to mount certain partition to certain mount point, so you can do it with script, you only define it one time then when you plug in devices, you run the script then it will mount the device to the correct mount point, even you previously mount to wrong mount point it will unmount the old path and mount to the correct path.

script save it as


import os
import sys

lsc = os.popen("ls -l /dev/disk/by-id/")
lscl ="\n")
conf = "mount.conf"
def checkcorrectmount(config, mounted):
    newlist = {}
    for i in config:
        for j in mounted:
            if i == j:
                if config[i] == mounted[j][1]:
                    print "wrong mount point " + i + " to " + mounted[j][1]
                    print "it should be " + config[i]
                    print "so unmount it"
                    os.system("umount " + mounted[j][1])
                    mountcommand = "mount /dev/" + mounted[j][0] + " " + config[i]
                    print mountcommand
                    os.system(mountcommand + "&")
if os.path.isfile(conf):
    config = open(conf,"r").read()
    disktomount = eval(config)
    print disktomount
    if type(disktomount) == dict and len(disktomount)> 0:
    print "you have you create mount.conf file in the same directory as script file"
    print "write you mount option in the format {\"disk-id\":\"mountpoint\",\"disk-id\":\"mountpoint\"}"
    print "you can check you disk-id ny useing ls  -l /dev/disk/by-id/"
mountcmd = os.popen("mount").read()
mountlist = mountcmd.split("\n")
if len(sys.argv) <= 1:
    onlytomount = {}
    mounted = []
    mountdict = {}
    onetimedict = {}
    for i in lscl:
        if i == "" or len(i)<10:
        i = i.split()
        for k in disktomount.keys():
            if k == i[8]:
                device = i[10].split("/")
                device = device[len(device) - 1]
                for m in mountlist:
                    if m == "":
                    ml = m.split()
                    m = ml[0].split("/")
                    mountdevice = m[len(m) -1]
                    if device == mountdevice:
                        print device + " is already mount"
                        mountdict[k] = [device,ml[2]]
                        onetimedict[k] = [device,ml[2]]
                        if device not in mounted:
                            onetimedict.pop(k, None)
                        if device not in onlytomount.keys():
                            onlytomount[device] = disktomount[k]
    for tm in onlytomount:
        if tm in mounted:
        print "mount " + tm
        mountcommand = "mount /dev/" + tm + " " + onlytomount[tm]
        print mountcommand
        os.system(mountcommand + "&")
elif sys.argv[1] == "unmount":
    for i in disktomount.keys():
        os.system("umount " + disktomount[i])
    print "without parameter, it will mount all the device listed in mount.conf"
    print "use unmount parameter to unmount all that listed in mount.conf"
    print "otherwise will show this message"

--- create a config file name mount.conf in the same directory as script format {"disk-id":"mountpoint"} --- :

{"usb-ADATA_NH03_1200007052802000029-0:0-part1":"/media/4tb","usb-External_SATA_Storage_000000555555-0:0-part1":"/media/600gb", "usb-StoreJet_Transcend_WD-WXL1E842TP75-0:0-part1":"/media/2tb"}

--- you can find the disk-id by using  ls -l /dev/disk/by-id/
--- usage
python   (to mount)
python unmount  (to unmount)

No comments:

Post a Comment