#!/usr/bin/env python '''Another export misconversion fixup script for bad 7.2->8.0.X upgrades''' import isi.gconfig as gconfig import isi.config.nfs as nfs import isi.misc import sys from optparse import OptionParser THIS_MAJOR_VERS = isi.misc.osrelease().split('.')[0][1] ZONE_BASE_PATH = 'zone_list.zones' EXP_DIR_PATH = 'exports.exports' def export_replace_sys(options, ctx, zid, nexports, export): '''Check if an export has the "sys"/"unix" security flavor set and fix it''' expid = export['id'] security_flavors = export['options']['security_flavors'] if not 'sys' in security_flavors: return if options.dryrun: print "Found affected export: %d" % expid return if options.prevent: # Overwrite gconfig filtered_flavors = security_flavors.replace('sys', 'unix') export['options']['security_flavors'] = filtered_flavors print 'Replaced %s with %s for export %d' % (security_flavors, filtered_flavors, expid) if export['type'] in ('template', 'prototype'): #prototype type can be template for some reason this_path = '%s.%d.exports.prototype.options.security_flavors' % (ZONE_BASE_PATH, zid) elif export['type'] == 'export': this_path = '%s.%d.%s.%d.options.security_flavors' % (ZONE_BASE_PATH, zid, EXP_DIR_PATH, expid) else: print export['type'] raise ctx.write(this_path, filtered_flavors) if options.repair: # Edit current options print "Adding unix security to export: %d" % expid export = nexports.export_get(expid) export.security_flavors.append("unix") nexports.export_set(expid, export, zid, nfs.NFS_FLAG_FORCE) if __name__ == '__main__': parser = OptionParser(usage="Usage: %prog [options]\n\n" "Tool for repairing or preventing unwanted NFS export security flavor changes following a 7.2->8.0.X upgrade.") parser.add_option("-d", "--dry-run", dest="dryrun", action="store_true", default=False, help="Just print affected exports.") parser.add_option("-p", "--prevent", dest="prevent", action="store_true", default=False, help="Update the on-disk 7.2 configuration to avoid issues.") parser.add_option("-r", "--repair", dest="repair", action="store_true", default=False, help="Update the existing 8.0.X configuration to repair the issue.") (options, args) = parser.parse_args() if options.dryrun and (options.prevent or options.repair): print "--dryrun is incompatible with --prevent and --repair." parser.print_usage() sys.exit(1) if not options.dryrun and not options.prevent and not options.repair: if THIS_MAJOR_VERS == '7': options.prevent = True else: options.repair = True if options.repair and THIS_MAJOR_VERS == '7': print "--repair is not available on 7.2" sys.exit(1) base = gconfig.open(gconfig.gcfg_nfs_config) ctx = base.ctx_new() for zone in ctx.read(ZONE_BASE_PATH): zid = zone['zid'] if options.repair: nexports = nfs.Exports(zid) else: nexports = None export_replace_sys(options, ctx, zid, nexports, zone['exports']['prototype']) for export in zone['exports']['exports']: export_replace_sys(options, ctx, zid, nexports, export) if nexports: nexports.commit() del nexports ctx.commit() print 'Done.' sys.exit(0)