def expand_file_system(block_device_path, fs_type): fs_type = fs_type.strip() if fs_type == 'devtmpfs': raise DriverError( StatusCode.INVALID_ARGUMENT, 'Device not formatted with FileSystem found fs type {}'.format( fs_type)) elif fs_type.startswith('ext'): cmd = 'resize2fs {}'.format(block_device_path) elif fs_type == 'xfs': cmd = 'xfs_growfs {}'.format(block_device_path) else: raise DriverError(StatusCode.INVALID_ARGUMENT, 'unknown fs_type {}'.format(fs_type)) exit_code, stdout, stderr = Utils.run_command(cmd) logger.debug("resize file-system finished {} {} {}".format( exit_code, stdout, stderr)) if exit_code != 0: raise DriverError( StatusCode.INTERNAL, 'Error expanding File System {} on block device {}'.format( fs_type, block_device_path)) return exit_code, stdout, stderr
def mount(source, target, flags=None, mount_options=None): flags_str = ' '.join(flags) if flags else '' mount_opts_str = '-o ' + ','.join(mount_options) if mount_options else '' cmd = 'mount {flags_str} {options} {source} {target}'.format(flags_str=flags_str, options=mount_opts_str, source=source, target=target) exit_code, stdout, stderr = Utils.run_command(cmd) if exit_code != 0: raise Exception("mount failed {} {} {}".format(exit_code, stdout, stderr))
def umount(target): cmd = 'umount {target}'.format(target=target) exit_code, stdout, stderr = Utils.run_command(cmd) logger.debug("umount finished {} {} {}".format(exit_code, stdout, stderr)) if exit_code != 0: if 'not mounted' in stderr: # this is not an error return elif 'target is busy' in stderr: raise MountTargetIsBusyError(stderr) else: raise Exception("umount failed exit_code={} stdout={} stderr={}".format(exit_code, stdout, stderr))
def mkfs(fs_type, target_path, flags=None): if not fs_type: raise ArgumentError( "fs_type argument Cannot be None or an empty string") if not flags: flags = [] cmd = "mkfs.{fs_type} {flags} {target_path}".format( fs_type=fs_type, flags=' '.join(flags), target_path=target_path) exit_code, stdout, stderr = Utils.run_command(cmd) if exit_code != 0: raise OSError("mkfs failed {} {} {}".format( exit_code, stdout, stderr))
def mkfs(fs_type, target_path, flags=None): if not fs_type: raise ArgumentError("fs_type argument Cannot be None or an empty string") if not flags: flags = [] if fs_type == FSType.XFS: # support older host kernels which don't support this feature flags.append('-m reflink=0') cmd = "mkfs.{fs_type} {flags} {target_path}".format(fs_type=fs_type, flags=' '.join(flags), target_path=target_path) exit_code, stdout, stderr = Utils.run_command(cmd) if exit_code != 0: raise OSError("mkfs failed {} {} {}".format(exit_code, stdout, stderr))
def get_fs_type(target_path): # returns an empty string for a block device that has no FileSystem on it # An alternate method is to use `df --output=fstype {target_path} | tail -1` but this will return "devtmpfs" if the block device has no FileSystem on it cmd = "blkid -o export {}".format(target_path) exit_code, stdout, stderr = Utils.run_command(cmd) try: blkid_output = stdout.strip() if blkid_output == '': return blkid_output for line in blkid_output.split('\n'): key, value = line.split('=') if key == 'TYPE': return value raise ValueError('Could not find TYPE key in blkid output') except Exception as ex: raise DriverError(StatusCode.INVALID_ARGUMENT, 'Could not determine file system type for path {}. Error: {}'.format(target_path, ex))
def get_fs_type(target_path): cmd = "blkid {} | awk '{{ print $3}}' | cut -c 7- | rev | cut -c 2- | rev".format( target_path) exit_code, stdout, stderr = Utils.run_command(cmd) return stdout.strip()
def is_mounted(mount_path): cmd = 'grep -qs "{} " /proc/mounts'.format(mount_path) exit_code, stdout, stderr = Utils.run_command(cmd) return exit_code == 0
def get_block_device_size(block_device_path): cmd = "lsblk {} --output SIZE | tail -1".format(block_device_path) exit_code, stdout, stderr = Utils.run_command(cmd) return stdout.strip()
def get_file_system_type(target_path): cmd = "df -T {} | tail -1 | awk '{{ print $2}}'".format(target_path) exit_code, stdout, stderr = Utils.run_command(cmd) return stdout
def chmod(permissions_mask, path): cmd = 'chmod {mask} {path}'.format(mask=permissions_mask, path=path) exit_code, stdout, stderr = Utils.run_command(cmd) if exit_code != 0: raise Exception("Failed changing permissions on {} code:{} stdout:{} stderr:{}".format(path, exit_code, stdout, stderr))