From 0bd1243eec67647186f4153ea3f395d77b979ba5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 26 Mar 2015 15:47:58 +0000 Subject: [PATCH] mclu: boot: Fix quoting issues. Create a wrapper script that sets the variables and runs the build script. --- mclu_boot.ml | 66 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/mclu_boot.ml b/mclu_boot.ml index a253981..5bf742f 100644 --- a/mclu_boot.ml +++ b/mclu_boot.ml @@ -149,7 +149,8 @@ Try: `mclu on %s'\n" hostname hostname; (* Where we upload the template and image on remote. *) let format, extension = "qcow2", "qcow2" in - let remote_filename = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in + let remote_template = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in + let remote_template_wrapper = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in let remote_image = sprintf "/var/tmp/%s.%s" name extension in (* Get ready to generate the guest XML. *) @@ -243,38 +244,53 @@ Try: `mclu on %s'\n" hostname hostname; " in - (* Copy the template to remote and build the guest. *) + (* Copy the template to remote. *) let cmd = sprintf "scp %s root@%s:%s" - (quote template_filename) (quote hostname) remote_filename in + (quote template_filename) (quote hostname) remote_template in if verbose then printf "%s\n%!" cmd; if Sys.command cmd <> 0 then ( eprintf "mclu: scp template to remote failed\n"; exit 1 ); - let cmd = + + (* Create a wrapper script that sets the variables and runs the + * template. This just avoids complex quoting. + *) + let () = + let chan = open_out remote_template_wrapper in + let fpf fs = fprintf chan fs in + fpf "#!/bin/sh\n"; (* XXX Don't hard-code network_bridge here. *) - sprintf "ssh root@%s \ -LIBGUESTFS_BACKEND_SETTINGS=network_bridge=br0 \ -base_image=%s \ -format=%s \ -name=%s \ -output=%s \ -%s \ -%s \ -%s build" - (quote hostname) - (quote template_info.Template.base_image) (* base_image *) - format (* format *) - name (* name *) - (quote remote_image) (* output *) - (match !size with - | 0L -> "" - | size -> sprintf "size=%Ldb" size) (* size *) - (match !timezone with - | "" -> "" - | tz -> quote "timezone=" ^ tz) (* timezone *) - remote_filename in + fpf "export LIBGUESTFS_BACKEND_SETTINGS=network_bridge=br0\n"; + fpf "export base_image=%s\n" (quote template_info.Template.base_image); + fpf "export format=%s\n" (quote format); + fpf "export name=%s\n" (quote name); + fpf "export output=%s\n" (quote remote_image); + (match !size with + | 0L -> () + | size -> fpf "export size=%s\n" (quote (sprintf "--size %Ldb" size)) + ); + (match !timezone with + | "" -> () + | tz -> fpf "export timezone=%s\n" (quote (sprintf "--timezone %s" tz)) + ); + fpf "%s build\n" remote_template; + close_out chan; + Unix.chmod remote_template_wrapper 0o755 in + + let cmd = + sprintf "scp %s root@%s:%s" + (quote remote_template_wrapper) (quote hostname) + (quote remote_template_wrapper) in + if verbose then printf "%s\n%!" cmd; + if Sys.command cmd <> 0 then ( + eprintf "mclu: scp template wrapper to remote failed\n"; + exit 1 + ); + + let cmd = + sprintf "ssh root@%s %s" (quote hostname) (quote remote_template_wrapper) in if verbose then printf "%s\n%!" cmd; if Sys.command cmd <> 0 then ( eprintf "mclu: remote build failed\n"; -- 1.8.3.1