Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Add mounting functions (U.T. needs to be done).
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille Pontvieux committed Jan 25, 2013
1 parent c9abe45 commit b161960
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
10 changes: 2 additions & 8 deletions src/salix-live-installer/freesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
83 changes: 82 additions & 1 deletion src/salix-live-installer/mounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

0 comments on commit b161960

Please sign in to comment.