Source code for vinstall.backend.utils

#!/bin/env python

"""Collection of small utilities."""


__version__ = "0.0.1a"
__date__ = "08/20/09"
__author__ = "rbistolfi"
__credits__ = "M0E-lnx, Uelsk8s, Kidd"


import os
import subprocess
import re


[docs]def get_mem_size(): """Get the amount of RAM availablein the system in mB. """ meminfo = "/proc/meminfo" fo = open(meminfo) line = fo.readline() fo.close() label, size, unit = line.split() return float(size) / 1024.0
[docs]def format_partition(path, filesystem): """Create a filesystem in a partition using mkfs. """ command = 'mkfs.%s -f %s' % (filesystem, path) subprocess.call(command.split())
[docs]def mount(src, mountpoint, extra=None, filesystem='auto'): """Mount a filesystem. Arguments: src: the filesystem or device to be mounted. extra: extra options to be passed to the mount command, for example: "-o loop". Default is None. filesystem: filesystem type passed to the mount command, for example: "ext3". Default is auto. """ os.system('mount -t %s %s %s %s' % (filesystem, extra, src, mountpoint)) return mountpoint
[docs]def umount(mounted): """Unmount a filesystem. Arguments: A mounted filesystem or device. >>> umount('/mnt/loop') 0 """ return os.system('umount %s' % (mounted))
[docs]def exec_chroot(root, command): """Change the root dir for current shell and run the given command. Arguments: dir: The directory to for chrooting to. command: The command to be executed under the new root. >>> exec_chroot('/', 'pwd') 0 """ command = 'chroot %s %s' % (root, command) subprocess.call(command.split())
[docs]def is_mounted(device): """Returns true if device is mounted.""" command = subprocess.Popen('mount', stdout=subprocess.PIPE) mounted = command.stdout.read() command.stdout.close() if device in mounted: return True else: return False
[docs]def supported_filesystems(): """Returns a list of filesystems supported by the system.""" fo = open('/proc/filesystems') data = fo.read() fo.close() kernel_fs = re.findall(r'^\s+(\w+)', data, re.M) path = os.environ['PATH'].split(':') for p in path: for k in kernel_fs: if os.path.isfile(os.path.join(p, 'mkfs.' + k)): yield k