From b161960e46933c79797584819c0c397f2cfbe9fe Mon Sep 17 00:00:00 2001 From: Cyrille Pontvieux Date: Fri, 25 Jan 2013 19:23:13 +0100 Subject: [PATCH] Add mounting functions (U.T. needs to be done). --- src/salix-live-installer/freesize.py | 10 +--- src/salix-live-installer/mounting.py | 83 +++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/salix-live-installer/freesize.py b/src/salix-live-installer/freesize.py index 21bfe7e..0110a6e 100644 --- a/src/salix-live-installer/freesize.py +++ b/src/salix-live-installer/freesize.py @@ -12,6 +12,7 @@ import os from stat import * from execute import * +import mounting def getHumanSize(size): "Returns the human readable format of the size in bytes" @@ -34,14 +35,7 @@ def getSizes(path): + all of them with the corresponding 'Human' suffix. """ if S_ISBLK(os.stat(path).st_mode): - mountpoint = None - for line in open('/proc/mounts').read().splitlines(): - p, mp, _ = line.split(' ', 2) # 2 splits max, _ is discarded - if os.path.islink(p): - p = os.path.realpath(p) - if p == path: - mountpoint = mp - break + mountpoint = getMountPoint(path) if mountpoint: # mounted, so will use mountpoint to get information about different sizes path = mountpoint diff --git a/src/salix-live-installer/mounting.py b/src/salix-live-installer/mounting.py index a4d9ecf..d32b7a4 100644 --- a/src/salix-live-installer/mounting.py +++ b/src/salix-live-installer/mounting.py @@ -2,5 +2,86 @@ # -*- coding: utf-8 -*- # vim: set et ai sta sw=2 ts=2 tw=0: """ -Help mounting/unmounting a filesystem +Help mounting/unmounting a filesystem. +Functions: + - getMountPoint + - isMounted + - mountDevice + - umountDevice """ +from execute import * +import os +from stat import * + +def getMountPoint(device): + """ + Will find the mount point to this 'device' or None if not mounted. + """ + mountpoint = None + if S_ISBLK(os.stat(device).st_mode): + for line in open('/proc/mounts').read().splitlines(): + p, mp, _ = line.split(' ', 2) # 2 splits max, _ is discarded + if os.path.islink(p): + p = os.path.realpath(p) + if p == device: + mountpoint = mp + break + return mountpoint + +def isMounted(device): + """ + Same as os.path.ismount(path) but using a block device. + """ + if getMountPoint(device): + return True + else: + return False + +def mountDevice(device, fsType, mountPoint = None): + """ + Mount the 'device' of 'fsType' filesystem under 'mountPoint'. + If 'mountPoint' is not specified, '/mnt/.temp/device' will be used. + Returns False if it fails. + """ + if not mountPoint: + mountPoint = '/mnt/.temp/{0}'.format(device) + if os.path.exists(mountPoint): + return False + if not os.path.exists(mountPoint): + os.path.makedirs(mountPoint) + return execCall(['mount', '-t', fsType, device, path]) + +def umountDevice(deviceOrPath, tryLazyUmount = True, deleteMountPoint = True): + """ + Umount the 'deviceOrPath' which could be a device or a mount point. + If the umount failed, try again with a lazyUmount if 'tryLazyUmount' is True. + Will delete the mount point if 'deleteMountPoint' is True. + Returns False if it fails. + """ + if S_ISBLK(os.stat(device).st_mode): + path = getMountPoint(device) + else: + path = deviceOrPath + if os.path.ismounted(path): + ret = execCall(['umount', path]) + if not ret: + ret = execCall(['umount', '-l', path]) + if ret and deleteMountPoint: + # delete the empty directory + try: + os.rmdir(path) + except: + pass + # delete the temporary directory if not empty + if os.path.isdir('/mnt/.temp'): + try: + os.rmdir('/mnt/.temp') + except: + pass + return ret + else: + return False + +# Unit test +if __name__ == '__main__': + from assertPlus import *