-def ExportSnapshot(disk, dest_node, instance, cluster_name, idx, debug):
- """Export a block device snapshot to a remote node.
-
- @type disk: L{objects.Disk}
- @param disk: the description of the disk to export
- @type dest_node: str
- @param dest_node: the destination node to export to
- @type instance: L{objects.Instance}
- @param instance: the instance object to whom the disk belongs
- @type cluster_name: str
- @param cluster_name: the cluster name, needed for SSH hostalias
- @type idx: int
- @param idx: the index of the disk in the instance's disk list,
- used to export to the OS scripts environment
- @type debug: integer
- @param debug: debug level, passed to the OS scripts
- @rtype: None
-
- """
- inst_os = OSFromDisk(instance.os)
- export_env = OSEnvironment(instance, inst_os, debug)
-
- export_script = inst_os.export_script
-
- logfile = _InstanceLogName("export", inst_os.name, instance.name)
- if not os.path.exists(constants.LOG_OS_DIR):
- os.mkdir(constants.LOG_OS_DIR, 0750)
-
- real_disk = _OpenRealBD(disk)
-
- export_env['EXPORT_DEVICE'] = real_disk.dev_path
- export_env['EXPORT_INDEX'] = str(idx)
-
- destdir = utils.PathJoin(constants.EXPORT_DIR, instance.name + ".new")
- destfile = disk.physical_id[1]
-
- # the target command is built out of three individual commands,
- # which are joined by pipes; we check each individual command for
- # valid parameters
- expcmd = utils.BuildShellCmd("set -e; set -o pipefail; cd %s; %s 2>%s",
- inst_os.path, export_script, logfile)
-
- comprcmd = "gzip"
-
- destcmd = utils.BuildShellCmd("mkdir -p %s && cat > %s",
- destdir, utils.PathJoin(destdir, destfile))
- remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
- constants.GANETI_RUNAS,
- destcmd)
-
- # all commands have been checked, so we're safe to combine them
- command = '|'.join([expcmd, comprcmd, utils.ShellQuoteArgs(remotecmd)])
-
- result = utils.RunCmd(["bash", "-c", command], env=export_env)
-
- if result.failed:
- _Fail("OS snapshot export command '%s' returned error: %s"
- " output: %s", command, result.fail_reason, result.output)
-
-