def destroy(mpoint, force=False): if not os.path.ismount(mpoint): raise StorageError('Directory %s is not valid mount point' % mpoint) cmd = [UMOUNT_EXEC, mpoint] if force: cmd.insert(1, '-f') system(tuple(cmd), error_text="Can't destroy ramdisk.")
def rmloop(device): try: system((LOSETUP_EXEC, '-d', device)) except StorageError, e: if 'No such device or address' in e.err: ''' Silently pass non-existed loop removal ''' pass else: raise
def listloop(): ret = {} loop_lines = system((LOSETUP_EXEC, '-a'))[0].strip().splitlines() for loop_line in loop_lines: words = loop_line.split() ret[words[0][:-1]] = words[-1][1:-1] return ret
def free(): cmd = (FREE_EXEC, '-m') out_lines = system(cmd)[0].splitlines() free_ram = int(out_lines[1].split()[3]) free_swap = int(out_lines[3].split()[3]) return (free_ram, free_swap)
def mkloop(filename, device=None, size=None, quick=False): ''' Create loop device ''' if size and not os.path.exists(filename): cmd = ['dd', 'if=/dev/zero', 'of=%s' % filename, 'bs=1M'] if quick: cmd.extend(['seek=%d' % (size - 1,), 'count=1']) else: cmd.extend(['count=%d' % size]) system(cmd) if not device: system((LOSETUP_EXEC, '-f', filename)) loops = listloop() for loopdev, fname in loops.iteritems(): if fname == filename: return loopdev else: raise Exception('Cannot find loop device with %s file' % file) else: system((LOSETUP_EXEC, device, filename)) return device
def mkloop(filename, device=None, size=None, quick=False): ''' Create loop device ''' if size and not os.path.exists(filename): cmd = ['dd', 'if=/dev/zero', 'of=%s' % filename, 'bs=1M'] if quick: cmd.extend(['seek=%d' % (size - 1, ), 'count=1']) else: cmd.extend(['count=%d' % size]) system(cmd) if not device: system((LOSETUP_EXEC, '-f', filename)) loops = listloop() for loopdev, fname in loops.iteritems(): if fname == filename: return loopdev else: raise Exception('Cannot find loop device with %s file' % file) else: system((LOSETUP_EXEC, device, filename)) return device
def create(size, mpoint): if not os.path.exists(mpoint): os.makedirs(mpoint) cmd = (MOUNT_EXEC, '-t', 'tmpfs', '-o','size=%sM'%size, 'tmpfs', mpoint) system(cmd, error_text="Can't create ramdisk.")
def create(size, mpoint): if not os.path.exists(mpoint): os.makedirs(mpoint) cmd = (MOUNT_EXEC, '-t', 'tmpfs', '-o', 'size=%sM' % size, 'tmpfs', mpoint) system(cmd, error_text="Can't create ramdisk.")