def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
  assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)

  env = None
  prefix = None
  suffix = None
  exp_size = None

  if ieio == constants.IEIO_FILE:
    (filename, ) = ieargs

    if not utils.IsNormAbsPath(filename):
      _Fail("Path '%s' is not normalized or absolute", filename)

    real_filename = os.path.realpath(filename)
    directory = os.path.dirname(real_filename)

    if not utils.IsBelowDir(pathutils.EXPORT_DIR, real_filename):
      _Fail("File '%s' is not under exports directory '%s': %s",
            filename, pathutils.EXPORT_DIR, real_filename)

    # Create directory
    utils.Makedirs(directory, mode=0750)

    quoted_filename = utils.ShellQuote(filename)

    if mode == constants.IEM_IMPORT:
      suffix = "> %s" % quoted_filename
    elif mode == constants.IEM_EXPORT:
      suffix = "< %s" % quoted_filename

      # Retrieve file size
      try:
        st = os.stat(filename)
      except EnvironmentError, err:
        logging.error("Can't stat(2) %s: %s", filename, err)
      else:
        exp_size = utils.BytesToMebibyte(st.st_size)

  elif ieio == constants.IEIO_RAW_DISK:
    (disk, ) = ieargs
    real_disk = _OpenRealBD(disk)

    if mode == constants.IEM_IMPORT:
      suffix = "| %s" % utils.ShellQuoteArgs(real_disk.Import())

    elif mode == constants.IEM_EXPORT:
      prefix = "%s |" % utils.ShellQuoteArgs(real_disk.Export())
      exp_size = disk.size

  elif ieio == constants.IEIO_SCRIPT:
    (disk, disk_index, ) = ieargs

    assert isinstance(disk_index, (int, long))

    inst_os = OSFromDisk(instance.os)
    env = OSEnvironment(instance, inst_os)

    if mode == constants.IEM_IMPORT:
      disk_path_var = "DISK_%d_PATH" % disk_index
      if disk_path_var in env:
        env["IMPORT_DEVICE"] = env[disk_path_var]
        env["IMPORT_DISK_PATH"] = env[disk_path_var]

      disk_uri_var = "DISK_%d_URI" % disk_index
      if disk_uri_var in env:
        env["IMPORT_DISK_URI"] = env[disk_uri_var]

      env["IMPORT_INDEX"] = str(disk_index)
      script = inst_os.import_script

    elif mode == constants.IEM_EXPORT:
      disk_path_var = "DISK_%d_PATH" % disk_index
      if disk_path_var in env:
        env["EXPORT_DEVICE"] = env[disk_path_var]
        env["EXPORT_DISK_PATH"] = env[disk_path_var]

      disk_uri_var = "DISK_%d_URI" % disk_index
      if disk_uri_var in env:
        env["EXPORT_DISK_URI"] = env[disk_uri_var]

      env["EXPORT_INDEX"] = str(disk_index)
      script = inst_os.export_script

    # TODO: Pass special environment only to script
    script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)

    if mode == constants.IEM_IMPORT:
      suffix = "| %s" % script_cmd

    elif mode == constants.IEM_EXPORT:
      prefix = "%s |" % script_cmd

    # Let script predict size
    exp_size = constants.IE_CUSTOM_SIZE

  else:
    _Fail("Invalid %s I/O mode %r", mode, ieio)

  return (env, prefix, suffix, exp_size)
Returns the command for the requested input/output.
dataset/ETHPy150Open ganeti/ganeti/lib/backend.py/_GetImportExportIoCommand