9c46e38
#!/usr/bin/env python3
9c46e38
"""
9c46e38
Read in the deprecated /etc/sysconfig/nfs file and
9c46e38
set the corresponding values in nfs.conf
9c46e38
"""
9c46e38
9c46e38
from __future__ import print_function
9c46e38
import os
9c46e38
import sys
9c46e38
import getopt
9c46e38
import subprocess
9c46e38
import configparser
9c46e38
9c46e38
CONF_NFS = '/etc/nfs.conf'
9c46e38
CONF_IDMAP = '/etc/idmapd.conf'
9c46e38
SYSCONF_NFS = '/etc/sysconfig/nfs'
9c46e38
SYSCONF_BACKUP = ".rpmsave"
9c46e38
CONF_TOOL = '/usr/sbin/nfsconf'
9c46e38
9c46e38
# options for nfsd found in RPCNFSDARGS
9c46e38
OPTS_NFSD = 'dH:p:rR:N:V:stTuUG:L:'
9c46e38
LONG_NFSD = ['debug', 'host=', 'port=', 'rdma=', 'nfs-version=', 'no-nfs-version=',
9c46e38
             'tcp', 'no-tcp', 'udp', 'no-udp', 'grace-time=', 'lease-time=']
9c46e38
CONV_NFSD = {'-d': (CONF_NFS, 'nfsd', 'debug', 'all'),
9c46e38
             '-H': (CONF_NFS, 'nfsd', 'host', ','),
9c46e38
             '-p': (CONF_NFS, 'nfsd', 'port', '$1'),
9c46e38
             '-r': (CONF_NFS, 'nfsd', 'rdma', 'nfsrdma'),
9c46e38
             '-R': (CONF_NFS, 'nfsd', 'rdma', '$1'),
9c46e38
             '-N': (CONF_NFS, 'nfsd', 'vers$1', 'n'),
9c46e38
             '-V': (CONF_NFS, 'nfsd', 'vers$1', 'y'),
9c46e38
             '-t': (CONF_NFS, 'nfsd', 'tcp', '1'),
9c46e38
             '-T': (CONF_NFS, 'nfsd', 'tcp', '0'),
9c46e38
             '-u': (CONF_NFS, 'nfsd', 'udp', '1'),
9c46e38
             '-U': (CONF_NFS, 'nfsd', 'udp', '0'),
9c46e38
             '-G': (CONF_NFS, 'nfsd', 'grace-time', '$1'),
9c46e38
             '-L': (CONF_NFS, 'nfsd', 'lease-time', '$1'),
9c46e38
             '$1': (CONF_NFS, 'nfsd', 'threads', '$1'),
9c46e38
             '--debug': (CONF_NFS, 'nfsd', 'debug', 'all'),
9c46e38
             '--host': (CONF_NFS, 'nfsd', 'host', ','),
9c46e38
             '--port': (CONF_NFS, 'nfsd', 'port', '$1'),
9c46e38
             '--rdma': (CONF_NFS, 'nfsd', 'rdma', '$1'),
9c46e38
             '--no-nfs-version': (CONF_NFS, 'nfsd', 'vers$1', 'n'),
9c46e38
             '--nfs-version': (CONF_NFS, 'nfsd', 'vers$1', 'y'),
9c46e38
             '--tcp': (CONF_NFS, 'nfsd', 'tcp', '1'),
9c46e38
             '--no-tcp': (CONF_NFS, 'nfsd', 'tcp', '0'),
9c46e38
             '--udp': (CONF_NFS, 'nfsd', 'udp', '1'),
9c46e38
             '--no-udp': (CONF_NFS, 'nfsd', 'udp', '0'),
9c46e38
             '--grace-time': (CONF_NFS, 'nfsd', 'grace-time', '$1'),
9c46e38
             '--lease-time': (CONF_NFS, 'nfsd', 'lease-time', '$1'),
9c46e38
            }
