def _get_fsid_path(fs_id): """Return the mount point path for the give FSID""" parts = list_mounts() for prt in parts: try: fsid = get_fsid(prt) if fsid == fs_id: return prt except: pass return None
def fs(self, search_key=None, search_value=None, flags=0): """List filesystems, required for other operations""" fss = [] parts = list_mounts() for prt in parts: try: (total_size, avail_size) = NFSPlugin._get_fs_sizes(prt) fsid = get_fsid(prt) fss.append( FileSystem(fsid, prt, total_size, avail_size, fsid, self._SYSID)) except OSError: pass return search_property(fss, search_key, search_value)
def pools(self, search_key=None, search_value=None, flags=0): pools = [] parts = list_mounts() for prt in parts: try: (total_size, avail_size) = NFSPlugin._get_fs_sizes(prt) fsid = get_fsid(prt) pooltype = Pool.ELEMENT_TYPE_FS unsup_actions = 0 status = System.STATUS_OK status_info = '' pools.append( Pool(fsid, prt, pooltype, unsup_actions, total_size, avail_size, status, status_info, self._SYSID)) except OSError: pass return search_property(pools, search_key, search_value)
def export_fs(self, fs_id, export_path, root_list, rw_list, ro_list, anon_uid=NfsExport.ANON_UID_GID_NA, anon_gid=NfsExport.ANON_UID_GID_NA, auth_type=None, options=None, flags=None): """Add an export""" if fs_id is None and export_path is None: raise LsmError(ErrorNumber.INVALID_ARGUMENT, 'Must provide fs_id or export_path') if fs_id is None: try: fs_id = get_fsid(export_path) except: raise LsmError(ErrorNumber.NOT_FOUND_FS, 'FileSystem not found') else: if NFSPlugin._get_fsid_path(fs_id) is None: raise LsmError(ErrorNumber.NOT_FOUND_FS, 'No FileSystem found with that ID') if export_path is None: export_path = NFSPlugin._get_fsid_path(fs_id) if export_path is None: raise LsmError(ErrorNumber.INVALID_ARGUMENT, 'Could not locate filesystem') else: try: get_fsid(export_path) except: raise LsmError(ErrorNumber.NOT_FOUND_FS, 'Export path not found') if auth_type is not None: authlist = NFSPlugin._AUTH_LIST if auth_type not in authlist: raise LsmError(ErrorNumber.INVALID_ARGUMENT, 'Unsupported auth type') for host in root_list: if host not in rw_list and host not in ro_list: raise LsmError(ErrorNumber.INVALID_ARGUMENT, 'Root hosts must also be in rw or ro lists') expid = NFSPlugin._export_id(export_path, auth_type, anon_uid, anon_gid, options) newexp = NfsExport(expid, fs_id, export_path, auth_type, root_list, rw_list, ro_list, anon_uid, anon_gid, options) efile = NFSPlugin._open_exports(readonly=False) exports = NFSPlugin._read_exports(efile) result = None for idx, oldexp in enumerate(exports): if NFSPlugin._match_path(newexp, oldexp): exports[idx] = newexp result = exports[idx] newexp = None if newexp is not None: exports.append(newexp) result = newexp NFSPlugin._write_exports(efile, exports) NFSPlugin._close_exports(efile) NFSPlugin._update_exports() return result
def _write_exports(efile, exports): """write out the exports list to a file""" try: efile.seek(0) efile.truncate() efile.write('# NFS exports managed by libstoragemgmt.' ' do not edit.\n') for exp in exports: common_opts = {} if exp._options is not None: common_opts = NFSPlugin._parse_options(exp._options) if exp._anonuid is not None and \ exp._anonuid != NfsExport.ANON_UID_GID_NA: common_opts['anonuid'] = exp._anonuid if exp.anongid is not None and \ exp._anongid != NfsExport.ANON_UID_GID_NA: common_opts['anongid'] = exp._anongid if exp._auth is not None: common_opts['sec'] = str(exp._auth) if exp._export_path is not None: if not os.path.isdir(exp._export_path): raise LsmError(ErrorNumber.INVALID_ARGUMENT, 'Export path does not exist') if exp._fs_id is not None and exp._export_path is not None: try: export_id = get_fsid(exp._export_path) if export_id != exp._fs_id: raise LsmError( ErrorNumber.INVALID_ARGUMENT, 'FS ID and Path\'s FS ID do not match') except: raise for host in exp._rw: opts = copy.copy(common_opts) if host in exp._root: if 'root_squash' in opts: del opts['root_squash'] opts['no_root_squash'] = 'no_root_squash' if 'ro' in opts: del opts['ro'] opts['rw'] = 'rw' if " " in exp._export_path: efile.write("\"%s\" %s(%s)\n" % (exp._export_path, host, NFSPlugin._print_option(opts))) else: efile.write("%s %s(%s)\n" % (exp._export_path, host, NFSPlugin._print_option(opts))) for host in exp._ro: opts = copy.copy(common_opts) if host in exp._root: if 'root_squash' in opts: del opts['root_squash'] opts['no_root_squash'] = 'no_root_squash' if 'rw' in opts: del opts['rw'] opts['ro'] = 'ro' efile.write("%s %s(%s)\n" % (exp._export_path, host, NFSPlugin._print_option(opts))) except IOError: raise LsmError(ErrorNumber.PLUGIN_BUG, 'error writing exports')
def _parse_export(parts=None): """Parse a line of export file""" if len(parts) < 1: return None path = parts[0] host = '*' optionstring = "" if len(parts) > 1: host = parts[1] if '(' and ')' in host: if host[0] != '(': host, optionstring = host[:-1].split('(') else: optionstring = host[1:-1] host = '*' options = NFSPlugin._parse_options(optionstring) root_list = [] rw_list = [] ro_list = [] sec = None anonuid = None anongid = None if 'rw' in options: rw_list.append(host) del options['rw'] if 'ro' in options: ro_list.append(host) del options['ro'] if 'no_root_squash' in options: root_list.append(host) del options['no_root_squash'] else: if 'root_squash' in options: del options['root_squash'] if 'sec' in options: sec = options['sec'] del options['sec'] else: sec = None if 'anonuid' in options: anonuid = int(options['anonuid']) del options['anonuid'] else: anonuid = NfsExport.ANON_UID_GID_NA if 'anongid' in options: anongid = int(options['anongid']) del options['anongid'] else: anongid = NfsExport.ANON_UID_GID_NA try: fsid = get_fsid(path) optionstring = NFSPlugin._print_option(options) export_id = NFSPlugin._export_id(path, sec, anonuid, anongid, optionstring) result = NfsExport(export_id, fsid, path, sec, root_list, rw_list, ro_list, anonuid, anongid, optionstring) return result except: pass return None