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