9c46e38
9c46e38
# options for mountd found in RPCMOUNTDOPTS
9c46e38
OPTS_MOUNTD = 'go:d:H:p:N:nrs:t:V:'
9c46e38
LONG_MOUNTD = ['descriptors=', 'debug=', 'nfs-version=', 'no-nfs-version=',
9c46e38
               'port=', 'no-tcp', 'ha-callout=', 'state-directory-path=',
9c46e38
               'num-threads=', 'reverse-lookup', 'manage-gids', 'no-udp']
9c46e38
9c46e38
CONV_MOUNTD = {'-g': (CONF_NFS, 'mountd', 'manage-gids', '1'),
9c46e38
               '-o': (CONF_NFS, 'mountd', 'descriptors', '$1'),
9c46e38
               '-d': (CONF_NFS, 'mountd', 'debug', '$1'),
9c46e38
               '-H': (CONF_NFS, 'mountd', 'ha-callout', '$1'),
9c46e38
               '-p': (CONF_NFS, 'mountd', 'port', '$1'),
9c46e38
               '-N': (CONF_NFS, 'nfsd', 'vers$1', 'n'),
9c46e38
               '-V': (CONF_NFS, 'nfsd', 'vers$1', 'y'),
9c46e38
               '-n': (CONF_NFS, 'nfsd', 'tcp', '0'),
9c46e38
               '-s': (CONF_NFS, 'mountd', 'stat-directory-path', '$1'),
9c46e38
               '-t': (CONF_NFS, 'mountd', 'threads', '$1'),
9c46e38
               '-r': (CONF_NFS, 'mountd', 'reverse-lookup', '1'),
9c46e38
               '-u': (CONF_NFS, 'nfsd', 'udp', '0'),
9c46e38
               '--manage-gids': (CONF_NFS, 'mountd', 'manage-gids', '1'),
9c46e38
               '--descriptors': (CONF_NFS, 'mountd', 'descriptors', '$1'),
9c46e38
               '--debug': (CONF_NFS, 'mountd', 'debug', '$1'),
9c46e38
               '--ha-callout': (CONF_NFS, 'mountd', 'ha-callout', '$1'),
9c46e38
               '--port': (CONF_NFS, 'mountd', 'port', '$1'),
9c46e38
               '--nfs-version': (CONF_NFS, 'nfsd', 'vers$1', 'y'),
9c46e38
               '--no-nfs-version': (CONF_NFS, 'nfsd', 'vers$1', 'n'),
9c46e38
               '--no-tcp': (CONF_NFS, 'nfsd', 'tcp', '0'),
9c46e38
               '--state-directory-path': (CONF_NFS, 'mountd', 'state-directory-path', '$1'),
9c46e38
               '--num-threads': (CONF_NFS, 'mountd', 'threads', '$1'),
9c46e38
               '--reverse-lookup': (CONF_NFS, 'mountd', 'reverse-lookup', '1'),
9c46e38
               '--no-udp': (CONF_NFS, 'nfsd', 'udp', '0'),
9c46e38
              }
9c46e38
9c46e38
# options for statd found in STATDARG
9c46e38
OPTS_STATD = 'o:p:T:U:n:P:H:'
9c46e38
LONG_STATD = ['outgoing-port=', 'port=', 'name=', 'state-directory-path=',
9c46e38
              'ha-callout=', 'nlm-port=', 'nlm-udp-port=']
9c46e38
CONV_STATD = {'-o': (CONF_NFS, 'statd', 'outgoing-port', '$1'),
9c46e38
              '-p': (CONF_NFS, 'statd', 'port', '$1'),
9c46e38
              '-T': (CONF_NFS, 'lockd', 'port', '$1'),
9c46e38
              '-U': (CONF_NFS, 'lockd', 'udp-port', '$1'),
9c46e38
              '-n': (CONF_NFS, 'statd', 'name', '$1'),
9c46e38
              '-P': (CONF_NFS, 'statd', 'state-directory-path', '$1'),
9c46e38
              '-H': (CONF_NFS, 'statd', 'ha-callout', '$1'),
9c46e38
              '--outgoing-port': (CONF_NFS, 'statd', 'outgoing-port', '$1'),
9c46e38
              '--port': (CONF_NFS, 'statd', 'port', '$1'),
9c46e38
              '--name': (CONF_NFS, 'statd', 'name', '$1'),
9c46e38
              '--state-directory-path': (CONF_NFS, 'statd', 'state-directory-path', '$1'),
9c46e38
              '--ha-callout': (CONF_NFS, 'statd', 'ha-callout', '$1'),
9c46e38
              '--nlm-port': (CONF_NFS, 'lockd', 'port', '$1'),
9c46e38
              '--nlm-udp-port': (CONF_NFS, 'lockd', 'udp-port', '$1'),
9c46e38
             }
