2 * Copyright (C) 2014-2015 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* Implement 'mclu boot'. *)
21 module C = Libvirt.Connect
22 module D = Libvirt.Domain
23 module MS = Mclu_status
29 let memory = ref 0L (* 0 = choose for me *)
31 try memory := bytes_of_human_size s
33 eprintf "mclu: don't understand --memory parameter '%s'
34 Try something like --memory 1G\n" s;
36 let size = ref 0L (* 0 = default *)
38 try size := bytes_of_human_size s
40 eprintf "mclu: don't understand --size parameter '%s'
41 Try something like --size 20G\n" s;
43 let timezone = ref "" (* "" = no timezone set *)
44 let vcpus = ref 0 (* 0 = choose for me *)
46 let open_console = ref false
47 let open_viewer = ref false
49 let get_arg_speclist () = Arg.align [
50 "--console", Arg.Set open_console, " Open the serial console";
51 "--cpus", Arg.Set_int vcpus, "n Number of virtual CPUs";
52 "--memory", Arg.String set_memory, "nnG Amount of RAM to give guest";
53 "--ram", Arg.String set_memory, "nnG Amount of RAM to give guest";
54 "--size", Arg.String set_size, "nnG Size of disk to give guest";
55 "--timezone", Arg.Set_string timezone, "TZ Set timezone of guest";
56 "--vcpus", Arg.Set_int vcpus, "n Number of virtual CPUs";
57 "--viewer", Arg.Set open_viewer, " Open the graphical console";
60 let boot ~verbose template name =
61 let templates = Template.templates () in
63 (* Does the template exist? *)
64 let template_filename =
65 try List.assoc template templates
67 eprintf "mclu: template %s not found
68 Try `mclu list --templates' to list all known templates.\n" template;
71 (* Probe the template for various features. *)
72 let template_info = Template.probe ~verbose template_filename in
74 (* Check --size is not too small. *)
76 match !size, template_info.Template.minimum_size with
77 | 0L, None -> 0L (* virt-builder default *)
78 | 0L, Some min_size -> (* go with template minimum size *)
80 | size, Some min_size when size < min_size ->
81 eprintf "mclu: --size parameter is smaller than the minimum specified by the template (%s).\n"
82 (human_size min_size);
84 | size, _ -> size in (* go with user-specified size *)
86 (* Decide how much RAM we will give the guest. This affects our
87 * choice of node, so do it early.
89 let memory = !memory in
92 (* User requested, just check it's above the minimum. *)
93 match template_info.Template.minimum_memory with
95 | Some min when min > memory ->
96 eprintf "mclu: minimum memory for this template is %s\n"
101 (* User didn't request any memory setting, use the recommended. *)
102 match template_info.Template.recommended_memory with
103 | Some memory -> memory
104 | None -> 4L *^ 1024L *^ 1024L *^ 1024L (* 4 GB *)
107 (* Check what's running. *)
108 let summary = MS.node_guest_summary ~verbose () in
110 (* Did the user request a specific host? If not, choose one. *)
112 match name_parse name with
113 | Some hostname, name -> hostname, name
115 (* Choose the first host with enough free memory. *)
116 let nodes = List.filter (
117 fun { MS.free_memory = free_memory } -> free_memory >= memory
121 eprintf "mclu: no node with enough free memory found
122 Try: `mclu status' and `mclu on <node>'\n";
126 node.MS.node_status.MS.node.Mclu_conf.hostname in
129 (* Check there isn't a guest with this name running anywhere
130 * in the cluster already.
133 fun ({ MS.active_guests = guests } as node) ->
135 fun { Mclu_list.dom_name = n } ->
138 node.MS.node_status.MS.node.Mclu_conf.hostname
140 eprintf "mclu: there is already a guest called '%s' (running on %s)\n"
147 (* Convert hostname to a specific node, and check it is up. *)
151 node.MS.node_status.MS.node.Mclu_conf.hostname = hostname
154 eprintf "mclu: no node is called '%s'\n" hostname;
156 if not node.MS.node_status.MS.node_on then (
157 eprintf "mclu: node '%s' is switched off
158 Try: `mclu on %s'\n" hostname hostname;
162 (* Where we upload the template and image on remote. *)
163 let format, extension = "qcow2", "qcow2" in
164 let remote_template = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in
165 let remote_template_wrapper = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in
166 let xml_template_wrapper = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in
167 let remote_image = sprintf "/var/tmp/%s.%s" name extension in
168 let remote_external_kernel_dir = sprintf "/var/tmp/%s.boot" name in
169 let remote_external_kernel = sprintf "/var/tmp/%s.boot/kernel" name in
170 let remote_external_initrd = sprintf "/var/tmp/%s.boot/initrd" name in
171 let remote_arch = node.MS.node_status.node_info.model in
173 (* Guest arch defaults to the node host arch, but can be overridden
177 match template_info.Template.guest_arch with
179 | None -> remote_arch in
181 (* UEFI firmware and NVRAM on remote, if required. *)
183 match guest_arch with
185 Some ("/usr/share/edk2.git/aarch64/QEMU_EFI-pflash.raw",
186 "/usr/share/edk2.git/aarch64/vars-template-pflash.raw",
187 remote_image ^ ".nvram")
190 (* Get ready to generate the guest XML. *)
191 let vcpus = !vcpus in
193 if vcpus > 0 then vcpus
194 else min 4 node.MS.node_status.MS.node_info.C.cpus in
196 sprintf "52:54:00:%02x:%02x:%02x"
197 (Random.int 256) (Random.int 256) (Random.int 256) in
199 (* Generate the guest XML. *)
200 let generate_standard_xml () =
201 (* XXX Better quoting. *)
205 <memory unit='KiB'>%Ld</memory>
206 <currentMemory unit='KiB'>%Ld</currentMemory>
208 " name (memory /^ 1024L) (memory /^ 1024L) vcpus in
215 match guest_arch with
216 | "arm" | "armv7" | "armv7l" | "armv7hl" ->
218 <type arch='armv7l' machine='virt'>hvm</type>
222 <type machine='virt'>hvm</type>
231 | Some (loader, nvram_template, nvram) ->
233 <loader readonly='yes' type='pflash'>%s</loader>
234 <nvram template='%s'>%s</nvram>
235 " loader nvram_template nvram
239 if template_info.Template.needs_external_kernel then
243 " remote_external_kernel remote_external_initrd
247 match template_info.Template.cmdline with
248 | Some cmdline -> sprintf " <cmdline>%s</cmdline>\n" cmdline
258 <cpu mode='host-passthrough'/> <!-- -cpu host, also allows nested -->
260 <timer name='rtc' tickpolicy='catchup'/>
261 <timer name='pit' tickpolicy='delay'/>
262 <timer name='hpet' present='no'/>
264 <on_poweroff>destroy</on_poweroff>
265 <on_reboot>restart</on_reboot>
266 <on_crash>restart</on_crash>
270 let xml = xml ^ sprintf "\
271 <disk type='file' device='disk'>
272 <driver name='qemu' type='%s' cache='none' io='native'/>
274 " format remote_image in
276 match template_info.Template.disk_bus with
278 " <target dev='sda' bus='ide'/>\n"
280 " <target dev='vda' bus='virtio'/>\n"
281 | Some "virtio-scsi" | None ->
282 " <target dev='sda' bus='scsi'/>\n"
284 eprintf "mclu: unknown disk-bus: %s\n" bus;
292 if template_info.Template.disk_bus = Some "virtio-scsi" then
293 " <controller type='scsi' index='0' model='virtio-scsi'/>\n"
297 (* XXX Don't hard-code bridge name here. *)
299 match template_info with
300 | { Template.network_model = None } -> "virtio"
301 | { Template.network_model = Some d } -> d in
302 let xml = xml ^ sprintf "\
303 <interface type='bridge'>
305 <source bridge='br0'/>
308 " mac_addr network_model in
315 <target type='serial' port='0'/>
319 match guest_arch with
320 | "i386" | "i486" | "i586" | "i686"
323 <input type='tablet' bus='usb'/>
324 <input type='mouse' bus='ps2'/>
325 <input type='keyboard' bus='ps2'/>
326 <graphics type='vnc' autoport='yes'/>
328 <model type='cirrus' vram='9216' heads='1'/>
337 and generate_custom_xml () =
338 (* Generate a wrapper script to make passing the variables
339 * to the template easier.
342 let chan = open_out xml_template_wrapper in
343 let fpf fs = fprintf chan fs in
345 fpf "export format=%s\n" (quote format);
346 fpf "export initrd=%s\n" (quote remote_external_initrd);
347 fpf "export kernel=%s\n" (quote remote_external_kernel);
348 fpf "export mac_addr=%s\n" (quote mac_addr);
349 fpf "export memory_kb=%Ld\n" (memory /^ 1024L);
350 fpf "export name=%s\n" (quote name);
351 fpf "export output=%s\n" (quote remote_image);
352 fpf "export vcpus=%d\n" vcpus;
353 fpf "%s xml\n" template_filename;
355 Unix.chmod xml_template_wrapper 0o755 in
357 if verbose then printf "%s\n%!" xml_template_wrapper;
358 let chan = Unix.open_process_in xml_template_wrapper in
359 let lines = ref [] in
360 (try while true do lines := input_line chan :: !lines done
361 with End_of_file -> ());
362 let stat = Unix.close_process_in chan in
364 | Unix.WEXITED 0 -> ()
366 eprintf "mclu: template '%s' subcmd xml exited with error %d\n"
369 | Unix.WSIGNALED i ->
370 eprintf "mclu: template '%s' subcmd xml killed by signal %d\n"
374 eprintf "mclu: template '%s' subcmd xml stopped by signal %d\n"
378 let xml = String.concat "\n" (List.rev !lines) in
383 if not template_info.Template.has_xml_target then
384 generate_standard_xml ()
386 generate_custom_xml () in
388 (* Copy the template to remote. *)
390 sprintf "scp %s root@%s:%s"
391 (quote template_filename) (quote hostname) remote_template in
392 if verbose then printf "%s\n%!" cmd;
393 if Sys.command cmd <> 0 then (
394 eprintf "mclu: scp template to remote failed\n";
398 (* Create a wrapper script that sets the variables and runs the
399 * template. This just avoids complex quoting.
402 let chan = open_out remote_template_wrapper in
403 let fpf fs = fprintf chan fs in
406 (* XXX Don't hard-code network_bridge here. *)
407 fpf "export LIBGUESTFS_BACKEND_SETTINGS=network_bridge=br0\n";
408 fpf "export base_image=%s\n" (quote template_info.Template.base_image);
409 fpf "export format=%s\n" (quote format);
410 fpf "export guest_arch=%s\n" (quote guest_arch);
411 fpf "export name=%s\n" (quote name);
412 fpf "export output=%s\n" (quote remote_image);
415 | size -> fpf "export size=%s\n" (quote (sprintf "--size %Ldb" size))
417 (match !timezone with
419 | tz -> fpf "export timezone=%s\n" (quote (sprintf "--timezone %s" tz))
422 | Some (_, nvram_template, nvram) ->
423 fpf "cp %s %s\n" (quote nvram_template) (quote nvram)
426 fpf "%s build\n" remote_template;
427 if template_info.Template.needs_external_kernel then (
428 fpf "rm -rf %s\n" (quote remote_external_kernel_dir);
429 fpf "mkdir %s\n" (quote remote_external_kernel_dir);
430 fpf "pushd %s\n" (quote remote_external_kernel_dir);
431 fpf "virt-builder --get-kernel %s\n" (quote remote_image);
432 fpf "ln vmlinuz-* kernel\n";
433 fpf "ln init* initrd\n";
437 Unix.chmod remote_template_wrapper 0o755 in
440 sprintf "scp %s root@%s:%s"
441 (quote remote_template_wrapper) (quote hostname)
442 (quote remote_template_wrapper) in
443 if verbose then printf "%s\n%!" cmd;
444 if Sys.command cmd <> 0 then (
445 eprintf "mclu: scp template wrapper to remote failed\n";
450 sprintf "ssh root@%s %s" (quote hostname) (quote remote_template_wrapper) in
451 if verbose then printf "%s\n%!" cmd;
452 if Sys.command cmd <> 0 then (
453 eprintf "mclu: remote build failed\n";
457 (* Start the guest. *)
461 let name = node.MS.node_status.MS.node.Mclu_conf.libvirt_uri in
462 C.connect ~name () in
463 let dom = D.create_xml conn xml [] in
464 printf "mclu: %s:%s started\n" hostname (D.get_name dom);
466 with Libvirt.Virterror msg ->
467 eprintf "mclu: %s: %s\n" hostname (Libvirt.Virterror.to_string msg);
470 (* Graphical console? *)
472 Mclu_viewer.viewer ~verbose ~host:hostname (D.get_name dom);
474 (* Serial console? (Interactive, so run it last) *)
475 if !open_console then
476 Mclu_console.console ~verbose ~host:hostname (D.get_name dom)
478 let run ~verbose = function
479 | [ template; name ] ->
480 boot ~verbose template name
482 eprintf "Usage: mclu boot <template> <[host:]name>\n";