diff --git a/builder/cmdline.ml b/builder/cmdline.ml index 7fbce1d90..3a620feed 100644 --- a/builder/cmdline.ml +++ b/builder/cmdline.ml @@ -81,6 +81,8 @@ let parse_cmdline () = eprintf (f_"%s: invalid --list-format type '%s', see the man page.\n") prog fmt; exit 1 in + let machine_readable = ref false in + let memsize = ref None in let set_memsize arg = memsize := Some arg in @@ -134,6 +136,7 @@ let parse_cmdline () = "--list-format", Arg.String list_set_format, "short|long|json" ^ " " ^ s_"Set the format for --list (default: short)"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; + "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable"; "-m", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size"; "--memsize", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size"; "--network", Arg.Set network, " " ^ s_"Enable appliance network (default)"; @@ -201,6 +204,7 @@ read the man page virt-builder(1). let format = match !format with "" -> None | s -> Some s in let gpg = !gpg in let list_format = !list_format in + let machine_readable = !machine_readable in let memsize = !memsize in let network = !network in let ops = get_customize_ops () in @@ -211,6 +215,17 @@ read the man page virt-builder(1). let sources = List.rev !sources in let sync = !sync in + (* No arguments and machine-readable mode? Print some facts. *) + if args = [] && machine_readable then ( + printf "virt-builder\n"; + printf "arch\n"; + printf "config-file\n"; + printf "customize\n"; + printf "json-list\n"; + if Pxzcat.using_parallel_xzcat () then printf "pxzcat\n"; + exit 0 + ); + (* Check options. *) let arg = match mode with diff --git a/builder/pxzcat-c.c b/builder/pxzcat-c.c index 941599581..42610c0e5 100644 --- a/builder/pxzcat-c.c +++ b/builder/pxzcat-c.c @@ -55,6 +55,12 @@ extern void unix_error (int errcode, char * cmdname, value arg) Noreturn; #define PARALLEL_XZCAT 0 #endif +value +virt_builder_using_parallel_xzcat (value unitv) +{ + return PARALLEL_XZCAT ? Val_true : Val_false; +} + #if PARALLEL_XZCAT static void pxzcat (value filenamev, value outputfilev, unsigned nr_threads); #endif /* PARALLEL_XZCAT */ diff --git a/builder/pxzcat.ml b/builder/pxzcat.ml index 56453709b..b34b43603 100644 --- a/builder/pxzcat.ml +++ b/builder/pxzcat.ml @@ -17,3 +17,4 @@ *) external pxzcat : string -> string -> unit = "virt_builder_pxzcat" +external using_parallel_xzcat : unit -> bool = "virt_builder_using_parallel_xzcat" "noalloc" diff --git a/builder/pxzcat.mli b/builder/pxzcat.mli index a2830f059..502defefb 100644 --- a/builder/pxzcat.mli +++ b/builder/pxzcat.mli @@ -29,3 +29,6 @@ val pxzcat : string -> string -> unit If liblzma was found at compile time, this uses an internal implementation of parallel xzcat. Otherwise regular xzcat is used. *) + +val using_parallel_xzcat : unit -> bool +(** Returns [true] iff the implementation uses parallel xzcat. *) diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod index c6e66ff4f..f18c4b06f 100644 --- a/builder/virt-builder.pod +++ b/builder/virt-builder.pod @@ -344,6 +344,12 @@ I<--long> is a shorthand for the C format. See also: I<--source>, I<--notes>, L. +=item B<--machine-readable> + +This option is used to make the output more machine friendly +when being parsed by other programs. See +L below. + =item B<-m> MB =item B<--memsize> MB @@ -1542,6 +1548,26 @@ which is normal and harmless. =back +=head1 MACHINE READABLE OUTPUT + +The I<--machine-readable> option can be used to make the output more +machine friendly, which is useful when calling virt-builder from other +programs, GUIs etc. + +Use the option on its own to query the capabilities of the +virt-builder binary. Typical output looks like this: + + $ virt-builder --machine-readable + virt-builder + arch + config-file + customize + json-list + pxzcat + +A list of features is printed, one per line, and the program exits +with status 0. + =head1 ENVIRONMENT VARIABLES For other environment variables which affect all libguestfs programs,