9c46e38
9c46e38
# options for sm-notify found in SMNOTIFYARGS
9c46e38
OPTS_SMNOTIFY = 'dm:np:v:P:'
9c46e38
CONV_SMNOTIFY = {'-d': (CONF_NFS, 'sm-notify', 'debug', 'all'),
9c46e38
                 '-m': (CONF_NFS, 'sm-notify', 'retry-time', '$1'),
9c46e38
                 '-n': (CONF_NFS, 'sm-notify', 'update-state', '1'),
9c46e38
                 '-p': (CONF_NFS, 'sm-notify', 'outgoing-port', '$1'),
9c46e38
                 '-v': (CONF_NFS, 'sm-notify', 'outgoing-addr', '$1'),
9c46e38
                 '-P': (CONF_NFS, 'statd', 'state-directory-path', '$1'),
9c46e38
                }
9c46e38
9c46e38
# options for idmapd found in RPCIDMAPDARGS
9c46e38
OPTS_IDMAPD = 'vp:CS'
9c46e38
CONV_IDMAPD = {'-v': (CONF_IDMAP, 'general', 'verbosity', '+'),
9c46e38
               '-p': (CONF_NFS, 'general', 'pipefs-directory', '$1'),
9c46e38
               '-C': (CONF_IDMAP, 'general', 'client-only', '1'),
9c46e38
               '-S': (CONF_IDMAP, 'general', 'server-only', '1'),
9c46e38
              }
9c46e38
9c46e38
# options for gssd found in RPCGSSDARGS
9c46e38
OPTS_GSSD = 'Mnvrp:k:d:t:T:R:lD'
9c46e38
CONV_GSSD = {'-M': (CONF_NFS, 'gssd', 'use-memcache', '1'),
9c46e38
             '-n': (CONF_NFS, 'gssd', 'root_uses_machine_creds', '0'),
9c46e38
             '-v': (CONF_NFS, 'gssd', 'verbosity', '+'),
9c46e38
             '-r': (CONF_NFS, 'gssd', 'rpc-verbosity', '+'),
9c46e38
             '-p': (CONF_NFS, 'general', 'pipefs-directory', '$1'),
9c46e38
             '-k': (CONF_NFS, 'gssd', 'keytab-file', '$1'),
9c46e38
             '-d': (CONF_NFS, 'gssd', 'cred-cache-directory', '$1'),
9c46e38
             '-t': (CONF_NFS, 'gssd', 'context-timeout', '$1'),
9c46e38
             '-T': (CONF_NFS, 'gssd', 'rpc-timeout', '$1'),
9c46e38
             '-R': (CONF_NFS, 'gssd', 'preferred-realm', '$1'),
9c46e38
             '-l': (CONF_NFS, 'gssd', 'limit-to-legacy-enctypes', '0'),
9c46e38
             '-D': (CONF_NFS, 'gssd', 'avoid-dns', '0'),
9c46e38
            }
9c46e38
9c46e38
# options for blkmapd found in BLKMAPDARGS
9c46e38
OPTS_BLKMAPD = ''
9c46e38
CONV_BLKMAPD = {}
9c46e38
9c46e38
# meta list of all the getopt lists
9c46e38
GETOPT_MAPS = [('RPCNFSDARGS', OPTS_NFSD, LONG_NFSD, CONV_NFSD),
9c46e38
               ('RPCMOUNTDOPTS', OPTS_MOUNTD, LONG_MOUNTD, CONV_MOUNTD),
9c46e38
               ('STATDARG', OPTS_STATD, LONG_STATD, CONV_STATD),
9c46e38
               ('STATDARGS', OPTS_STATD, LONG_STATD, CONV_STATD),
9c46e38
               ('SMNOTIFYARGS', OPTS_SMNOTIFY, [], CONV_SMNOTIFY),
9c46e38
               ('RPCIDMAPDARGS', OPTS_IDMAPD, [], CONV_IDMAPD),
9c46e38
               ('RPCGSSDARGS', OPTS_GSSD, [], CONV_GSSD),
9c46e38
               ('BLKMAPDARGS', OPTS_BLKMAPD, [], CONV_BLKMAPD),
9c46e38
              ]
9c46e38
9c46e38
# map for all of the single option values
9c46e38
VALUE_MAPS = {'LOCKD_TCPPORT': (CONF_NFS, 'lockd', 'port', '$1'),
9c46e38
              'LOCKD_UDPPORT': (CONF_NFS, 'lockd', 'udp-port', '$1'),
9c46e38
              'RPCNFSDCOUNT': (CONF_NFS, 'nfsd', 'threads', '$1'),
9c46e38
              'NFSD_V4_GRACE': (CONF_NFS, 'nfsd', 'grace-time', '$1'),
9c46e38
              'NFSD_V4_LEASE': (CONF_NFS, 'nfsd', 'lease-time', '$1'),
9c46e38
              'MOUNTD_PORT': (CONF_NFS, 'mountd', 'port', '$1'),
9c46e38
              'STATD_PORT': (CONF_NFS, 'statd', 'port', '$1'),
9c46e38
              'STATD_OUTGOING_PORT': (CONF_NFS, 'statd', 'outgoing-port', '$1'),
9c46e38
              'STATD_HA_CALLOUT': (CONF_NFS, 'statd', 'ha-callout', '$1'),
9c46e38
              'GSS_USE_PROXY': (CONF_NFS, 'gssd', 'use-gss-proxy', '$1')
9c46e38
             }
9c46e38
9c46e38
def eprint(*args, **kwargs):
9c46e38
    """ Print error to stderr """
9c46e38
    print(*args, file=sys.stderr, **kwargs)
9c46e38
9c46e38
def makesub(param, value):
9c46e38
    """ Variable substitution """
9c46e38
    return param.replace('$1', value)
9c46e38
9c46e38
def set_value(value, entry):
9c46e38
    """ Set a configuration value by running nfsconf tool"""
9c46e38
    cfile, section, tag, param = entry
9c46e38
9c46e38
    tag = makesub(tag, value)
9c46e38
    param = makesub(param, value)
9c46e38
    if param == '+':
9c46e38
        param = value
9c46e38
    if param == ',':
9c46e38
        param = value
9c46e38
    args = [CONF_TOOL, "--file", cfile, "--set", section, tag, param]
9c46e38
9c46e38
    try:
9c46e38
        subprocess.check_output(args, stderr=subprocess.STDOUT)
9c46e38
    except subprocess.CalledProcessError as e:
9c46e38
        print("Error running nfs-conf tool:\n %s" % (e.output.decode()))
9c46e38
        print("Args: %s\n" % args)
9c46e38
        raise Exception
9c46e38
9c46e38
def convert_getopt(optname, options, optstring, longopts, conversions):
9c46e38
    """ Parse option string into seperate config items
9c46e38
9c46e38
        Take a getopt string and a table of conversions
9c46e38
        parse it all and spit out the converted config
9c46e38
9c46e38
        Keyword arguments:
9c46e38
        options -- the argv string to convert
9c46e38
        optstring --  getopt format option list
9c46e38
        conversions -- table of translations
9c46e38
    """
9c46e38
    optcount = 0
9c46e38
    try:
9c46e38
        args = options.strip('\"').split()
9c46e38
        optlist, optargs = getopt.gnu_getopt(args, optstring, longopts=longopts)
9c46e38
    except getopt.GetoptError as err:
9c46e38
        eprint(err)
9c46e38
        raise Exception
9c46e38
9c46e38
    setlist = {}
9c46e38
    for (k, v) in optlist:
9c46e38
        if k in conversions:
9c46e38
            # it's already been set once
9c46e38
            param = conversions[k][3]
9c46e38
            tag = k + makesub(conversions[k][2], v)
9c46e38
            if tag in setlist:
9c46e38
                value = setlist[tag][0]
9c46e38
                # is it a cummulative entry
9c46e38
                if param == '+':
9c46e38
                    value = str(int(value) + 1)
9c46e38
                if param == ',':
9c46e38
                    value += "," + v
9c46e38
            else:
9c46e38
                if param == '+':
9c46e38
                    value = "1"
9c46e38
                elif param == ',':
9c46e38
                    value = v
9c46e38
                else:
9c46e38
                    value = v
9c46e38
            setlist[tag] = (value, conversions[k])
9c46e38
        else:
9c46e38
            if v:
9c46e38
                eprint("Ignoring unrecognised option %s=%s in %s" % (k, v, optname))
9c46e38
            else:
9c46e38
                eprint("Ignoring unrecognised option %s in %s" % (k, optname))
9c46e38
9c46e38
9c46e38
    for v, c in setlist.values():
9c46e38
        try:
9c46e38
            set_value(v, c)
9c46e38
            optcount += 1
9c46e38
        except Exception:
9c46e38
            raise
9c46e38
9c46e38
    i = 1
9c46e38
    for o in optargs:
9c46e38
        opname = '$' + str(i)
9c46e38
        if opname in conversions:
9c46e38
            try:
9c46e38
                set_value(o, conversions[opname])
9c46e38
                optcount += 1
9c46e38
            except Exception:
9c46e38
                raise
9c46e38
        else:
9c46e38
            eprint("Unrecognised trailing arguments")
9c46e38
            raise Exception
9c46e38
        i += 1
9c46e38
9c46e38
    return optcount
9c46e38
9c46e38
def map_values():
9c46e38
    """ Main function """
9c46e38
    mapcount = 0
9c46e38
9c46e38
    # Lets load the old config
9c46e38
    with open(SYSCONF_NFS) as cfile:
9c46e38
        file_content = '[sysconf]\n' + cfile.read()
9c46e38
    sysconfig = configparser.RawConfigParser()
9c46e38
    sysconfig.read_string(file_content)
9c46e38
9c46e38
    # Map all the getopt option lists
9c46e38
    for (name, opts, lopts, conv) in GETOPT_MAPS:
9c46e38
        if name in sysconfig['sysconf']:
9c46e38
            try:
9c46e38
                mapcount += convert_getopt(name, sysconfig['sysconf'][name], opts,
9c46e38
                                           lopts, conv)
9c46e38
            except Exception:
9c46e38
                eprint("Error whilst converting %s to nfsconf options." % (name))
9c46e38
                raise
9c46e38
9c46e38
    # Map the single value options
9c46e38
    for name, opts in VALUE_MAPS.items():
9c46e38
        if name in sysconfig['sysconf']:
9c46e38
            try:
9c46e38
                value = sysconfig['sysconf'][name]
9c46e38
                set_value(value.strip('\"'), opts)
9c46e38
                mapcount += 1
9c46e38
            except Exception:
9c46e38
                raise
9c46e38
9c46e38
    # All went well, move aside the old file
9c46e38
    # but dont bother if there were no changes and
9c46e38
    # an old config file already exists
9c46e38
    backupfile = SYSCONF_NFS + SYSCONF_BACKUP
9c46e38
    if mapcount > 0 or not os.path.exists(backupfile):
9c46e38
        try:
9c46e38
            os.replace(SYSCONF_NFS, backupfile)
9c46e38
        except OSError as err:
9c46e38
            eprint("Error moving old config %s: %s" % (SYSCONF_NFS, err))
9c46e38
            raise
9c46e38
9c46e38
# Main routine
9c46e38
try:
9c46e38
    map_values()
9c46e38
except Exception as e:
9c46e38
    eprint(e)
9c46e38
    eprint("Conversion failed. Please correct the error and try again.")
9c46e38
    exit(1)